summaryrefslogtreecommitdiff
path: root/src/shadow.js
blob: 462b3af85224d598da78a8e1332ad98a83860ea1 (plain)
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
import Animation from './animation.js'

export default class Shadow {
  zIndex = '1000'
  isOpen = false

  /**
   * ============
   * Constructors
   * ============
   */

  constructor(image) {
    this.original = image;
    this.create(image);

    this.startingState = {
      top: this.element.offsetTop,
      left: this.element.offsetLeft,
      width: this.element.offsetWidth,
      height: this.element.offsetHeight
    }

    return this;
  }

  create(image) {
    // create shadow image
    const shadow = document.createElement('img');
    shadow.src = image.src;
    shadow.id = `libra-shadow-${image.id}`;
    shadow.classList.add('libra-shadow');

    // style and position shadow image
    shadow.style.top = image.offsetTop;
    shadow.style.left = image.offsetLeft;
    shadow.style.width = image.offsetWidth;
    shadow.style.height = image.offsetHeight;
    shadow.style.zIndex = this.zIndex;

    document.body.appendChild(shadow);
    this.element = shadow;
  }

  /**
   * ============
   * Actions
   * ============
   */

  open() {
    this.isOpen = true;

    this.original.style.visibility = 'hidden';
    if (!document.body.contains(this.element)) this.placeItself();

    Animation.transformMatrix(
      this.element,
      this.startingState,
      Animation.calculateFinalState(this.element)
    );
  }

  close() {
    this.isOpen = false;

    this.original.style.visibility = 'visible';

    Animation.resetTransformMatrix(this.element);
    setTimeout(() => this.destroyItself(), 300);
  }

  placeItself() {
    document.body.appendChild(this.element);
  }

  destroyItself() {
    if (!document.body.contains(this.element)) return;
    document.body.removeChild(this.element);
  }
}