-
Notifications
You must be signed in to change notification settings - Fork 0
/
slideshow.js
552 lines (438 loc) · 21.3 KB
/
slideshow.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
"use strict";
$(function (event) {
console.group('App');
console.info('DOM loaded.', event);
console.groupEnd();
// Demo settings
var loadDemo = false;
var demoDuration = 60000;
var infiniteDemo = true;
// Slideshow settings
var imagesPath = 'http://127.0.0.1:8001/images';
var infiniteSlideshow = true;
var slideshowAnimationEnter = 'fade in';
var slideshowAnimationLeave = 'fade out';
var slideshowPauseDuration = 8000;
var animationDuration = 800;
var animationLoadingTime = 800;
// Do not change settings below
var demoStopped = false;
var slideshowStopped = false;
var slideshowDisplayDuration = (slideshowPauseDuration - 1000);
var checkInterval = 100;
var modalHideTimeout = 1000;
// Internal dimmer settings
$('.image .bottom.dimmer').dimmer({
transition: 'fade up',
});
// Fullscreen event handler on keypress
document.addEventListener("keypress", function(e) {
if (e.keyCode === 13) {
console.log('User want to go fullscreen.', e);
toggleFullScreen();
}
}, false);
// Fullscreen event handler on double click
document.addEventListener("dblclick", function(e) {
console.log('User want to go fullscreen.', e);
toggleFullScreen();
}, false);
// Fullscreen function
function toggleFullScreen() {
if (!document.fullscreenElement) {
console.log('Init fullscreen...');
document.documentElement.requestFullscreen();
// Hide slideshow header nicely
console.log('Hiding slideshow header...');
$('.ui.overlay.fullscreen.inverted.modal .header').hide('show');
}
else {
// User entered 'Esc' key
if (document.exitFullscreen) {
console.log('Exit fullscreen...');
document.exitFullscreen();
}
}
}
function random(min, max) {
const num = Math.floor(Math.random() * (max - min + 1)) + min;
return num;
}
function loadSlideshow(images, source) {
console.log('Loading slideshow...');
console.log('Loaded image list:', images);
console.log('Slideshow mode:', (infiniteSlideshow !== true ? 'Ends when all pictures are displayed' : 'Infinite'));
// Hide mouse cursor during slideshow
$('html').css('cursor', 'none');
// Changing metas display style (required for left alignment)
$('#picture-metas').css('width', 'inherit');
// $('#picture-metas').css('padding-left', '1rem');
// Adjust slideshow size
if (document.fullscreenElement) {
$('.image.content .dimmable').css('height', '97vh');
}
else {
$('.image.content .dimmable').css('height', '90vh');
}
var limit = images.length;
var baseURL = imagesPath;
if (typeof source !== 'undefined') {
var limit = images.length;
var baseURL = source;
}
console.log('Total pictures:', limit);
console.log('Base URL:', baseURL);
// Init slideshow
var slideshowStop = setInterval(function () {
// All pictures displayed, stopping
if (limit === 0 && infiniteSlideshow === false) {
slideshowStopped = true;
if (slideshow) {
console.log('End of slideshow.');
clearInterval(slideshow);
}
console.log('No more pictures to display.');
// Hiding modal
var delayedModalHide = setTimeout(function () {
$('.modal').modal('hide');
if ($('.modal').modal('is active') === false) {
console.log('Modal closed.');
}
clearTimeout(delayedModalHide);
}, modalHideTimeout);
// Show mouse cursor when slideshow is finished
$('html').css('cursor', 'default');
// Update main container content
$('.ui.basic.inverted.segment').addClass('very padded center aligned');
$('.ui.basic.inverted.segment').html('<h1>End of slideshow</h1><br><button class="ui inverted primary button" onclick="window.location.reload();">Restart</button>');
clearInterval(slideshowStop);
}
// All pictures displayed, reloading
if (limit === 0 && infiniteSlideshow === true) {
console.log('End of slideshow.');
console.log('Reloading in 5 seconds...');
// Hiding modal
var delayedModalHide = setTimeout(function () {
$('.modal').modal('hide');
if ($('.modal').modal('is active') === false) {
console.log('Modal closed.');
}
clearTimeout(delayedModalHide);
}, modalHideTimeout);
// Show mouse cursor when slideshow is finished
$('html').css('cursor', 'default');
// Update main container content
$('.ui.basic.inverted.segment').addClass('very padded center aligned');
$('.ui.basic.inverted.segment').html('<h1>End of slideshow</h1><br><button class="ui inverted primary button" id="btnReload">Reloading...</button>');
// Reload counter
var reloadTimeInSeconds = 5;
var reloadCounter = setInterval(function () {
$('#btnReload').html('Reload in ' + reloadTimeInSeconds + ' seconds...');
if (reloadTimeInSeconds === 0) {
// Clear all intervals
clearInterval(reloadCounter);
clearInterval(slideshow);
clearInterval(slideshowStop);
// Reload slideshow
loadSlideshow(images);
}
reloadTimeInSeconds--;
}, 1000);
}
}, slideshowPauseDuration);
// Display all received pictures
if (limit !== 0) {
// Load initial picture
loadPictures(images, baseURL);
// Load all other pictures
var slideshow = setInterval(function () {
// Continue till we have pictures to display
if (limit !== 0) {
limit--;
// Display slideshow header nicely
if ($('.ui.overlay.fullscreen.inverted.modal .header').css('display') === 'none' && !document.fullscreenElement) {
console.log('Display slideshow header...');
$('.ui.overlay.fullscreen.inverted.modal .header').show('slow');
}
// Adjust slideshow size
if (document.fullscreenElement) {
$('.image.content .dimmable').css('height', '97vh');
}
else {
$('.image.content .dimmable').css('height', '90vh');
}
console.log('Pictures left:', limit);
loadPictures(images, imagesPath);
}
}, slideshowPauseDuration);
}
}
function loadPictures(images, source) {
var totalImages = images.length;
var randomImageId = random(0, (totalImages-1));
console.log('==> Random ID:', randomImageId);
if (typeof images[randomImageId] !== 'undefined' && slideshowStopped === false) {
// Top level images
if (images[randomImageId].type !== 'folder') {
var randomPicture = images[randomImageId];
var randomPictureURL = source + '/' + randomPicture.name;
console.log('Analyzing file extension...');
// Avoid displaying videos for now
var parsedName = String(randomPicture.name).split('.');
var fileExtension = parsedName[(parsedName.length-1)];
// Gather time details
var fileDate = new Date(Date.parse(randomPicture.time));
var fileDateLang = window.navigator.language;
var fileDateOptions = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
};
console.log('Found extension:', fileExtension);
if (String(fileExtension).toLowerCase() === 'mp4' || String(fileExtension).toLowerCase() === '3gp') {
console.log('Video detected, skipping it...');
loadPictures(images, source);
return;
}
console.log('New image:', randomPicture);
console.log('Loading new picture [' + randomPictureURL + '].');
// Assign new random image
$('#slideshow-image')[0].src = randomPictureURL;
// $('#slideshow-image')[0].width = (window.innerWidth - 40);
$('#slideshow-image')[0].height = (document.fullscreenElement ? (window.innerHeight - 40) : (window.innerHeight - 100));
// $('#slideshow-image')[0].height = (window.innerHeight - 10);
// $('#slideshow-image').css('height', '90vh');
// $('#slideshow-image')[0].height = (window.innerHeight - 100) * (window.innerWidth - 40) / $('#slideshow-image')[0].width;
// $('#slideshow-image')[0].width = (window.innerWidth - 40);
// Show image with delay
var checkImage = setInterval(function () {
if ($('#slideshow-image')[0].src !== '') {
if ($('.modal').modal('is active') === false) {
$('.modal').modal('show');
}
if ($('.ui.basic.inverted.segment').hasClass('loading')) {
$('.ui.basic.inverted.segment').removeClass('loading');
}
var delayedAnimation = setTimeout(function () {
console.log('Picture loaded, running animation.');
$('#slideshow-image').transition(slideshowAnimationEnter, animationDuration);
if (typeof randomPicture.name !== 'undefined') {
var html = '<div class="ui basic left aligned segment">';
html += '<strong>File:</strong> <span>' + randomPicture.name + '</span>';
html += '<br><br>';
html += '<strong>Date:</strong> <span>' + fileDate.toLocaleDateString(fileDateLang, fileDateOptions) + '</span>';
html += '</div>';
$('#picture-metas').html(html);
var delayedDimmerAnimation = setTimeout(function () {
console.log('Metas defined, showing dimmer.');
$('.image .bottom.dimmer').dimmer('show');
clearTimeout(delayedDimmerAnimation);
}, animationLoadingTime);
}
clearTimeout(delayedAnimation);
}, animationLoadingTime);
clearInterval(checkImage);
}
}, checkInterval);
// Hide image nicely
var displayTime = setTimeout(function () {
if ($('#slideshow-image')[0].src !== '') {
console.log('Display timeout reached, hidding dimmer and picture.');
$('.image .bottom.dimmer').dimmer('hide');
$('#slideshow-image').transition(slideshowAnimationLeave, animationDuration, function () {
$('#slideshow-image')[0].src = '';
});
}
clearTimeout(displayTime);
}, slideshowDisplayDuration);
}
// Images from subfolders
else {
console.log('Sub folder found:', images[randomImageId].name);
console.log('Sub folder content:', images[randomImageId].children);
console.log('Sub folder path:', source + '/' + images[randomImageId].name);
loadPictures(images[randomImageId].children, source + '/' + images[randomImageId].name);
}
}
}
function loadDemoSlideshow(images) {
console.log('Loading demo slideshow...');
console.log('Loaded image list:', images);
console.log('Demo duration:', (infiniteDemo !== true ? demoDuration : 'Infinite'));
// Hide mouse cursor during slideshow
$('html').css('cursor', 'none');
// Adjust slideshow size
if (document.fullscreenElement) {
$('.image.content .dimmable').css('height', '99vh');
}
else {
$('.image.content .dimmable').css('height', '90vh');
}
var limit = images.length;
var imagesPath = 'https://picsum.photos/id/';
console.log('Total pictures:', limit);
console.log('Base URL:', imagesPath);
// Init demo slideshow
var demoStop = setTimeout(function () {
// Demo duration reached, stopping
if (infiniteDemo === false) {
demoStopped = true;
if (slideshow) {
console.log('Slideshow finished.');
clearInterval(slideshow);
}
console.log('End of demo slideshow.');
// Hiding modal
var delayedModalHide = setTimeout(function () {
$('.modal').modal('hide');
if ($('.modal').modal('is active') === false) {
console.log('Modal closed.');
}
clearTimeout(delayedModalHide);
}, modalHideTimeout);
// Show mouse cursor when slideshow is finished
$('html').css('cursor', 'default');
// Update main container content
$('.ui.basic.inverted.segment').addClass('very padded center aligned');
$('.ui.basic.inverted.segment').html('<h1>End of demo slideshow</h1><br><button class="ui inverted primary button" onclick="window.location.reload();">Restart</button>');
clearTimeout(demoStop);
}
// All pictures displayed, reloading
if (limit === 0 && infiniteDemo === true) {
console.log('End of demo slideshow.');
console.log('Reloading in 5 seconds...');
// Hiding modal
var delayedModalHide = setTimeout(function () {
$('.modal').modal('hide');
if ($('.modal').modal('is active') === false) {
console.log('Modal closed.');
}
clearTimeout(delayedModalHide);
}, modalHideTimeout);
// Show mouse cursor when slideshow is finished
$('html').css('cursor', 'default');
// Update main container content
$('.ui.basic.inverted.segment').addClass('very padded center aligned');
$('.ui.basic.inverted.segment').html('<h1>End of demo slideshow</h1><br><button class="ui inverted primary button" id="btnReload">Reloading...</button>');
// Reload counter
var reloadTimeInSeconds = 5;
var reloadCounter = setInterval(function () {
$('#btnReload').html('Reload in ' + reloadTimeInSeconds + ' seconds...');
if (reloadTimeInSeconds === 0) {
// Clear all intervals
clearInterval(reloadCounter);
clearInterval(slideshow);
clearInterval(demoStop);
// Reload demo slideshow
loadDemoSlideshow(images);
}
reloadTimeInSeconds--;
}, 1000);
}
}, demoDuration);
// Display all received pictures
if (limit !== 0) {
// Load initial picture
loadDemoPictures(images, imagesPath);
// Load all other pictures
var slideshow = setInterval(function () {
// Continue till we have pictures to display
if (limit !== 0) {
limit--;
// Display slideshow header nicely
if ($('.ui.overlay.fullscreen.inverted.modal .header').css('display') === 'none' && !document.fullscreenElement) {
console.log('Display slideshow header...');
$('.ui.overlay.fullscreen.inverted.modal .header').show('slow');
}
// Adjust slideshow size
if (document.fullscreenElement) {
$('.image.content .dimmable').css('height', '99vh');
}
else {
$('.image.content .dimmable').css('height', '90vh');
}
console.log('Pictures left:', limit);
loadDemoPictures(images, imagesPath);
}
}, slideshowPauseDuration);
}
}
function loadDemoPictures(images, source) {
var totalImages = images.length;
var randomImageId = random(0, (totalImages-1));
console.log('==> Random ID:', randomImageId);
if (typeof images[randomImageId] !== 'undefined' && demoStopped === false) {
var randomPicture = images[randomImageId];
var randomPictureURL = source + images[randomImageId].id + '/' + (window.innerWidth - 40) + '/' + (window.innerHeight - 100);
console.log('New image:', randomPicture);
console.log('Loading new picture [' + randomPictureURL + '].');
// Assign new random image
$('#slideshow-image')[0].src = randomPictureURL;
// Show image with delay
var checkImage = setInterval(function () {
if ($('#slideshow-image')[0].src !== '') {
if ($('.modal').modal('is active') === false) {
$('.modal').modal('show');
}
if ($('.ui.basic.inverted.segment').hasClass('loading')) {
$('.ui.basic.inverted.segment').removeClass('loading');
}
var delayedAnimation = setTimeout(function () {
console.log('Picture loaded, running animation.');
$('#slideshow-image').transition(slideshowAnimationEnter, animationDuration);
if (typeof randomPicture.author !== 'undefined') {
var html = '<p>';
html += '<strong>Author</strong>';
html += ' – ';
html += '<span>' + randomPicture.author + '</span>';
html += '</p>';
$('#picture-metas').html(html);
var delayedDimmerAnimation = setTimeout(function () {
console.log('Metas defined, showing dimmer.');
$('.image .bottom.dimmer').dimmer('show');
clearTimeout(delayedDimmerAnimation);
}, animationLoadingTime);
}
clearTimeout(delayedAnimation);
}, animationLoadingTime);
clearInterval(checkImage);
}
}, checkInterval);
// Hide image nicely
var displayTime = setTimeout(function () {
if ($('#slideshow-image')[0].src !== '') {
console.log('Display timeout reached, hidding dimmer and picture.');
$('.image .bottom.dimmer').dimmer('hide');
$('#slideshow-image').transition(slideshowAnimationLeave, animationDuration, function () {
$('#slideshow-image')[0].src = '';
});
}
clearTimeout(displayTime);
}, slideshowDisplayDuration);
}
}
if (loadDemo === true) {
// Get demo pictures from JSON list
$.getJSON('https://picsum.photos/list')
.done(function (imagesList) {
loadDemoSlideshow(imagesList);
})
.fail(function (jqxhr, error, textResponse) {
console.error(error, textResponse, jqxhr);
});
}
else {
// Get local pictures from JSON list
$.getJSON(imagesPath)
.done(function (imagesList) {
loadSlideshow(imagesList);
})
.fail(function (jqxhr, error, textResponse) {
console.error(error, textResponse, jqxhr);
});
}
});