fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2011-03-15 10:31 am

[dw-free] Add Additional Navigational Links to Inbox and Pages

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

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

Duplicate the links at the top/bottom of an inbox notification when the
message is long (has > 10 newlines, or > 2000 characters).

Patch by [personal profile] wyntarvox.

Files modified:
  • cgi-bin/LJ/Event/JournalNewComment.pm
  • cgi-bin/LJ/Event/JournalNewEntry.pm
  • cgi-bin/LJ/Event/UserMessageRecvd.pm
  • cgi-bin/LJ/Widget/InboxFolder.pm
  • cgi-bin/ljtextutil.pl
  • htdocs/inbox/index.bml
  • htdocs/js/esn_inbox.js
  • t/textutil.t
--------------------------------------------------------------------------------
diff -r 946170f4ce7a -r b7d0caa4dfb2 cgi-bin/LJ/Event/JournalNewComment.pm
--- a/cgi-bin/LJ/Event/JournalNewComment.pm	Fri Mar 11 19:32:34 2011 +0800
+++ b/cgi-bin/LJ/Event/JournalNewComment.pm	Tue Mar 15 18:31:28 2011 +0800
@@ -180,7 +180,7 @@ sub content {
     my $dtalkid = $comment->dtalkid;
     my $htmlid  = LJ::Talk::comment_htmlid( $dtalkid );
 
-    $comment_body =~ s/\n/<br \/>/g;
+    $comment_body = LJ::html_newlines( $comment_body );
 
     if ( $comment->is_edited ) {
         my $reason = LJ::ehtml( $comment->edit_reason );
@@ -217,6 +217,10 @@ sub content {
     $ret .= qq {
         </script>
         };
+
+    $ret = "<div class='actions_top'>" . $self->as_html_actions . "</div>" . $ret
+        if LJ::has_too_many( $ret, linebreaks => 10, chars => 2000 );
+
     $ret .= $self->as_html_actions;
 
     return $ret;
diff -r 946170f4ce7a -r b7d0caa4dfb2 cgi-bin/LJ/Event/JournalNewEntry.pm
--- a/cgi-bin/LJ/Event/JournalNewEntry.pm	Fri Mar 11 19:32:34 2011 +0800
+++ b/cgi-bin/LJ/Event/JournalNewEntry.pm	Tue Mar 15 18:31:28 2011 +0800
@@ -81,16 +81,20 @@ sub content {
     my $entry = $self->entry;
     return undef unless $self->_can_view_content( $entry, $target );
 
-    return $entry->event_html( {
-                # double negatives, ouch!
-                ljcut_disable => ! $target->cut_inbox,
-                cuturl => $entry->url,
-                sandbox => 1,
-                preformatted => $entry->prop( "opt_preformatted" ),
-             } )
+    my $entry_body = $entry->event_html( {
+                        # double negatives, ouch!
+                        ljcut_disable => ! $target->cut_inbox,
+                        cuturl => $entry->url,
+                        sandbox => 1,
+                        preformatted => $entry->prop( "opt_preformatted" ),
+                    } )
+                    . $self->as_html_tags( $target );
 
-            . $self->as_html_tags( $target )
-            . $self->as_html_actions;
+    $entry_body = "<div class='actions_top'>" . $self->as_html_actions . "</div>" . $entry_body
+        if LJ::has_too_many( $entry_body, linebreaks => 10, chars => 2000 );
+    $entry_body .= $self->as_html_actions;
+
+    return $entry_body;
 }
 
 sub as_html_tags {
diff -r 946170f4ce7a -r b7d0caa4dfb2 cgi-bin/LJ/Event/UserMessageRecvd.pm
--- a/cgi-bin/LJ/Event/UserMessageRecvd.pm	Fri Mar 11 19:32:34 2011 +0800
+++ b/cgi-bin/LJ/Event/UserMessageRecvd.pm	Tue Mar 15 18:31:28 2011 +0800
@@ -162,6 +162,8 @@ sub content {
 
     my $body = $msg->body;
     $body = LJ::html_newlines($body);
+    $body = "<div class='actions_top'>" . $self->as_html_actions . "</div>" . $body
+        if LJ::has_too_many( $body, linebreaks => 10, characters => 2000 );
 
     return $body . $self->as_html_actions;
 }
diff -r 946170f4ce7a -r b7d0caa4dfb2 cgi-bin/LJ/Widget/InboxFolder.pm
--- a/cgi-bin/LJ/Widget/InboxFolder.pm	Fri Mar 11 19:32:34 2011 +0800
+++ b/cgi-bin/LJ/Widget/InboxFolder.pm	Tue Mar 15 18:31:28 2011 +0800
@@ -84,6 +84,9 @@ sub render_body {
     my $last_page = POSIX::ceil((scalar @$nitems) / $page_limit);
     $last_page ||= 1;
     $page = $last_page if $page > $last_page;
+    my $starting_index = ($page - 1) * $page_limit;
+    ${$opts{itemcount}} = scalar( @$nitems ) - $starting_index;
+    my $duplicate_menus = ${$opts{itemcount}} >= 10;
 
     my $prev_disabled = ($page <= 1) ? 'disabled' : '';
     my $next_disabled = ($page >= $last_page) ? 'disabled' : '';
@@ -132,7 +135,8 @@ sub render_body {
         <div id="${name}_Table" class="NotificationTable">
         <table summary='' id="${name}" class="inbox" cellspacing="0" border="0" cellpadding="0">
     };
-    $messagetable .= $actionsrow->(1);
+    $messagetable .= $actionsrow->(1)
+        if $duplicate_menus;
     $messagetable .= "<tbody id='${name}_Body'>";
 
     unless (@$nitems) {
@@ -146,7 +150,6 @@ sub render_body {
     # print out messages
     my $rownum = 0;
 
-    my $starting_index = ($page - 1) * $page_limit;
     for (my $i = $starting_index; $i < $starting_index + $page_limit; $i++) {
         my $inbox_item = $nitems->[$i];
         last unless $inbox_item;
@@ -212,11 +215,13 @@ sub render_body {
         };
     }
 
-    $messagetable .= $actionsrow->(2);
+    my $actionnumber = $duplicate_menus ? 2 : 1;
+    $messagetable .= $actionsrow->($actionnumber);
 
     $messagetable .= '</tbody></table></div>';
 
-    $messagetable .= $markdeleteall->(2);
+    $messagetable .= $markdeleteall->(2)
+        if $duplicate_menus;
 
     $msgs_body .= $messagetable;
 
diff -r 946170f4ce7a -r b7d0caa4dfb2 cgi-bin/ljtextutil.pl
--- a/cgi-bin/ljtextutil.pl	Fri Mar 11 19:32:34 2011 +0800
+++ b/cgi-bin/ljtextutil.pl	Tue Mar 15 18:31:28 2011 +0800
@@ -315,6 +315,28 @@ sub is_utf8_wrapper {
     }
 }
 
+# <LJFUNC>
+# name: LJ::has_too_many
+# des: checks if text is too long
+# args: text, maxbreaks, maxchars
+# des-text: text to check if too long
+# des-maxbreaks: maximum number of linebreak
+# des-maxchars: maximum number of characters
+# returns: true if text has more than maxbreaks linebreaks or more than maxchars characters
+# </LJFUNC>
+sub has_too_many {
+    my ( $text, %opts ) = @_;
+
+    return 1 if exists $opts{chars} && length( $text ) > $opts{chars};
+
+    if ( exists $opts{linebreaks} ) {
+        my @breaks = $text =~ m/(<br \/>|\n)/g;
+        return 1 if scalar @breaks > $opts{linebreaks};
+    }
+
+    return 0;
+}
+
 
 # alternate version of "lc" that handles UTF-8
 # args: text string for lowercasing
diff -r 946170f4ce7a -r b7d0caa4dfb2 htdocs/inbox/index.bml
--- a/htdocs/inbox/index.bml	Fri Mar 11 19:32:34 2011 +0800
+++ b/htdocs/inbox/index.bml	Tue Mar 15 18:31:28 2011 +0800
@@ -247,14 +247,17 @@ function setup (e) {
                   "button": $(name + "_CheckAll_" + i),
                   "parent": tableview.getView()
             });
-            checkallButton.button.checked = false;
+            if( checkallButton.button )
+                checkallButton.button.checked = false;
         }
     }
 
 // 2 instances of action buttons
 for (var i=1; i<=2; i++) {
-    DOM.addEventListener($("Page_Prev_"+i), "click", Page_Prev);
-    DOM.addEventListener($("Page_Next_"+i), "click", Page_Next);
+    if( $("Page_Prev_"+i) )
+        DOM.addEventListener($("Page_Prev_"+i), "click", Page_Prev);
+    if( $("Page_Next_"+i) )
+        DOM.addEventListener($("Page_Next_"+i), "click", Page_Next);
 }
 
 if ($("pageNum")) pageNum = parseInt($("pageNum").value);
diff -r 946170f4ce7a -r b7d0caa4dfb2 htdocs/js/esn_inbox.js
--- a/htdocs/js/esn_inbox.js	Fri Mar 11 19:32:34 2011 +0800
+++ b/htdocs/js/esn_inbox.js	Tue Mar 15 18:31:28 2011 +0800
@@ -159,15 +159,20 @@ ESN_Inbox.initInboxBtns = function (fold
 ESN_Inbox.initInboxBtns = function (folder, cur_folder, itemid) {
     // 2 instances of action buttons
     for (var i=1; i<=2; i++) {
-        DOM.addEventListener($(folder + "_MarkRead_" + i), "click", function(e) { ESN_Inbox.markRead(e, folder) });
-        DOM.addEventListener($(folder + "_MarkUnread_" + i), "click", function(e) { ESN_Inbox.markUnread(e, folder) });
-        DOM.addEventListener($(folder + "_Delete_" + i), "click", function(e) { ESN_Inbox.deleteItems(e, folder) });
+        if( $(folder + "_MarkRead_" + i) )
+            DOM.addEventListener($(folder + "_MarkRead_" + i), "click", function(e) { ESN_Inbox.markRead(e, folder) });
+        if( $(folder + "_MarkUnread_" + i) )
+            DOM.addEventListener($(folder + "_MarkUnread_" + i), "click", function(e) { ESN_Inbox.markUnread(e, folder) });
+        if( $(folder + "_Delete_" + i) )
+            DOM.addEventListener($(folder + "_Delete_" + i), "click", function(e) { ESN_Inbox.deleteItems(e, folder) });
     }
 
     // 2 instances of mark all and delete all buttons
     for (var i=1; i<=2; i++) {
-        DOM.addEventListener($(folder + "_MarkAllRead_" + i), "click", function(e) { ESN_Inbox.markAllRead(e, folder, cur_folder, itemid) });
-        DOM.addEventListener($(folder + "_DeleteAll_" + i), "click", function(e) { ESN_Inbox.deleteAll(e, folder, cur_folder, itemid) });
+        if( $(folder + "_MarkAllRead_" + i) )
+            DOM.addEventListener($(folder + "_MarkAllRead_" + i), "click", function(e) { ESN_Inbox.markAllRead(e, folder, cur_folder, itemid) });
+        if( $(folder + "_DeleteAll_" + i) )
+            DOM.addEventListener($(folder + "_DeleteAll_" + i), "click", function(e) { ESN_Inbox.deleteAll(e, folder, cur_folder, itemid) });
     }
 };
 
@@ -216,7 +221,8 @@ ESN_Inbox.deleteItems = function (evt, f
 
     // uncheck the checkbox after deleting anything
     for (var i=1; i<=2; i++) {
-        $(folder + "_CheckAll_" + i).checked = false;
+        if( $(folder + "_CheckAll_" + i) )
+            $(folder + "_CheckAll_" + i).checked = false;
     }
 
     return false;
@@ -394,8 +400,10 @@ ESN_Inbox.finishedUpdate = function (inf
 
     // 2 instances of action buttons with suffix 1 and 2
     for (var i=1; i<=2; i++) {
-        $(folder + "_MarkRead_" + i).disabled    = unread_count ? false : true;
-        $(folder + "_MarkAllRead_" + i).disabled = unread_count ? false : true;
+        if( $(folder + "_MarkRead_" + i) )
+            $(folder + "_MarkRead_" + i).disabled    = unread_count ? false : true;
+        if( $(folder + "_MarkAllRead_" + i) )
+            $(folder + "_MarkAllRead_" + i).disabled = unread_count ? false : true;
     }
 };
 
diff -r 946170f4ce7a -r b7d0caa4dfb2 t/textutil.t
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/t/textutil.t	Tue Mar 15 18:31:28 2011 +0800
@@ -0,0 +1,44 @@
+# -*-perl-*-
+
+use strict;
+use Test::More;
+
+use lib "$ENV{LJHOME}/cgi-bin";
+require 'ljtextutil.pl';
+
+plan tests => 16;
+
+note("html breaks");
+ok(   LJ::has_too_many( "abcdn<br />" x 1, linebreaks => 0 ), "0 max, 1 break" );
+
+ok( ! LJ::has_too_many( "abcdn<br />" x 1, linebreaks => 2 ), "2 max, 1 break" );
+ok( ! LJ::has_too_many( "abcdn<br />" x 2, linebreaks => 2 ), "2 max, 2 breaks" );
+ok(   LJ::has_too_many( "abcdn<br />" x 3, linebreaks => 2 ), "2 max, 3 breaks" );
+
+
+note("newlines");
+ok(   LJ::has_too_many( "abcdn\n" x 1, linebreaks => 0 ), "0 max, 1 newline" );
+
+ok( ! LJ::has_too_many( "abcdn\n" x 1, linebreaks => 2 ), "2 max, 1 newline" );
+ok( ! LJ::has_too_many( "abcdn\n" x 2, linebreaks => 2 ), "2 max, 2 newlines" );
+ok(   LJ::has_too_many( "abcdn\n" x 3, linebreaks => 2 ), "2 max, 3 newlines" );
+
+
+note("characters");
+ok(   LJ::has_too_many( "abcdn\n", chars => 0 ), "0 max, 6 characters" );
+
+ok( ! LJ::has_too_many( "abcde\n",   chars => 7 ), "7 max, 6 characters" );
+ok( ! LJ::has_too_many( "abcdef\n",  chars => 7 ), "7 max, 7 characters" );
+ok(   LJ::has_too_many( "abcdefg\n", chars => 7 ), "7 max, 8 characters" );
+
+
+note("mix");
+ok(   LJ::has_too_many( "abcdn\n", chars => 9, linebreaks => 0 ),
+                                "9 chars, 6 chars; 0 linebreaks, 1 break" );
+ok( ! LJ::has_too_many( "abcdn\n", chars => 9, linebreaks => 1 ),
+                                "9 chars, 6 chars; 1 linebreaks, 1 break" );
+
+ok(   LJ::has_too_many( "abcdn\n", chars => 0, linebreaks => 5 ),
+                                "0 chars, 6 chars; 5 linebreaks, 1 break" );
+ok( ! LJ::has_too_many( "abcdn\n", chars => 9, linebreaks => 5 ),
+                                "9 chars, 6 chars; 5 linebreaks, 1 break" );
--------------------------------------------------------------------------------