show.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. "use strict";
  2. (() => {
  3. function Show(parent, files, delay) {
  4. this.index = 0;
  5. this.elapsed = 0;
  6. this.playing = true;
  7. this.loaded = false;
  8. this.list = [];
  9. this.updated = Date.now();
  10. this.delay = !isNaN(delay) ? delay : 3000;
  11. this.parent = parent;
  12. this.parent.innerHTML = '';
  13. this.parent.appendChild(document.createElement('span'));
  14. (async () => {
  15. await this.parse(files);
  16. window.requestAnimationFrame(() => { this.render(); });
  17. window.addEventListener('keyup', (e) => {
  18. if (!e.keyCode) return;
  19. if (e.keyCode === 32) { this.toggle(); }
  20. else if (e.keyCode === 37) { this.prev(); }
  21. else if (e.keyCode === 39) { this.next(); }
  22. }, false);
  23. })();
  24. };
  25. Show.prototype.toggle = function() { this.playing = !this.playing; };
  26. Show.prototype.next = function() {
  27. if (!this.loaded) return;
  28. if (++this.index >= this.list.length) this.index = 0;
  29. this.elapsed = 0;
  30. };
  31. Show.prototype.prev = function() {
  32. if (!this.loaded) return;
  33. if (--this.index < 0) this.index = this.list.length-1; this.elapsed = 0;
  34. };
  35. Show.prototype.format = function(f, a) {
  36. let start = f.indexOf('%');
  37. if (f.length > start && f[start+1] === 's') {
  38. return f.replace('%s', a);
  39. } else if (f.length > start+3 && f[start+3] === 's') {
  40. let c = String(f[start+1]);
  41. let w = parseInt(f[start+2]);
  42. let r = (a.length >= w) ? a : (new Array(w).join(c) + a).slice(-w);
  43. return f.replace(f.slice(start, start+4), r);
  44. }
  45. return f;
  46. };
  47. Show.prototype.preload = async function(file, delay) {
  48. const o = { p: file };
  49. if (o.p.endsWith('.mp4') || o.p.endsWith('.webm') || o.p.endsWith('.ogg')) {
  50. let e = document.createElement('video');
  51. e.addEventListener('error', () => { this.list.splice(this.list.indexOf(o), 1); }, {once: true});
  52. e.addEventListener('canplay', () => { o.d = (isNaN(delay) ? 1 : delay) * e.duration * 1000; o.e = e; }, {once: true});
  53. e.muted = true;
  54. e.loop = true;
  55. e.src = o.p;
  56. } else {
  57. let e = document.createElement('img');
  58. e.addEventListener('error', () => { this.list.splice(this.list.indexOf(o), 1); }, {once: true});
  59. e.addEventListener('load', () => { o.e = e; }, {once: true});
  60. o.d = isNaN(delay) ? this.delay : delay;
  61. e.src = o.p;
  62. }
  63. this.list.push(o);
  64. }
  65. Show.prototype.parse = async function(files) {
  66. if (!(files instanceof Array)) return;
  67. files.map((o) => {
  68. if (typeof o === 'string') {
  69. this.preload(o);
  70. } else if (o instanceof Object && !(o instanceof Array) && typeof o.file !== 'undefined' && typeof o.file === 'string') {
  71. if (o.file.indexOf('%') > -1 && typeof o.range !== 'undefined' && o.range instanceof Array) {
  72. if (o.range.length === 2 && !(isNaN(o.range[2]) && isNaN(o.range[1])) && parseInt(o.range[0]) < parseInt(o.range[1])) {
  73. for (let i = parseInt(o.range[0]); i <= parseInt(o.range[1]); i++) {
  74. this.preload(this.format(o.file, i), o.delay);
  75. }
  76. } else {
  77. for (let i of o.range) {
  78. this.preload(this.format(o.file, i), o.delay);
  79. }
  80. }
  81. } else {
  82. this.preload(o.file, o.delay);
  83. }
  84. }
  85. });
  86. };
  87. Show.prototype.render = function() {
  88. let d = Date.now();
  89. if (this.loaded) {
  90. let o = this.list[this.index];
  91. let vid = (o.e.tagName == 'VIDEO') ? 'video' : 'natural';
  92. o.e.className = (o.e[vid+'Width'] / o.e[vid+'Height'] < this.parent.clientWidth / this.parent.clientHeight) ? 'fillheight' : 'fillwidth';
  93. if (this.parent.firstChild !== o.e) {
  94. o.e.src = o.e.src;
  95. this.parent.replaceChild(o.e, this.parent.firstChild);
  96. if (vid === 'video') o.e.play();
  97. }
  98. if (this.playing && document.hasFocus() && (this.elapsed += (d - this.updated)) && this.elapsed >= o.d) this.next();
  99. } else {
  100. this.loaded = (this.list.length > 0) && this.list.every((l) => { return typeof l.e !== 'undefined'; });
  101. }
  102. s.updated = d;
  103. window.requestAnimationFrame(() => { this.render(); });
  104. }
  105. window.Show = Show;
  106. })();