File size: 6,526 Bytes
22c7264
 
76763d7
 
 
 
22c7264
8755ee2
 
8a554f1
a47eab3
 
 
 
 
8755ee2
76763d7
0c119f7
22c7264
0c119f7
 
27c03db
76763d7
10f92b3
 
22c7264
 
8755ee2
 
 
 
22c7264
0c119f7
22c7264
 
 
 
 
 
 
 
 
 
 
 
 
 
a47eab3
 
 
 
095d0ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edbe668
 
 
 
 
 
 
 
 
 
 
a47eab3
 
 
095d0ef
 
 
 
 
edbe668
 
 
 
 
 
 
 
841b172
 
edbe668
 
 
 
841b172
095d0ef
 
8755ee2
 
 
 
 
22c7264
 
 
 
 
 
 
 
 
 
0c119f7
22c7264
 
0c119f7
 
22c7264
 
 
 
b1c5f8c
5d08a61
 
 
 
ede8cb9
10f92b3
dc1e1c1
 
 
10f92b3
5d08a61
10f92b3
 
 
ede8cb9
 
dc1e1c1
 
 
 
 
 
 
 
5d08a61
 
 
 
 
ede8cb9
 
10f92b3
 
 
 
 
 
 
 
ede8cb9
 
a47eab3
 
 
b1c5f8c
10f92b3
 
dc1e1c1
 
10f92b3
27c03db
 
 
389540f
27c03db
 
 
 
 
 
389540f
5d08a61
389540f
 
00da064
 
 
 
27c03db
10f92b3
 
 
 
 
 
6f9d983
 
 
 
 
 
 
 
10f92b3
 
 
 
 
 
ede8cb9
 
a47eab3
 
 
ede8cb9
10f92b3
 
 
 
ede8cb9
 
76763d7
b1c5f8c
ec45071
a47eab3
 
 
095d0ef
ec45071
 
 
 
095d0ef
 
 
 
ec45071
22c7264
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import Position from './position';

/**
 * Wrapper around DOMElements to enrich them
 * with the functionality necessary
 */
export default class Element {
  /**
   * DOM element object
   * @param {Node|HTMLElement} node
   * @param {Object} options
   * @param {Popover} popover
   * @param {Overlay} overlay
   * @param {Window} window
   * @param {Document} document
   */
  constructor(node, options, popover, overlay, window, document) {
    this.node = node;
    this.document = document;
    this.window = window;
    this.options = options;
    this.overlay = overlay;
    this.popover = popover;

    this.highlightFinished = false; // To track when the element has fully highlighted
  }

  /**
   * Gets the screen co-ordinates (x,y) for the current dom element
   * @returns {{x: number, y: number}}
   */
  getScreenCoordinates() {
    let tempNode = this.node;

    let x = this.document.documentElement.offsetLeft;
    let y = this.document.documentElement.offsetTop;

    if (tempNode.offsetParent) {
      do {
        x += tempNode.offsetLeft;
        y += tempNode.offsetTop;
      } while (tempNode = tempNode.offsetParent);
    }

    return { x, y };
  }

  /**
   * Checks if the current element is visible in viewport
   * @returns {boolean}
   */
  isInView() {
    let top = this.node.offsetTop;
    let left = this.node.offsetLeft;
    const width = this.node.offsetWidth;
    const height = this.node.offsetHeight;

    let el = this.node;

    while (el.offsetParent) {
      el = el.offsetParent;
      top += el.offsetTop;
      left += el.offsetLeft;
    }

    return (
      top >= this.window.pageYOffset &&
      left >= this.window.pageXOffset &&
      (top + height) <= (this.window.pageYOffset + this.window.innerHeight) &&
      (left + width) <= (this.window.pageXOffset + this.window.innerWidth)
    );
  }

  /**
   * Manually scrolls to the position of element if `scrollIntoView` fails
   */
  scrollManually() {
    const elementRect = this.node.getBoundingClientRect();
    const absoluteElementTop = elementRect.top + this.window.pageYOffset;
    const middle = absoluteElementTop - (this.window.innerHeight / 2);

    this.window.scrollTo(0, middle);
  }

