]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 40896: Bugzilla needs a "preview" mode for comments
authorByron Jones <bjones@mozilla.com>
Thu, 13 Feb 2014 05:45:42 +0000 (13:45 +0800)
committerByron Jones <bjones@mozilla.com>
Thu, 13 Feb 2014 05:45:42 +0000 (13:45 +0800)
r=gerv, a=justdave

Bugzilla/WebService/Bug.pm
js/field.js
skins/standard/global.css
template/en/default/bug/comment.html.tmpl [new file with mode: 0644]
template/en/default/bug/create/create.html.tmpl
template/en/default/bug/edit.html.tmpl

index de78530926162ff8b49dee1b9ff5e3ffdab7c759..9ab217b04c22ab0912636ef3f396da80193bbf07 100644 (file)
@@ -318,6 +318,30 @@ sub comments {
     return { bugs => \%bugs, comments => \%comments };
 }
 
+sub render_comment {
+    my ($self, $params) = @_;
+
+    unless (defined $params->{text}) {
+        ThrowCodeError('params_required',
+                       { function => 'Bug.render_comment',
+                         params   => ['text'] });
+    }
+
+    Bugzilla->switch_to_shadow_db();
+    my $bug = $params->{id} ? Bugzilla::Bug->check($params->{id}) : undef;
+
+    my $tmpl = '[% text FILTER quoteUrls(bug) %]';
+    my $html;
+    my $template = Bugzilla->template;
+    $template->process(
+        \$tmpl,
+        { bug => $bug, text => $params->{text}},
+        \$html
+    );
+
+    return { html => $html };
+}
+
 # Helper for Bug.comments
 sub _translate_comment {
     my ($self, $comment, $filters) = @_;
@@ -4682,6 +4706,48 @@ The comment tag provided is longer than the maximum length.
 
 =back
 
+=head2 render_comment
+
+B<UNSTABLE>
+
+=over
+
+=item B<Description>
+
+Returns the HTML rendering of the provided comment text.
+
+=item B<Params>
+
+=over
+
+=item C<text>
+
+B<Required> C<strings> Text comment text to render.
+
+=item C<id>
+
+C<int> The ID of the bug to render the comment against.
+
+=back
+
+=item B<Returns>
+
+C<html> containing the HTML rendering.
+
+=item B<Errors>
+
+This method can throw all of the errors that L</get> throws.
+
+=item B<History>
+
+=over
+
+=item Added in Bugzilla B<5.0>.
+
+=back
+
+=back
+
 =head1 B<Methods in need of POD>
 
 =over
index 900299ee0e8b5f9974fd4827e79fbd878e98dfa2..670c70a86c6d171499a97876a46ad00947a9c742 100644 (file)
@@ -983,3 +983,79 @@ function initDirtyFieldTracking() {
         });
     }
 }
+
+/**
+ * Comment preview
+ */
+
+var last_comment_text = '';
+
+function show_comment_preview(bug_id) {
+    var Dom = YAHOO.util.Dom;
+    var comment = document.getElementById('comment');
+    var preview = document.getElementById('comment_preview');
+    if (!comment || !preview) return;
+    if (Dom.hasClass('comment_preview_tab', 'active_comment_tab')) return;
+
+    preview.style.width = (comment.clientWidth - 4) + 'px';
+    preview.style.height = comment.offsetHeight + 'px';
+
+    Dom.addClass(comment, 'bz_default_hidden');
+    Dom.removeClass('comment_tab', 'active_comment_tab');
+    Dom.removeClass(preview, 'bz_default_hidden');
+    Dom.addClass('comment_preview_tab', 'active_comment_tab');
+
+    Dom.addClass('comment_preview_error', 'bz_default_hidden');
+
+    if (last_comment_text == comment.value)
+        return;
+
+    Dom.addClass('comment_preview_text', 'bz_default_hidden');
+    Dom.removeClass('comment_preview_loading', 'bz_default_hidden');
+
+    YAHOO.util.Connect.setDefaultPostHeader('application/json', true);
+    YAHOO.util.Connect.asyncRequest('POST', 'jsonrpc.cgi',
+    {
+        success: function(res) {
+            data = YAHOO.lang.JSON.parse(res.responseText);
+            if (data.error) {
+                Dom.addClass('comment_preview_loading', 'bz_default_hidden');
+                Dom.removeClass('comment_preview_error', 'bz_default_hidden');
+                Dom.get('comment_preview_error').innerHTML =
+                    YAHOO.lang.escapeHTML(data.error.message);
+            } else {
+                document.getElementById('comment_preview_text').innerHTML = data.result.html;
+                Dom.addClass('comment_preview_loading', 'bz_default_hidden');
+                Dom.removeClass('comment_preview_text', 'bz_default_hidden');
+                last_comment_text = comment.value;
+            }
+        },
+        failure: function(res) {
+            Dom.addClass('comment_preview_loading', 'bz_default_hidden');
+            Dom.removeClass('comment_preview_error', 'bz_default_hidden');
+            Dom.get('comment_preview_error').innerHTML =
+                YAHOO.lang.escapeHTML(res.responseText);
+        }
+    },
+    YAHOO.lang.JSON.stringify({
+        version: "1.1",
+        method: 'Bug.render_comment',
+        params: {
+            id: bug_id,
+            text: comment.value
+        }
+    })
+    );
+}
+
+function show_comment_edit() {
+    var comment = document.getElementById('comment');
+    var preview = document.getElementById('comment_preview');
+    if (!comment || !preview) return;
+    if (YAHOO.util.Dom.hasClass(comment, 'active_comment_tab')) return;
+
+    YAHOO.util.Dom.addClass(preview, 'bz_default_hidden');
+    YAHOO.util.Dom.removeClass('comment_preview_tab', 'active_comment_tab');
+    YAHOO.util.Dom.removeClass(comment, 'bz_default_hidden');
+    YAHOO.util.Dom.addClass('comment_tab', 'active_comment_tab');
+}
index 29dd9f44e48cc5e1d41b42147069777461e70be5..18bec598a27c70f46c1ab145b8ec0f3b1c9fa9c5 100644 (file)
@@ -678,6 +678,40 @@ input.required, select.required, span.required_explanation {
     margin-left: -1px;
 }
 
