show.js 3.8 KB

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