~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/app/javascript/hide_comment.js

Merge db-devel.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2011 Canonical Ltd.  This software is licensed under the
 
2
 * GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 *
 
4
 * @namespace Y.lp.comments.hide
 
5
 * @requires dom, node, lazr.anim, lp.client
 
6
 */
 
7
YUI.add('lp.comments.hide', function(Y) {
 
8
var namespace = Y.namespace('lp.comments.hide');
 
9
 
 
10
var hidden_class = "adminHiddenComment";
 
11
var hide_text = "Hide comment";
 
12
var unhide_text = "Unhide comment";
 
13
 
 
14
function update_comment(link, comment) {
 
15
    var text = link.get('text').trim();
 
16
    if (text === hide_text) {
 
17
        comment.removeClass(hidden_class);
 
18
        link.set('text', unhide_text);
 
19
    } else {
 
20
        comment.addClass(hidden_class);
 
21
        link.set('text', hide_text);
 
22
    }
 
23
}
 
24
 
 
25
function set_visibility(parameters, callbacks) {
 
26
    // comment_context must be setup on pages using this js, and is the
 
27
    // context for a comment (e.g. bug).
 
28
    var comment_context = LP.cache.comment_context;
 
29
    var lp_client = new Y.lp.client.Launchpad();
 
30
    var config = {
 
31
        on: {
 
32
            success: callbacks.success,
 
33
            failure: callbacks.failure
 
34
            },
 
35
        parameters: parameters
 
36
        }
 
37
    lp_client.named_post(
 
38
        comment_context.self_link, 'setCommentVisibility', config);
 
39
}
 
40
 
 
41
function toggle_hidden(link) {
 
42
    var comment = link.get('parentNode').get('parentNode');
 
43
    var visible = comment.hasClass('adminHiddenComment');
 
44
    var comment_number = parseInt(
 
45
            link.get('id').replace('mark-spam-', ''), 10);
 
46
    parameters = {
 
47
        visible: visible,
 
48
        comment_number: comment_number
 
49
        };
 
50
    set_visibility(parameters, {
 
51
        // We use red flash on failure so admins know it didn't work.
 
52
        // There's no green flash on success, b/c the change in bg
 
53
        // color provides an immediate visual cue.
 
54
        success: function () {
 
55
            update_comment(link, comment);
 
56
            comment.toggleClass(hidden_class);
 
57
            },
 
58
        failure: function () {
 
59
            Y.lazr.anim.red_flash({node:comment});
 
60
            }
 
61
        });
 
62
}
 
63
namespace.toggle_hidden = toggle_hidden;
 
64
 
 
65
function setup_hide_controls() {
 
66
  Y.on('click', function(e) {
 
67
      e.halt();
 
68
      namespace.toggle_hidden(this);
 
69
  }, '.mark-spam');
 
70
}
 
71
namespace.setup_hide_controls = setup_hide_controls;
 
72
}, "0.1", {"requires": ["dom", "node", "lazr.anim", "lp.client"]});