+.comment_tab {
+    float: left;
+    border: 1px solid silver;
+    border-bottom: 0px;
+    padding: 2px 1em;
+    cursor: pointer;
+    background: transparent;
+}
+
+.active_comment_tab {
+    background: #fff;
+    font-weight: bold;
+}
+
+#comment_preview {
+    border: 1px solid silver;
+    padding: 2px;
+    overflow: auto;
+    margin: 0px;
+}
+
+#comment_preview_text {
+    margin: 0px;
+    width: auto;
+}
+
+#comment_preview_loading {
+    font-style: italic;
+}
+
+#comment {
+    margin-top: 0px;
+}
+
 /*******************/
 /* Form Validation */
 /*******************/
diff --git a/template/en/default/bug/comment.html.tmpl b/template/en/default/bug/comment.html.tmpl
new file mode 100644 (file)
index 0000000..1d8cf0b
--- /dev/null
@@ -0,0 +1,36 @@
+[%# This Source Code Form is subject to the terms of the Mozilla Public
+  # License, v. 2.0. If a copy of the MPL was not distributed with this
+  # file, You can obtain one at http://mozilla.org/MPL/2.0/.
+  #
+  # This Source Code Form is "Incompatible With Secondary Licenses", as
+  # defined by the Mozilla Public License, v. 2.0.
+  #%]
+
+[%# INTERFACE:
+  #
+  # This template supports the same parameters as global/textarea.html.tmpl
+  # with the exception of "name" and "id", which will always be "comment".
+  #%]
+
+[% IF feature_enabled('jsonrpc') %]
+  <div id="comment_tabs">
+    <div id="comment_tab" class="comment_tab active_comment_tab"
+          onclick="show_comment_edit()">Comment</div>
+    <div id="comment_preview_tab" class="comment_tab"
+          onclick="show_comment_preview([% bug.id FILTER none %])">Preview</div>
+  </div>
+[% END %]
+
+[% INCLUDE global/textarea.html.tmpl
+   name = "comment"
+   id   = "comment"
+%]
+<br>
+
+[% IF feature_enabled('jsonrpc') %]
+  <div id="comment_preview" class="bz_default_hidden bz_comment">
+    <div id="comment_preview_loading" class="bz_default_hidden">Generating Preview...</div>
+    <div id="comment_preview_error" class="bz_default_hidden"></div>
+    <pre id="comment_preview_text" class="bz_comment_text"></pre>
+  </div>
+[% END %]
index 6adf75102174b23bdfdf4c4d1f4604109aa1be70..d372c4d08e755c6684a24dedc61b7273320815b6 100644 (file)
@@ -542,9 +542,7 @@ TUI_hide_default('attachment_text_field');
           # by global/textarea.html.tmpl. So we must not escape the comment here. %]
         [% comment FILTER none %]
       [%- END %]
-      [% INCLUDE global/textarea.html.tmpl
-         name           = 'comment'
-         id             = 'comment'
+      [% INCLUDE bug/comment.html.tmpl
          minrows        = 10
          maxrows        = 25
          cols           = constants.COMMENT_COLS
index 3b93ebe2be7503f2595cd4a035a7a53a6b46f7f5..89b78bc9f37a99c476f43e9a07f94474cbe4a7bd 100644 (file)
       <!-- This table keeps the submit button aligned with the box. -->
       <table><tr><td>
         [% IF bug.check_can_change_field('longdesc', 0, 1) %]
-          [% INCLUDE global/textarea.html.tmpl
-                     name      = 'comment'
-                     id        = 'comment'
+          [% INCLUDE bug/comment.html.tmpl
                      minrows   = 10
                      maxrows   = 25
                      cols      = constants.COMMENT_COLS