background.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import * as util from './util';
  2. export class BackgroundManager {
  3. element: HTMLElement;
  4. video: HTMLElement;
  5. last: HTMLElement | null;
  6. next: HTMLElement;
  7. fnContext: util.FnContext = new util.FnContext();
  8. constructor(element: HTMLElement) {
  9. this.element = element;
  10. this.last = null;
  11. this.video = util.getElement(element, '#video');
  12. this.video.addEventListener('transitionend', () => {
  13. this.video.classList.add('settled');
  14. });
  15. this.next = document.createElement('div');
  16. this.element.appendChild(this.next);
  17. }
  18. setBackground(background: string) {
  19. this.fnContext.invalidate();
  20. util.loadBackground(background).then(
  21. this.fnContext.wrap(() => {
  22. this.setBackgroundActual(background);
  23. })
  24. );
  25. }
  26. showVideo() {
  27. this.video.classList.add('show');
  28. this.last?.classList.remove('show');
  29. }
  30. hideVideo() {
  31. this.last?.classList.add('show');
  32. }
  33. setVideo(element: HTMLElement) {
  34. this.video.innerHTML = '';
  35. this.video.classList.remove('settled');
  36. this.video.appendChild(element);
  37. }
  38. private setBackgroundActual(background: string) {
  39. if (background.indexOf('.') >= 0) {
  40. this.next.style.backgroundImage = `url(${background})`;
  41. this.next.style.backgroundColor = 'black';
  42. this.next.classList.add('image');
  43. } else {
  44. this.next.style.backgroundColor = background;
  45. }
  46. this.next.classList.add('show');
  47. if (this.last != null) {
  48. const toRemove = this.last;
  49. this.last.classList.remove('show');
  50. this.next.addEventListener(
  51. 'transitionend',
  52. () => {
  53. this.element.removeChild(toRemove);
  54. this.video.classList.remove('show');
  55. this.video.innerHTML = '';
  56. },
  57. { once: true }
  58. );
  59. }
  60. this.last = this.next;
  61. this.next = document.createElement('div');
  62. this.element.appendChild(this.next);
  63. }
  64. }