CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/assets/misc/talk-20121004/ui/default/slides.js
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
// S5 v1.1 slides.js -- released into the Public Domain
7
//
8
// Please see http://www.meyerweb.com/eric/tools/s5/credits.html for information
9
// about all the wonderful and talented contributors to this code!
10
11
var undef;
12
var slideCSS = '';
13
var snum = 0;
14
var smax = 1;
15
var incpos = 0;
16
var number = undef;
17
var s5mode = true;
18
var defaultView = 'slideshow';
19
var controlVis = 'visible';
20
21
var isIE = navigator.appName == 'Microsoft Internet Explorer' && navigator.userAgent.indexOf('Opera') < 1 ? 1 : 0;
22
var isOp = navigator.userAgent.indexOf('Opera') > -1 ? 1 : 0;
23
var isGe = navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('Safari') < 1 ? 1 : 0;
24
25
function hasClass(object, className) {
26
if (!object.className) return false;
27
return (object.className.search('(^|\\s)' + className + '(\\s|$)') != -1);
28
}
29
30
function hasValue(object, value) {
31
if (!object) return false;
32
return (object.search('(^|\\s)' + value + '(\\s|$)') != -1);
33
}
34
35
function removeClass(object,className) {
36
if (!object) return;
37
object.className = object.className.replace(new RegExp('(^|\\s)'+className+'(\\s|$)'), RegExp.$1+RegExp.$2);
38
}
39
40
function addClass(object,className) {
41
if (!object || hasClass(object, className)) return;
42
if (object.className) {
43
object.className += ' '+className;
44
} else {
45
object.className = className;
46
}
47
}
48
49
function GetElementsWithClassName(elementName,className) {
50
var allElements = document.getElementsByTagName(elementName);
51
var elemColl = new Array();
52
for (var i = 0; i< allElements.length; i++) {
53
if (hasClass(allElements[i], className)) {
54
elemColl[elemColl.length] = allElements[i];
55
}
56
}
57
return elemColl;
58
}
59
60
function isParentOrSelf(element, id) {
61
if (element == null || element.nodeName=='BODY') return false;
62
else if (element.id == id) return true;
63
else return isParentOrSelf(element.parentNode, id);
64
}
65
66
function nodeValue(node) {
67
var result = "";
68
if (node.nodeType == 1) {
69
var children = node.childNodes;
70
for (var i = 0; i < children.length; ++i) {
71
result += nodeValue(children[i]);
72
}
73
}
74
else if (node.nodeType == 3) {
75
result = node.nodeValue;
76
}
77
return(result);
78
}
79
80
function slideLabel() {
81
var slideColl = GetElementsWithClassName('*','slide');
82
var list = document.getElementById('jumplist');
83
smax = slideColl.length;
84
for (var n = 0; n < smax; n++) {
85
var obj = slideColl[n];
86
87
var did = 'slide' + n.toString();
88
obj.setAttribute('id',did);
89
if (isOp) continue;
90
91
var otext = '';
92
var menu = obj.firstChild;
93
if (!menu) continue; // to cope with empty slides
94
while (menu && menu.nodeType == 3) {
95
menu = menu.nextSibling;
96
}
97
if (!menu) continue; // to cope with slides with only text nodes
98
99
var menunodes = menu.childNodes;
100
for (var o = 0; o < menunodes.length; o++) {
101
otext += nodeValue(menunodes[o]);
102
}
103
list.options[list.length] = new Option(n + ' : ' + otext, n);
104
}
105
}
106
107
function currentSlide() {
108
var cs;
109
if (document.getElementById) {
110
cs = document.getElementById('currentSlide');
111
} else {
112
cs = document.currentSlide;
113
}
114
cs.innerHTML = '<span id="csHere">' + snum + '<\/span> ' +
115
'<span id="csSep">\/<\/span> ' +
116
'<span id="csTotal">' + (smax-1) + '<\/span>';
117
if (snum == 0) {
118
cs.style.visibility = 'hidden';
119
} else {
120
cs.style.visibility = 'visible';
121
}
122
}
123
124
function go(step) {
125
if (document.getElementById('slideProj').disabled || step == 0) return;
126
var jl = document.getElementById('jumplist');
127
var cid = 'slide' + snum;
128
var ce = document.getElementById(cid);
129
if (incrementals[snum].length > 0) {
130
for (var i = 0; i < incrementals[snum].length; i++) {
131
removeClass(incrementals[snum][i], 'current');
132
removeClass(incrementals[snum][i], 'incremental');
133
}
134
}
135
if (step != 'j') {
136
snum += step;
137
lmax = smax - 1;
138
if (snum > lmax) snum = lmax;
139
if (snum < 0) snum = 0;
140
} else
141
snum = parseInt(jl.value);
142
var nid = 'slide' + snum;
143
var ne = document.getElementById(nid);
144
if (!ne) {
145
ne = document.getElementById('slide0');
146
snum = 0;
147
}
148
if (step < 0) {incpos = incrementals[snum].length;} else {incpos = 0;}
149
if (incrementals[snum].length > 0 && incpos == 0) {
150
for (var i = 0; i < incrementals[snum].length; i++) {
151
if (hasClass(incrementals[snum][i], 'current'))
152
incpos = i + 1;
153
else
154
addClass(incrementals[snum][i], 'incremental');
155
}
156
}
157
if (incrementals[snum].length > 0 && incpos > 0)
158
addClass(incrementals[snum][incpos - 1], 'current');
159
ce.style.visibility = 'hidden';
160
ne.style.visibility = 'visible';
161
jl.selectedIndex = snum;
162
currentSlide();
163
number = 0;
164
}
165
166
function goTo(target) {
167
if (target >= smax || target == snum) return;
168
go(target - snum);
169
}
170
171
function subgo(step) {
172
if (step > 0) {
173
removeClass(incrementals[snum][incpos - 1],'current');
174
removeClass(incrementals[snum][incpos], 'incremental');
175
addClass(incrementals[snum][incpos],'current');
176
incpos++;
177
} else {
178
incpos--;
179
removeClass(incrementals[snum][incpos],'current');
180
addClass(incrementals[snum][incpos], 'incremental');
181
addClass(incrementals[snum][incpos - 1],'current');
182
}
183
}
184
185
function toggle() {
186
var slideColl = GetElementsWithClassName('*','slide');
187
var slides = document.getElementById('slideProj');
188
var outline = document.getElementById('outlineStyle');
189
if (!slides.disabled) {
190
slides.disabled = true;
191
outline.disabled = false;
192
s5mode = false;
193
fontSize('1em');
194
for (var n = 0; n < smax; n++) {
195
var slide = slideColl[n];
196
slide.style.visibility = 'visible';
197
}
198
} else {
199
slides.disabled = false;
200
outline.disabled = true;
201
s5mode = true;
202
fontScale();
203
for (var n = 0; n < smax; n++) {
204
var slide = slideColl[n];
205
slide.style.visibility = 'hidden';
206
}
207
slideColl[snum].style.visibility = 'visible';
208
}
209
}
210
211
function showHide(action) {
212
var obj = GetElementsWithClassName('*','hideme')[0];
213
switch (action) {
214
case 's': obj.style.visibility = 'visible'; break;
215
case 'h': obj.style.visibility = 'hidden'; break;
216
case 'k':
217
if (obj.style.visibility != 'visible') {
218
obj.style.visibility = 'visible';
219
} else {
220
obj.style.visibility = 'hidden';
221
}
222
break;
223
}
224
}
225
226
// 'keys' code adapted from MozPoint (http://mozpoint.mozdev.org/)
227
function keys(key) {
228
if (!key) {
229
key = event;
230
key.which = key.keyCode;
231
}
232
if (key.which == 84) {
233
toggle();
234
return;
235
}
236
if (s5mode) {
237
switch (key.which) {
238
case 10: // return
239
case 13: // enter
240
if (window.event && isParentOrSelf(window.event.srcElement, 'controls')) return;
241
if (key.target && isParentOrSelf(key.target, 'controls')) return;
242
if(number != undef) {
243
goTo(number);
244
break;
245
}
246
case 32: // spacebar
247
case 34: // page down
248
case 39: // rightkey
249
case 40: // downkey
250
if(number != undef) {
251
go(number);
252
} else if (!incrementals[snum] || incpos >= incrementals[snum].length) {
253
go(1);
254
} else {
255
subgo(1);
256
}
257
break;
258
case 33: // page up
259
case 37: // leftkey
260
case 38: // upkey
261
if(number != undef) {
262
go(-1 * number);
263
} else if (!incrementals[snum] || incpos <= 0) {
264
go(-1);
265
} else {
266
subgo(-1);
267
}
268
break;
269
case 36: // home
270
goTo(0);
271
break;
272
case 35: // end
273
goTo(smax-1);
274
break;
275
case 67: // c
276
showHide('k');
277
break;
278
}
279
if (key.which < 48 || key.which > 57) {
280
number = undef;
281
} else {
282
if (window.event && isParentOrSelf(window.event.srcElement, 'controls')) return;
283
if (key.target && isParentOrSelf(key.target, 'controls')) return;
284
number = (((number != undef) ? number : 0) * 10) + (key.which - 48);
285
}
286
}
287
return false;
288
}
289
290
function clicker(e) {
291
number = undef;
292
var target;
293
if (window.event) {
294
target = window.event.srcElement;
295
e = window.event;
296
} else target = e.target;
297
if (target.getAttribute('href') != null || hasValue(target.rel, 'external') || isParentOrSelf(target, 'controls') || isParentOrSelf(target,'embed') || isParentOrSelf(target,'object')) return true;
298
if (!e.which || e.which == 1) {
299
if (!incrementals[snum] || incpos >= incrementals[snum].length) {
300
go(1);
301
} else {
302
subgo(1);
303
}
304
}
305
}
306
307
function findSlide(hash) {
308
var target = null;
309
var slides = GetElementsWithClassName('*','slide');
310
for (var i = 0; i < slides.length; i++) {
311
var targetSlide = slides[i];
312
if ( (targetSlide.name && targetSlide.name == hash)
313
|| (targetSlide.id && targetSlide.id == hash) ) {
314
target = targetSlide;
315
break;
316
}
317
}
318
while(target != null && target.nodeName != 'BODY') {
319
if (hasClass(target, 'slide')) {
320
return parseInt(target.id.slice(5));
321
}
322
target = target.parentNode;
323
}
324
return null;
325
}
326
327
function slideJump() {
328
if (window.location.hash == null) return;
329
var sregex = /^#slide(\d+)$/;
330
var matches = sregex.exec(window.location.hash);
331
var dest = null;
332
if (matches != null) {
333
dest = parseInt(matches[1]);
334
} else {
335
dest = findSlide(window.location.hash.slice(1));
336
}
337
if (dest != null)
338
go(dest - snum);
339
}
340
341
function fixLinks() {
342
var thisUri = window.location.href;
343
thisUri = thisUri.slice(0, thisUri.length - window.location.hash.length);
344
var aelements = document.getElementsByTagName('A');
345
for (var i = 0; i < aelements.length; i++) {
346
var a = aelements[i].href;
347
var slideID = a.match('\#slide[0-9]{1,2}');
348
if ((slideID) && (slideID[0].slice(0,1) == '#')) {
349
var dest = findSlide(slideID[0].slice(1));
350
if (dest != null) {
351
if (aelements[i].addEventListener) {
352
aelements[i].addEventListener("click", new Function("e",
353
"if (document.getElementById('slideProj').disabled) return;" +
354
"go("+dest+" - snum); " +
355
"if (e.preventDefault) e.preventDefault();"), true);
356
} else if (aelements[i].attachEvent) {
357
aelements[i].attachEvent("onclick", new Function("",
358
"if (document.getElementById('slideProj').disabled) return;" +
359
"go("+dest+" - snum); " +
360
"event.returnValue = false;"));
361
}
362
}
363
}
364
}
365
}
366
367
function externalLinks() {
368
if (!document.getElementsByTagName) return;
369
var anchors = document.getElementsByTagName('a');
370
for (var i=0; i<anchors.length; i++) {
371
var anchor = anchors[i];
372
if (anchor.getAttribute('href') && hasValue(anchor.rel, 'external')) {
373
anchor.target = '_blank';
374
addClass(anchor,'external');
375
}
376
}
377
}
378
379
function createControls() {
380
var controlsDiv = document.getElementById("controls");
381
if (!controlsDiv) return;
382
var hider = ' onmouseover="showHide(\'s\');" onmouseout="showHide(\'h\');"';
383
var hideDiv, hideList = '';
384
if (controlVis == 'hidden') {
385
hideDiv = hider;
386
} else {
387
hideList = hider;
388
}
389
controlsDiv.innerHTML = '<form action="#" id="controlForm"' + hideDiv + '>' +
390
'<div id="navLinks">' +
391
'<a accesskey="t" id="toggle" href="javascript:toggle();">&#216;<\/a>' +
392
'<a accesskey="z" id="prev" href="javascript:go(-1);">&laquo;<\/a>' +
393
'<a accesskey="x" id="next" href="javascript:go(1);">&raquo;<\/a>' +
394
'<div id="navList"' + hideList + '><select id="jumplist" onchange="go(\'j\');"><\/select><\/div>' +
395
'<\/div><\/form>';
396
if (controlVis == 'hidden') {
397
var hidden = document.getElementById('navLinks');
398
} else {
399
var hidden = document.getElementById('jumplist');
400
}
401
addClass(hidden,'hideme');
402
}
403
404
function fontScale() { // causes layout problems in FireFox that get fixed if browser's Reload is used; same may be true of other Gecko-based browsers
405
if (!s5mode) return false;
406
var vScale = 22; // both yield 32 (after rounding) at 1024x768
407
var hScale = 32; // perhaps should auto-calculate based on theme's declared value?
408
if (window.innerHeight) {
409
var vSize = window.innerHeight;
410
var hSize = window.innerWidth;
411
} else if (document.documentElement.clientHeight) {
412
var vSize = document.documentElement.clientHeight;
413
var hSize = document.documentElement.clientWidth;
414
} else if (document.body.clientHeight) {
415
var vSize = document.body.clientHeight;
416
var hSize = document.body.clientWidth;
417
} else {
418
var vSize = 700; // assuming 1024x768, minus chrome and such
419
var hSize = 1024; // these do not account for kiosk mode or Opera Show
420
}
421
var newSize = Math.min(Math.round(vSize/vScale),Math.round(hSize/hScale));
422
fontSize(newSize + 'px');
423
if (isGe) { // hack to counter incremental reflow bugs
424
var obj = document.getElementsByTagName('body')[0];
425
obj.style.display = 'none';
426
obj.style.display = 'block';
427
}
428
}
429
430
function fontSize(value) {
431
if (!(s5ss = document.getElementById('s5ss'))) {
432
if (!isIE) {
433
document.getElementsByTagName('head')[0].appendChild(s5ss = document.createElement('style'));
434
s5ss.setAttribute('media','screen, projection');
435
s5ss.setAttribute('id','s5ss');
436
} else {
437
document.createStyleSheet();
438
document.s5ss = document.styleSheets[document.styleSheets.length - 1];
439
}
440
}
441
if (!isIE) {
442
while (s5ss.lastChild) s5ss.removeChild(s5ss.lastChild);
443
s5ss.appendChild(document.createTextNode('body {font-size: ' + value + ' !important;}'));
444
} else {
445
document.s5ss.addRule('body','font-size: ' + value + ' !important;');
446
}
447
}
448
449
function notOperaFix() {
450
slideCSS = document.getElementById('slideProj').href;
451
var slides = document.getElementById('slideProj');
452
var outline = document.getElementById('outlineStyle');
453
slides.setAttribute('media','screen');
454
outline.disabled = true;
455
if (isGe) {
456
slides.setAttribute('href','null'); // Gecko fix
457
slides.setAttribute('href',slideCSS); // Gecko fix
458
}
459
if (isIE && document.styleSheets && document.styleSheets[0]) {
460
document.styleSheets[0].addRule('img', 'behavior: url(ui/default/iepngfix.htc)');
461
document.styleSheets[0].addRule('div', 'behavior: url(ui/default/iepngfix.htc)');
462
document.styleSheets[0].addRule('.slide', 'behavior: url(ui/default/iepngfix.htc)');
463
}
464
}
465
466
function getIncrementals(obj) {
467
var incrementals = new Array();
468
if (!obj)
469
return incrementals;
470
var children = obj.childNodes;
471
for (var i = 0; i < children.length; i++) {
472
var child = children[i];
473
if (hasClass(child, 'incremental')) {
474
if (child.nodeName == 'OL' || child.nodeName == 'UL') {
475
removeClass(child, 'incremental');
476
for (var j = 0; j < child.childNodes.length; j++) {
477
if (child.childNodes[j].nodeType == 1) {
478
addClass(child.childNodes[j], 'incremental');
479
}
480
}
481
} else {
482
incrementals[incrementals.length] = child;
483
removeClass(child,'incremental');
484
}
485
}
486
if (hasClass(child, 'show-first')) {
487
if (child.nodeName == 'OL' || child.nodeName == 'UL') {
488
removeClass(child, 'show-first');
489
if (child.childNodes[isGe].nodeType == 1) {
490
removeClass(child.childNodes[isGe], 'incremental');
491
}
492
} else {
493
incrementals[incrementals.length] = child;
494
}
495
}
496
incrementals = incrementals.concat(getIncrementals(child));
497
}
498
return incrementals;
499
}
500
501
function createIncrementals() {
502
var incrementals = new Array();
503
for (var i = 0; i < smax; i++) {
504
incrementals[i] = getIncrementals(document.getElementById('slide'+i));
505
}
506
return incrementals;
507
}
508
509
function defaultCheck() {
510
var allMetas = document.getElementsByTagName('meta');
511
for (var i = 0; i< allMetas.length; i++) {
512
if (allMetas[i].name == 'defaultView') {
513
defaultView = allMetas[i].content;
514
}
515
if (allMetas[i].name == 'controlVis') {
516
controlVis = allMetas[i].content;
517
}
518
}
519
}
520
521
// Key trap fix, new function body for trap()
522
function trap(e) {
523
if (!e) {
524
e = event;
525
e.which = e.keyCode;
526
}
527
try {
528
modifierKey = e.ctrlKey || e.altKey || e.metaKey;
529
}
530
catch(e) {
531
modifierKey = false;
532
}
533
return modifierKey || e.which == 0;
534
}
535
536
function startup() {
537
defaultCheck();
538
if (!isOp)
539
createControls();
540
slideLabel();
541
fixLinks();
542
externalLinks();
543
fontScale();
544
if (!isOp) {
545
notOperaFix();
546
incrementals = createIncrementals();
547
slideJump();
548
if (defaultView == 'outline') {
549
toggle();
550
}
551
document.onkeyup = keys;
552
document.onkeypress = trap;
553
document.onclick = clicker;
554
}
555
}
556
557
window.onload = startup;
558
window.onresize = function(){setTimeout('fontScale()', 50);}
559
560