/
uibar.js
376 lines (312 loc) · 10.1 KB
/
uibar.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
/***********************************************************************************************************************
uibar.js
Copyright © 2013–2021 Thomas Michael Edwards <thomasmedwards@gmail.com>. All rights reserved.
Use of this source code is governed by a BSD 2-clause "Simplified" License, which may be found in the LICENSE file.
***********************************************************************************************************************/
/*
global Alert, Dialog, Engine, L10n, Setting, State, Story, UI, Config, setDisplayTitle, setPageElement
*/
var UIBar = (() => { // eslint-disable-line no-unused-vars, no-var
'use strict';
// UI bar element cache.
let _$uiBar = null;
/*******************************************************************************
UI Bar Functions.
*******************************************************************************/
function uiBarDestroy() {
if (DEBUG) { console.log('[UIBar/uiBarDestroy()]'); }
if (!_$uiBar) {
return;
}
// Hide the UI bar.
_$uiBar.hide();
// Remove its namespaced events.
jQuery(document).off('.ui-bar');
// Remove its styles.
jQuery(document.head).find('#style-ui-bar').remove();
// Remove it from the DOM.
_$uiBar.remove();
// Drop the reference to the element.
_$uiBar = null;
}
function uiBarHide() {
if (_$uiBar) {
_$uiBar.hide();
}
return this;
}
function uiBarInit() {
if (DEBUG) { console.log('[UIBar/uiBarInit()]'); }
if (document.getElementById('ui-bar')) {
return;
}
// Generate the UI bar elements.
const $elems = (() => {
const toggleLabel = L10n.get('uiBarToggle');
const backwardLabel = L10n.get('uiBarBackward');
const jumptoLabel = L10n.get('uiBarJumpto');
const forwardLabel = L10n.get('uiBarForward');
return jQuery(document.createDocumentFragment())
.append(
/* eslint-disable max-len */
'<div id="ui-bar" aria-live="polite">'
+ '<div id="ui-bar-tray">'
+ `<button id="ui-bar-toggle" tabindex="0" title="${toggleLabel}" aria-label="${toggleLabel}"></button>`
+ '<div id="ui-bar-history">'
+ `<button id="history-backward" tabindex="0" title="${backwardLabel}" aria-label="${backwardLabel}">\uE821</button>`
+ `<button id="history-jumpto" tabindex="0" title="${jumptoLabel}" aria-label="${jumptoLabel}">\uE839</button>`
+ `<button id="history-forward" tabindex="0" title="${forwardLabel}" aria-label="${forwardLabel}">\uE822</button>`
+ '</div>'
+ '</div>'
+ '<div id="ui-bar-body">'
+ '<header id="title" role="banner">'
+ '<div id="story-banner"></div>'
+ '<h1 id="story-title"></h1>'
+ '<div id="story-subtitle"></div>'
+ '<div id="story-title-separator"></div>'
+ '<p id="story-author"></p>'
+ '</header>'
+ '<div id="story-caption"></div>'
+ '<nav id="menu" role="navigation">'
+ '<ul id="menu-story"></ul>'
+ '<ul id="menu-core">'
+ `<li id="menu-item-saves"><a tabindex="0">${L10n.get('savesTitle')}</a></li>`
+ `<li id="menu-item-settings"><a tabindex="0">${L10n.get('settingsTitle')}</a></li>`
+ `<li id="menu-item-restart"><a tabindex="0">${L10n.get('restartTitle')}</a></li>`
+ `<li id="menu-item-share"><a tabindex="0">${L10n.get('shareTitle')}</a></li>`
+ '</ul>'
+ '</nav>'
+ '</div>'
+ '</div>'
/* eslint-enable max-len */
);
})();
/*
Cache the UI bar element, since its going to be used often.
NOTE: We rewrap the element itself, rather than simply using the result
of `find()`, so that we cache an uncluttered jQuery-wrapper (i.e. `context`
refers to the element and there is no `prevObject`).
*/
_$uiBar = jQuery($elems.find('#ui-bar').get(0));
// Insert the UI bar elements into the page before the main script.
$elems.insertBefore('body>script#script-sugarcube');
// Set up the UI bar's global event handlers.
jQuery(document)
// Set up a handler for the history-backward/-forward buttons.
.on(':historyupdate.ui-bar', (($backward, $forward) => () => {
$backward.ariaDisabled(State.length < 2);
$forward.ariaDisabled(State.length === State.size);
})(jQuery('#history-backward'), jQuery('#history-forward')));
}
function uiBarIsHidden() {
return _$uiBar && _$uiBar.css('display') === 'none';
}
function uiBarIsStowed() {
return _$uiBar && _$uiBar.hasClass('stowed');
}
function uiBarShow() {
if (_$uiBar) {
_$uiBar.show();
}
return this;
}
function uiBarStart() {
if (DEBUG) { console.log('[UIBar/uiBarStart()]'); }
if (!_$uiBar) {
return;
}
// Set up the #ui-bar's initial state.
if (
typeof Config.ui.stowBarInitially === 'boolean'
? Config.ui.stowBarInitially
: jQuery(window).width() <= Config.ui.stowBarInitially
) {
uiBarStow(true);
}
// Set up the #ui-bar-toggle and #ui-bar-history widgets.
jQuery('#ui-bar-toggle')
.ariaClick({
label : L10n.get('uiBarToggle')
}, () => _$uiBar.toggleClass('stowed'));
if (Config.history.controls) {
jQuery('#history-backward')
.ariaDisabled(State.length < 2)
.ariaClick({
label : L10n.get('uiBarBackward')
}, () => Engine.backward());
if (Story.lookup('tags', 'bookmark').length > 0) {
jQuery('#history-jumpto')
.ariaClick({
label : L10n.get('uiBarJumpto')
}, () => UI.jumpto());
}
else {
jQuery('#history-jumpto').remove();
}
jQuery('#history-forward')
.ariaDisabled(State.length === State.size)
.ariaClick({
label : L10n.get('uiBarForward')
}, () => Engine.forward());
}
else {
jQuery('#ui-bar-history').remove();
}
// Set up the story display title.
if (Story.has('StoryDisplayTitle')) {
setDisplayTitle(Story.get('StoryDisplayTitle').processText());
}
else {
if (TWINE1) { // for Twine 1
setPageElement('story-title', 'StoryTitle', Story.title);
}
else { // for Twine 2
jQuery('#story-title').text(Story.title);
}
}
// Set up the dynamic page elements.
if (!Story.has('StoryCaption')) {
jQuery('#story-caption').remove();
}
if (!Story.has('StoryMenu')) {
jQuery('#menu-story').remove();
}
if (!Config.ui.updateStoryElements) {
// We only need to set the story elements here if `Config.ui.updateStoryElements`
// is falsy, since otherwise they will be set by `Engine.play()`.
uiBarUpdate();
}
// Set up the Saves menu item.
jQuery('#menu-item-saves a')
.ariaClick({
role : 'button'
}, ev => {
ev.preventDefault();
UI.buildSaves();
Dialog.open();
})
.text(L10n.get('savesTitle'));
// Set up the Settings menu item.
if (!Setting.isEmpty()) {
jQuery('#menu-item-settings a')
.ariaClick({
role : 'button'
}, ev => {
ev.preventDefault();
UI.buildSettings();
Dialog.open();
})
.text(L10n.get('settingsTitle'));
}
else {
jQuery('#menu-item-settings').remove();
}
// Set up the Restart menu item.
jQuery('#menu-item-restart a')
.ariaClick({
role : 'button'
}, ev => {
ev.preventDefault();
UI.buildRestart();
Dialog.open();
})
.text(L10n.get('restartTitle'));
// Set up the Share menu item.
if (Story.has('StoryShare')) {
jQuery('#menu-item-share a')
.ariaClick({
role : 'button'
}, ev => {
ev.preventDefault();
UI.buildShare();
Dialog.open();
})
.text(L10n.get('shareTitle'));
}
else {
jQuery('#menu-item-share').remove();
}
}
function uiBarStow(noAnimation) {
if (_$uiBar && !_$uiBar.hasClass('stowed')) {
let $story;
if (noAnimation) {
$story = jQuery('#story');
$story.addClass('no-transition');
_$uiBar.addClass('no-transition');
}
_$uiBar.addClass('stowed');
if (noAnimation) {
setTimeout(() => {
$story.removeClass('no-transition');
_$uiBar.removeClass('no-transition');
}, Engine.minDomActionDelay);
}
}
return this;
}
function uiBarUnstow(noAnimation) {
if (_$uiBar && _$uiBar.hasClass('stowed')) {
let $story;
if (noAnimation) {
$story = jQuery('#story');
$story.addClass('no-transition');
_$uiBar.addClass('no-transition');
}
_$uiBar.removeClass('stowed');
if (noAnimation) {
setTimeout(() => {
$story.removeClass('no-transition');
_$uiBar.removeClass('no-transition');
}, Engine.minDomActionDelay);
}
}
return this;
}
function uiBarUpdate() {
if (DEBUG) { console.log('[UIBar/uiBarUpdate()]'); }
// Set up the display title, both the document title and page element.
if (Story.has('StoryDisplayTitle')) {
setDisplayTitle(Story.get('StoryDisplayTitle').processText());
}
if (!_$uiBar) {
return;
}
// Set up the (non-navigation) dynamic page elements.
setPageElement('story-banner', 'StoryBanner');
setPageElement('story-subtitle', 'StorySubtitle');
setPageElement('story-author', 'StoryAuthor');
setPageElement('story-caption', 'StoryCaption');
// Set up the #menu-story items.
const menuStory = document.getElementById('menu-story');
if (menuStory !== null) {
jQuery(menuStory).empty();
if (Story.has('StoryMenu')) {
try {
UI.assembleLinkList('StoryMenu', menuStory);
}
catch (ex) {
console.error(ex);
Alert.error('StoryMenu', ex.message);
}
}
}
}
/*******************************************************************************
Object Exports.
*******************************************************************************/
return Object.freeze(Object.defineProperties({}, {
destroy : { value : uiBarDestroy },
hide : { value : uiBarHide },
init : { value : uiBarInit },
isHidden : { value : uiBarIsHidden },
isStowed : { value : uiBarIsStowed },
show : { value : uiBarShow },
start : { value : uiBarStart },
stow : { value : uiBarStow },
unstow : { value : uiBarUnstow },
update : { value : uiBarUpdate },
// Legacy Functions.
setStoryElements : { value : uiBarUpdate }
}));
})();