-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphabricator-snooze-demo.js
435 lines (361 loc) · 14.9 KB
/
phabricator-snooze-demo.js
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
// ==UserScript==
// @name Phabricator Snooze Demo
// @namespace https://www.jcbachmann.com
// @version 1.3
// @description Enables snoozing of items on Phabricator interface using local Browser storage
// @author J&C Bachmann GmbH
// @match https://secure.phabricator.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Constants
const DATEPICKER_JS = 'https://secure.phabricator.com/res/phabricator/8ae55229/rsrc/js/core/behavior-fancy-datepicker.js';
const DEBUG = false;
// State variables
var snoozeOverrideShow = false;
var items = new Map();
var snoozedCount = 0;
class Item {
constructor(id) {
this.id = id;
this.blocks = Array();
// Read date value from storage
var date = Date.parse(localStorage.getItem(this.id));
if (isNaN(date) === false) {
date = new Date(date);
} else {
// Item not snoozed - set to last midnight
date = new Date();
}
date.setHours(0, 0, 0, 0);
this.setDate(date);
debug('(' + this.id + ') new item');
}
setDate(date) {
if (this.date !== undefined && this.date.getTime() == date.getTime()) {
return;
}
this.date = date;
// Only store snoozed item dates
if (date > new Date()) {
// Date in future -> snoozed
localStorage.setItem(this.id, date.toISOString());
debug('(' + this.id + ') stored');
if (!this.snoozed) {
snoozedCount++;
updateItemCounter();
}
this.snoozed = true;
} else {
// Date in past -> active
if (localStorage.getItem(this.id) !== null) {
localStorage.removeItem(this.id);
debug('(' + this.id + ') removed');
}
if (this.snoozed) {
snoozedCount--;
updateItemCounter();
}
this.snoozed = false;
}
this.updateToBlocks();
}
addBlock(block) {
this.blocks.push(block);
debug('(' + this.id + ') block added');
}
updateToBlocks() {
var self = this;
this.blocks.forEach(function(block) {
updateItemBlock(self, block);
});
}
updateFromBlocks() {
var self = this;
this.blocks.forEach(function(block) {
self.setDate(getDateFromItemBlock(block));
});
}
}
// Debug logging
function debug(message) {
if (DEBUG) {
console.log(message);
}
}
// Two digit
function td(d) {
return (d < 10 ? '0' : '') + d;
}
// Convert date to string in format YYYY-MM-DD
function dateToString(date) {
return date.getFullYear() + '-' + td(date.getMonth() + 1) + '-' + td(date.getDate());
}
// Escape special regex characters in string
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
// Match link and extract item id - e.g. T123
function getItemId(item) {
var regexString = '^' + escapeRegExp(window.location.origin) + '\/(.+)$';
var match = item.href.match(new RegExp(regexString));
if (match && match.length === 2) {
return match[1];
} else {
console.log('could not match \'' + item.href + '\' with \'' + regexString + '\': ' + match);
return undefined;
}
}
// Climb up the hierarchy until a list item is reached and declare this as the item block
// If body is reached return null
function getItemBlock(item) {
var itemBlock = item;
while (itemBlock.nodeName !== 'LI') {
if (itemBlock == document.body) {
return null;
}
itemBlock = itemBlock.parentNode;
}
return itemBlock;
}
// Scale past days to hue
function daysToHue(days) {
var minHue = 0;
var maxHue = 200;
var minDays = 0;
var maxDays = 14;
var hue = (maxHue - minHue) * (days - minDays) / (maxDays - minDays) + minHue;
return Math.max(minHue, Math.min(hue, maxHue));
}
// If not overwritten by flat item is hidden completely
// On override item is marked with a color and otherwise displayed normally
function filterItemBlock(itemBlock, item) {
if (snoozeOverrideShow) {
var snoozeItems = itemBlock.getElementsByClassName('snooze-link');
if (snoozeItems.length > 0) {
var days = Math.ceil((item.date - new Date()) / (1000 * 60 * 60 * 24));
snoozeItems[0].style.backgroundColor = 'hsl(' + daysToHue(days) + ',100%,30%)';
snoozeItems[0].style.color = 'white';
snoozeItems[0].innerHTML = '<span class="visual-only phui-icon-view phui-font-fa fa-clock-o phui-list-item-icon" style="color:white;" aria-hidden="true"></span><div style="font-size:8pt;text-align:center;position:absolute;width:100%;padding-top:3px;">+' + days + '</div>';
}
itemBlock.style.display = '';
} else {
itemBlock.style.display = 'none';
}
}
// Revert changes introduced by item filter
function unfilterItemBlock(itemBlock) {
itemBlock.style.display = '';
itemBlock.style.backgroundColor = '';
}
// Properly style item blocks by snoozed status
function updateItemBlock(item, block) {
if (item.date > new Date()) {
filterItemBlock(block, item);
} else {
unfilterItemBlock(block);
}
setDateToItemBlock(block, item.date);
}
// Add snooze buttons in action blocks on right side
function addSnoozeButton(itemBlock, item) {
var actionsBlocks = itemBlock.getElementsByClassName('phui-oi-actions');
var actionsBlock;
// Check if an action block exists otherwise create one
if (actionsBlocks.length == 1) {
actionsBlock = actionsBlocks[0];
} else {
var frame = itemBlock.firstChild;
if (frame.childNodes.length == 1) {
actionsBlock = document.createElement('UL');
actionsBlock.classList.add('phui-oi-actions');
frame.appendChild(actionsBlock);
}
}
// If still untouched add button and datepicker magic
if (actionsBlock.getElementsByClassName('fa-clock-o').length === 0) {
// Add item block only once
item.addBlock(itemBlock);
var itemNode = document.createElement('LI');
itemNode.classList.add('phui-list-item-view');
itemNode.classList.add('phui-list-item-type-link');
itemNode.classList.add('phui-list-item-has-icon');
itemNode.dataset.sigil = 'phabricator-date-control';
// Date value is read and written by datepicker
var dateInput = document.createElement('INPUT');
dateInput.value = dateToString(item.date);
dateInput.type = 'hidden';
dateInput.classList.add('date-input');
dateInput.dataset.sigil = 'date-input';
itemNode.appendChild(dateInput);
// Time value is complete ignored by datepicker but still presence is required
var timeInput = document.createElement('INPUT');
timeInput.type = 'hidden';
timeInput.dataset.sigil = 'time-input';
itemNode.appendChild(timeInput);
var linkNode = document.createElement('A');
linkNode.classList.add('phui-list-item-href');
linkNode.classList.add('snooze-link');
linkNode.dataset.sigil = 'calendar-button';
linkNode.style.borderRadius = '3px';
linkNode.innerHTML = '<span class="visual-only phui-icon-view phui-font-fa fa-clock-o phui-list-item-icon" aria-hidden="true"></span>';
itemNode.appendChild(linkNode);
actionsBlock.appendChild(itemNode);
updateItemBlock(item, itemBlock);
}
// Correct right offset for other items
if (actionsBlock.offsetWidth > 0) {
itemBlock.getElementsByClassName('phui-oi-content-box')[0].style.marginRight = (actionsBlock.offsetWidth + 6) + 'px';
}
}
function getDateFromItemBlock(itemBlock) {
return new Date(itemBlock.getElementsByClassName('date-input')[0].value);
}
function setDateToItemBlock(itemBlock, date) {
itemBlock.getElementsByClassName('date-input')[0].value = dateToString(date);
}
function updateItemCounter() {
// Show total count of snoozed items near top icon
document.getElementById('snoozedCounter').innerHTML = snoozedCount;
}
// Refresh list of items
function seekItems() {
Array.prototype.forEach.call(
document.getElementsByClassName('phui-oi-link'),
function(itemItem) {
var itemBlock = getItemBlock(itemItem);
if (itemBlock === null) {
return;
}
// Get item object
var itemId = getItemId(itemItem);
var item = items.get(itemId);
if (item === undefined) {
item = new Item(itemId);
items.set(itemId, item);
}
// Take care of item block decoration
addSnoozeButton(itemBlock, item);
}
);
// Pull values from input fields
items.forEach(function(item) {
item.updateFromBlocks();
});
}
// Listen for DOM changes for very fast updates
function itemObserver() {
var observer = new MutationObserver(function(mutations) {
seekItems();
});
observer.observe(document.body, {
attributes: true,
childList: true,
characterData: true
});
}
// Slow polling as there are still changes which slip through the observer
function itemSeeker() {
seekItems();
setTimeout(itemSeeker, 500);
}
function updateAllItemBlocks() {
items.forEach(function(item) {
item.updateToBlocks();
});
}
function initSnoozeToggle() {
var mainMenuAlerts = document.getElementsByClassName('phabricator-main-menu-alerts');
if (mainMenuAlerts.length === 0) {
return false;
}
snoozeOverrideShow = localStorage.getItem('snooze override show') === 'true';
// Add toggle button near alarm icons
var snoozeToggle = document.createElement('A');
snoozeToggle.classList.add('alert-notifications');
if (!snoozeOverrideShow) {
// Start with correct default state
snoozeToggle.classList.add('alert-unread');
}
snoozeToggle.classList.add('snooze-toggle');
snoozeToggle.innerHTML = '<span class="phabricator-main-menu-alert-icon phui-icon-view phui-font-fa fa-clock-o" data-sigil="menu-icon"></span><span id="snoozedCounter" class="phabricator-main-menu-alert-count"></span>';
mainMenuAlerts[0].appendChild(snoozeToggle);
// Toggle override show snoozed on mouse click
snoozeToggle.onclick = function(event) {
snoozeOverrideShow = !snoozeOverrideShow;
localStorage.setItem('snooze override show', snoozeOverrideShow);
if (snoozeOverrideShow) {
snoozeToggle.classList.remove('alert-unread');
} else {
snoozeToggle.classList.add('alert-unread');
}
updateAllItemBlocks();
};
// Export
var exportDownload = document.createElement('A');
exportDownload.style.display = 'none';
exportDownload.setAttribute('download', 'phabricator-snoozed.json');
document.body.appendChild(exportDownload);
var exportButton = document.createElement('A');
exportButton.classList.add('alert-notifications');
exportButton.innerHTML = '<span class="phabricator-main-menu-alert-icon phui-icon-view phui-font-fa fa-download" data-sigil="menu-icon">';
document.getElementsByClassName('phabricator-main-menu-alerts')[0].appendChild(exportButton);
exportButton.onclick = function(event) {
exportDownload.setAttribute('href', 'data:text/json;charset=utf-8,' + encodeURIComponent(JSON.stringify(localStorage)));
exportDownload.click();
};
// Import
var importUpload = document.createElement('INPUT');
importUpload.type = 'file';
importUpload.style.display = 'none';
importUpload.addEventListener('change', function(e) {
var file = e.target.files[0];
if (file) {
var reader = new FileReader();
reader.onload = function(e) {
var data = JSON.parse(e.target.result);
for (var key in data) {
localStorage.setItem(key, data[key]);
}
location.reload();
};
reader.readAsText(file);
}
});
document.body.appendChild(importUpload);
var importButton = document.createElement('A');
importButton.classList.add('alert-notifications');
importButton.innerHTML = '<span class="phabricator-main-menu-alert-icon phui-icon-view phui-font-fa fa-upload" data-sigil="menu-icon">';
document.getElementsByClassName('phabricator-main-menu-alerts')[0].appendChild(importButton);
importButton.onclick = function(event) {
importUpload.click();
};
return true;
}
function initDatepicker() {
// Add datepicker script
var datepickerScript = document.createElement('SCRIPT');
datepickerScript.type = 'text/javascript';
datepickerScript.src = DATEPICKER_JS;
datepickerScript.onload = function() {
// Link date picker some time after javascript file is loaded
var initDatepickerScript = document.createElement('SCRIPT');
initDatepickerScript.type = 'text/javascript';
initDatepickerScript.src = chrome.runtime.getURL('init-datepicker.js');
document.body.appendChild(initDatepickerScript);
};
document.body.appendChild(datepickerScript);
}
// Initialization of whole system called once at start
function init() {
if (!initSnoozeToggle()) {
console.log('Could not initialize snooze toggle - most likely login is required');
return;
}
initDatepicker();
itemSeeker();
itemObserver();
}
init();
})();