-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathextracts-options.js
275 lines (224 loc) · 8.87 KB
/
extracts-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
Extracts = { ...Extracts,
/*****************/
/* Configuration.
*/
modeOptions: [
[ "on", "On", "Enable Pop-frames", "Pop-frames Enabled", "Enable link pop-frames.", "message-lines-solid" ],
[ "off", "Off", "Disable Pop-frames", "Pop-frames Disabled", "Disable link pop-frames.", "message-slash-solid" ],
],
selectedModeOptionNote: " [This option is currently selected.]",
popFramesDisableDespawnDelay: 1000,
popFramesDisableWidgetFlashStayDuration: 3000,
popFramesDisableAutoToggleDelay: 1000,
/******************/
/* Infrastructure.
*/
modeSelector: null,
modeSelectorInteractable: true,
/*************/
/* Functions.
*/
/******************/
/* Mode selection.
*/
setMode: (selectedMode) => {
if (selectedMode == "on")
Extracts.enableExtractPopFrames();
else
Extracts.disableExtractPopFrames();
},
// Called by: Extracts.injectModeSelector
modeSelectorHTML: (inline = false) => {
// Get saved mode setting (or default).
let currentMode = Extracts.extractPopFramesEnabled() ? "on" : "off";
let modeSelectorInnerHTML = Extracts.modeOptions.map(modeOption => {
let [ name, shortLabel, unselectedLabel, selectedLabel, desc, iconName ] = modeOption;
let selected = (name == currentMode ? " selected" : " selectable");
let disabled = (name == currentMode ? " disabled" : "");
unselectedLabel = unselectedLabel.replace("-frame", Extracts.popFrameTypeSuffix());
selectedLabel = selectedLabel.replace("-frame", Extracts.popFrameTypeSuffix());
desc = desc.replace("-frame", Extracts.popFrameTypeSuffix());
if (name == currentMode)
desc += Extracts.selectedModeOptionNote;
let label = inline
? shortLabel
: (name == currentMode
? selectedLabel
: unselectedLabel);
return `<button
type="button"
class="select-mode-${name}${selected}"
${disabled}
tabindex="-1"
data-name="${name}"
title="${desc}"
>`
+ `<span class="icon">${(GW.svg(iconName))}</span>`
+ `<span
class="label"
data-selected-label="${selectedLabel}"
data-unselected-label="${unselectedLabel}"
>${label}</span>`
+ `</button>`;
}).join("");
let selectorTag = (inline ? "span" : "div");
let selectorId = (inline ? "" : "extracts-mode-selector");
let selectorClass = ("extracts-mode-selector mode-selector" + (inline ? " mode-selector-inline" : ""));
return `<${selectorTag} id="${selectorId}" class="${selectorClass}">${modeSelectorInnerHTML}</${selectorTag}>`;
},
modeSelectButtonClicked: (event) => {
GWLog("Extracts.modeSelectButtonClicked", "extracts-options.js", 2);
let button = event.target.closest("button");
// Determine which setting was chosen (ie. which button was clicked).
let selectedMode = button.dataset.name;
/* We don’t want clicks to go through if the transition
between modes has not completed yet, so we disable the
button temporarily while we’re transitioning between
modes.
*/
doIfAllowed(() => {
// Check if this is a click or an accesskey press.
if (event.pointerId == -1) {
button.blur();
GW.pageToolbar.expandToolbarFlashWidgetDoThing("extracts-mode-selector", () => {
// Actually change the mode.
Extracts.setMode(selectedMode);
});
} else {
// Actually change the mode.
Extracts.setMode(selectedMode);
}
}, Extracts, "modeSelectorInteractable");
},
// Called by: Extracts.setup (extracts.js)
injectModeSelector: (replacedElement = null) => {
GWLog("Extracts.injectModeSelector", "extracts-options.js", 1);
// Inject the mode selector widget.
let modeSelector;
if (replacedElement) {
modeSelector = elementFromHTML(Extracts.modeSelectorHTML(true));
replacedElement.replaceWith(modeSelector);
wrapParenthesizedNodes("inline-mode-selector", modeSelector);
} else {
modeSelector = Extracts.modeSelector = GW.pageToolbar.addWidget(Extracts.modeSelectorHTML());
Extracts.activateModeSelector(modeSelector);
}
},
// Called by: Extracts.setup (extracts.js)
activateModeSelector: (modeSelector) => {
// Activate mode selector widget buttons.
modeSelector.querySelectorAll("button").forEach(button => {
button.addActivateEvent(Extracts.modeSelectButtonClicked);
});
// Register event handler to update mode selector state.
GW.notificationCenter.addHandlerForEvent("Extracts.didSetMode", (info) => {
Extracts.updateModeSelectorState(modeSelector);
});
// Update state now.
Extracts.updateModeSelectorState(modeSelector);
},
// Called by: Extracts.didSetMode event handler
updateModeSelectorState: (modeSelector = Extracts.modeSelector) => {
GWLog("Extracts.updateModeSelectorState", "extracts-options.js", 2);
/* If the mode selector has not yet been injected, then do nothing.
*/
if (modeSelector == null)
return;
// Get saved mode setting (or default).
let currentMode = Extracts.extractPopFramesEnabled() ? "on" : "off";
// Clear current buttons state.
modeSelector.querySelectorAll("button").forEach(button => {
button.classList.remove("active");
button.swapClasses([ "selectable", "selected" ], 0);
button.disabled = false;
// Remove “[This option is currently selected.]” note.
if (button.title.endsWith(Extracts.selectedModeOptionNote))
button.title = button.title.slice(0, (-1 * Extracts.selectedModeOptionNote.length));
// Reset label text to unselected state.
if (modeSelector.classList.contains("mode-selector-inline") == false) {
let label = button.querySelector(".label");
label.innerHTML = label.dataset.unselectedLabel;
}
// Clear accesskey.
button.accessKey = "";
});
// Set the correct button to be selected.
modeSelector.querySelectorAll(`.select-mode-${currentMode}`).forEach(button => {
button.swapClasses([ "selectable", "selected" ], 1);
button.disabled = true;
button.title += Extracts.selectedModeOptionNote;
// Set label text to selected state.
if (modeSelector.classList.contains("mode-selector-inline") == false) {
let label = button.querySelector(".label");
label.innerHTML = label.dataset.selectedLabel;
}
});
// Set accesskey.
let buttons = Array.from(modeSelector.querySelectorAll("button"));
buttons[(buttons.findIndex(button => button.classList.contains("selected")) + 1) % buttons.length].accessKey = "p";
},
// Called by: extracts.js
disableExtractPopFramesPopFrameTitleBarButton: () => {
let button = Extracts.popFrameProvider.titleBarComponents.genericButton();
button.title = `Disable link pop${(Extracts.popFrameTypeSuffix())}s [currently enabled]`;
button.innerHTML = Extracts.popFrameProvider == Popups
? GW.svg("eye-slash-solid")
: GW.svg("eye-slash-regular");
button.classList.add("extracts-disable-button");
button.addActivateEvent((event) => {
event.stopPropagation();
button.classList.add("disabled");
// Expand toolbar.
GW.pageToolbar.toggleCollapseState(false);
setTimeout(() => {
Extracts.popFrameProvider.cleanup();
GW.pageToolbar.flashWidget("extracts-mode-selector", {
flashStayDuration: Extracts.popFramesDisableWidgetFlashStayDuration,
showSelectedButtonLabel: true,
highlightSelectedButtonLabelAfterDelay: Extracts.popFramesDisableAutoToggleDelay
});
setTimeout(() => {
// Actually disable extract pop-frames.
Extracts.disableExtractPopFrames();
// Collapse toolbar, after a delay.
GW.pageToolbar.toggleCollapseState(true, {
delay: GW.pageToolbar.demoCollapseDelay
+ Extracts.popFramesDisableWidgetFlashStayDuration
+ GW.pageToolbar.widgetFlashFallDuration
});
}, GW.pageToolbar.widgetFlashRiseDuration + Extracts.popFramesDisableAutoToggleDelay);
}, Extracts.popFramesDisableDespawnDelay);
});
return button;
},
extractPopFramesDisabledLocalStorageItemKey: () => {
return (Extracts.popFrameProvider == Popups
? Extracts.popupsDisabledLocalStorageItemKey
: Extracts.popinsDisabledLocalStorageItemKey);
},
extractPopFramesEnabled: () => {
return (localStorage.getItem(Extracts.extractPopFramesDisabledLocalStorageItemKey()) != "true");
},
disableExtractPopFrames: () => {
GWLog("Extracts.disableExtractPopFrames", "extracts-options.js", 1);
// Save setting.
localStorage.setItem(Extracts.extractPopFramesDisabledLocalStorageItemKey(), "true");
// Fire event.
GW.notificationCenter.fireEvent("Extracts.didSetMode");
// Run cleanup.
Extracts.cleanup();
},
enableExtractPopFrames: () => {
GWLog("Extracts.enableExtractPopFrames", "extracts-options.js", 1);
// Clear saved setting.
localStorage.removeItem(Extracts.extractPopFramesDisabledLocalStorageItemKey());
// Fire event.
GW.notificationCenter.fireEvent("Extracts.didSetMode");
// Run setup.
Extracts.setup();
/* Since the main document has already loaded, we must trigger the
processing of targets manually.
*/
Extracts.processTargetsInContainer(Extracts.rootDocument);
},
};