File size: 9,978 Bytes
cb89283
 
 
 
 
 
fcd0eab
22c7264
 
76763d7
 
 
 
22c7264
8755ee2
 
8a554f1
a47eab3
 
a174130
a47eab3
 
 
8755ee2
a174130
 
 
 
 
 
 
 
 
0c119f7
22c7264
0c119f7
 
27c03db
76763d7
a174130
10f92b3
2f7f50d
22c7264
 
a47eab3
 
 
3ce1b66
a47eab3
095d0ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6213af
 
 
 
095d0ef
 
 
edbe668
 
3ce1b66
edbe668
 
 
 
 
 
 
 
 
a47eab3
 
3ce1b66
a47eab3
095d0ef
 
 
 
 
edbe668
 
 
 
 
 
 
 
2579d96
841b172
edbe668
 
 
 
841b172
095d0ef
 
8755ee2
 
 
3ce1b66
1d58af6
8755ee2
 
fcd0eab
 
 
22c7264
fcd0eab
 
 
22c7264
fcd0eab
 
 
 
 
 
22c7264
b1c5f8c
0faac17
 
 
 
 
 
 
 
 
5d08a61
 
 
3ce1b66
5d08a61
a174130
10f92b3
dc1e1c1
a174130
 
 
 
fcd0eab
2f7f50d
 
 
dc1e1c1
10f92b3
 
 
ede8cb9
 
a174130
 
 
 
3ce1b66
a174130
 
 
 
 
 
 
 
 
5d08a61
 
3ce1b66
5d08a61
ede8cb9
10f92b3
 
 
ede8cb9
 
a47eab3
 
3ce1b66
a47eab3
b1c5f8c
10f92b3
a174130
10f92b3
fcd0eab
dc1e1c1
27c03db
a174130
f3edffc
a174130
 
 
00da064
a174130
 
27c03db
10f92b3
 
 
 
 
 
cb89283
 
 
 
 
 
 
 
 
2e076cb
 
 
cb89283
 
 
 
 
 
 
fcd0eab
 
 
cb89283
fcd0eab
 
 
cb89283
 
 
fcd0eab
 
cb89283
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a731b1
 
cb89283
 
 
 
 
 
 
 
e6213af
 
 
 
 
 
 
cb89283
 
 
 
 
 
 
 
 
 
 
 
 
fcd0eab
 
 
 
 
 
c7b5b8a
fcd0eab
 
 
727f927
fcd0eab
 
 
 
 
 
 
 
a174130
 
3ce1b66
a174130
 
 
 
 
6f9d983
 
 
3ce1b66
6f9d983
 
 
 
 
3ce1b66
 
 
 
a174130
 
 
 
3ce1b66
 
 
 
10f92b3
 
 
 
 
 
ede8cb9
 
a47eab3
 
3ce1b66
a47eab3
ede8cb9
10f92b3
 
 
 
f3edffc
 
 
0797fb7
 
 
 
 
ede8cb9
2f7f50d
f3edffc
0797fb7
b1c5f8c
ec45071
a47eab3
 
3ce1b66
a47eab3
095d0ef
ec45071
 
 
 
095d0ef
 
 
 
ec45071
bdfd65a
 
 
 
