fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2011-04-19 09:05 am

[dw-free] jQuerify cut expander

[commit: http://hg.dwscoalition.org/dw-free/rev/1d5cfe1a8dab]

http://bugs.dwscoalition.org/show_bug.cgi?id=3581

Basic conversion of the cut expander to jQuery, available to users in beta
testing.

Patch by [personal profile] exor674.

Files modified:
  • cgi-bin/DW/BetaFeatures/journaljquery.pm
  • cgi-bin/DW/Controller/CutExpander.pm
  • cgi-bin/DW/Routing.pm
  • cgi-bin/LJ/S2/DayPage.pm
  • cgi-bin/LJ/S2/FriendsPage.pm
  • cgi-bin/LJ/S2/RecentPage.pm
  • htdocs/js/jquery.cuttag-ajax.js
--------------------------------------------------------------------------------
diff -r 05c6a83eaf56 -r 1d5cfe1a8dab cgi-bin/DW/BetaFeatures/journaljquery.pm
--- a/cgi-bin/DW/BetaFeatures/journaljquery.pm	Tue Apr 19 16:52:31 2011 +0800
+++ b/cgi-bin/DW/BetaFeatures/journaljquery.pm	Tue Apr 19 17:05:23 2011 +0800
@@ -18,6 +18,7 @@ use base "LJ::BetaFeatures::default";
 
 sub args_list {
     my @implemented = (
+        "Cut expand and collapse",
         "Logging in",
         "Screen/freeze/delete",
         "Control strip injection for non-supporting journals",
@@ -27,7 +28,6 @@ sub args_list {
 
     my @notimplemented = (
         "Contextual hover",
-        "Cut expand and collapse",
         "Media embed placeholder expansion",
         "Same-page poll submission",
         "Icon browser",
diff -r 05c6a83eaf56 -r 1d5cfe1a8dab cgi-bin/DW/Controller/CutExpander.pm
--- a/cgi-bin/DW/Controller/CutExpander.pm	Tue Apr 19 16:52:31 2011 +0800
+++ b/cgi-bin/DW/Controller/CutExpander.pm	Tue Apr 19 17:05:23 2011 +0800
@@ -34,6 +34,7 @@ sub cutexpander_handler {
     my $r = DW::Request->get;
     my $args = $r->get_args;
 
+
     my $remote = LJ::get_remote();
 
     my $format = $formats->{json};
@@ -53,6 +54,7 @@ sub cutexpander_handler {
         my $uid = LJ::get_userid( $args->{journal} );
         my $entry = LJ::Entry->new( $uid, ditemid => $ditemid ) if $uid;
 
+        # FIXME: This returns 200 due to old library, Make return proper when we are jQuery only.
         return $error_out->( 200, BML::ml( "error.nopermission" ) ) unless $entry;
         
         # make sure the user can read the entry
@@ -67,8 +69,7 @@ sub cutexpander_handler {
         }
     }
 
-    # we have to return as 200 rather than, say, 403 because the httpreq
-    # library won't let us do custom error messages without returning OK.
+    # FIXME: This returns 200 due to old library, Make return proper when we are jQuery only.
     return $error_out->( 200, BML::ml( "error.nopermission" ) );
 }
 
diff -r 05c6a83eaf56 -r 1d5cfe1a8dab cgi-bin/DW/Routing.pm
--- a/cgi-bin/DW/Routing.pm	Tue Apr 19 16:52:31 2011 +0800
+++ b/cgi-bin/DW/Routing.pm	Tue Apr 19 17:05:23 2011 +0800
@@ -191,8 +191,12 @@ sub _call_hash {
 
     # JSON error rendering
     if ( $format eq 'json' ) {
+        $msg = $err->as_string;
+        chomp $msg;
+
         my $text = $LJ::MSG_ERROR || "Sorry, there was a problem.";
         my $remote = LJ::get_remote();
+
         $text = "$msg" if ( $remote && $remote->show_raw_errors ) || $LJ::IS_DEV_SERVER;
 
         $r->status( 500 );
@@ -201,8 +205,8 @@ sub _call_hash {
     # default error rendering
     } elsif ( $format eq "html" ) {
         $msg = $err->as_html;
+        chomp $msg;
 
-        chomp $msg;
         $msg .= " \@ $LJ::SERVER_NAME" if $LJ::SERVER_NAME;
 
         $r->status( 500 );
@@ -217,8 +221,8 @@ sub _call_hash {
         return DW::Template->render_string( $text, $opts );
     } else {
         $msg = $err->as_string;
+        chomp $msg;
 
-        chomp $msg;
         $msg .= " \@ $LJ::SERVER_NAME" if $LJ::SERVER_NAME;
 
         my $text = $LJ::MSG_ERROR || "Sorry, there was a problem.";
diff -r 05c6a83eaf56 -r 1d5cfe1a8dab cgi-bin/LJ/S2/DayPage.pm
--- a/cgi-bin/LJ/S2/DayPage.pm	Tue Apr 19 16:52:31 2011 +0800
+++ b/cgi-bin/LJ/S2/DayPage.pm	Tue Apr 19 17:05:23 2011 +0800
@@ -35,6 +35,10 @@ sub DayPage
 
     # load for ajax cuttag
     LJ::need_res( 'js/cuttag-ajax.js' );
+    LJ::need_res( { group => "jquery" }, qw(
+            js/jquery/jquery.ui.widget.js
+            js/jquery.cuttag-ajax.js
+        ) );
     my $collapsed = BML::ml( 'widget.cuttag.collapsed' );
     my $expanded = BML::ml( 'widget.cuttag.expanded' );
     my $collapseAll = BML::ml( 'widget.cuttag.collapseAll' );
diff -r 05c6a83eaf56 -r 1d5cfe1a8dab cgi-bin/LJ/S2/FriendsPage.pm
--- a/cgi-bin/LJ/S2/FriendsPage.pm	Tue Apr 19 16:52:31 2011 +0800
+++ b/cgi-bin/LJ/S2/FriendsPage.pm	Tue Apr 19 17:05:23 2011 +0800
@@ -38,6 +38,10 @@ sub FriendsPage
 
     # load for ajax cuttag
     LJ::need_res( 'js/cuttag-ajax.js' );
+    LJ::need_res( { group => "jquery" }, qw(
+            js/jquery/jquery.ui.widget.js
+            js/jquery.cuttag-ajax.js
+        ) );
     my $collapsed = BML::ml( 'widget.cuttag.collapsed' );
     my $expanded = BML::ml( 'widget.cuttag.expanded' );
     my $collapseAll = BML::ml( 'widget.cuttag.collapseAll' );
diff -r 05c6a83eaf56 -r 1d5cfe1a8dab cgi-bin/LJ/S2/RecentPage.pm
--- a/cgi-bin/LJ/S2/RecentPage.pm	Tue Apr 19 16:52:31 2011 +0800
+++ b/cgi-bin/LJ/S2/RecentPage.pm	Tue Apr 19 17:05:23 2011 +0800
@@ -90,6 +90,10 @@ sub RecentPage
 
     # load for ajax cuttag
     LJ::need_res( 'js/cuttag-ajax.js' );
+    LJ::need_res( { group => "jquery" }, qw(
+            js/jquery/jquery.ui.widget.js
+            js/jquery.cuttag-ajax.js
+        ) );
     my $collapsed = BML::ml( 'widget.cuttag.collapsed' );
     my $expanded = BML::ml( 'widget.cuttag.expanded' );
     my $collapseAll = BML::ml( 'widget.cuttag.collapseAll' );
diff -r 05c6a83eaf56 -r 1d5cfe1a8dab htdocs/js/jquery.cuttag-ajax.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/htdocs/js/jquery.cuttag-ajax.js	Tue Apr 19 17:05:23 2011 +0800
@@ -0,0 +1,153 @@
+(function($,Site) {
+
+$.widget("dw.cuttag", {
+    options: {
+        journal: undefined,
+        ditemid: undefined,
+        cutid: undefined
+    },
+    config: {
+        text: {
+            collapse: 'Collapse',
+            expand: 'Expand',
+            loading: 'Expanding'
+        }
+    },
+    _create: function() {
+        var self = this;
+        var spanid = self.element.attr("id");
+
+        self.options.journal = spanid.replace( /^span-cuttag_(.*)_[0-9]+_[0-9]+/, "$1");
+        self.options.ditemid = spanid.replace( /^.*_([0-9]+)_[0-9]+/, "$1");
+        self.options.cutid = spanid.replace( /^.*_([0-9]+)/, "$1");
+
+        var identifier = self.options.journal + '_' + self.options.ditemid + '_' + self.options.cutid;
+
+        self.identifier = identifier;
+        self.ajaxUrl = "/__rpc_cuttag?journal=" + self.options.journal + "&ditemid=" + self.options.ditemid + "&cutid=" + self.options.cutid;
+
+        var a = $("<a>",{
+            href: "#",
+            id: "cuttag_" + identifier,
+            "class": "cuttag-action"
+        });
+        var img = $("<img>",{
+            style: "border: 0;",
+            "aria-controls": 'div-cuttag_' + identifier
+        });
+        a.append(img);
+
+        var theDiv = $("#div-cuttag_" + identifier);
+
+        self.tag = {
+            "a": a,
+            "img": img,
+            "div": theDiv
+        };
+
+        self._setArrow("/collapse.gif", self.config.text.expand);
+
+        self.element.append(a);
+
+        a.click(function(e) {
+            e.stopPropagation();
+            e.preventDefault();
+            self.toggle();
+        });
+
+        self.element.css("display","inline");
+    },
+    isOpen: function() {
+        return this.element.hasClass("cuttag-open") ? 1 : 0;
+    },
+    toggle: function() {
+        if ( this.isOpen() )
+            this.close();
+        else
+            this.open();
+    },
+    open: function() {
+        var self = this;
+        self._setArrow("/ajax-loader.gif", self.config.text.loading);
+        $.ajax({
+            "method": "GET",
+            "url": self.ajaxUrl,
+            success: function(data) {
+                self.replaceCutTag(data);
+            },
+            error: function(jqXHR, error) {
+                self.handleError(error);
+            }
+        });
+    },
+    close: function() {
+        this.element.removeClass("cuttag-open");
+        this._setArrow("/collapse.gif", this.config.text.expand);
+
+        this.tag.div.empty();
+        this.tag.div.css("display","none");
+    },
+    _setArrow: function(path,str) {
+        var i = this.tag.img;
+        i.attr("src",Site.imgprefix + path);
+        i.attr("alt",str);
+        i.attr("title",str);
+    },
+    handleError: function(error) {
+        this._setArrow("/collapse.gif", this.config.text.expand);
+        alert(error);
+    },
+    replaceCutTag: function(resObj) {
+        var self = this;
+        if ( resObj.error ) {
+            self._setArrow("/collapse.gif", self.config.text.expand);
+        } else {
+            var replaceDiv = self.tag.div;
+            replaceDiv.html(resObj.text);
+            replaceDiv.css("display","block");
+
+            var closeEnd = $("<span>");
+            var a = $("<a>",{
+                href: "#cuttag_" + self.identifier,
+                "class": "cuttag-action"
+            });
+            var img = $("<img>",{
+                style: "border: 0;",
+                src: Site.imgprefix + "/collapse-end.gif",
+                "aria-controls": 'div-cuttag_' + self.identifier,
+                alt: self.config.text.collapse,
+                title: self.config.text.collapse
+            });
+            a.append(img);
+            closeEnd.append(a);
+            replaceDiv.append(closeEnd);
+
+            a.click(function(e) {
+                e.stopPropagation();
+                e.preventDefault();
+                self.toggle();
+            });
+
+            self.element.addClass("cuttag-open");
+            self._setArrow("/expand.gif", self.config.text.collapse);
+
+            $.dw.cuttag.initLinks(replaceDiv);
+        }
+    }
+});
+
+$.extend( $.dw.cuttag, {
+    initLinks: function(context) {
+        var cuttags = $("span.cuttag",context);
+
+        cuttags.each(function (_,element) {
+            $(element).cuttag();
+        });
+    }
+});
+
+})(jQuery,Site);
+
+jQuery(document).ready(function($) {
+    $.dw.cuttag.initLinks(document);
+});
--------------------------------------------------------------------------------