forked from dannybrian/nfjs-fullstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient-present.js
1832 lines (1604 loc) · 77.8 KB
/
client-present.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2014 Danny Brian <[email protected]>
*
*/
(function(window, undefined) {
'use strict';
var appresent = function() {
var debug = _getParameterByName('debug');
var authormode = _getParameterByName('author');
var dragInertia = 200;
var mapWidth = 4000;
var mapHeight = 2689;
var freeTouch = false;
var freeRotate = false;
var freePanSwipe = 300; // how long free pan swipe should transition
var currentNum = 0;
var prevNum = -1;
var subNum = -1;
var eventNum = 0;
var mapMinScale = 0.2;
var mapMaxScale = 4;
var mapPadding = 0;
var mapzoomToggle = false;
var dbltapZoom = 3;
var touchWait = false;
var filters = new Object;
var realCordova = document.URL.indexOf( 'http://' ) === -1 && document.URL.indexOf( 'https://' ) === -1;
var authed = false;
var lkey = localStorage.lkey;
var privateMode = false;
var followMode = _getParameterByName('follow') || false;
var nodesMode = false;
var meta = 0;
var votes = {};
var answers = {};
var votePercents = [];
var averagePercent = 0;
var soundOn = true;
var notesOnly = false;
var cmdKeyPressed = false;
var paused = false;
// if we're running in a UIWebView component
var isWebView = /(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/i.test(navigator.userAgent);
// if we're in a UIWebView, we add a class to make the background transparent
// (among other things)
if (isWebView) {
var body = document.getElementById('body');
body.className += body.className ? ' webview' : 'webview';
}
// if the browser is in private mode, we'll try to disallow votes.
if (_setKey("testme", "test") == false) {
privateMode = true;
}
if (followMode) {
// to make sharing presos with remote audiences a bit easier, hide the menu button.
document.getElementById('controls-toggle').style.display = 'none';
}
if (_getParameterByName('freetouch')) {
freeTouch = true;
}
// our registration/origin points
var mapMidX = mapWidth / 2;
var mapMidY = mapHeight / 2;
var screenMidX = window.innerWidth / 2;
var screenMidY = window.innerHeight / 2;
var fullScale = window.innerWidth / mapWidth;
if (mapHeight * fullScale > window.innerHeight) {
fullScale = window.innerHeight / mapHeight;
}
var loadedImages = 0;
var preloaded = document.getElementById('preloaded');
var vids = document.querySelectorAll('video');
var sounds = document.querySelectorAll('audio');
var totalImagesVideos = document.querySelectorAll('img').length;
var presentScript = [];
var delayTimer = 0;
var presentedEvents = {};
var delayTimers = [];
var delayEvents = [];
var _scriptID = 0;
this.init = function (options, script) {
// console.log("init(): " + script);
mapWidth = options.mapWidth ? options.mapWidth : mapWidth;
mapHeight = options.mapHeight ? options.mapHeight : mapHeight;
presentScript = script;
// add _id's to all send events in the script, so that clients can
// skip dups in case of reconnects (where they get a dup from node) or
// a need for presenter reload (where the presenter resets)
// FIXME: clean up these script loops as recursive functions.
recurseAssignEventIds(presentScript);
if (!realCordova) {
window.addEventListener( 'load', _orient, false );
window.addEventListener( 'orientationchange', _orient, false );
}
else
{
_orient();
window.addEventListener( 'orientationchange', _orient, false );
setTimeout(function() {
navigator.splashscreen && navigator.splashscreen.hide(); // from the splashscreen plugin.
}, 100);
}
_scale();
// Preload images and video.
if (realCordova) {
// totalImagesVideos += vids.length + sounds.length;
}
for (i = 0; i < document.images.length; i++) {
document.images[i].onload = function() {
loadedImages++;
var pscale = (window.innerWidth / 10) * (loadedImages / totalImagesVideos);
preloaded.style.webkitTransform = 'scaleX(' + pscale + ')';
if (loadedImages >= totalImagesVideos) {
removeCover();
}
};
var isrc = document.images[i].src;
document.images[i].src = ''; // browser cache won't let onload fire, but it will still work here.
document.images[i].src = isrc;
// console.log(isrc);
}
if (realCordova) {
for (i = 0; i < vids.length; i++) {
vids[i].addEventListener('canplaythrough') = function () {
loadedImages++;
if (loadedImages >= totalImagesVideos) {
removeCover();
}
}
}
}
if (totalImagesVideos === 0) {
removeCover();
}
var hash = window.location.hash;
hash = hash.substring(hash.indexOf("#")+1);
var hashSlides = hash.split('/');
//console.log(hash);
if (typeof hashSlides[0] !== undefined) {
if (hashSlides[0] === 'end') {
hashSlides[0] = presentScript.length - 1;
}
presentEvent(hashSlides[0], true);
}
else
{
presentEvent(0, true);
}
if (typeof hashSlides[1] !== undefined && hashSlides[1]) {
presentSubevent(hashSlides[1], true);
}
window.onresize = _scale;
} // end init
// keyboard control.
document.onkeyup = function(ev) {
ev = ev || window.event;
switch (ev.keyCode) {
case 91: // command
cmdKeyPressed = false;
break;
}
}
document.onkeydown = function(ev) {
ev = ev || window.event;
//console.log(ev.keyCode);
switch (ev.keyCode) {
case 37: // left arrow
presentEvent(currentNum - 1);
break;
case 39: // right arrow
presentEvent(currentNum + 1);
break;
case 38: // up arrow
case 40: // down arrow
presentSubevent(subNum + 1);
break;
case 90: // Z
toggleFreeTouch();
break;
case 72: // h
presentEvent(0, true);
break;
case 69: // E
presentEvent(presentScript.length - 1, true);
break;
case 83: // s
toggleSound();
break;
case 91: // command
cmdKeyPressed = true;
break;
//case 70: // f (interferes with full screen browser keys)
//toggleFollow();
//break;
//case // hide the stuff.
// break;
case 13: // enter
case 27: // escape raises the admin menu
case 32: // space
case 9: // tab
ev.preventDefault();
break;
}
}
// gesture control.
function scriptNav (ev) {
switch(ev.gesture.direction) {
case "left":
presentEvent(currentNum + 1);
break;
case "right":
presentEvent(currentNum - 1);
break;
case "down":
case "up":
presentSubevent(subNum + 1);
break;
}
}
function recurseCatchupEvents (eventList, upToNum, ignoreDepth, recovering, skipdups) {
// console.log('recurseRepeatEvents: ' + eventList);
if (eventList === undefined) { return; }
if (Array.isArray(eventList)) {
var maxNum = ignoreDepth === undefined ? upToNum : eventList.length; // we only care about the top level with the upToNum limit.
for (var ii = 0; ii < maxNum; ++ii) {
recurseCatchupEvents(eventList[ii], upToNum, true, recovering);
}
}
else
{
if ('alwaysDo' in eventList) {
// console.log('alwaysRepeat:');
// console.log(eventList);
if (skipdups) {
if (! 'seen' in eventList) {
doEvent(eventList, recovering, upToNum);
}
}
else
{
doEvent(eventList, recovering, upToNum);
}
}
}
}
function recurseSubEvent (eventList, recovering) {
if (Array.isArray(eventList)) {
for (var ii = 0; ii < eventList.length; ++ii) {
recurseSubEvent(eventList[ii], recovering);
}
}
else
{
doEvent(eventList, recovering);
}
}
function recurseAssignEventIds (eventList) {
if (!eventList) { return; }
if (Array.isArray(eventList)) {
for (var iq = 0; iq < eventList.length; iq++) {
recurseAssignEventIds(eventList[iq]);
}
}
else
{
if ('meta' in eventList) {
meta = eventList.meta;
eventList['meta']['_id'] = ++_scriptID;
}
if ('send' in eventList) {
eventList['send']['_id'] = ++_scriptID;
}
}
}
function presentSubevent (subEventNum, recovering) {
if (authed && !followMode && !recovering) {
io.emit('follow', { key: lkey, eventNum: currentNum, subEventNum: subEventNum });
}
/*
// we do need to send dup events to followers, since we might want/need to backtrack in the presentation.
*/
// all subevents for an event are an array (and it can only contain one of these). So we need to find it first.
for (i = 0; i < presentScript[currentNum].length; i++) {
if (Array.isArray(presentScript[currentNum][i])) { // list of subevents found.
if (presentScript[currentNum][i].length > subEventNum) { // are there more subevents?
subNum = subEventNum; // set the one we're on.
recurseSubEvent(presentScript[currentNum][i][subNum], recovering); // recuse for all of these, so we can use an array of subevents to trigger at once.
}
}
}
}
function presentEvent (eventNum, recovering) {
if (authed && !followMode && !recovering) {
io.emit('follow', { key: lkey, eventNum: eventNum});
}
/*
// we do need to send dup events to followers, since we might want/need to backtrack in the presentation.
*/
subNum = -1;
eventNum = Number(eventNum) || 0;
// cancel timers for the previous event.
for (var i = 0; i < delayTimers.length; i++) {
clearTimeout(delayTimers[i]);
}
// clear the deleteDelay flag on the event, for delayed events.
for (var i = 0; i < delayEvents.length; i++) {
delete delayEvents[i]['_deleteDelay'];
}
delayTimers = [];
delayEvents = [];
if (presentScript.length <= eventNum || eventNum < 0) {
console.log("Cannot navigate beyond presentation edge.");
return;
}
prevNum = currentNum;
currentNum = eventNum;
window.location.hash = '#' + currentNum;
// we don't do anything when this changes, just for the sake of
// staying simple. This just lets reload work for better dev.
// catch up if there are required repeat events
if (recovering) {
recurseCatchupEvents(presentScript, currentNum, undefined, recovering);
}
else
{
// otherwise, still repeat and repeatable events from the previous slide, in
// case we skipped subevents on advance. And do this for the slide
// after too, if we went backwards.
recurseCatchupEvents(presentScript[prevNum], undefined, true, recovering, true);
// recurseCatchupEvents(presentScript[currentNum - 1], undefined, true, recovering, true);
}
if (Array.isArray(presentScript[currentNum])) {
// this is a group of events that need to be triggered all together.
for (var i = 0; i < presentScript[currentNum].length; ++i) {
doEvent(presentScript[currentNum][i], recovering);
}
}
else
{
doEvent(presentScript[currentNum], recovering);
}
}
function doEvent (theevent, recovering, eventnum) {
var notrel = false; // tell zoom to not treat coordinates as relative.
var posX = 0, posY = 0, scale = 0, rotation = 0, duration = 0;
if ('_ignore' in theevent) { // !debug?
// console.log('ignoring??');
return;
}
if ('_deleteDelay' in theevent) {
delete theevent['delay'];
delete theevent['_deleteDelay'];
}
if (debug) {
console.log(theevent)
if (recovering) { console.log('(recovering)'); }
}
if ('backOnly' in theevent) {
if (prevNum !== currentNum + 1) {
return;
}
}
if ('delay' in theevent) { // I used to have && !recovering here; why?
// delay is tough, because we still want to still use it even if we're recovering
// *on the same page*. But we want to skip it if we're recovering on a later page.
if (eventnum !== currentNum || !recovering) {
var tdelay = Number(theevent['delay']);
// we only allow one delay timer at a time. This means you need to use noReset if you want multiple timers.
// delete theevent['delay']; // so it's gone next time. BUT not until it plays!
theevent['_deleteDelay'] = true;
if ('noReset' in theevent) { // don't let other timers cancel this one, except on whole main event advance.
(function(event, delay) {
var thisTimer = setTimeout(function() { doEvent(event) }, delay);
delayTimers.push(thisTimer);
})(theevent, tdelay);
}
else
{
(function(event, delay) {
var thisTimer = setTimeout(function() { doEvent(event) }, delay);
delayTimers.push(thisTimer);
delayEvents.push(event);
})(theevent, tdelay);
}
return;
}
}
// arbitrary data to send to attendee apps
if ('send' in theevent) {
// FIXME: the recursion needs attention; with each presentEvent, we're
// repeating all the events before it! Sucks. So we're keeping track with
// the stupid 'seen' attribute on the events.
if (theevent['seen']) { return; }
theevent['seen'] = true;
// rather than do this, I'm just instantiating as votes come in, relying
// on Node to only broadcast to us things we have already requested.
/*if ('vote-prompt' in theevent.send) {
if (votes[theevent.send['vote-prompt']] == undefined) {
votes[theevent.send['vote-prompt']] = {};
}
if (votes[theevent.send['vote-prompt']['votegroup']] == undefined) {
votes[theevent.send['vote-prompt']['votegroup']] = {};
}
}
*/
if (authed && !followMode && !recovering) {
io.emit('msg', { key: lkey, content: theevent.send });
}
}
if ('meta' in theevent) {
// console.log('saved meta');
meta = theevent.meta;
// meta._id = ++_scriptID;
}
if ('admin-msg' in theevent && authed && !followMode) {
io.emit('admin-msg', { key: lkey, content: theevent['admin-msg'] } );
}
// notes put speaker notes in the sidebar.
if ('notes' in theevent) {
var text = "<ol>";
for (i = 0; i < theevent.notes.length; i++) {
text += "<li>" + theevent.notes[i] + "</li>\n";
}
text += "</ol>";
document.getElementById('notes-text').innerHTML = text;
}
if (notesOnly) { // in this mode we don't do anything locally.
// return;
}
// FIXME: make hideClass behave like hide, delaying a display: none.
if ('hideClass' in theevent) {
var hlist = document.querySelectorAll('.' + theevent['hideClass']);
for (var h = 0; h < hlist.length; h++) {
// console.log(hlist[h]);
(function(el, tclass) {
setTimeout(function() {
_removeClass(el, tclass);
}, 10);
})(hlist[h], 'shown');
}
}
if ('startParticle' in theevent) {
var particle = document.getElementById(theevent['startParticle']);
}
if ('startAnim' in theevent) {
// to keep this simple, all we're doing is reading the data-moveto attribute
// and transitioning to that location.
var sprite = document.getElementById(theevent['startAnim']);
_addClass(sprite, 'shown');
var tposString = sprite.getAttribute('data-moveto-1');
var rotate = sprite.getAttribute('data-rotate-1');
// this doesn't work at all; Webkit disables masks above transiting elements.
// tried 3D too. I wish it did work, it would have saved me a ton of effort.
var translateString = 'translate(' + tposString + ')';
if (rotate) {
translateString += " rotate(" + rotate + ")";
}
if (debug) { console.log(translateString); }
//sprite.style.webkitTransform = translateString;
setTimeout(function() {
(function(sprite, translate) {
sprite.style.webkitTransform = translate;
})(sprite, translateString);
}, 100);
if (sprite.getAttribute('data-movetime-1')) {
sprite.style.webkitTransitionDuration = sprite.getAttribute('data-movetime-1');
}
sprite.setAttribute('data-leg', 1);
sprite.addEventListener(_transEndEventName, function(e) { setAnimState(e); });
}
if ('addaudio' in theevent) { // singleton bullshit from Apple
if (!soundOn) { return; }
var el = document.getElementById(theevent['el']);
var audio = document.createElement('audio');
audio.setAttribute('preload','auto');
audio.setAttribute('autoplay', 'yes');
if (theevent['loop']) {
audio.setAttribute('loop', 'yes');
}
audio.setAttribute('src', theevent['addaudio']);
el.appendChild(audio);
}
if ('rmaudio' in theevent) {
var el = document.getElementById(theevent['el']);
if (el) {
el.parentNode.removeChild(el.firstChild);
}
}
if ('adddiv' in theevent) { // this is all about safari memory management.
console.log(theevent);
var imagecont = document.getElementById(theevent['adddiv']);
var image = document.createElement('div');
image.setAttribute('id', imagecont.getAttribute('data-id') || '');
image.setAttribute('class', imagecont.getAttribute('data-class') || '');
image.setAttribute('style', imagecont.getAttribute('data-style') || '');
imagecont.appendChild(image);
// <div id="map9-img-holder" data-id="map9-img" data-class="evolved" data-src="images/butler-evolved_09.jpg"></div>
}
if ('addimage' in theevent) { // this is all about safari memory management.
// console.log(theevent);
var imagecont = document.getElementById(theevent['addimage']);
var image = document.createElement('img');
if (!imagecont) {
if (debug) { console.log("Image not found: " + theevent['addimage']); }
return;
}
image.setAttribute('src', imagecont.getAttribute('data-src') || '');
image.setAttribute('id', imagecont.getAttribute('data-id') || '');
image.setAttribute('class', imagecont.getAttribute('data-class') || '');
image.setAttribute('style', imagecont.getAttribute('data-style') || '');
imagecont.appendChild(image);
// <div id="map9-img-holder" data-id="map9-img" data-class="evolved" data-src="images/butler-evolved_09.jpg"></div>
}
if ('rmdiv' in theevent) {
var rmdiv = document.getElementById(theevent['rmdiv']);
rmdiv.parentNode.removeChild(rmdiv);
}
if ('rmimage' in theevent) { // used to manage memory, so we take care to reset src first.
//console.log('rmimage: ');
//console.log(theevent['rmimage'])
var rmimage = document.getElementById(theevent['rmimage']);
if (rmimage == undefined) {
console.log("Can't rmimage that doesn't exist.");
return;
}
rmimage.style.display = 'none';
if (rmimage.children.length > 0) {
rmimage.children[0].setAttribute('src','data:image/gif;base64,' +
'R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=');
}
else
{
rmimage.setAttribute('src','data:image/gif;base64,' +
'R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=');
}
setTimeout(function() {
// to match the behavior of addimage, this will delete the first
// child if found. Otherwise, it will delete the referenced
// ID.
if (rmimage.children.length > 0) {
// console.log('removing child:');
// console.log(rmimage.children[0]);
rmimage.removeChild(rmimage.children[0]);
totalImagesVideos--;
}
else
{
if (!rmimage.parentNode) {
console.log("no image to remove? in the timeout?");
return;
}
rmimage.parentNode.removeChild(rmimage);
totalImagesVideos--;
}
},
1000);
}
if ('addvideo' in theevent) { // here we will find a placeholder referred to by el and insert our video player.
// We have to do this dynamically since iOS can only handle one <video> per doc, and preloading doesn't
// really work well anway.
var el = document.getElementById(theevent['el']) || document.getElementById(theevent['show']);
var video = document.createElement('video');
var width = 640;
var height = 480;
var muted = false;
var loop = false;
if (theevent['width']) { width = theevent['width']; }
if (theevent['height']) { height = theevent['height']; }
if (theevent['muted']) { muted = theevent['muted']; }
if (!soundOn) {
muted = true;
}
if (theevent['loop']) { loop = theevent['loop']; }
video.setAttribute('width', width);
video.setAttribute('height', height);
video.setAttribute('preload', 'auto');
if (muted) {
video.setAttribute('muted', 'muted'); // doesn't work
}
if (loop) {
video.setAttribute('loop', 'loop');
}
//video.setAttribute('controls', '');
video.setAttribute('id', 'video-player');
video.setAttribute('autoplay', 'yes');
var source = document.createElement('source');
source.setAttribute('src', theevent['addvideo']);
source.setAttribute('type', 'video/mp4;codecs="avc1.42E01E, mp4a.40.2"');
video.appendChild(source);
el.appendChild(video);
/*
<video id="myth1-video" width="640" height="480" preload="auto" controls>
<source src="video/html5ux-high.mp4" type='video/mp4;codecs="avc1.42E01E, mp4a.40.2"'/>
</video>
*/
}
if ('rmvideo' in theevent) {
setTimeout(function() {
var el = document.getElementById(theevent['el']) || document.getElementById(theevent['hide']);
var video = el.getElementsByTagName('video')[0];
if (video) {
video.parentNode.removeChild(video);
}
}, 1000); // delay this a bit so that the parent div can transition off.
}
// this message won't be processed again for this session.
if ('ignoreAfter' in theevent) { // FIXME: does not care about authed? How not since it need to work with followers.
theevent._ignore = true;
}
// the rest of these are actual "slide" data.
if ('pos' in theevent) {
var poss = theevent['pos'].split('x');
posX = Number(poss[0]);
posY = Number(poss[1]);
notrel = true;
}
else if ('mpos' in theevent) {
var poss = theevent['mpos'].split('x');
posX = Number(poss[0]) + mapMidX;
posY = Number(poss[1]) + mapMidY;
}
if ('mscale' in theevent) {
scale = theevent['mscale'] * fullScale;
}
else if ('scale' in theevent) {
scale = theevent['scale'];
}
if ('rotation' in theevent) {
rotation = theevent['rotation'];
}
if ('transtime' in theevent && !recovering) {
duration = theevent['transtime']
}
var el;
if ('el' in theevent) {
el = document.getElementById(theevent['el']);
}
if ('origin' in theevent) {
var origin = theevent['origin'].split(' ');
_setTransformOrigin(el, origin[0], origin[1]);
}
if ('rmclass' in theevent) {
var classes = theevent['rmclass'].split(' ');
for (var i = 0; i < classes.length; ++i) {
_removeClass(el, classes[i]);
}
}
if ('resetTrans' in theevent) {
var el = document.getElementById(theevent['resetTrans']);
el.style.transform = '';
el.style.webkitTransform = '';
el.style.msTransform = '';
el.style.oTransform = '';
el.style.transformDuration = '';
el.style.webkitTransitionDuration = '';
}
if ('show' in theevent) {
// this was partly an attempt to address iOS Safari memory problems.
// it sets display: inline and delays the class addition a moment, since
// quickly switching display will prevent transitions of stuff like opacity.
var els = theevent['show'].split(' ');
for (var i = 0; i < els.length; ++i) {
var el = document.getElementById(els[i]);
if (!el) {
console.log("BAD ID: '" + theevent['show'] + "'");
return;
}
el.style.display = 'inline';
setTimeout(function(el, tclass) { return function() { _addClass(el, tclass); }; }(el, 'shown' ), 5);
}
}
if ('hide' in theevent) {
// hmm. Turns out most of what I do with addclass/rmclass is show and hide.
// So let's display:none too.
var els = theevent['hide'].split(' ');
for (var i = 0; i < els.length; ++i) {
var el = document.getElementById(els[i]);
if (!el) {
if (debug) {
console.log("Warning: node is missing (hide). You probably need to clean up the presentation script.");
}
return;
}
_removeClass(el, 'shown');
if (!theevent['noRemove']) {
setTimeout(function(el) { return function() { el.style.display = 'none'; }; }(el), 2100);
}
}
}
if ('addclass' in theevent) {
setTimeout(function() { _addClass(el, theevent['addclass']) }, 1);
}
if ('tallyvote' in theevent) {
//console.log(votes);
//console.log(theevent);
if (votes[theevent.votegroup] !== undefined) {
// set text for each
var percent = (Math.round(votes[theevent.votegroup][theevent.tallyvote] / votes[theevent.votegroup]['_TOTAL'] * 100));
if (isNaN(percent)) { percent = 0; }
votePercents.push(percent);
var totalPercent = 0;
for (var i = 0; i < votePercents.length; i++) {
totalPercent += votePercents[i];
}
console.log(percent);
averagePercent = Math.round(totalPercent / votePercents.length); // overall
if ('setpercent' in theevent) {
document.getElementById(theevent['setpercent']).innerHTML = percent + '%';
}
var spans = el.getElementsByTagName('span');
for (var i = 0; i < spans.length; i++) {
spans[i].innerHTML = percent + '%';
}
_addClass(el.querySelector('.c100'), 'p'+ percent);
}
}
if ('tallyaverage' in theevent) {
var span = el.querySelector('span');
if (averagePercent === 0 || averagePercent == undefined) {
averagePercent = 92; // just to look good when viewing the end only.
}
span.innerHTML = averagePercent + '%';
_addClass(el.querySelector('.c100'), 'p'+ averagePercent);
}
if ('triggerWV' in theevent) {
triggerWebView(theevent['triggerWV']);
}
if ('start' in theevent) { // we only allow MINUTES:SECONDS
// console.log('starting timer');
var time = theevent['start'] || '2:00';
var parts = time.split(':');
var seconds = (parts[0] * 60) + Number(parts[1]);
el.innerHTML = time;
var timeTick = function () {
seconds -= 1;
if (seconds < 1) {
el.innerHTML = "0:00";
setTimeout(function() { _addClass(el, "hidden") }, 2000);
if ('timerEndSend' in theevent) {
io.emit('msg', { key: lkey, content: theevent.timerEndSend });
}
return;
}
var tseconds = ("0" + (seconds % 60)).slice(-2);
var minutes = parseInt(seconds / 60);
el.innerHTML = minutes + ":" + tseconds;
setTimeout(timeTick, 1000);
}
setTimeout(timeTick, 1000);
}
if (el && el.hasAttribute('data-relscale')) { // relative scaling, works well.
scale = Number(el.getAttribute('data-relscale')) * scale;
}
if (scale || posX) {
zoomToPoint(el, scale, posX, posY, rotation, duration, notrel);
}
}
function zoomToPoint (el, scale, x, y, rot, time, notrel) {
if (parseInt(authormode)) { return; }
// This would be easier if we could just set the transform-origin to the point.
// However, transitioning the origin yields unsmooth animation, so sadly we
// need more math here to keep the origin always at top left.
if (typeof el._dannyCache === 'undefined') {
setupCache(el);
}
var cache = el._dannyCache;
scale = typeof rot !== 'undefined' ? scale : cache.scale;
rot = typeof rot !== 'undefined' ? rot : cache.rotation;
time = typeof time !== 'undefined' ? time : 0;
var tx = x;
var ty = y;
if (!notrel) {
tx = _pointToScreenX(x, scale);
ty = _pointToScreenY(y, scale);
}
cache.posX = cache.lastPosX = tx;
cache.posY = cache.lastPosY = ty;
cache.scale = cache.lastScale = scale;
cache.rotation = cache.lastRotation = rot;
_setTransitionDuration(el, time);
setTimeout( function() { _setTransitionDuration(el, '') }, time + 100 );
setTimeout(function() { // to give safari mobile and other browsers enough time to see the class changes.
_keepOnScreen(el);
}, 1);
}
/////// Controls setup
// map touch indicators on desktop for testing.
/*
if(!Hammer.HAS_TOUCHEVENTS && !Hammer.HAS_POINTEREVENTS) {
Hammer.plugins.showTouches();
}
*/
if(!Hammer.HAS_TOUCHEVENTS && !Hammer.HAS_POINTEREVENTS) {
Hammer.plugins.fakeMultitouch();
}
// prevent the browser from doing its thing, except with
// input tags.
var app = document.getElementById('app');
document.getElementById('body').ontouchmove = function(e) {
var target = e.srcElement || e.target;
if (target.tagName != "INPUT") {
e.preventDefault();
}
}
document.getElementById('contrast-slider').addEventListener('change', function(ev) {
//console.log(ev.srcElement.value);
filters['contrast'] = (ev.srcElement.value / 15);
renderFilters();
});
document.getElementById('sat-slider').addEventListener('change', function(ev) {
//console.log(ev.srcElement.value);
filters['saturate'] = (ev.srcElement.value / 15);
renderFilters();
});
document.getElementById('bright-slider').addEventListener('change', function(ev) {
//console.log(ev.srcElement.value);
filters['brightness'] = (ev.srcElement.value / 15);
renderFilters();
});
// Setup touch events. Careful here. You only want one element to be touchable.
// At author time, this works well as the #map so you can pan/zoom around and use the
// resulting coordinates. But at present time, it's better to be #app so that you can
// still swipe on the notes pane only, also it can be useful to position something late
// to be sized right on a foreign screen (and #app isn't changed by the presentation
// script).
var touchables = document.querySelectorAll('[data-touchable]');
for (var i = 0; i < touchables.length; i++) {
setupCache(touchables[i]);
setupTouch(touchables[i]);
}
// buttons en masse
var blist = document.querySelectorAll('.button');
for (var i = 0; i < blist.length; ++i) {
var button = blist[i];
setupButton(button);
}
function setupButton (button) {
Hammer(button).on('tap', function(ev) {
_addClass(button, 'active');
setTimeout(function() { _removeClass(button, 'active') }, 200);
if (button.hasAttribute('data-open')) {
var target = document.getElementById(button.getAttribute('data-open'));
if ( _hasClass(target, 'open' )) {
_removeClass(target, 'open');
}
else
{
_addClass(target, 'open');
}
}
if (button.hasAttribute('data-toggle')) {
var toggle = button.getAttribute('data-toggle');
if ( _hasClass(button, 'toggled' )) {
_removeClass(button, 'toggled');
}
else
{
_addClass(button, 'toggled');
}
if (toggle == "freetouch") {
toggleFreeTouch();
}
if (toggle == "follow") {
toggleFollow();
}
if (toggle == "sound") {
toggleSound();
}