-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsox.dialog.js
380 lines (325 loc) · 16.2 KB
/
sox.dialog.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
(function(sox, $) {
'use strict';
sox.dialog = {
init: function(options) {
if (!$('.s-topbar').length) return;
sox.debug('initializing SOX dialog');
const version = options.version;
const features = options.features;
const settings = options.settings;
const lastVersionInstalled = options.lastVersionInstalled;
const html = GM_getResourceText('dialog');
const $soxSettingsDialog = $(html);
const $soxSettingsDialogFeatures = $soxSettingsDialog.find('#sox-settings-dialog-features');
const $soxSettingsDialogVersion = $soxSettingsDialog.find('#sox-settings-dialog-version');
const $soxSettingsSave = $soxSettingsDialog.find('#sox-settings-dialog-save');
const $soxSettingsReset = $soxSettingsDialog.find('#sox-settings-dialog-reset');
const $soxSettingsDebugging = $soxSettingsDialog.find('#sox-settings-dialog-debugging');
const $soxSettingsNewAccessTokenButton = $soxSettingsDialog.find('#sox-settings-dialog-access-token');
const $soxSettingsToggle = $soxSettingsDialog.find('#sox-settings-dialog-check-toggle');
const $soxSettingsClose = $soxSettingsDialog.find('#sox-settings-dialog-close');
const $searchBox = $soxSettingsDialog.find('#search');
const $importSettingsButton = $soxSettingsDialog.find('#sox-settings-import');
const $exportSettingsButton = $soxSettingsDialog.find('#sox-settings-export');
const $featurePackButtons = $soxSettingsDialog.find('.sox-settings-dialog-feature-pack');
// Array of HTML strings that will be displayed as `li` items if the user has installed a new version.
const changes = [
"Fix bugs in various features due to SE layout changes",
"Add feature to add answer count to question header",
"Behind-the-scenes performance improvements (e.g., reduce usage of jQuery for efficiency - thanks @double-beep!)",
'Deprecate "align badges by their class on user profile pages" feature (now natively implemented!)',
'Deprecate "differentiate spoilers from empty blockquotes" (now native!)',
];
function addCategory(name) {
const $div = $('<div/>', {
'class': 'header category',
'id': 'header-for-' + name,
});
const $h3 = $('<h3/>', {
text: name.toLowerCase(),
});
const $content = $('<div/>', {
id: name,
'class': 'modal-content features',
});
$div.append($h3);
if (!$soxSettingsDialogFeatures.find('div#header-for-' + name).length) {
$soxSettingsDialogFeatures.append($div);
$div.after($content);
}
}
function addFeature(category, name, description, featureSettings, extendedDescription, metaLink, featurePacks, usesApi) {
const blockFeatureSelection = usesApi && !sox.settings.accessToken;
const $div = $('<div/>', {
'class': 'sox-feature ' + (featurePacks.length ? featurePacks.join(' ') : '') + (blockFeatureSelection ? ' disabled-feature' : ''),
'title': blockFeatureSelection ? 'You must get an access token to enable this feature (click the key button at the bottom of the SOX dialog)' : '',
});
const $info = $(sox.sprites.getSvg('info')).hover(function() {
if (extendedDescription && !$(this).parent().find('.sox-feature-info').length) {
$(this).parent().append($('<div/>', {
'class': 'sox-feature-info',
'html': extendedDescription + (metaLink ? ' <a href="' + metaLink + '">[meta]</a>' : ''),
}));
}
});
const $label = $('<label/>');
const $input = $('<input/>', {
id: name,
'class': 's-checkbox',
type: 'checkbox',
}).prop('disabled', blockFeatureSelection);
$div.on('mouseleave', function() {
$(this).find('.sox-feature-info').remove();
}).append($label).append(extendedDescription ? $info : '');
$label.append($input);
$input.after(description);
$soxSettingsDialogFeatures.find('#' + category).append($div);
if (featureSettings) {
const $settingsDiv = $('<div/>', {
id: 'feature-settings-' + name,
'class': 'sox-feature-settings',
style: 'display: none; margin-top: 5px;',
});
const $settingsToggle = $(sox.sprites.getSvg('wrench', 'Edit this feature\'s settings')).click(e => {
e.preventDefault(); //don't uncheck the checkbox
const $settingsPanel = $('#feature-settings-' + name);
if ($settingsPanel.is(':visible')) {
$settingsPanel.fadeOut();
} else {
$settingsPanel.fadeIn();
}
});
const optionalSettings = GM_getValue('SOX-' + name + '-settings', -1);
for (let i = 0; i < featureSettings.length; i++) {
const currentSetting = featureSettings[i];
$settingsDiv
.append(sox.helpers.newElement(currentSetting.type, { //use newElement helper so the type can be things like 'checkbox' or 'radio'
id: currentSetting.id,
'class': currentSetting.type === 'textarea' ? 'featureSetting s-input' : 'featureSetting s-checkbox',
'style': 'margin-right: 5px',
'checked': (currentSetting.type === 'checkbox' ? JSON.parse(optionalSettings)[currentSetting.id] : false),
value: (optionalSettings === -1 ? '' : JSON.parse(optionalSettings)[currentSetting.id]),
}))
.append(currentSetting.desc)
.append('<br>');
}
const $saveFeatureSettings = $('<a/>', {
id: 'saveSettings-' + name,
'class': 'action s-btn s-btn__secondary s-btn__sm',
text: 'Save Settings',
click: function(e) {
e.preventDefault(); //don't uncheck the checkbox
const settingsToSave = {};
$('.sox-feature.disabled-feature input[type="checkbox"]').prop('checked', false); //uncheck any features that somehow were checked (they shouldn't be able to through the UI) but should be disabled (user doesn't have access token)
$(this).parent().find('.featureSetting').each(function() {
settingsToSave[$(this).attr('id')] = ($(this).is(':checkbox') ? $(this).is(':checked') : $(this).val());
});
GM_setValue('SOX-' + name + '-settings', JSON.stringify(settingsToSave));
sox.settings.writeToConsole(true);
alert('Saved!');
},
});
$settingsDiv.append($saveFeatureSettings);
const $feature = $soxSettingsDialogFeatures.find('input#' + name).parent();
$feature.append($settingsToggle);
if ($div.has('.sox-sprite-info').length) {
$info.after($settingsDiv);
} else {
$feature.append($settingsDiv);
}
}
}
// display sox version number in the dialog
if (version != 'unknown' && version !== null) {
$soxSettingsDialogVersion.text(' v' + (version ? version.toLowerCase() : ''));
} else {
$soxSettingsDialogVersion.text('');
}
if (version !== lastVersionInstalled) {
GM_setValue('SOX-lastVersionInstalled', version);
const $newVersionDetailsContainer = $('<div/>', {
'class': 'sox-new-version-details',
});
const $newVersionHeader = $('<div/>', {
'class': 'header category',
'html': '<h3>new in this version</h3>',
});
const $changes = $('<ul/>');
for (let i = 0; i < changes.length; i++) {
$changes.append($('<li/>', {
'html': changes[i], //this array is defined near the top of the file
'class': 'sox-new-version-item',
}));
}
const $newVersionInfoContainer = $('<div/>', {
'class': 'modal-content',
'html': $changes,
});
$soxSettingsDialogFeatures.append($newVersionDetailsContainer.append($newVersionHeader).append($newVersionInfoContainer));
}
if (sox.info.debugging) $soxSettingsDebugging.text('Disable debugging');
// wire up event handlers
$soxSettingsClose.on('click', () => {
$soxSettingsDialog.hide();
});
$soxSettingsReset.on('click', () => {
if (confirm('Are you sure you want to reset SOX?')) {
sox.settings.reset();
location.reload(); // reload page to reflect changed settings
}
});
$soxSettingsDebugging.on('click', () => {
const currentState = sox.info.debugging;
if (typeof currentState === 'undefined') {
GM_setValue('SOX-debug', true);
$soxSettingsDebugging.text('Disable debugging');
} else {
GM_setValue('SOX-debug', !currentState);
$soxSettingsDebugging.text(currentState ? 'Enable debugging' : 'Disable debugging');
}
location.reload();
});
$soxSettingsNewAccessTokenButton.on('click', () => {
window.open('https://stackexchange.com/oauth/dialog?client_id=7138&scope=no_expiry&redirect_uri=http://soscripted.github.io/sox/');
sox.loginfo('To get a new access token, please go to the following URL', 'https://stackexchange.com/oauth/dialog?client_id=7138&scope=no_expiry&redirect_uri=http://soscripted.github.io/sox/');
});
$soxSettingsToggle.on('click', function() {
const $icon = $(this).find('i');
const checked = $icon.hasClass('fas');
if (checked) {
$icon.removeClass('fas').addClass('far');
} else {
$icon.removeClass('far').addClass('fas');
}
$soxSettingsDialogFeatures.find('input').prop('checked', !checked);
});
$soxSettingsSave.on('click', () => {
const settings = [];
$soxSettingsDialogFeatures.find('input[type=checkbox]:checked').not('.featureSetting').each(function() { //NOT the per-feature featureSetting checkboxes, because they are saved in THEIR OWN setting object!
const x = $(this).closest('.modal-content').attr('id') + '-' + $(this).attr('id');
settings.push(x); //Add the function's ID (also the checkbox's ID) to the array
});
sox.settings.save(settings);
location.reload(); // reload page to reflect changed settings
});
$importSettingsButton.on('click', () => {
const settingsToImport = window.prompt('Please paste the settings exactly as they were given to you');
if (!settingsToImport) return;
sox.settings.save(settingsToImport);
location.reload();
});
$exportSettingsButton.on('click', () => {
window.prompt('Your settings are below. Press Ctrl/Cmd + C to copy.', JSON.stringify(sox.settings.load()));
});
$searchBox.on('keyup keydown', function() { //search box
if ($(this).val() !== '') {
const searchQuery = $(this).val();
$('.sox-new-version-details').hide();
$('#sox-settings-dialog .sox-feature').each(function() {
if ($(this).find('label').text().toLowerCase().indexOf(searchQuery) == -1) {
$(this).hide();
} else {
$(this).show();
}
});
} else {
$('.category, .features, #sox-settings-dialog .sox-feature, .sox-new-version-details').fadeIn();
}
});
$featurePackButtons.click(function() {
$('#sox-settings-dialog .sox-feature').removeClass('feature-fade-out');
if ($(this).is('.clear-feature-pack-selection')) return;
$('#sox-settings-dialog .sox-feature').not('.' + $(this).attr('data-feature-pack-id')).addClass('feature-fade-out');
});
// create sox settings button
const $soxSettingsButton = $('<a/>', {
id: 'soxSettingsButton',
'class': 'sox-settings-button s-topbar--item',
title: 'Change SOX settings',
href: '#',
click: function(e) {
e.preventDefault();
$('#sox-settings-dialog').toggle();
if ($soxSettingsDialog.is(':visible')) {
$(this).addClass('is-selected');
$soxSettingsDialog.find('#search').focus();
$soxSettingsDialog.css('right', 'calc(95vw - ' + $(e.target).offset().left + 'px)');
} else {
$(this).removeClass('is-selected');
}
},
});
// Very basic 'dark theme' support. See https://github.com/soscripted/sox/issues/406
// Not sure what the best way to detect dark mode is; the following just checks to
// see if <body>'s text color is #ccc (grey/rgb(204,204,204)). Might need changing
// in future
if ($('body').css('color') === 'rgb(204, 204, 204)') {
sox.debug('Dark mode detected, tweaking SOX CSS');
$soxSettingsDialog.addClass('dark-mode');
}
const $icon = $(sox.sprites.getSvg('settings', 'Change your SOX settings')).css({
fill: $('.top-bar .-secondary .-link').css('color'),
width: '25px',
height: '25px',
}).addClass('svg-icon iconInbox');
//close dialog if clicked outside it
$(document).click(e => { //close dialog if clicked outside it
const $target = $(e.target);
const isToggle = $target.is('#soxSettingsButton, #sox-settings-dialog');
const isChild = $target.parents('#soxSettingsButton, #sox-settings-dialog').is('#soxSettingsButton, #sox-settings-dialog');
if (!isToggle && !isChild) {
$soxSettingsDialog.hide();
$soxSettingsButton.removeClass('is-selected');
}
});
//close dialog if one of the links on the topbar is clicked
$('.s-topbar--content .s-topbar--item').not('.sox-settings-button').click(() => {
$soxSettingsDialog.hide();
$soxSettingsButton.removeClass('is-selected');
});
// load features into dialog
sox.debug('injecting features into dialog');
for (const category in features.categories) {
addCategory(category);
for (const feature in features.categories[category]) {
const currentFeature = features.categories[category][feature];
addFeature(
category,
currentFeature.name,
currentFeature.desc,
(currentFeature.settings ? currentFeature.settings : false), //add the settings panel for this feautre if indicated in the JSON
(currentFeature.extended_description ? currentFeature.extended_description : false), //add the extra description on hover if the feature has the extended description
(currentFeature.meta ? currentFeature.meta : false), //add the meta link to the extra description on hover
(currentFeature.feature_packs ? currentFeature.feature_packs : []),
(currentFeature.usesApi ? currentFeature.usesApi : false)
);
}
}
if (settings) {
for (let i = 0; i < settings.length; ++i) {
$soxSettingsDialogFeatures.find('#' + settings[i].split('-')[1]).prop('checked', true);
}
} else { // no settings found, mark all inputs as checked and display settings dialog
//note: the .not() is to make sure any disabled features (user doesn't have access token) aren't selected by default
$soxSettingsDialogFeatures.find('input').not('.sox-feature.disabled-feature input').prop('checked', true);
$soxSettingsButton.addClass('topbar-icon-on');
$soxSettingsDialog.show();
}
// add dialog to corral and sox button to topbar
$soxSettingsButton.append($icon);
const $loggedInTopbarTarget = $('.s-topbar--item.s-user-card');
const $loggedOutTopbarTarget = $(".s-topbar--item.js-help-button");
if ($loggedInTopbarTarget.length) {
$loggedInTopbarTarget.parent().after($('<li/>').append($soxSettingsButton));
} else {
$loggedOutTopbarTarget.parent().before($('<li/>').append($soxSettingsButton));
}
$soxSettingsDialog.css({
'top': $('.top-bar').height(),
'right': $('.-container').outerWidth() - $('#soxSettingsButton').parent().position().left - $('#soxSettingsButton').outerWidth(),
});
//only add dialog if button was added successfully
if ($('#soxSettingsButton').length) $('.js-topbar-dialog-corral').append($soxSettingsDialog);
},
};
})(window.sox = window.sox || {}, jQuery);