comment-tagging.js 14.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* 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;

YAHOO.bugzilla.commentTagging = {
    ctag_div  : false,
    ctag_add  : false,
    counter   : 0,
    min_len   : 3,
    max_len   : 24,
    tags_by_no: {},
    nos_by_tag: {},
    current_id: 0,
    current_no: -1,
    can_edit  : false,
    pending   : {},

23 24 25 26
    label        : '',
    min_len_error: '',
    max_len_error: '',

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
    init : function(can_edit) {
        this.can_edit = can_edit;
        this.ctag_div = Dom.get('bz_ctag_div');
        this.ctag_add = Dom.get('bz_ctag_add');
        YAHOO.util.Event.on(this.ctag_add, 'keypress', this.onKeyPress);
        YAHOO.util.Event.onDOMReady(function() {
            YAHOO.bugzilla.commentTagging.updateCollapseControls();
        });
        if (!can_edit) return;

        var ds = new YAHOO.util.XHRDataSource("jsonrpc.cgi");
        ds.connTimeout = 30000;
        ds.connMethodPost = true;
        ds.connXhrMode = "cancelStaleRequests";
        ds.maxCacheEntries = 5;
        ds.responseSchema = {
            metaFields : { error: "error", jsonRpcId: "id"},
            resultsList : "result"
        };

        var ac = new YAHOO.widget.AutoComplete('bz_ctag_add', 'bz_ctag_autocomp', ds);
        ac.maxResultsDisplayed = 7;
        ac.generateRequest = function(query) {
            query = YAHOO.lang.trim(query);
            YAHOO.bugzilla.commentTagging.last_query = query;
            YAHOO.bugzilla.commentTagging.counter = YAHOO.bugzilla.commentTagging.counter + 1;
            YAHOO.util.Connect.setDefaultPostHeader('application/json', true);
            return YAHOO.lang.JSON.stringify({
55
                version: "1.1",
56 57
                method : "Bug.search_comment_tags",
                id : YAHOO.bugzilla.commentTagging.counter,
58 59 60 61 62
                params : {
                    Bugzilla_api_token: BUGZILLA.api_token,
                    query : query,
                    limit : 10
                }
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
            });
        };
        ac.minQueryLength = this.min_len;
        ac.autoHighlight = false;
        ac.typeAhead = true;
        ac.queryDelay = 0.5;
        ac.dataReturnEvent.subscribe(function(type, args) {
            args[0].autoHighlight = args[2].length == 1;
        });
    },

    toggle : function(comment_id, comment_no) {
        if (!this.ctag_div) return;
        var tags_container = Dom.get('ct_' + comment_no);

        if (this.current_id == comment_id) {
            // hide
            this.current_id = 0;
            this.current_no = -1;
            Dom.addClass(this.ctag_div, 'bz_default_hidden');
            this.hideError();
            window.focus();

        } else {
            // show or move
            this.rpcRefresh(comment_id, comment_no);
            this.current_id = comment_id;
            this.current_no = comment_no;
            this.ctag_add.value = '';
            tags_container.parentNode.insertBefore(this.ctag_div, tags_container);
            Dom.removeClass(this.ctag_div, 'bz_default_hidden');
            Dom.removeClass(tags_container, 'bz_default_hidden');
            var comment = Dom.get('comment_text_' + comment_no);
            if (Dom.hasClass(comment, 'collapsed')) {
                var link = Dom.get('comment_link_' + comment_no);
                expand_comment(link, comment, comment_no);
            }
            window.setTimeout(function() {
                YAHOO.bugzilla.commentTagging.ctag_add.focus();
            }, 50);
        }
    },

    hideInput : function() {
        if (this.current_id != 0) {
            this.toggle(this.current_id, this.current_no);
        }
        this.hideError();
    },

    showError : function(comment_id, comment_no, error) {
        var bz_ctag_error = Dom.get('bz_ctag_error');
        var tags_container = Dom.get('ct_' + comment_no);
        tags_container.parentNode.appendChild(bz_ctag_error, tags_container);
        Dom.get('bz_ctag_error_msg').innerHTML = YAHOO.lang.escapeHTML(error);
        Dom.removeClass(bz_ctag_error, 'bz_default_hidden');
    },

    hideError : function() {
        Dom.addClass('bz_ctag_error', 'bz_default_hidden');
    },

    onKeyPress : function(evt) {
        evt = evt || window.event;
        var charCode = evt.charCode || evt.keyCode;
        if (evt.keyCode == 27) {
            // escape
            YAHOO.bugzilla.commentTagging.hideInput();
            YAHOO.util.Event.stopEvent(evt);

        } else if (evt.keyCode == 13) {
            // return
            YAHOO.util.Event.stopEvent(evt);
            var tags = YAHOO.bugzilla.commentTagging.ctag_add.value.split(/[ ,]/);
            var comment_id = YAHOO.bugzilla.commentTagging.current_id;
            var comment_no = YAHOO.bugzilla.commentTagging.current_no;
            YAHOO.bugzilla.commentTagging.hideInput();
            try {
                YAHOO.bugzilla.commentTagging.add(comment_id, comment_no, tags);
            } catch(e) {
                YAHOO.bugzilla.commentTagging.showError(comment_id, comment_no, e.message);
            }
        }
    },

    showTags : function(comment_id, comment_no, tags) {
        // remove existing tags
        var tags_container = Dom.get('ct_' + comment_no);
        while (tags_container.hasChildNodes()) {
            tags_container.removeChild(tags_container.lastChild);
        }
        // add tags
        if (tags != '') {
            if (typeof(tags) == 'string') {
                tags = tags.split(',');
            }
            for (var i = 0, l = tags.length; i < l; i++) {
                tags_container.appendChild(this.buildTagHtml(comment_id, comment_no, tags[i]));
            }
        }
        // update tracking array
        this.tags_by_no['c' + comment_no] = tags;
        this.updateCollapseControls();
    },

    updateCollapseControls : function() {
        var container = Dom.get('comment_tags_collapse_expand_container');
        if (!container) return;
        // build list of tags
        this.nos_by_tag = {};
        for (var id in this.tags_by_no) {
            if (this.tags_by_no.hasOwnProperty(id)) {
                for (var i = 0, l = this.tags_by_no[id].length; i < l; i++) {
                    var tag = this.tags_by_no[id][i].toLowerCase();
                    if (!this.nos_by_tag.hasOwnProperty(tag)) {
                        this.nos_by_tag[tag] = [];
                    }
                    this.nos_by_tag[tag].push(id);
                }
            }
        }
        var tags = [];
        for (var tag in this.nos_by_tag) {
            if (this.nos_by_tag.hasOwnProperty(tag)) {
                tags.push(tag);
            }
        }
        tags.sort();
        if (tags.length) {
            var div = document.createElement('div');
193
            div.appendChild(document.createTextNode(this.label));
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
            var ul = document.createElement('ul');
            ul.id = 'comment_tags_collapse_expand';
            div.appendChild(ul);
            for (var i = 0, l = tags.length; i < l; i++) {
                var tag = tags[i];
                var li = document.createElement('li');
                ul.appendChild(li);
                var a = document.createElement('a');
                li.appendChild(a);
                Dom.setAttribute(a, 'href', '#');
                YAHOO.util.Event.addListener(a, 'click', function(evt, tag) {
                    YAHOO.bugzilla.commentTagging.toggleCollapse(tag);
                    YAHOO.util.Event.stopEvent(evt);
                }, tag);
                li.appendChild(document.createTextNode(' (' + this.nos_by_tag[tag].length + ')'));
209
                a.innerHTML = YAHOO.lang.escapeHTML(tag);
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
            }
            while (container.hasChildNodes()) {
                container.removeChild(container.lastChild);
            }
            container.appendChild(div);
        } else {
            while (container.hasChildNodes()) {
                container.removeChild(container.lastChild);
            }
        }
    },

    toggleCollapse : function(tag) {
        var nos = this.nos_by_tag[tag];
        if (!nos) return;
        toggle_all_comments('collapse');
        for (var i = 0, l = nos.length; i < l; i++) {
            var comment_no = nos[i].match(/\d+$/)[0];
            var comment = Dom.get('comment_text_' + comment_no);
            var link = Dom.get('comment_link_' + comment_no);
            expand_comment(link, comment, comment_no);
        }
    },

    buildTagHtml : function(comment_id, comment_no, tag) {
        var el = document.createElement('span');
        Dom.setAttribute(el, 'id', 'ct_' + comment_no + '_' + tag);
        Dom.addClass(el, 'bz_comment_tag');
        if (this.can_edit) {
            var a = document.createElement('a');
            Dom.setAttribute(a, 'href', '#');
            YAHOO.util.Event.addListener(a, 'click', function(evt, args) {
                YAHOO.bugzilla.commentTagging.remove(args[0], args[1], args[2])
                YAHOO.util.Event.stopEvent(evt);
            }, [comment_id, comment_no, tag]);
            a.appendChild(document.createTextNode('x'));
            el.appendChild(a);
            el.appendChild(document.createTextNode("\u00a0"));
        }
        el.appendChild(document.createTextNode(tag));
        return el;
    },

    add : function(comment_id, comment_no, add_tags) {
        // build list of current tags from html
        var tags = new Array();
        var spans = Dom.getElementsByClassName('bz_comment_tag', undefined, 'ct_' + comment_no);
        for (var i = 0, l = spans.length; i < l; i++) {
            tags.push(spans[i].textContent.substr(2));
        }
        // add new tags
        var new_tags = new Array();
        for (var i = 0, l = add_tags.length; i < l; i++) {
            var tag = YAHOO.lang.trim(add_tags[i]);
            // validation
            if (tag == '')
                continue;
            if (tag.length < YAHOO.bugzilla.commentTagging.min_len)
268
                throw new Error(this.min_len_error)
269
            if (tag.length > YAHOO.bugzilla.commentTagging.max_len)
270
                throw new Error(this.max_len_error)
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
            // append new tag
            if (bz_isValueInArrayIgnoreCase(tags, tag))
                continue;
            new_tags.push(tag);
            tags.push(tag);
        }
        tags.sort();
        // update
        this.showTags(comment_id, comment_no, tags);
        this.rpcUpdate(comment_id, comment_no, new_tags, undefined);
    },

    remove : function(comment_id, comment_no, tag) {
        var el = Dom.get('ct_' + comment_no + '_' + tag);
        if (el) {
            el.parentNode.removeChild(el);
            this.rpcUpdate(comment_id, comment_no, undefined, [ tag ]);
        }
    },

    // If multiple updates are triggered quickly, overlapping refresh events
    // are generated. We ignore all events except the last one.
    incPending : function(comment_id) {
        if (this.pending['c' + comment_id] == undefined) {
            this.pending['c' + comment_id] = 1;
        } else {
            this.pending['c' + comment_id]++;
        }
    },

    decPending : function(comment_id) {
        if (this.pending['c' + comment_id] != undefined)
            this.pending['c' + comment_id]--;
    },

    hasPending : function(comment_id) {
        return this.pending['c' + comment_id] != undefined
               && this.pending['c' + comment_id] > 0;
    },

    rpcRefresh : function(comment_id, comment_no, noRefreshOnError) {
        this.incPending(comment_id);
        YAHOO.util.Connect.setDefaultPostHeader('application/json', true);
        YAHOO.util.Connect.asyncRequest('POST', 'jsonrpc.cgi',
        {
            success: function(res) {
                YAHOO.bugzilla.commentTagging.decPending(comment_id);
                data = YAHOO.lang.JSON.parse(res.responseText);
                if (data.error) {
                    YAHOO.bugzilla.commentTagging.handleRpcError(
                        comment_id, comment_no, data.error.message, noRefreshOnError);
                    return;
                }

                if (!YAHOO.bugzilla.commentTagging.hasPending(comment_id))
                    YAHOO.bugzilla.commentTagging.showTags(
                        comment_id, comment_no, data.result.comments[comment_id].tags);
            },
            failure: function(res) {
                YAHOO.bugzilla.commentTagging.decPending(comment_id);
                YAHOO.bugzilla.commentTagging.handleRpcError(
                    comment_id, comment_no, res.responseText, noRefreshOnError);
            }
        },
        YAHOO.lang.JSON.stringify({
            version: "1.1",
            method: 'Bug.comments',
            params: {
339
                Bugzilla_api_token: BUGZILLA.api_token,
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
                comment_ids: [ comment_id ],
                include_fields: [ 'tags' ]
            }
        })
        );
    },

    rpcUpdate : function(comment_id, comment_no, add, remove) {
        this.incPending(comment_id);
        YAHOO.util.Connect.setDefaultPostHeader('application/json', true);
        YAHOO.util.Connect.asyncRequest('POST', 'jsonrpc.cgi',
        {
            success: function(res) {
                YAHOO.bugzilla.commentTagging.decPending(comment_id);
                data = YAHOO.lang.JSON.parse(res.responseText);
                if (data.error) {
                    YAHOO.bugzilla.commentTagging.handleRpcError(comment_id, comment_no, data.error.message);
                    return;
                }

                if (!YAHOO.bugzilla.commentTagging.hasPending(comment_id))
                    YAHOO.bugzilla.commentTagging.showTags(comment_id, comment_no, data.result);
            },
            failure: function(res) {
                YAHOO.bugzilla.commentTagging.decPending(comment_id);
                YAHOO.bugzilla.commentTagging.handleRpcError(comment_id, comment_no, res.responseText);
            }
        },
        YAHOO.lang.JSON.stringify({
            version: "1.1",
            method: 'Bug.update_comment_tags',
            params: {
372
                Bugzilla_api_token: BUGZILLA.api_token,
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
                comment_id: comment_id,
                add: add,
                remove: remove
            }
        })
        );
    },

    handleRpcError : function(comment_id, comment_no, message, noRefreshOnError) {
        YAHOO.bugzilla.commentTagging.showError(comment_id, comment_no, message);
        if (!noRefreshOnError) {
            YAHOO.bugzilla.commentTagging.rpcRefresh(comment_id, comment_no, true);
        }
    }
}