3ce1b66
bdfd65a
 
 
 
 
 
 
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import {
  ANIMATION_DURATION_MS,
  CLASS_DRIVER_HIGHLIGHTED_ELEMENT,
  CLASS_FIX_STACKING_CONTEXT,
  CLASS_POSITION_RELATIVE,
} from '../common/constants';
import { getStyleProperty } from '../common/utils';
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 {Stage} stage
   * @param {Overlay} overlay
   * @param {Window} window
   * @param {Document} document
   */
  constructor({
    node,
    options,
    popover,
    stage,
    overlay,
    window,
    document,
  } = {}) {
    this.node = node;
    this.document = document;
    this.window = window;
    this.options = options;
    this.overlay = overlay;
    this.popover = popover;
    this.stage = stage;

    this.animationTimeout = null;
  }

  /**
   * Checks if the current element is visible in viewport
   * @returns {boolean}
   * @private
   */
  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
   * @private
   */
  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
   * @private
   */
  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: 'instant',
        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
   * @public
   * @return {Position}
   */
  getCalculatedPosition() {
    const body = this.document.body;
    const documentElement = this.document.documentElement;
    const window = this.window;

    const scrollTop = this.window.pageYOffset || documentElement.scrollTop || body.scrollTop;
    const scrollLeft = window.pageXOffset || documentElement.scrollLeft || body.scrollLeft;
    const elementRect = this.node.getBoundingClientRect();

    return new Position({
      top: elementRect.top + scrollTop,
      left: elementRect.left + scrollLeft,
      right: elementRect.left + scrollLeft + elementRect.width,
      bottom: elementRect.top + scrollTop + elementRect.height,
    });
  }

  /**
   * Gets the popover for the current element if any
   * @returns {Popover|*}
   * @public
   */
  getPopover() {
    return this.popover;
  }

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

    if (hideStage) {
      this.hideStage();
    }

    this.removeHighlightClasses();

    // If there was any animation in progress, cancel that
    this.window.clearTimeout(this.animationTimeout);

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

  /**
   * Checks if the given element is same as the current element
   * @param {Element} element
   * @returns {boolean}
   * @public
   */
  isSame(element) {
    if (!element || !element.node) {
      return false;
    }

    return element.node === this.node;
  }

  /**
   * Is called when the element is about to be highlighted
   * @public
   */
  onHighlightStarted() {
    if (this.options.onHighlightStarted) {
      this.options.onHighlightStarted(this);
    }
  }

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

    this.addHighlightClasses();

    const highlightedElement = this;
    const popoverElement = this.popover;

    if (popoverElement && !popoverElement.isInView()) {
      popoverElement.bringInView();
    }

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

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

  /**
   * Removes the stacking context fix classes and the highlighter classes
   * @private
   */
  removeHighlightClasses() {
    this.node.classList.remove(CLASS_DRIVER_HIGHLIGHTED_ELEMENT);
    this.node.classList.remove(CLASS_POSITION_RELATIVE);

    const stackFixes = this.document.querySelectorAll(`.${CLASS_FIX_STACKING_CONTEXT}`);
    for (let counter = 0; counter < stackFixes.length; counter++) {
      stackFixes[counter].classList.remove(CLASS_FIX_STACKING_CONTEXT);
    }
  }

  /**
   * Adds the highlight class on the current element and "fixes"
   * the parent nodes if they
   * @private
   */
  addHighlightClasses() {
    this.node.classList.add(CLASS_DRIVER_HIGHLIGHTED_ELEMENT);

    // Don't make relative if element already has some position set
    if (this.canMakeRelative()) {
      this.node.classList.add(CLASS_POSITION_RELATIVE);
    }

    // Check and re-define the stacking context
    this.fixStackingContext();
  }

  /**
   * Walks through the parents of the current element and fixes
   * the stacking context
   * @private
   */
  fixStackingContext() {
    let parentNode = this.node.parentNode;
    while (parentNode) {
      if (!parentNode.tagName || parentNode.tagName.toLowerCase() === 'body') {
        break;
      }

      const zIndex = getStyleProperty(parentNode, 'z-index');
      const opacity = parseFloat(getStyleProperty(parentNode, 'opacity'));
      const transform = getStyleProperty(parentNode, 'transform', true);
      const transformStyle = getStyleProperty(parentNode, 'transform-style', true);
      const transformBox = getStyleProperty(parentNode, 'transform-box', true);
      const filter = getStyleProperty(parentNode, 'filter', true);
      const perspective = getStyleProperty(parentNode, 'perspective', true);

      // Stacking context gets disturbed if
      // - Parent has z-index
      // - Opacity is below 0
      // - Filter/transform or perspective is applied
      if (
        /[0-9]+/.test(zIndex)
        || opacity < 1
        || (transform && transform !== 'none')
        || (transformStyle && transformStyle !== 'flat')
        || (transformBox && transformBox !== 'border-box')
        || (filter && filter !== 'none')
        || (perspective && perspective !== 'none')
      ) {
        parentNode.classList.add(CLASS_FIX_STACKING_CONTEXT);
      }

      parentNode = parentNode.parentNode;
    }
  }

  /**
   * Checks if we can make the current element relative or not
   * @return {boolean}
   * @private
   */
  canMakeRelative() {
    const currentPosition = this.getStyleProperty('position');
    const avoidPositionsList = ['absolute', 'fixed', 'relative'];

    // Because if the element has any of these positions, making it
    // relative will break the UI
    return avoidPositionsList.indexOf(currentPosition) === -1;
  }

  /**
   * Get current element's CSS attribute value
   * @param {string} property
   * @returns string
   * @private
   */
  getStyleProperty(property) {
    return getStyleProperty(this.node, property);
  }

  /**
   * Shows the stage behind the element
   * @public
   */
  showStage() {
    this.stage.show(this.getCalculatedPosition());
  }

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

  /**
   * Hides the stage
   * @public
   */
  hideStage() {
    this.stage.hide();
  }

  /**
   * Hides the popover if possible
   * @public
   */
  hidePopover() {
    if (!this.popover) {
      return;
    }

    this.popover.hide();
  }

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

    const showAtPosition = this.getCalculatedPosition();

    // For first highlight, show it immediately because there won't be any animation
    let showAfterMs = ANIMATION_DURATION_MS;
    // If animation is disabled or  if it is the first display, show it immediately
    if (!this.options.animate || !this.overlay.lastHighlightedElement) {
      showAfterMs = 0;
    }

    this.animationTimeout = this.window.setTimeout(() => {
      this.popover.show(showAtPosition);
    }, showAfterMs);
  }

  /**
   * @returns {{height: number, width: number}}
   * @public
   */
  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),
    };
  }

  /**
   * Gets the size for popover
   * @returns {{height: number, width: number}}
   * @public
   */
  getSize() {
    return {
      height: Math.max(this.node.scrollHeight, this.node.offsetHeight),
      width: Math.max(this.node.scrollWidth, this.node.offsetWidth),
    };
  }
}