summaryrefslogtreecommitdiffstats
path: root/extensions/EditComments/web/js/editcomments.js
blob: 222c081cff4377bb3052a65526d3955c5a9ab5f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* 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.
 */

$(function() {
    $('.edit-comment-btn')
        .click(function(event) {
            event.preventDefault();
            var that = $(this);
            var id = that.data('id');
            var no = that.data('no');

            // cancel editing
            if (that.data('editing')) {
                that.data('editing', false).text('Edit');
                $('#edit_comment_textarea_' + id).remove();
                $('#ct-' + no).show();
                return;
            }
            that.text('Unedit');

            // replace comment <pre> with loading message
            $('#ct-' + no)
                .hide()
                .after(
                    $('<pre/>')
                        .attr('id', 'edit-comment-loading-' + id)
                        .addClass('edit-comment-loading')
                        .text('Loading...')
                );

            // load original comment text
            bugzilla_ajax(
                {
                    url: 'rest/editcomments/comment/' + id,
                    hideError: true
                },
                function(data) {
                    // create editing textarea
                    $('#edit-comment-loading-' + id).remove();
                    that.data('editing', true);
                    $('#ct-' + no)
                        .after(
                            $('<textarea/>')
                                .attr('name', 'edit_comment_textarea_' + id)
                                .attr('id', 'edit_comment_textarea_' + id)
                                .addClass('edit-comment-textarea')
                                .val(data.comments[id])
                        );
                },
                function(message) {
                    // unedit and show message
                    that.data('editing', false).text('Edit');
                    $('#edit-comment-loading-' + id).remove();
                    $('#ct-' + no).show();
                    alert(message);
                }
            );
        });
});