~launchpad-pqm/launchpad/devel

13042.2.1 by j.c.sackett
Created consolidated comment hiding javascript.
1
/* Copyright 2011 Canonical Ltd.  This software is licensed under the
2
 * GNU Affero General Public License version 3 (see the file LICENSE).
3
 *
13042.2.7 by j.c.sackett
Updated bug and question templates and removed old javascript.
4
 * @namespace Y.lp.comments.hide
13588.9.2 by Gavin Panella
Rename lazr.anim to lp.anim.
5
 * @requires dom, node, lp.anim, lp.client
13042.2.1 by j.c.sackett
Created consolidated comment hiding javascript.
6
 */
13042.2.6 by j.c.sackett
Cleaner names/namespace.
7
YUI.add('lp.comments.hide', function(Y) {
8
var namespace = Y.namespace('lp.comments.hide');
13042.2.1 by j.c.sackett
Created consolidated comment hiding javascript.
9
10
var hidden_class = "adminHiddenComment";
13042.2.9 by j.c.sackett
Final mess of refactor done.
11
var hide_text = "Hide comment";
12
var unhide_text = "Unhide comment";
13042.2.1 by j.c.sackett
Created consolidated comment hiding javascript.
13
14
function update_comment(link, comment) {
15
    var text = link.get('text').trim();
13042.2.13 by j.c.sackett
Clean up from review.
16
    if (text === hide_text) {
13042.2.1 by j.c.sackett
Created consolidated comment hiding javascript.
17
        comment.removeClass(hidden_class);
13042.2.9 by j.c.sackett
Final mess of refactor done.
18
        link.set('text', unhide_text);
13042.2.1 by j.c.sackett
Created consolidated comment hiding javascript.
19
    } else {
20
        comment.addClass(hidden_class);
13042.2.9 by j.c.sackett
Final mess of refactor done.
21
        link.set('text', hide_text);
13042.2.1 by j.c.sackett
Created consolidated comment hiding javascript.
22
    }
23
}
24
25
function set_visibility(parameters, callbacks) {
13042.2.13 by j.c.sackett
Clean up from review.
26
    // comment_context must be setup on pages using this js, and is the
13042.2.1 by j.c.sackett
Created consolidated comment hiding javascript.
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
13042.2.9 by j.c.sackett
Final mess of refactor done.
41
function toggle_hidden(link) {
13042.2.1 by j.c.sackett
Created consolidated comment hiding javascript.
42
    var comment = link.get('parentNode').get('parentNode');
43
    var visible = comment.hasClass('adminHiddenComment');
13042.2.13 by j.c.sackett
Clean up from review.
44
    var comment_number = parseInt(
45
            link.get('id').replace('mark-spam-', ''), 10);
13042.2.1 by j.c.sackett
Created consolidated comment hiding javascript.
46
    parameters = {
47
        visible: visible,
48
        comment_number: comment_number
49
        };
50
    set_visibility(parameters, {
13042.2.12 by j.c.sackett
Fixed a misspelling.
51
        // We use red flash on failure so admins know it didn't work.
13042.2.1 by j.c.sackett
Created consolidated comment hiding javascript.
52
        // There's no green flash on success, b/c the change in bg
53
        // color provides an immediate visual cue.
13042.2.13 by j.c.sackett
Clean up from review.
54
        success: function () {
13042.2.1 by j.c.sackett
Created consolidated comment hiding javascript.
55
            update_comment(link, comment);
56
            comment.toggleClass(hidden_class);
57
            },
58
        failure: function () {
13588.9.2 by Gavin Panella
Rename lazr.anim to lp.anim.
59
            Y.lp.anim.red_flash({node:comment});
13042.2.1 by j.c.sackett
Created consolidated comment hiding javascript.
60
            }
61
        });
62
}
13042.2.9 by j.c.sackett
Final mess of refactor done.
63
namespace.toggle_hidden = toggle_hidden;
13042.2.1 by j.c.sackett
Created consolidated comment hiding javascript.
64
65
function setup_hide_controls() {
66
  Y.on('click', function(e) {
67
      e.halt();
13042.2.9 by j.c.sackett
Final mess of refactor done.
68
      namespace.toggle_hidden(this);
13042.2.1 by j.c.sackett
Created consolidated comment hiding javascript.
69
  }, '.mark-spam');
70
}
13042.2.13 by j.c.sackett
Clean up from review.
71
namespace.setup_hide_controls = setup_hide_controls;
13588.9.2 by Gavin Panella
Rename lazr.anim to lp.anim.
72
}, "0.1", {"requires": ["dom", "node", "lp.anim", "lp.client"]});