fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2010-06-15 01:35 pm

[dw-free] LJ::Talk::js_quote_button

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

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

Code cleanup: reduce code duplication. Refactor out common code for the
quote button (used on site scheme and custom comment pages)

Patch by [personal profile] kareila.

Files modified:
  • bin/upgrading/en.dat
  • cgi-bin/LJ/Talk.pm
  • cgi-bin/weblib.pl
--------------------------------------------------------------------------------
diff -r 79b951d76c59 -r a288bf7c1f88 bin/upgrading/en.dat
--- a/bin/upgrading/en.dat	Tue Jun 15 20:56:18 2010 +0800
+++ b/bin/upgrading/en.dat	Tue Jun 15 21:40:53 2010 +0800
@@ -3629,6 +3629,8 @@ talk.error.notauthorised.comm.closed=Thi
 
 talk.error.purged=This journal has been deleted and purged.
 
+talk.error.quickquote=If you'd like to quote a portion of the original message, highlight it then press 'Quote'.
+
 talk.error.readonly_journal=This journal is read-only. You cannot comment in it.
 
 talk.error.readonly_journal.title=Read-Only
diff -r 79b951d76c59 -r a288bf7c1f88 cgi-bin/LJ/Talk.pm
--- a/cgi-bin/LJ/Talk.pm	Tue Jun 15 20:56:18 2010 +0800
+++ b/cgi-bin/LJ/Talk.pm	Tue Jun 15 21:40:53 2010 +0800
@@ -1752,53 +1752,11 @@ sub talkform {
 
     $ret .= LJ::help_icon_html("noautoformat", " ");
 
-    if ($remote) {
-        # only show on initial compostion
-        my $quickquote;
-        unless ($opts->{errors} && @{$opts->{errors}}) {
-            # quick quote button
-            $quickquote = "&nbsp;&nbsp;" . LJ::ejs('<input type="button" value="Quote" onmousedown="quote();" onclick="quote();" />');
-        }
-
+    if ( $remote ) {
+        # only show quick quote button on initial composition
+        my $hidebutton = ( $opts->{errors} && @{ $opts->{errors} } );
         $ret .= "<script type='text/javascript' language='JavaScript'>\n<!--\n";
-        $ret .= <<"QQ";
-
-        var helped = 0; var pasted = 0;
-        function quote () {
-            var text = '';
-
-            if (document.getSelection) {
-                text = document.getSelection();
-            } else if (document.selection) {
-                text = document.selection.createRange().text;
-            } else if (window.getSelection) {
-                text = window.getSelection();
-            }
-
-            text = text.replace(/^\\s+/, '').replace(/\\s+\$/, '');
-
-            if (text == '') {
-                if (helped != 1 && pasted != 1) {
-                    helped = 1; alert("If you'd like to quote a portion of the original message, highlight it then press 'Quote'");
-                }
-                return false;
-            } else {
-                pasted = 1;
-            }
-
-            var element = text.search(/\\n/) == -1 ? 'q' : 'blockquote';
-            var textarea = document.getElementById('commenttext');
-            textarea.focus();
-            textarea.value = textarea.value + "<" + element + ">" + text + "</" + element + ">";
-            textarea.caretPos = textarea.value;
-            textarea.focus();
-            return false;
-        }
-        if (document.getElementById && (document.getSelection || document.selection || window.getSelection)) {
-            document.write('$quickquote');
-        }
-QQ
-
+        $ret .= LJ::Talk::js_quote_button( 'commenttext', $hidebutton );
         $ret .= "-->\n</script>\n";
     }
 
@@ -1926,6 +1884,61 @@ LOGIN
     $ret .= "</form>\n";
 
     return $ret;
+}
+
+# generate the javascript code for the quick quote button
+# arg1: element corresponds to textarea of caller (body or commenttext)
+# arg2: boolean to hide the button HTML (optional)
+sub js_quote_button {
+    my ( $element, $hidebutton ) = @_;
+    return '' unless $element;
+    my $button = LJ::ejs( '<input type="button" value="Quote"'
+                        . ' onmousedown="quote();" onclick="quote();" />' );
+    $button = '' if $hidebutton;
+    my $buttontext = "document.write('&nbsp;&nbsp;$button')";
+    if ( $element eq 'body' ) {
+        my $span = "document.getElementById('quotebuttonspan').innerHTML";
+        $buttontext = "$span = $span + '$button'";
+    }
+    my $alerttext = LJ::Lang::ml( 'talk.error.quickquote' );
+    return <<"QQ"
+
+    var helped = 0; var pasted = 0;
+    function quote () {
+        var text = '';
+
+        if (document.getSelection) {
+            text = document.getSelection();
+        } else if (document.selection) {
+            text = document.selection.createRange().text;
+        } else if (window.getSelection) {
+            text = window.getSelection();
+        }
+
+        text = text.replace(/^\\s+/, '').replace(/\\s+\$/, '');
+
+        if (text == '') {
+            if (helped != 1 && pasted != 1) {
+                helped = 1;
+                alert("$alerttext");
+            }
+            return false;
+        } else {
+            pasted = 1;
+        }
+
+        var element = text.search(/\\n/) == -1 ? 'q' : 'blockquote';
+        var textarea = document.getElementById('$element');
+        textarea.focus();
+        textarea.value = textarea.value + "<" + element + ">" + text + "</" + element + ">";
+        textarea.caretPos = textarea.value;
+        textarea.focus();
+        return false;
+    }
+    if (document.getElementById && (document.getSelection || document.selection || window.getSelection)) {
+        $buttontext;
+    }
+QQ
 }
 
 # <LJFUNC>
diff -r 79b951d76c59 -r a288bf7c1f88 cgi-bin/weblib.pl
--- a/cgi-bin/weblib.pl	Tue Jun 15 20:56:18 2010 +0800
+++ b/cgi-bin/weblib.pl	Tue Jun 15 21:40:53 2010 +0800
@@ -748,47 +748,6 @@ sub create_qr_div {
         $qrhtml .= "</label>";
     }
 
-    # quick quote button
-    my $quickquote = LJ::ejs( '<input type="button" value="Quote" onmousedown="quote();" onclick="quote();" />' );
-
-    my $qrjscript = <<"QQ";
-
-    var helped = 0; var pasted = 0;
-    function quote () {
-        var text = '';
-
-        if (document.getSelection) {
-            text = document.getSelection();
-        } else if (document.selection) {
-            text = document.selection.createRange().text;
-        } else if (window.getSelection) {
-            text = window.getSelection();
-        }
-
-        text = text.replace(/^\\s+/, '').replace(/\\s+\$/, '');
-
-        if (text == '') {
-            if (helped != 1 && pasted != 1) {
-                helped = 1; alert("If you'd like to quote a portion of the original message, highlight it then press 'Quote'");
-            }
-            return false;
-        } else {
-            pasted = 1;
-        }
-
-        var element = text.search(/\\n/) == -1 ? 'q' : 'blockquote';
-        var textarea = document.getElementById('body');
-        textarea.focus();
-        textarea.value = textarea.value + "<" + element + ">" + text + "</" + element + ">";
-        textarea.caretPos = textarea.value;
-        textarea.focus();
-        return false;
-    }
-    if (document.getElementById && (document.getSelection || document.selection || window.getSelection)) {
-        document.getElementById('quotebuttonspan').innerHTML = document.getElementById('quotebuttonspan').innerHTML + '$quickquote';
-    }
-QQ
-
     if ($u->opt_logcommentips eq 'A') {
         $qrhtml .= '<br />';
         $qrhtml .= LJ::deemp(BML::ml('/talkpost.bml.logyourip'));
@@ -836,7 +795,8 @@ QQ
                }
            };
 
-    $ret .= $qrjscript;
+    # quick quote button
+    $ret .= LJ::Talk::js_quote_button( 'body' );
 
     $ret .= "\n</script>";
 
--------------------------------------------------------------------------------