background.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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(this.fnContext.wrap(() => {
  21. this.setBackgroundActual(background);
  22. }));
  23. }
  24. showVideo() {
  25. this.video.classList.add('show');
  26. this.last?.classList.remove('show');
  27. }
  28. hideVideo() {
  29. this.last?.classList.add('show');
  30. }
  31. setVideo(element: HTMLElement) {
  32. this.video.innerHTML = '';
  33. this.video.classList.remove('settled');
  34. this.video.appendChild(element);
  35. }
  36. private setBackgroundActual(background: string) {
  37. if (background.indexOf('.') >= 0) {
  38. this.next.style.backgroundImage = `url(${background})`;
  39. this.next.style.backgroundColor = 'black';
  40. this.next.classList.add('image');
  41. } else {
  42. this.next.style.backgroundColor = background;
  43. }
  44. this.next.classList.add('show');
  45. if (this.last != null) {
  46. const toRemove = this.last;
  47. this.last.classList.remove('show');
  48. this.next.addEventListener('transitionend', () => {
  49. this.element.removeChild(toRemove);
  50. this.video.classList.remove('show');
  51. this.video.innerHTML = '';
  52. }, { once: true });
  53. }
  54. this.last = this.next;
  55. this.next = document.createElement('div');
  56. this.element.appendChild(this.next);
  57. }
  58. }