-
Notifications
You must be signed in to change notification settings - Fork 22
/
toolbar.js
354 lines (318 loc) · 10.3 KB
/
toolbar.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
/*
* Reveal.js toolbar plugin
* MIT licensed
* (c) Greg Denehy 2017
*/
/* TODO:
* - Fix issue with toolbar access after using overview button
* - Reimplement auto slide button to allow restyling ?
* - Notes ?
* - PDF export ?
* - custom buttons
* - tooltips
*/
var RevealToolbar =
window.RevealToolbar ||
(function() {
var ieVersion = (function() {
var browser = /(msie) ([\w.]+)/.exec(
window.navigator.userAgent.toLowerCase()
);
if (browser && browser[1] === 'msie') {
return parseFloat(browser[2]);
}
return null;
})();
var config = Reveal.getConfig();
var options = config.toolbar || {};
options.path = options.path || scriptPath() || 'plugin/toolbar/';
if (!options.path.endsWith('/')) {
options.path += '/';
}
var loadIcons = options.loadIcons;
if (typeof loadIcons === 'undefined') loadIcons = true;
// Cached references to DOM elements
var dom = {};
loadResource(
options.path + '/toolbar.css',
'stylesheet',
'toolbar-defaults',
function() {
loadResource(
options.path + '/lib/screenfull/screenfull.min.js',
'script',
null,
function() {
if (loadIcons) {
loadResource(
options.path +
'/font-awesome-5.0.2/css/fontawesome-all.min.css',
'stylesheet',
'',
loadPlugin
);
} else {
loadPlugin();
}
}
);
}
);
function loadPlugin() {
// does not support IE8 or below
var initialise = !ieVersion || ieVersion >= 9;
// does not support IE8 or below
if (initialise) {
function option(opt, def) {
if (typeof opt === 'undefined') return def;
return opt;
}
//
// Set option defaults
//
var position = option(options.position, 'bottom'); // 'top' or 'bottom'
var showFullscreen = option(options.fullscreen, false);
var showOverview = option(options.overview, false);
var showPause = option(options.pause, false);
var showNotes = option(options.notes, false);
var showHelp = option(options.help, false);
var captureMenu = option(options.captureMenu, true);
var capturePlaybackControl = option(
options.capturePlaybackControl,
true
);
// Cache references to key DOM elements
dom.reveal = document.querySelector('.reveal');
dom.toolbar = document.querySelector('.reveal-toolbar');
if (!dom.toolbar) {
dom.toolbar = createNode(dom.reveal, 'div', 'reveal-toolbar', null);
} else {
// move the existing toolbar after the other Reveal components
dom.reveal.appendChild(dom.toolbar);
}
dom.toolbar.classList.add(
position == 'top' ? 'reveal-toolbar-top' : 'reveal-toolbar-bottom'
);
function createToolbarButton(icon, cb) {
var button = createNode(
dom.toolbar,
'a',
'reveal-toolbar-button',
null
);
button.setAttribute('href', '#');
button.onclick = function(event) {
event.preventDefault();
cb(event);
};
createNode(button, 'i', ['fa', icon]);
return button;
}
if (showOverview) {
dom.overviewButton = createToolbarButton(
'fa-th-large',
Reveal.toggleOverview
);
}
if (showHelp) {
dom.helpButton = createToolbarButton(
'fa-question',
Reveal.toggleHelp
);
}
if (showNotes && !Reveal.isSpeakerNotes()) {
createToolbarButton('fa-list-alt', function() {
if (RevealNotes) {
RevealNotes.open();
}
});
// createToolbarButton('fa-list-alt', function() { Reveal.triggerKey(83) });
}
if (showFullscreen) {
dom.fullscreenButton = createToolbarButton('fa-expand', function() {
if (screenfull.enabled) {
screenfull.toggle(document.documentElement);
}
});
}
// set fullscreen button icon to match fullscreen state
if (screenfull.enabled) {
screenfull.on('change', function() {
var icon = dom.fullscreenButton.querySelector('i');
icon.classList.remove(
screenfull.isFullscreen ? 'fa-expand' : 'fa-compress'
);
icon.classList.add(
screenfull.isFullscreen ? 'fa-compress' : 'fa-expand'
);
});
}
if (showPause) {
dom.pauseButton = createToolbarButton(
'fa-eye-slash',
Reveal.togglePause
);
dom.pauseButton.classList.add('reveal-toolbar-pause-button');
Reveal.addEventListener('paused', function() {
var icon = dom.pauseButton.querySelector('i');
icon.classList.remove('fa-eye-slash');
icon.classList.add('fa-eye');
});
Reveal.addEventListener('resumed', function() {
var icon = dom.pauseButton.querySelector('i');
icon.classList.remove('fa-eye');
icon.classList.add('fa-eye-slash');
});
}
if (captureMenu) {
// handle async loading of plugins
var id_menu = setInterval(function() {
if (RevealMenu && RevealMenu.isInit()) {
dom.menu = document.querySelector('div.slide-menu-button');
if (dom.menu) {
console.log('Moving menu button');
dom.toolbar.insertBefore(dom.menu, dom.toolbar.firstChild);
dom.menu.classList.add('reveal-toolbar-button');
}
clearInterval(id_menu);
}
}, 50);
}
if (capturePlaybackControl) {
dom.playback = document.querySelector('canvas.playback');
if (dom.playback) {
console.log('Moving playback control');
dom.toolbar.appendChild(dom.playback);
}
}
// place default footer stylesheet before first footer stylesheet to ensure footer styles override defaults
var defaultStylesheet = document.querySelector('#toolbar-defaults');
var themeStylesheet = document.querySelector('.toolbar-theme');
if (themeStylesheet) {
themeStylesheet.parentElement.insertBefore(
defaultStylesheet,
themeStylesheet
);
}
/**
* Extend object a with the properties of object b.
* If there's a conflict, object b takes precedence.
*/
function extend(a, b) {
for (var i in b) {
a[i] = b[i];
}
}
/**
* Dispatches an event of the specified type from the
* reveal DOM element.
*/
function dispatchEvent(type, args) {
var event = document.createEvent('HTMLEvents', 1, 2);
event.initEvent(type, true, true);
extend(event, args);
document.querySelector('.reveal').dispatchEvent(event);
// If we're in an iframe, post each reveal.js event to the
// parent window. Used by the notes plugin
if (config.postMessageEvents && window.parent !== window.self) {
window.parent.postMessage(
JSON.stringify({
namespace: 'reveal',
eventName: type,
state: getState()
}),
'*'
);
}
}
dispatchEvent('toolbar-ready');
}
}
// modified from math plugin
function loadResource(url, type, id, callback) {
var head = document.querySelector('head');
var resource;
if (type === 'script') {
resource = document.createElement('script');
resource.type = 'text/javascript';
resource.src = url;
if (id) resource.id = id;
} else if (type === 'stylesheet') {
resource = document.createElement('link');
resource.rel = 'stylesheet';
resource.href = url;
if (id) resource.id = id;
}
// Wrapper for callback to make sure it only fires once
var finish = function() {
if (typeof callback === 'function') {
callback.call();
callback = null;
}
};
resource.onload = finish;
// IE
resource.onreadystatechange = function() {
if (this.readyState === 'loaded') {
finish();
}
};
// Normal browsers
head.appendChild(resource);
}
function scriptPath() {
// obtain plugin path from the script element
var path;
if (document.currentScript) {
path = document.currentScript.src.slice(0, -10);
} else {
var sel = document.querySelector('script[src$="toolbar.js"]');
if (sel) {
path = sel.src.slice(0, -10);
}
}
return path;
}
// polyfill
if (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position) {
return this.substr(position || 0, searchString.length) === searchString;
};
}
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(search, this_len) {
if (this_len === undefined || this_len > this.length) {
this_len = this.length;
}
return this.substring(this_len - search.length, this_len) === search;
};
}
/**
* Creates an HTML element and returns a reference to it.
*
* @param {HTMLElement} container
* @param {string} tagname
* @param {string} classname
* @param {string} innerHTML
*
* @return {HTMLElement}
*/
function createNode(container, tagname, classname, innerHTML) {
var node = document.createElement(tagname);
if (classname) {
if (Array.isArray(classname)) {
classname.forEach(function(c) {
node.classList.add(c);
});
} else {
node.classList.add(classname);
}
}
if (typeof innerHTML === 'string') {
node.innerHTML = innerHTML;
}
container.appendChild(node);
return node;
}
return {}; // API
})();