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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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
268
269
270
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
339
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
|
/* 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() {
'use strict';
// comment collapse/expand
function toggleChange(spinner, forced) {
var spinnerID = spinner.attr('id');
var id = spinnerID.substring(spinnerID.indexOf('-') + 1);
var activitySelector = $('#view-toggle-cc').data('shown') === '1' ? '.activity' : '.activity:not(.cc-only)';
// non-comment toggle
if (spinnerID.substr(0, 1) == 'a') {
var changeSet = spinner.parents('.change-set');
if (forced == 'hide') {
changeSet.find(activitySelector).hide();
changeSet.find('.gravatar').css('width', '16px').css('height', '16px');
$('#ar-' + id).hide();
spinner.text('+');
}
else if (forced == 'show' || forced == 'reset') {
changeSet.find(activitySelector).show();
changeSet.find('.gravatar').css('width', '32px').css('height', '32px');
$('#ar-' + id).show();
spinner.text('-');
}
else {
changeSet.find(activitySelector).slideToggle('fast', function() {
$('#ar-' + id).toggle();
if (changeSet.find(activitySelector + ':visible').length) {
changeSet.find('.gravatar').css('width', '32px').css('height', '32px');
spinner.text('-');
}
else {
changeSet.find('.gravatar').css('width', '16px').css('height', '16px');
spinner.text('+');
}
});
}
return;
}
// find the "real spinner", which is the one on the non-default-collapsed block
var realSpinner = $('#cs-' + id);
var defaultCollapsed = realSpinner.data('ch');
if (defaultCollapsed === undefined) {
defaultCollapsed = spinner.attr('id').substring(0, 4) === 'ccs-';
realSpinner.data('ch', defaultCollapsed);
}
if (forced === 'reset') {
forced = defaultCollapsed ? 'hide' : 'show';
}
// comment toggle
if (forced === 'hide') {
if (defaultCollapsed) {
$('#ch-' + id).hide();
$('#cc-' + id).show();
}
$('#ct-' + id).hide();
if (BUGZILLA.user.id !== 0)
$('#ctag-' + id).hide();
$('#c' + id).find(activitySelector).hide();
$('#c' + id).find('.comment-tags').hide();
$('#c' + id).find('.comment-tags').hide();
$('#c' + id).find('.gravatar').css('width', '16px').css('height', '16px');
$('#cr-' + id).hide();
realSpinner.text('+');
}
else if (forced == 'show') {
if (defaultCollapsed) {
$('#cc-' + id).hide();
$('#ch-' + id).show();
}
$('#ct-' + id).show();
if (BUGZILLA.user.id !== 0)
$('#ctag-' + id).show();
$('#c' + id).find(activitySelector).show();
$('#c' + id).find('.comment-tags').show();
$('#c' + id).find('.comment-tags').show();
$('#c' + id).find('.gravatar').css('width', '32px').css('height', '32px');
$('#cr-' + id).show();
realSpinner.text('-');
}
else {
$('#ct-' + id).slideToggle('fast', function() {
$('#c' + id).find(activitySelector).toggle();
if ($('#ct-' + id + ':visible').length) {
$('#c' + id).find('.comment-tags').show();
realSpinner.text('-');
$('#cr-' + id).show();
if (BUGZILLA.user.id !== 0)
$('#ctag-' + id).show();
$('#c' + id).find('.gravatar').css('width', '32px').css('height', '32px');
if (defaultCollapsed) {
$('#cc-' + id).hide();
$('#ch-' + id).show();
}
}
else {
$('#c' + id).find('.comment-tags').hide();
realSpinner.text('+');
$('#cr-' + id).hide();
if (BUGZILLA.user.id !== 0)
$('#ctag-' + id).hide();
$('#c' + id).find('.gravatar').css('width', '16px').css('height', '16px');
if (defaultCollapsed) {
$('#ch-' + id).hide();
$('#cc-' + id).show();
}
}
});
}
}
$('.change-spinner')
.click(function(event) {
event.preventDefault();
toggleChange($(this));
});
// view and tag menus
$('#view-reset')
.click(function() {
$('.change-spinner:visible').each(function() {
toggleChange($(this), 'reset');
});
});
$('#view-collapse-all')
.click(function() {
$('.change-spinner:visible').each(function() {
toggleChange($(this), 'hide');
});
});
$('#view-expand-all')
.click(function() {
$('.change-spinner:visible').each(function() {
toggleChange($(this), 'show');
});
});
$('#view-comments-only')
.click(function() {
$('.change-spinner:visible').each(function() {
toggleChange($(this), this.id.substr(0, 3) === 'cs-' ? 'show' : 'hide');
});
});
$('#view-toggle-cc')
.click(function() {
var that = $(this);
var item = $('.context-menu-item.hover');
if (that.data('shown') === '1') {
that.data('shown', '0');
item.text('Show CC Changes');
$('.cc-only').hide();
}
else {
that.data('shown', '1');
item.text('Hide CC Changes');
$('.cc-only').show();
}
});
$('#view-toggle-treeherder')
.click(function() {
var that = $(this);
console.log(that.data('userid'));
var item = $('.context-menu-item.hover');
if (that.data('hidden') === '1') {
that.data('hidden', '0');
item.text('Hide Treeherder Comments');
$('.ca-' + that.data('userid')).show();
}
else {
that.data('hidden', '1');
item.text('Show Treeherder Comments');
$('.ca-' + that.data('userid')).hide();
}
});
function updateTagsMenu() {
var tags = [];
$('.comment-tags').each(function() {
$.each(tagsFromDom($(this)), function() {
var tag = this.toLowerCase();
if (tag in tags) {
tags[tag]++;
}
else {
tags[tag] = 1;
}
});
});
var tagNames = Object.keys(tags);
tagNames.sort();
var btn = $('#comment-tags-btn');
if (tagNames.length === 0) {
btn.hide();
return;
}
btn.show();
// clear out old li items. Always leave the first one (Reset)
var $li = $('#comment-tags-menu li');
for (var i = 1, l = $li.length; i < l; i++) {
$li.eq(i).remove();
}
// add new li items
$.each(tagNames, function(key, value) {
$('#comment-tags-menu')
.append($('<li role="presentation">')
.append($('<a role="menuitem" tabindex="-1" data-comment-tag="' + value + '">')
.append(value + ' (' + tags[value] + ')')));
});
$('a[data-comment-tag]').each(function() {
$(this).click(function() {
var $that = $(this);
var tag = $that.data('comment-tag');
if (tag === '') {
$('.change-spinner:visible').each(function() {
toggleChange($(this), 'reset');
});
return;
}
var firstComment = false;
$('.change-spinner:visible').each(function() {
var $that = $(this);
var commentTags = tagsFromDom($that.parents('.comment').find('.comment-tags'));
var hasTag = $.inArrayIn(tag, commentTags) >= 0;
toggleChange($that, hasTag ? 'show' : 'hide');
if (hasTag && !firstComment) {
firstComment = $that;
}
});
if (firstComment)
$.scrollTo(firstComment);
});
});
}
//
// anything after this point is only executed for logged in users
//
if (BUGZILLA.user.id === 0) return;
// comment tagging
function taggingError(commentNo, message) {
$('#ctag-' + commentNo + ' .comment-tags').append($('#ctag-error'));
$('#ctag-error-message').text(message);
$('#ctag-error').show();
}
function deleteTag(event) {
event.preventDefault();
$('#ctag-error').hide();
var that = $(this);
var comment = that.parents('.comment');
var commentNo = comment.data('no');
var commentID = comment.data('id');
var tag = that.parent('.comment-tag').contents().filter(function() {
return this.nodeType === 3;
}).text();
var container = that.parents('.comment-tags');
// update ui
that.parent('.comment-tag').remove();
renderTags(commentNo, tagsFromDom(container));
updateTagsMenu();
// update bugzilla
bugzilla_ajax(
{
url: 'rest/bug/comment/' + commentID + '/tags',
type: 'PUT',
data: { remove: [ tag ] },
hideError: true
},
function(data) {
renderTags(commentNo, data);
updateTagsMenu();
},
function(message) {
taggingError(commentNo, message);
}
);
}
$('.comment-tag a').click(deleteTag);
function tagsFromDom(commentTagsDiv) {
return commentTagsDiv
.find('.comment-tag')
.contents()
.filter(function() { return this.nodeType === 3; })
.map(function() { return $(this).text(); })
.toArray();
}
function renderTags(commentNo, tags) {
cancelRefresh();
var root = $('#ctag-' + commentNo + ' .comment-tags');
root.find('.comment-tag').remove();
$.each(tags, function() {
var span = $('<span/>').addClass('comment-tag').text(this);
if (BUGZILLA.user.can_tag) {
span.prepend($('<a>x</a>').click(deleteTag));
}
root.append(span);
});
$('#ctag-' + commentNo + ' .comment-tags').append($('#ctag-error'));
}
var refreshXHR;
function refreshTags(commentNo, commentID) {
cancelRefresh();
refreshXHR = bugzilla_ajax(
{
url: 'rest/bug/comment/' + commentID + '?include_fields=tags',
hideError: true
},
function(data) {
refreshXHR = false;
renderTags(commentNo, data.comments[commentID].tags);
},
function(message) {
refreshXHR = false;
taggingError(commentNo, message);
}
);
}
function cancelRefresh() {
if (refreshXHR) {
refreshXHR.abort();
refreshXHR = false;
}
}
$('#ctag-add')
.devbridgeAutocomplete({
serviceUrl: function(query) {
return 'rest/bug/comment/tags/' + encodeURIComponent(query);
},
params: {
Bugzilla_api_token: (BUGZILLA.api_token ? BUGZILLA.api_token : '')
},
deferRequestBy: 250,
minChars: 3,
tabDisabled: true,
autoSelectFirst: true,
triggerSelectOnValidInput: false,
transformResult: function(response) {
response = $.parseJSON(response);
return {
suggestions: $.map(response, function(tag) {
return { value: tag };
})
};
},
formatResult: function(suggestion, currentValue) {
// disable <b> wrapping of matched substring
return suggestion.value.htmlEncode();
}
})
.keydown(function(event) {
if (event.which === 27) {
event.preventDefault();
$('#ctag-close').click();
}
else if (event.which === 13) {
event.preventDefault();
$('#ctag-error').hide();
var ctag = $('#ctag');
var newTags = $('#ctag-add').val().trim().split(/[ ,]/);
var commentNo = ctag.data('commentNo');
var commentID = ctag.data('commentID');
$('#ctag-close').click();
// update ui
var tags = tagsFromDom($(this).parents('.comment-tags'));
var dirty = false;
var addTags = [];
$.each(newTags, function(index, value) {
if ($.inArrayIn(value, tags) == -1)
addTags.push(value);
});
if (addTags.length === 0)
return;
// validate
try {
$.each(addTags, function(index, value) {
if (value.length < BUGZILLA.constant.min_comment_tag_length) {
throw 'Comment tags must be at least ' +
BUGZILLA.constant.min_comment_tag_length + ' characters.';
}
if (value.length > BUGZILLA.constant.max_comment_tag_length) {
throw 'Comment tags cannot be longer than ' +
BUGZILLA.constant.min_comment_tag_length + ' characters.';
}
});
} catch(ex) {
taggingError(commentNo, ex);
return;
}
Array.prototype.push.apply(tags, addTags);
tags.sort();
renderTags(commentNo, tags);
// update bugzilla
bugzilla_ajax(
{
url: 'rest/bug/comment/' + commentID + '/tags',
type: 'PUT',
data: { add: addTags },
hideError: true
},
function(data) {
renderTags(commentNo, data);
updateTagsMenu();
},
function(message) {
taggingError(commentNo, message);
refreshTags(commentNo, commentID);
}
);
}
});
$('#ctag-close')
.click(function(event) {
event.preventDefault();
$('#ctag').hide().data('commentNo', '');
});
$('.tag-btn')
.click(function(event) {
event.preventDefault();
var that = $(this);
var commentNo = that.data('no');
var commentID = that.data('id');
var ctag = $('#ctag');
$('#ctag-error').hide();
// toggle -> hide
if (ctag.data('commentNo') === commentNo) {
ctag.hide().data('commentNo', '');
window.focus();
return;
}
ctag.data('commentNo', commentNo);
ctag.data('commentID', commentID);
// kick off a refresh of the tags
refreshTags(commentNo, commentID);
// expand collapsed comments
if ($('#ct-' + commentNo + ':visible').length === 0) {
$('#cs-' + commentNo + ', #ccs-' + commentNo).click();
}
// move, show, and focus tagging ui
ctag.prependTo('#ctag-' + commentNo + ' .comment-tags').show();
$('#ctag-add').val('').focus();
});
$('.close-btn')
.click(function(event) {
event.preventDefault();
$('#' + $(this).data('for')).hide();
});
updateTagsMenu();
});
|