summaryrefslogtreecommitdiffstats
path: root/extensions/TrackingFlags/web/js/tracking_flags.js
blob: 041ae43f5f76ac3b6939fd121bbc8aa9a574f9d9 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* 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.
 */

var Dom = YAHOO.util.Dom;

function hide_tracking_flags() {
    for (var i = 0, l = TrackingFlags.types.length; i < l; i++) {
        var flag_type = TrackingFlags.types[i];
        for (var field in TrackingFlags.flags[flag_type]) {
            var el = Dom.get(field);
            var value = el ? el.value : TrackingFlags.flags[flag_type][field];
            if (el && (value != TrackingFlags.flags[flag_type][field])) {
                show_tracking_flags(flag_type);
                return;
            }
            if (value == '---') {
                Dom.addClass('row_' + field, 'bz_default_hidden');
            } else {
                Dom.addClass(field, 'bz_default_hidden');
                Dom.removeClass('ro_' + field, 'bz_default_hidden');
            }
        }
    }
}

function show_tracking_flags(flag_type) {
    Dom.addClass('edit_' + flag_type + '_flags_action', 'bz_default_hidden');
    for (var field in TrackingFlags.flags[flag_type]) {
        if (Dom.get(field).value == '---') {
            Dom.removeClass('row_' + field, 'bz_default_hidden');
        } else {
            Dom.removeClass(field, 'bz_default_hidden');
            Dom.addClass('ro_' + field, 'bz_default_hidden');
        }
    }
}

function tracking_flag_change(e) {
    var value = e.value;
    var prefill;
    if (TrackingFlags.comments[e.name])
        prefill = TrackingFlags.comments[e.name][e.value];
    if (!prefill) {
        var cr = document.getElementById('cr_' + e.id);
        if (cr)
            cr.parentElement.removeChild(cr);
        return;
    }
    if (!document.getElementById('cr_' + e.id)) {
        // create "comment required"
        var span = document.createElement('span');
        span.id = 'cr_' + e.id;
        span.appendChild(document.createTextNode('('));
        var a = document.createElement('a');
        a.appendChild(document.createTextNode('comment required'));
        a.href = '#';
        a.onclick = function() {
            var c = document.getElementById('comment');
            c.focus();
            c.select();
            document.getElementById('add_comment').scrollIntoView();
            return false;
        };
        span.appendChild(a);
        span.appendChild(document.createTextNode(')'));
        e.parentNode.appendChild(span);
    }
    // prefill comment
    var commentEl = document.getElementById('comment');
    if (!commentEl)
        return;
    var value = commentEl.value;
    if (value == prefill)
        return;
    if (value == '') {
        commentEl.value = prefill;
    } else {
        commentEl.value = prefill + "\n\n" + value;
    }
}

YAHOO.util.Event.onDOMReady(function() {
    var edit_tracking_links = Dom.getElementsByClassName('edit_tracking_flags_link');
    for (var i = 0, l = edit_tracking_links.length; i < l; i++) {
        YAHOO.util.Event.addListener(edit_tracking_links[i], 'click', function(e) {
            e.preventDefault();
            show_tracking_flags(this.name);
        });
    }
});