This repository was archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathoptions.js
443 lines (368 loc) · 18.9 KB
/
options.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
436
437
438
439
440
441
442
443
(function(globals) {
'use strict';
class OptionsPage {
/**
* Returns the list of browsers versions for which to compare the current browser version from. This allows to
* set CSS classes which can be used to target version-specific styles.
*/
get browser_versions_to_compare() {
return {
'firefox': ['89']
};
}
/**
* Class which handles everything related to the options page of the extension. Preferences are persisted in
* the browser's local storage.
*/
constructor() {
this.addBrowserDiscriminatingClassesToBody();
this.preferencesManager = new globals.Gmrle.PreferencesManager();
this.getDomNodes();
this.restoreOptionsFromStorage();
this.attachEventListenersToDomNodes();
}
/**
* Queries and caches every relevant DOM nodes for further manipulations.
*/
getDomNodes() {
this.optionsForm = document.querySelector('form');
this.submitButtonInOptionsForm = this.optionsForm.querySelector('button[type="submit"]');
this.displaySourceTargetBranchesOptionsDiv = document.querySelector('div#display-source-target-branches-options');
this.displaySourceAndTargetBranchesCheckbox = document.querySelector('input#display_source_and_target_branches');
this.enableButtonsToCopySourceAndTargetBranchesNameCheckbox = document.querySelector('input#enable_buttons_to_copy_source_and_target_branches_name');
this.copyMrInfoOptionsDiv = document.querySelector('div#copy-mr-info-options');
this.enableButtonToCopyMrInfoCheckbox = document.querySelector('input#enable_button_to_copy_mr_info');
this.copyMrInfoFormatTextarea = document.querySelector('textarea#copy_mr_info_format');
this.jiraTicketLinkOptionsDiv = document.querySelector('div#jira-ticket-link-options');
this.enableJiraTicketLinkCheckbox = document.querySelector('input#enable_jira_ticket_link');
this.baseJiraUrlInput = document.querySelector('input#base_jira_url');
this.jiraTicketLinkLabelTypeRadioButtons = Array.from(document.querySelectorAll('input[name="jira_ticket_link_label_type"]'));
this.enableButtonToToggleWipStatusCheckbox = document.querySelector('input#enable_button_to_toggle_wip_status');
this.enableUnresolvedDiscussionsIndicatorCheckbox = document.querySelector('input#enable_unresolved_discussions_indicator');
}
/**
* Retrieve preferences from local storage and update the UI accordingly.
*/
restoreOptionsFromStorage() {
let self = this;
this.preferencesManager.getAll(function(preferences) {
self.displaySourceAndTargetBranchesCheckbox.checked = preferences.display_source_and_target_branches;
self.displaySourceAndTargetBranchesCheckbox.dispatchEvent(new CustomEvent('change'));
self.enableButtonsToCopySourceAndTargetBranchesNameCheckbox.checked = preferences.enable_buttons_to_copy_source_and_target_branches_name;
self.enableButtonToCopyMrInfoCheckbox.checked = preferences.enable_button_to_copy_mr_info;
self.enableButtonToCopyMrInfoCheckbox.dispatchEvent(new CustomEvent('change'));
self.copyMrInfoFormatTextarea.value = preferences.copy_mr_info_format;
self.enableJiraTicketLinkCheckbox.checked = preferences.enable_jira_ticket_link;
self.enableJiraTicketLinkCheckbox.dispatchEvent(new CustomEvent('change'));
self.baseJiraUrlInput.value = preferences.base_jira_url;
self.jiraTicketLinkLabelTypeRadioButtons.find(function(el) {
return el.value == preferences.jira_ticket_link_label_type;
}).checked = true;
self.enableButtonToToggleWipStatusCheckbox.checked = preferences.enable_button_to_toggle_wip_status;
self.enableButtonToToggleWipStatusCheckbox.dispatchEvent(new CustomEvent('change'));
self.enableUnresolvedDiscussionsIndicatorCheckbox.checked = preferences.enable_unresolved_discussions_indicator;
self.enableUnresolvedDiscussionsIndicatorCheckbox.dispatchEvent(new CustomEvent('change'));
});
}
/**
* Attach some events to DOM nodes that were queried early.
*/
attachEventListenersToDomNodes() {
let self = this;
this.optionsForm.addEventListener('submit', function(e) {
e.preventDefault();
if (self.hasUserDisabledAllFeatures()) {
return false;
}
if (!self.initializeVisualFeedbackOnSubmitButton()) {
return false;
}
self.saveOptionsToStorage();
});
this.displaySourceAndTargetBranchesCheckbox.addEventListener('change', function() {
self.displaySourceTargetBranchesOptionsDiv.classList.toggle('is-hidden', !this.checked);
self.forceUserToEnableAtLeastOneFeatureIfNecessarily();
});
this.enableButtonToCopyMrInfoCheckbox.addEventListener('change', function() {
self.copyMrInfoOptionsDiv.classList.toggle('is-hidden', !this.checked);
self.copyMrInfoFormatTextarea.toggleAttribute('required', this.checked);
self.forceUserToEnableAtLeastOneFeatureIfNecessarily();
});
this.enableJiraTicketLinkCheckbox.addEventListener('change', function() {
self.jiraTicketLinkOptionsDiv.classList.toggle('is-hidden', !this.checked);
self.baseJiraUrlInput.toggleAttribute('required', this.checked);
self.jiraTicketLinkLabelTypeRadioButtons.forEach(function(el) {
el.toggleAttribute('required', this.checked);
}, this);
self.forceUserToEnableAtLeastOneFeatureIfNecessarily();
});
this.enableButtonToToggleWipStatusCheckbox.addEventListener('change', function() {
self.forceUserToEnableAtLeastOneFeatureIfNecessarily();
});
this.enableUnresolvedDiscussionsIndicatorCheckbox.addEventListener('change', function() {
self.forceUserToEnableAtLeastOneFeatureIfNecessarily();
});
}
/**
* Take all DOM nodes values and persist them in the local storage.
*/
saveOptionsToStorage() {
let self = this;
let jira_ticket_link_label_type = this.jiraTicketLinkLabelTypeRadioButtons.find(function(el) {
return el.checked;
}).value;
this.preferencesManager.setAll(
{
display_source_and_target_branches: this.displaySourceAndTargetBranchesCheckbox.checked,
enable_buttons_to_copy_source_and_target_branches_name: this.enableButtonsToCopySourceAndTargetBranchesNameCheckbox.checked,
enable_button_to_copy_mr_info: this.enableButtonToCopyMrInfoCheckbox.checked,
copy_mr_info_format: this.copyMrInfoFormatTextarea.value,
enable_jira_ticket_link: this.enableJiraTicketLinkCheckbox.checked,
base_jira_url: this.baseJiraUrlInput.value,
jira_ticket_link_label_type: jira_ticket_link_label_type,
enable_button_to_toggle_wip_status: this.enableButtonToToggleWipStatusCheckbox.checked,
enable_unresolved_discussions_indicator: this.enableUnresolvedDiscussionsIndicatorCheckbox.checked
},
function() {
self.setSuccessfulVisualFeedbackOnSubmitButton();
},
function() {
self.revertVisualFeedbackOnSubmitButton();
}
);
}
/**
* Force the user to enable at least one feature if he disabled all the features of
* the extension (which is useless).
*/
forceUserToEnableAtLeastOneFeatureIfNecessarily() {
if (this.hasUserDisabledAllFeatures() && !this.submitButtonInOptionsForm.disabled) {
this.submitButtonInOptionsForm.disabled = true;
this.submitButtonInOptionsForm.dataset.originalTextContent = this.submitButtonInOptionsForm.textContent;
this.submitButtonInOptionsForm.textContent = '⚠️ Please enable at least one feature';
} else if (this.submitButtonInOptionsForm.disabled) {
this.submitButtonInOptionsForm.disabled = false;
this.submitButtonInOptionsForm.textContent = this.submitButtonInOptionsForm.dataset.originalTextContent;
delete this.submitButtonInOptionsForm.dataset.originalTextContent;
}
}
/**
* Determine if the user has disabled all the features of the extension (which is useless).
*/
hasUserDisabledAllFeatures() {
return !this.displaySourceAndTargetBranchesCheckbox.checked
&& !this.enableButtonToCopyMrInfoCheckbox.checked
&& !this.enableJiraTicketLinkCheckbox.checked
&& !this.enableButtonToToggleWipStatusCheckbox.checked
&& !this.enableUnresolvedDiscussionsIndicatorCheckbox.checked;
}
/**
* Returns the browser name the extension is currently running on, as well as its version.
*
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent
*/
getCurrentBrowserNameAndVersion() {
let browserName = null;
let browserVersion = null;
let ua = navigator.userAgent;
if (ua.includes('Firefox/') && !ua.includes('Seamonkey/')) {
browserName = 'firefox';
let firefoxVersionRegex = new RegExp('Firefox\/([0-9.]+)');
let results = firefoxVersionRegex.exec(ua);
if (results) {
browserVersion = results[1];
}
} else if (ua.includes('Chrome/') && !ua.includes('Chromium/')) {
browserName = 'chrome';
} else if (ua.includes('Edg/')) {
browserName = 'edge';
} else if (ua.includes('OPR/')) {
browserName = 'opera';
}
return [browserName, browserVersion];
}
/**
* Adds CSS classes names to the <body> tag identifying the browser the extension is currently running on, as well as its version.
*/
addBrowserDiscriminatingClassesToBody() {
let [currentBrowserName, currentBrowserVersion] = this.getCurrentBrowserNameAndVersion();
if (!currentBrowserName) {
return;
}
let body = document.querySelector('body');
if (!body) {
return;
}
body.classList.add('is-' + currentBrowserName);
this.addBrowserVersionsComparisonsClasses(currentBrowserName, currentBrowserVersion, body);
}
addBrowserVersionsComparisonsClasses(currentBrowserName, currentBrowserVersion, el) {
if (!currentBrowserName || !currentBrowserVersion || !(currentBrowserName in this.browser_versions_to_compare)) {
return;
}
this.browser_versions_to_compare[currentBrowserName].forEach(function(targetBrowserVersion) {
['gt', 'ge', 'lt', 'le', 'eq', 'ne'].forEach(function(operator) {
if (this.versionCompare(currentBrowserVersion, targetBrowserVersion, operator)) {
el.classList.add(operator + '-' + targetBrowserVersion.replace(new RegExp('\\.'), '-'));
}
}, this);
}, this);
}
/**
* Sets the submit button to a "saving" state when preferences are being saved: disable the button, update
* its label.
*/
initializeVisualFeedbackOnSubmitButton() {
if (this.submitButtonInOptionsForm.disabled) {
return false;
}
this.submitButtonInOptionsForm.disabled = true;
if (!('originalTextContent' in this.submitButtonInOptionsForm.dataset)) {
this.submitButtonInOptionsForm.dataset.originalTextContent = this.submitButtonInOptionsForm.textContent;
}
this.submitButtonInOptionsForm.textContent = 'Saving...';
if ('timeOutId' in this.submitButtonInOptionsForm.dataset) {
clearTimeout(this.submitButtonInOptionsForm.dataset.timeOutId);
delete this.submitButtonInOptionsForm.dataset.timeOutId;
}
return true;
}
/**
* Sets the submit button to a "successfull" state when preferences are successfully saved: enable the button,
* update its label, and set a timeout when the label will be reverted back to the original one.
*/
setSuccessfulVisualFeedbackOnSubmitButton() {
let self = this;
this.submitButtonInOptionsForm.disabled = false;
this.submitButtonInOptionsForm.textContent = 'Saved!';
this.submitButtonInOptionsForm.dataset.timeOutId = setTimeout(function() {
self.submitButtonInOptionsForm.textContent = self.submitButtonInOptionsForm.dataset.originalTextContent;
delete self.submitButtonInOptionsForm.dataset.timeOutId;
delete self.submitButtonInOptionsForm.dataset.originalTextContent;
}, 700);
}
/**
* Reverts the submit button back to its initial state: enabled it and revert its label.
*/
revertVisualFeedbackOnSubmitButton() {
this.submitButtonInOptionsForm.disabled = false;
this.submitButtonInOptionsForm.textContent = this.submitButtonInOptionsForm.dataset.originalTextContent;
delete this.submitButtonInOptionsForm.dataset.originalTextContent;
}
/**
* This function has been stolen from https://github.com/locutusjs/locutus/blob/master/src/php/info/version_compare.js
*/
versionCompare(v1, v2, operator) { // eslint-disable-line camelcase
// discuss at: https://locutus.io/php/version_compare/
// original by: Philippe Jausions (https://pear.php.net/user/jausions)
// original by: Aidan Lister (https://aidanlister.com/)
// reimplemented by: Kankrelune (https://www.webfaktory.info/)
// improved by: Brett Zamir (https://brett-zamir.me)
// improved by: Scott Baker
// improved by: Theriault (https://github.com/Theriault)
// example 1: version_compare('8.2.5rc', '8.2.5a')
// returns 1: 1
// example 2: version_compare('8.2.50', '8.2.52', '<')
// returns 2: true
// example 3: version_compare('5.3.0-dev', '5.3.0')
// returns 3: -1
// example 4: version_compare('4.1.0.52','4.01.0.51')
// returns 4: 1
// Important: compare must be initialized at 0.
let i
let x
let compare = 0
// vm maps textual PHP versions to negatives so they're less than 0.
// PHP currently defines these as CASE-SENSITIVE. It is important to
// leave these as negatives so that they can come before numerical versions
// and as if no letters were there to begin with.
// (1alpha is < 1 and < 1.1 but > 1dev1)
// If a non-numerical value can't be mapped to this table, it receives
// -7 as its value.
const vm = {
dev: -6,
alpha: -5,
a: -5,
beta: -4,
b: -4,
RC: -3,
rc: -3,
'#': -2,
p: 1,
pl: 1
}
// This function will be called to prepare each version argument.
// It replaces every _, -, and + with a dot.
// It surrounds any nonsequence of numbers/dots with dots.
// It replaces sequences of dots with a single dot.
// version_compare('4..0', '4.0') === 0
// Important: A string of 0 length needs to be converted into a value
// even less than an unexisting value in vm (-7), hence [-8].
// It's also important to not strip spaces because of this.
// version_compare('', ' ') === 1
const _prepVersion = function (v) {
v = ('' + v).replace(/[_\-+]/g, '.')
v = v.replace(/([^.\d]+)/g, '.$1.').replace(/\.{2,}/g, '.')
return (!v.length ? [-8] : v.split('.'))
}
// This converts a version component to a number.
// Empty component becomes 0.
// Non-numerical component becomes a negative number.
// Numerical component becomes itself as an integer.
const _numVersion = function (v) {
return !v ? 0 : (isNaN(v) ? vm[v] || -7 : parseInt(v, 10))
}
v1 = _prepVersion(v1)
v2 = _prepVersion(v2)
x = Math.max(v1.length, v2.length)
for (i = 0; i < x; i++) {
if (v1[i] === v2[i]) {
continue
}
v1[i] = _numVersion(v1[i])
v2[i] = _numVersion(v2[i])
if (v1[i] < v2[i]) {
compare = -1
break
} else if (v1[i] > v2[i]) {
compare = 1
break
}
}
if (!operator) {
return compare
}
// Important: operator is CASE-SENSITIVE.
// "No operator" seems to be treated as "<."
// Any other values seem to make the function return null.
switch (operator) {
case '>':
case 'gt':
return (compare > 0)
case '>=':
case 'ge':
return (compare >= 0)
case '<=':
case 'le':
return (compare <= 0)
case '===':
case '=':
case 'eq':
return (compare === 0)
case '<>':
case '!==':
case 'ne':
return (compare !== 0)
case '':
case '<':
case 'lt':
return (compare < 0)
default:
return null
}
}
}
document.addEventListener('DOMContentLoaded', function() {
let op = new OptionsPage();
});
}(this));