  /**
   * Brings the element to middle of the view port if not in view
   */
  bringInView() {
    if (this.isInView()) {
      return;
    }

    // If browser does not support scrollIntoView
    if (!this.node.scrollIntoView) {
      this.scrollManually();
      return;
    }

    try {
      this.node.scrollIntoView(this.options.scrollIntoViewOptions || {
        behavior: 'smooth',
        block: 'center',
      });
    } catch (e) {
      // `block` option is not allowed in older versions of firefox, scroll manually
      this.scrollManually();
    }
  }

  /**
   * Gets the calculated position on screen, around which
   * we need to draw
   */
  getCalculatedPosition() {
    const coordinates = this.getScreenCoordinates();
    const position = new Position({
      left: Number.MAX_VALUE,
      top: Number.MAX_VALUE,
      right: 0,
      bottom: 0,
    });

    // If we have the position for this element
    // and the element is visible on screen (has some height)
    if (typeof coordinates.x === 'number' && typeof coordinates.y === 'number' && (this.node.offsetWidth > 0 || this.node.offsetHeight > 0)) {
      position.left = Math.min(position.left, coordinates.x);
      position.top = Math.min(position.top, coordinates.y);
      position.right = Math.max(position.right, coordinates.x + this.node.offsetWidth);
      position.bottom = Math.max(position.bottom, coordinates.y + this.node.offsetHeight);
    }

    return position;
  }

  /**
   * Is called when element is about to be deselected
   * i.e. when moving the focus to next element of closing
   */
  onDeselected() {
    this.hidePopover();

    this.node.classList.remove('driver-highlighted-element');

    this.highlightFinished = false;

    if (this.options.onDeselected) {
      this.options.onDeselected(this);
    }
  }

  getSize() {
    const boundingRect = this.node.getBoundingClientRect();
    return {
      width: boundingRect.width,
      height: boundingRect.height
    };
  }

  /**
   * Is called when the element is about to be highlighted
   * i.e. either if overlay has started moving the highlight towards
   * this element of has just decided to highlight it
   */
  onHighlightStarted() {
    this.showPopover();

    // Because element has just started highlighting
    // and hasn't completely highlighted
    this.highlightFinished = false;

    if (this.options.onHighlightStarted) {
      this.options.onHighlightStarted(this);
    }
  }

  /**
   * Is called when the element has been successfully highlighted
   */
  onHighlighted() {
    this.showPopover();

    this.node.classList.add('driver-highlighted-element');

    this.highlightFinished = true;

    const highlightedElement = this;
    const lastHighlightedElement = this.overlay.getLastHighlightedElement();
    const popoverElement = this.popover;

    const highlightedNode = this.node;
    const lastHighlightedNode = lastHighlightedElement && lastHighlightedElement.node;

    // If this element is not already highlighted (because this call could
    // be from the resize or scroll) and is not in view
    if (highlightedNode !== lastHighlightedNode) {
      if (popoverElement && !popoverElement.isInView()) {
        popoverElement.bringInView();
      }

      if (!highlightedElement.isInView()) {
        highlightedElement.bringInView();
      }
    }

    if (this.options.onHighlighted) {
      this.options.onHighlighted(this);
    }
  }

  /**
   * Gets the DOM Element behind this element
   * @returns {Node|HTMLElement|*}
   */
  getNode() {
    return this.node;
  }

  hidePopover() {
    if (!this.popover) {
      return;
    }

    this.popover.hide();
  }

  /**
   * Shows the popover on the current element
   */
  showPopover() {
    if (!this.popover) {
      return;
    }

    const position = this.getCalculatedPosition();

    this.popover.show(position);
  }

  /**
   * @returns {{height: number, width: number}}
   */
  getFullPageSize() {
    // eslint-disable-next-line prefer-destructuring
    const body = this.document.body;
    const html = this.document.documentElement;

    return {
      height: Math.max(body.scrollHeight, body.offsetHeight, html.scrollHeight, html.offsetHeight),
      width: Math.max(body.scrollWidth, body.offsetWidth, html.scrollWidth, html.offsetWidth),
    };
  }
}