@enact/spotlight 3.4.9-experimental-3 → 3.4.9-experimental-7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/{build/Accelerator → Accelerator}/Accelerator.d.ts +0 -0
  2. package/Accelerator/Accelerator.js +149 -156
  3. package/CHANGELOG.md +22 -0
  4. package/{build/Pause → Pause}/Pause.d.ts +0 -0
  5. package/Pause/Pause.js +102 -80
  6. package/Pause/tests/Pause-specs.js +65 -97
  7. package/SpotlightContainerDecorator/SpotlightContainer.js +175 -140
  8. package/{build/SpotlightContainerDecorator → SpotlightContainerDecorator}/SpotlightContainerDecorator.d.ts +0 -0
  9. package/SpotlightContainerDecorator/SpotlightContainerDecorator.js +222 -176
  10. package/SpotlightContainerDecorator/tests/Element.prototype.closest.js +44 -40
  11. package/SpotlightContainerDecorator/tests/SpotlightContainerDecorator-specs.js +221 -210
  12. package/SpotlightContainerDecorator/tests/useSpotlightContainer-specs.js +150 -243
  13. package/SpotlightContainerDecorator/useSpotlightContainer.js +47 -28
  14. package/{build/SpotlightRootDecorator → SpotlightRootDecorator}/SpotlightRootDecorator.d.ts +0 -0
  15. package/SpotlightRootDecorator/SpotlightRootDecorator.js +206 -111
  16. package/{build/Spottable → Spottable}/Spottable.d.ts +0 -0
  17. package/Spottable/Spottable.js +448 -410
  18. package/Spottable/tests/Spottable-specs.js +90 -117
  19. package/package.json +3 -2
  20. package/src/container.js +718 -653
  21. package/src/navigate.js +406 -491
  22. package/src/pointer.js +86 -78
  23. package/{build/src → src}/spotlight.d.ts +0 -0
  24. package/src/spotlight.js +846 -885
  25. package/src/target.js +448 -474
  26. package/src/tests/container-specs.js +643 -1251
  27. package/src/tests/navigate-specs.js +77 -134
  28. package/src/tests/pointer-specs.js +126 -182
  29. package/src/tests/spotlight-specs.js +103 -133
  30. package/src/tests/target-specs.js +597 -960
  31. package/src/tests/utils.js +89 -67
  32. package/src/utils.js +155 -158
  33. package/build/Accelerator/Accelerator.js +0 -162
  34. package/build/Accelerator/package.json +0 -3
  35. package/build/CHANGELOG.md +0 -838
  36. package/build/Pause/Pause.js +0 -164
  37. package/build/Pause/package.json +0 -3
  38. package/build/Pause/tests/Pause-specs.js +0 -65
  39. package/build/README.md +0 -61
  40. package/build/SpotlightContainerDecorator/SpotlightContainer.js +0 -176
  41. package/build/SpotlightContainerDecorator/SpotlightContainerDecorator.js +0 -282
  42. package/build/SpotlightContainerDecorator/package.json +0 -3
  43. package/build/SpotlightContainerDecorator/tests/Element.prototype.closest.js +0 -45
  44. package/build/SpotlightContainerDecorator/tests/SpotlightContainerDecorator-specs.js +0 -221
  45. package/build/SpotlightContainerDecorator/tests/useSpotlightContainer-specs.js +0 -160
  46. package/build/SpotlightContainerDecorator/useSpotlightContainer.js +0 -133
  47. package/build/SpotlightRootDecorator/SpotlightRootDecorator.js +0 -189
  48. package/build/SpotlightRootDecorator/package.json +0 -3
  49. package/build/Spottable/Spottable.js +0 -491
  50. package/build/Spottable/package.json +0 -3
  51. package/build/Spottable/tests/Spottable-specs.js +0 -90
  52. package/build/package-lock.json +0 -229
  53. package/build/package.json +0 -28
  54. package/build/src/container.js +0 -1201
  55. package/build/src/navigate.js +0 -419
  56. package/build/src/pointer.js +0 -183
  57. package/build/src/spotlight.js +0 -896
  58. package/build/src/target.js +0 -507
  59. package/build/src/tests/container-specs.js +0 -653
  60. package/build/src/tests/navigate-specs.js +0 -87
  61. package/build/src/tests/pointer-specs.js +0 -130
  62. package/build/src/tests/spotlight-specs.js +0 -112
  63. package/build/src/tests/target-specs.js +0 -614
  64. package/build/src/tests/utils.js +0 -104
  65. package/build/src/utils.js +0 -180
  66. package/build/styles/debug.less +0 -97
  67. package/build/styles/mixins.less +0 -63
  68. package/docs/index.md +0 -364
package/src/spotlight.js CHANGED
@@ -1,3 +1,34 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.Spotlight = exports.getDirection = exports["default"] = void 0;
7
+
8
+ var _keymap = require("@enact/core/keymap");
9
+
10
+ var _snapshot = require("@enact/core/snapshot");
11
+
12
+ var _platform = _interopRequireDefault(require("@enact/core/platform"));
13
+
14
+ var _last = _interopRequireDefault(require("ramda/src/last"));
15
+
16
+ var _Accelerator = _interopRequireDefault(require("../Accelerator"));
17
+
18
+ var _Spottable = require("../Spottable");
19
+
20
+ var _Pause = require("../Pause");
21
+
22
+ var _utils = require("./utils");
23
+
24
+ var _container = require("./container");
25
+
26
+ var _pointer = require("./pointer");
27
+
28
+ var _target = require("./target");
29
+
30
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
31
+
1
32
  /*
2
33
  * A javascript-based implementation of Spatial Navigation.
3
34
  *
@@ -17,78 +48,11 @@
17
48
  * @exports default Spotlight
18
49
  * @exports getDirection
19
50
  */
20
-
21
- import {is} from '@enact/core/keymap';
22
- import {isWindowReady} from '@enact/core/snapshot';
23
- import platform from '@enact/core/platform';
24
- import last from 'ramda/src/last';
25
-
26
- import Accelerator from '../Accelerator';
27
- import {spottableClass} from '../Spottable';
28
- import {isPaused, pause, resume} from '../Pause';
29
- import {contains} from './utils';
30
-
31
- import {
32
- addContainer,
33
- configureContainer,
34
- configureDefaults,
35
- getAllContainerIds,
36
- getContainerConfig,
37
- getContainerId,
38
- getContainerLastFocusedElement,
39
- getContainerNode,
40
- getContainersForNode,
41
- getLastContainer,
42
- getSpottableDescendants,
43
- isContainer,
44
- isContainer5WayHoldable,
45
- isNavigable,
46
- isWithinOverflowContainer,
47
- mayActivateContainer,
48
- notifyLeaveContainer,
49
- notifyLeaveContainerFail,
50
- notifyEnterContainer,
51
- removeAllContainers,
52
- removeContainer,
53
- rootContainerId,
54
- setContainerLastFocusedElement,
55
- setContainerPreviousTarget,
56
- setDefaultContainer,
57
- setLastContainer,
58
- setLastContainerFromTarget,
59
- unmountContainer
60
- } from './container';
61
-
62
- import {
63
- getLastPointerPosition,
64
- getPointerMode,
65
- hasPointerMoved,
66
- notifyKeyDown,
67
- notifyPointerMove,
68
- setPointerMode
69
- } from './pointer';
70
-
71
- import {
72
- getNavigableTarget,
73
- getNearestTargetFromPosition,
74
- getTargetByContainer,
75
- getTargetByDirectionFromElement,
76
- getTargetByDirectionFromPosition,
77
- getTargetBySelector,
78
- isFocusable
79
- } from './target';
80
-
81
- import {
82
- matchSelector,
83
- parseSelector
84
- } from './utils';
85
-
86
- const isDown = is('down');
87
- const isEnter = is('enter');
88
- const isLeft = is('left');
89
- const isRight = is('right');
90
- const isUp = is('up');
91
-
51
+ var isDown = (0, _keymap.is)('down');
52
+ var isEnter = (0, _keymap.is)('enter');
53
+ var isLeft = (0, _keymap.is)('left');
54
+ var isRight = (0, _keymap.is)('right');
55
+ var isUp = (0, _keymap.is)('up');
92
56
  /**
93
57
  * Translates keyCodes into 5-way direction descriptions (e.g. `'down'`)
94
58
  *
@@ -98,16 +62,13 @@ const isUp = is('up');
98
62
  * @returns {String|false} - One of `'up'`, `'down'`, `'left'`, `'right'` or `false` if not a direction key
99
63
  * @public
100
64
  */
101
- const getDirection = function (keyCode) {
102
- return isDown(keyCode) && 'down' ||
103
- isLeft(keyCode) && 'left' ||
104
- isRight(keyCode) && 'right' ||
105
- isUp(keyCode) && 'up';
106
- };
107
-
108
65
 
109
- const SpotlightAccelerator = new Accelerator();
66
+ var getDirection = function getDirection(keyCode) {
67
+ return isDown(keyCode) && 'down' || isLeft(keyCode) && 'left' || isRight(keyCode) && 'right' || isUp(keyCode) && 'up';
68
+ };
110
69
 
70
+ exports.getDirection = getDirection;
71
+ var SpotlightAccelerator = new _Accelerator["default"]();
111
72
  /**
112
73
  * Provides 5-way navigation and focus support
113
74
  *
@@ -128,808 +89,808 @@ const SpotlightAccelerator = new Accelerator();
128
89
  * @memberof spotlight
129
90
  * @public
130
91
  */
131
- const Spotlight = (function () {
132
- 'use strict';
133
-
134
- /*
135
- /* private vars
136
- */
137
- let _initialized = false;
138
- let _duringFocusChange = false;
139
-
140
- /*
141
- * Whether a 5-way directional key is being held.
142
- *
143
- * @type {Boolean}
144
- * @default false
145
- */
146
- let _5WayKeyHold = false;
147
-
148
- /*
149
- * Whether to set focus during the next window focus event
150
- *
151
- * @type {Boolean}
152
- * @default false
153
- */
154
- let _spotOnWindowFocus = false;
155
-
156
- /*
157
- * `true` when a pointer move event occurs during a keypress. Used to short circuit key down
158
- * handling until the next keyup occurs.
159
- *
160
- * @type {Boolean}
161
- * @default false
162
- */
163
- let _pointerMoveDuringKeyPress = false;
164
-
165
- /*
166
- * protected methods
167
- */
168
-
169
- function preventDefault (evt) {
170
- evt.preventDefault();
171
- evt.stopPropagation();
172
- return false;
173
- }
174
-
175
- function shouldPreventNavigation () {
176
- return isPaused() || getAllContainerIds().length === 0;
177
- }
178
-
179
- function getCurrent () {
180
- if (!isWindowReady()) return;
181
-
182
- let activeElement = document.activeElement;
183
- if (activeElement && activeElement !== document.body) {
184
- return activeElement;
185
- }
186
- }
187
-
188
- // An extension point for updating pointer mode based on the current platform.
189
- // Currently only webOS
190
- function setPlatformPointerMode () {
191
- const palmSystem = window.PalmSystem;
192
- if (palmSystem && palmSystem.cursor) {
193
- setPointerMode(palmSystem.cursor.visibility);
194
- }
195
- }
196
-
197
- function focusElement (elem, containerIds, fromPointer) {
198
- if (!elem) {
199
- return false;
200
- }
201
-
202
- if ((getPointerMode() && !fromPointer)) {
203
- setContainerLastFocusedElement(elem, containerIds);
204
- return false;
205
- }
206
-
207
- let currentFocusedElement = getCurrent();
208
-
209
- if (elem === currentFocusedElement) {
210
- return true;
211
- }
212
-
213
- const focusOptions = isWithinOverflowContainer(elem, containerIds) ? {preventScroll: true} : null;
214
-
215
- let silentFocus = function () {
216
- elem.focus(focusOptions);
217
- focusChanged(elem, containerIds);
218
- };
219
-
220
- if (_duringFocusChange) {
221
- silentFocus();
222
- return true;
223
- }
224
-
225
- _duringFocusChange = true;
226
-
227
- if (isPaused()) {
228
- silentFocus();
229
- _duringFocusChange = false;
230
- return true;
231
- }
232
-
233
- elem.focus(focusOptions);
234
-
235
- _duringFocusChange = false;
236
-
237
- focusChanged(elem, containerIds);
238
- return true;
239
- }
240
-
241
- function focusChanged (elem, containerIds) {
242
- if (!containerIds || !containerIds.length) {
243
- containerIds = getContainersForNode(elem);
244
- }
245
- const containerId = last(containerIds);
246
- if (containerId) {
247
- setContainerLastFocusedElement(elem, containerIds);
248
- setLastContainer(containerId);
249
- }
250
-
251
- if (__DEV__) {
252
- assignFocusPreview(elem);
253
- }
254
- }
255
-
256
- function restoreFocus () {
257
- const lastContainerId = getLastContainer();
258
- let next;
259
-
260
- if (lastContainerId) {
261
- const position = getLastPointerPosition();
262
-
263
- // walk up the chain of containers from the last to attempt to find a target
264
- next = getContainersForNode(getContainerNode(lastContainerId)).reverse();
265
-
266
- // only prepend last focused if it exists so that Spotlight.focus() doesn't receive
267
- // a falsy target
268
- let lastFocusedElement = getContainerLastFocusedElement(lastContainerId);
269
-
270
- while (isContainer(lastFocusedElement)) {
271
- ({lastFocusedElement} = getContainerConfig(lastFocusedElement));
272
- }
273
-
274
- const lastContainerNode = getContainerNode(lastContainerId);
275
-
276
- if (!lastFocusedElement || (lastContainerNode.getBoundingClientRect && lastFocusedElement.getBoundingClientRect && !contains(lastContainerNode.getBoundingClientRect(), lastFocusedElement.getBoundingClientRect()))) {
277
- lastFocusedElement = getContainerConfig(lastContainerId).overflow && getNearestTargetFromPosition(position, lastContainerId);
278
- }
279
-
280
- if (lastFocusedElement) {
281
- next.unshift(lastFocusedElement);
282
- }
283
- } else {
284
- next = [rootContainerId];
285
- }
286
-
287
- // attempt to find a target starting with the last focused element in the last
288
- // container, followed by the last container, and finally the root container
289
- return next.reduce((focused, target) => focused || Spotlight.focus(target), false);
290
- }
291
-
292
- // The below should be gated on non-production environment only.
293
- function assignFocusPreview (elem) {
294
- const directions = ['up', 'right', 'down', 'left'],
295
- nextClassBase = spottableClass + '-next-';
296
-
297
- // Remove all previous targets
298
- directions.forEach((dir) => {
299
- const nextClass = nextClassBase + dir,
300
- prevElems = parseSelector('.' + nextClass);
301
- if (prevElems && prevElems.length !== 0) {
302
- prevElems.forEach(prevElem => prevElem.classList.remove(nextClass));
303
- }
304
- });
305
-
306
- // Find all next targets and identify them
307
- directions.forEach((dir) => {
308
- const nextElem = getTargetByDirectionFromElement(dir, elem);
309
- if (nextElem) {
310
- nextElem.classList.add(nextClassBase + dir);
311
- }
312
- });
313
- }
314
-
315
- function spotNextFromPoint (direction, position) {
316
- const containerId = Spotlight.getActiveContainer();
317
- const next = getTargetByDirectionFromPosition(direction, position, containerId);
318
-
319
- if (next) {
320
- setContainerPreviousTarget(
321
- containerId,
322
- direction,
323
- next,
324
- getContainerLastFocusedElement(containerId)
325
- );
326
-
327
- return focusElement(next, getContainersForNode(next));
328
- }
329
-
330
- return false;
331
- }
332
-
333
- function spotNext (direction, currentFocusedElement, currentContainerIds) {
334
- const next = getTargetByDirectionFromElement(direction, currentFocusedElement);
335
-
336
- if (next) {
337
- const currentContainerId = last(currentContainerIds);
338
- const nextContainerIds = getContainersForNode(next);
339
-
340
- // prevent focus if 5-way is being held and the next element isn't wrapped by
341
- // the current element's immediate container
342
- if (_5WayKeyHold && nextContainerIds.indexOf(currentContainerId) < 0 && !isContainer5WayHoldable(currentContainerId)) {
343
- return false;
344
- }
345
-
346
- notifyLeaveContainer(
347
- direction,
348
- currentFocusedElement,
349
- currentContainerIds,
350
- next,
351
- nextContainerIds
352
- );
353
-
354
- setContainerPreviousTarget(
355
- currentContainerId,
356
- direction,
357
- next,
358
- currentFocusedElement
359
- );
360
-
361
- const focused = focusElement(next, nextContainerIds);
362
-
363
- notifyEnterContainer(
364
- direction,
365
- currentFocusedElement,
366
- currentContainerIds,
367
- next,
368
- nextContainerIds
369
- );
370
-
371
- return focused;
372
- }
373
-
374
- notifyLeaveContainerFail(direction, currentFocusedElement, currentContainerIds);
375
-
376
- return false;
377
- }
378
-
379
- function onAcceleratedKeyDown (evt) {
380
- const direction = getDirection(evt.keyCode);
381
-
382
- if (!direction) return;
383
-
384
- const currentFocusedElement = getCurrent();
385
- const currentContainerIds = getContainersForNode(currentFocusedElement);
386
-
387
- spotNext(direction, currentFocusedElement, currentContainerIds);
388
- }
389
-
390
- function onBlur () {
391
- const current = getCurrent();
392
-
393
- if (current) {
394
- current.blur();
395
- }
396
- Spotlight.setPointerMode(false);
397
- _spotOnWindowFocus = true;
398
- _pointerMoveDuringKeyPress = false;
399
- }
400
-
401
- function handleWebOSMouseEvent (ev) {
402
- if (!isPaused() && ev && ev.detail && ev.detail.type === 'Leave') {
403
- onBlur();
404
- }
405
- }
406
-
407
- function handleKeyboardStateChangeEvent ({visibility}) {
408
- if (!visibility) {
409
- setPlatformPointerMode();
410
- }
411
- }
412
-
413
- function onFocus () {
414
- // Normally, there isn't focus here unless the window has been blurred above. On webOS, the
415
- // platform may focus the window after the app has already focused a component so we prevent
416
- // trying to focus something else (potentially) unless the window was previously blurred
417
- if (_spotOnWindowFocus) {
418
- setPlatformPointerMode();
419
-
420
- // If the window was previously blurred while in pointer mode, the last active containerId may
421
- // not have yet set focus to its spottable elements. For this reason we can't rely on setting focus
422
- // to the last focused element of the last active containerId, so we use rootContainerId instead
423
- let lastFocusedElement = getContainerLastFocusedElement(rootContainerId);
424
- while (isContainer(lastFocusedElement)) {
425
- ({lastFocusedElement} = getContainerConfig(lastFocusedElement));
426
- }
427
-
428
- if (!Spotlight.focus(lastFocusedElement)) {
429
- // If the last focused element was previously also disabled (or no longer exists), we
430
- // need to set focus somewhere
431
- Spotlight.focus();
432
- }
433
- _spotOnWindowFocus = false;
434
- }
435
- }
436
-
437
- function onKeyUp (evt) {
438
- _pointerMoveDuringKeyPress = false;
439
- const keyCode = evt.keyCode;
440
-
441
- if (getDirection(keyCode) || isEnter(keyCode)) {
442
- SpotlightAccelerator.reset();
443
- _5WayKeyHold = false;
444
- }
445
- }
446
-
447
- function handlePointerHide () {
448
- if (!getCurrent()) {
449
- restoreFocus();
450
- }
451
- }
452
-
453
- function onKeyDown (evt) {
454
- if (shouldPreventNavigation()) {
455
- notifyKeyDown(evt.keyCode);
456
- return;
457
- }
458
-
459
- const keyCode = evt.keyCode;
460
- const direction = getDirection(keyCode);
461
- const pointerHandled = notifyKeyDown(keyCode, handlePointerHide);
462
-
463
- if (pointerHandled || !(direction || isEnter(keyCode))) {
464
- return;
465
- }
466
-
467
- if (!isPaused() && !_pointerMoveDuringKeyPress) {
468
- if (getCurrent()) {
469
- SpotlightAccelerator.processKey(evt, onAcceleratedKeyDown);
470
- } else if (!spotNextFromPoint(direction, getLastPointerPosition())) {
471
- restoreFocus();
472
- }
473
- _5WayKeyHold = true;
474
- }
475
-
476
- if (direction) {
477
- preventDefault(evt);
478
- }
479
- }
480
-
481
- function onMouseMove ({target, clientX, clientY}) {
482
- if (shouldPreventNavigation()) {
483
- notifyPointerMove(null, target, clientX, clientY);
484
- return;
485
- }
486
-
487
- const current = getCurrent();
488
- const update = notifyPointerMove(current, target, clientX, clientY);
489
-
490
- if (update) {
491
- if (_5WayKeyHold) {
492
- _pointerMoveDuringKeyPress = true;
493
- }
494
-
495
- const next = getNavigableTarget(target);
496
-
497
- // TODO: Consider encapsulating this work within focusElement
498
- if (next !== current) {
499
- if (next) {
500
- focusElement(next, getContainersForNode(next), true);
501
-
502
- return true;
503
- } else if (current) {
504
- current.blur();
505
- setLastContainerFromTarget(current, target);
506
- }
507
- }
508
- }
509
- }
510
-
511
- function onMouseOver (evt) {
512
- if (shouldPreventNavigation()) return;
513
-
514
- const {target} = evt;
515
-
516
- if (getPointerMode() && hasPointerMoved(evt.clientX, evt.clientY)) {
517
- const next = getNavigableTarget(target); // account for child controls
518
-
519
- if (next && next !== getCurrent()) {
520
- focusElement(next, getContainersForNode(next), true);
521
-
522
- return true;
523
- }
524
-
525
- preventDefault(evt);
526
- }
527
- }
528
-
529
- function onTouchEnd (evt) {
530
- const current = getCurrent();
531
- if (current && !current.contains(evt.target)) {
532
- current.blur();
533
- }
534
- }
535
-
536
- /*
537
- * public methods
538
- */
539
- const exports = /** @lends spotlight.Spotlight */ { // eslint-disable-line no-shadow
540
- /**
541
- * Initializes Spotlight. This is generally handled by
542
- * {@link spotlight/SpotlightRootDecorator.SpotlightRootDecorator}.
543
- *
544
- * @param {Object} containerDefaults Default configuration for new spotlight containers
545
- * @returns {undefined}
546
- * @public
547
- */
548
- initialize: function (containerDefaults) {
549
- if (!_initialized) {
550
- window.addEventListener('blur', onBlur);
551
- window.addEventListener('focus', onFocus);
552
- window.addEventListener('keydown', onKeyDown);
553
- window.addEventListener('keyup', onKeyUp);
554
- window.addEventListener('mouseover', onMouseOver);
555
- window.addEventListener('mousemove', onMouseMove);
556
-
557
- if (platform.touch) {
558
- window.addEventListener('touchend', onTouchEnd);
559
- }
560
-
561
- if (platform.webos) {
562
- window.top.document.addEventListener('webOSMouse', handleWebOSMouseEvent);
563
- window.top.document.addEventListener('keyboardStateChange', handleKeyboardStateChangeEvent);
564
- }
565
-
566
- setLastContainer(rootContainerId);
567
- configureDefaults(containerDefaults);
568
- configureContainer(rootContainerId);
569
- // by default, pointer mode is off but the platform's current state will override that
570
- setPointerMode(false);
571
- setPlatformPointerMode();
572
- _initialized = true;
573
- }
574
- },
575
-
576
- /**
577
- * Terminates Spotlight. This is generally handled by {@link spotlight.SpotlightRootDecorator}.
578
- *
579
- * @public
580
- */
581
- terminate: function () {
582
- window.removeEventListener('blur', onBlur);
583
- window.removeEventListener('focus', onFocus);
584
- window.removeEventListener('keydown', onKeyDown);
585
- window.removeEventListener('keyup', onKeyUp);
586
- window.removeEventListener('mouseover', onMouseOver);
587
- window.removeEventListener('mousemove', onMouseMove);
588
-
589
- if (platform.touch) {
590
- window.removeEventListener('touchend', onTouchEnd);
591
- }
592
-
593
- if (platform.webos) {
594
- window.top.document.removeEventListener('webOSMouse', handleWebOSMouseEvent);
595
- window.top.document.removeEventListener('keyboardStateChange', handleKeyboardStateChangeEvent);
596
- }
597
- Spotlight.clear();
598
- _initialized = false;
599
- },
600
-
601
- /**
602
- * Resets spotlight container information
603
- *
604
- * @private
605
- */
606
- clear: function () {
607
- removeAllContainers();
608
- setDefaultContainer();
609
- setLastContainer();
610
- _duringFocusChange = false;
611
- },
612
-
613
- // set(<config>);
614
- // set(<containerId>, <config>);
615
- /**
616
- * Sets the config for spotlight or the specified containerID
617
- *
618
- * @function
619
- * @param {String|Object} containerIdOrConfig Configuration object or container ID
620
- * @param {Object} [config] Configuration object if container ID supplied
621
- * in `containerIdOrConfig`
622
- * @returns {undefined}
623
- * @public
624
- */
625
- set: configureContainer,
626
-
627
- // add(<config>);
628
- // add(<containerId>, <config>);
629
- /**
630
- * Adds the config for a new container. The container ID may be passed in the configuration
631
- * object. If no container ID is supplied, a new container ID will be generated.
632
- *
633
- * @function
634
- * @param {String|Object} containerIdOrConfig Configuration object or container ID
635
- * @param {Object} [config] Configuration object if container ID supplied
636
- * in `containerIdOrConfig`
637
- * @returns {String} The container ID of the container
638
- * @public
639
- */
640
- add: addContainer,
641
-
642
- unmount: function (containerId) {
643
- if (!containerId || typeof containerId !== 'string') {
644
- throw new Error('Please assign the "containerId"!');
645
- }
646
- unmountContainer(containerId);
647
- },
648
-
649
- /**
650
- * Removes a container from Spotlight
651
- *
652
- * @param {String} containerId Container ID to remove
653
- * @returns {Boolean} `true` if container removed, `false` if container does not exist
654
- * @public
655
- */
656
- remove: function (containerId) {
657
- if (!containerId || typeof containerId !== 'string') {
658
- throw new Error('Please assign the "containerId"!');
659
- }
660
- if (getContainerConfig(containerId)) {
661
- removeContainer(containerId);
662
- if (getLastContainer() === containerId) {
663
- Spotlight.setActiveContainer(null);
664
- }
665
- return true;
666
- }
667
- return false;
668
- },
669
-
670
- /**
671
- * Disables the selector rules of the specified container
672
- *
673
- * @param {String} containerId Container ID selector rules to disable
674
- * @returns {Boolean} `true` if container's selector rules are disabled, `false` if container does not exist
675
- * @public
676
- */
677
- disableSelector: function (containerId) {
678
- if (isContainer(containerId)) {
679
- configureContainer(containerId, {selectorDisabled: false});
680
- return true;
681
- }
682
-
683
- return false;
684
- },
685
-
686
- /**
687
- * Enables the selector rules of the specified container
688
- *
689
- * @param {String} containerId Container ID selector rules to enable
690
- * @returns {Boolean} `true` if container's selector rules are enabled, `false` if container does not exist
691
- * @public
692
- */
693
- enableSelector: function (containerId) {
694
- if (isContainer(containerId)) {
695
- configureContainer(containerId, {selectorDisabled: false});
696
- return true;
697
- }
698
-
699
- return false;
700
- },
701
-
702
- /**
703
- * Pauses Spotlight
704
- *
705
- * @function
706
- * @returns {undefined}
707
- * @public
708
- */
709
- pause,
710
-
711
- /**
712
- * Resumes Spotlight
713
- *
714
- * @function
715
- * @returns {undefined}
716
- * @public
717
- */
718
- resume,
719
-
720
- // focus()
721
- // focus(<containerId>)
722
- // focus(<extSelector>)
723
- /**
724
- * Focuses the specified component ID, container ID, element selector, or the default
725
- * container.
726
- *
727
- * If Spotlight is in pointer mode, focus is not changed but `elem` will be set as the last
728
- * focused element of its spotlight containers.
729
- *
730
- * @param {String|Node} [elem] The spotlight ID or selector for either a spottable
731
- * component or a spotlight container, or spottable node. If not supplied, the default
732
- * container will be focused.
733
- * @returns {Boolean} `true` if focus successful, `false` if not.
734
- * @public
735
- */
736
- focus: function (elem) {
737
- let target = elem;
738
- let wasContainerId = false;
739
-
740
- if (!elem) {
741
- target = getTargetByContainer();
742
- } else if (typeof elem === 'string') {
743
- if (getContainerConfig(elem)) {
744
- target = getTargetByContainer(elem);
745
- wasContainerId = true;
746
- } else if (/^[\w\d-]+$/.test(elem)) {
747
- // support component IDs consisting of alphanumeric, dash, or underscore
748
- target = getTargetBySelector(`[data-spotlight-id=${elem}]`);
749
- } else {
750
- target = getTargetBySelector(elem);
751
- }
752
- } else if (isContainer(elem)) {
753
- target = getTargetByContainer(getContainerId(elem));
754
- }
755
-
756
- const nextContainerIds = getContainersForNode(target);
757
- const nextContainerId = last(nextContainerIds);
758
- if (isNavigable(target, nextContainerId, true)) {
759
- const focused = focusElement(target, nextContainerIds);
760
-
761
- if (!focused && wasContainerId) {
762
- setLastContainer(elem);
763
- }
764
-
765
- return focused;
766
- } else if (wasContainerId) {
767
- // if we failed to find a spottable target within the provided container, we'll set
768
- // it as the active container to allow it to focus itself if its contents change
769
- setLastContainer(elem);
770
- }
771
-
772
- return false;
773
- },
774
-
775
- // move(<direction>)
776
- // move(<direction>, <selector>)
777
- /**
778
- * Moves focus to the next spottable control in the direction specified. Optionally, a source
779
- * element selector may be supplied as the starting point.
780
- *
781
- * @param {String} direction Direction to move, one of `'left'`, `'right'`, `'up'` or `'down'`
782
- * @param {String|undefined} selector If supplied, the element to move from. If not supplied,
783
- * the currently focused item will be used.
784
- * @returns {Boolean} `true` if focus successful, `false` if not.
785
- * @public
786
- */
787
- move: function (direction, selector) {
788
- direction = direction.toLowerCase();
789
- if (direction !== 'up' && direction !== 'down' && direction !== 'left' && direction !== 'right') {
790
- return false;
791
- }
792
-
793
- const elem = selector ? parseSelector(selector)[0] : getCurrent();
794
- if (!elem) {
795
- return false;
796
- }
797
-
798
- const containerIds = getContainersForNode(elem);
799
- if (!containerIds.length) {
800
- return false;
801
- }
802
-
803
- return spotNext(direction, elem, containerIds);
804
- },
805
-
806
- /**
807
- * Sets or clears the default container that will receive focus.
808
- *
809
- * @function
810
- * @param {String} [containerId] The container ID or a falsy value to clear default
811
- * container
812
- * @returns {undefined}
813
- * @public
814
- */
815
- setDefaultContainer,
816
-
817
- /**
818
- * Gets the currently active container.
819
- *
820
- * @returns {String} The id of the currently active container
821
- * @public
822
- */
823
- getActiveContainer: function () {
824
- return getLastContainer() || rootContainerId;
825
- },
826
-
827
- /**
828
- * Sets the currently active container.
829
- *
830
- * Note: If the current container is restricted to 'self-only' and `containerId` is not
831
- * contained within the current container then the active container will not be updated.
832
- *
833
- * @param {String} [containerId] The id of the currently active container. If this is not
834
- * provided, the root container is set as the currently active container.
835
- * @public
836
- */
837
- setActiveContainer: function (containerId) {
838
- if (mayActivateContainer(containerId)) {
839
- setLastContainer(containerId || rootContainerId);
840
- }
841
- },
842
-
843
- /**
844
- * Gets the current pointer mode
845
- *
846
- * @function
847
- * @returns {Boolean} `true` if spotlight is in pointer mode
848
- * @public
849
- */
850
- getPointerMode,
851
-
852
- /**
853
- * Sets the current pointer mode
854
- *
855
- * @function
856
- * @param {Boolean} pointerMode The value of the pointer mode. This determines how
857
- * spotlight manages focus change behaviors.
858
- * @public
859
- */
860
- setPointerMode,
861
-
862
- /**
863
- * Gets the muted mode value of a spottable element.
864
- *
865
- * @param {Object} elem The dom element used to determine the muted status.
866
- * @returns {Boolean} `true` if the passed-in control is in muted mode.
867
- * @public
868
- */
869
- isMuted: function (elem) {
870
- if (!elem) {
871
- return false;
872
- }
873
-
874
- return matchSelector('[data-spotlight-container-muted="true"] .' + spottableClass, elem);
875
- },
876
-
877
- /**
878
- * Determines whether Spotlight is currently paused.
879
- *
880
- * @function
881
- * @returns {Boolean} `true` if Spotlight is currently paused.
882
- * @public
883
- */
884
- isPaused,
885
-
886
- /**
887
- * Determines whether an element is spottable.
888
- *
889
- * @param {Object} elem The dom element used to determine the spottable status.
890
- * @returns {Boolean} `true` if the element being evaluated is currently spottable.
891
- * @public
892
- */
893
- isSpottable: function (elem) {
894
- if (!elem) {
895
- return false;
896
- }
897
-
898
- return isFocusable(elem);
899
- },
900
-
901
- /**
902
- * Returns the currently spotted control.
903
- *
904
- * @returns {Node} The control that currently has focus, if available
905
- * @public
906
- */
907
- getCurrent: function () {
908
- return getCurrent();
909
- },
910
-
911
- /**
912
- * Returns a list of spottable elements wrapped by the supplied container.
913
- *
914
- * @param {String} containerId The id of the container used to determine the list of spottable elements
915
- * @returns {Node[]} The spottable elements that are wrapped by the supplied container
916
- * @public
917
- */
918
- getSpottableDescendants: function (containerId) {
919
- if (!containerId || typeof containerId !== 'string') {
920
- throw new Error('Please assign the "containerId"!');
921
- }
922
-
923
- return getSpottableDescendants(containerId);
924
- }
925
- };
926
-
927
- return exports;
928
-
929
- })();
930
-
931
- export default Spotlight;
932
- export {
933
- getDirection,
934
- Spotlight
935
- };
92
+
93
+ var Spotlight = function () {
94
+ 'use strict';
95
+ /*
96
+ /* private vars
97
+ */
98
+
99
+ var _initialized = false;
100
+ var _duringFocusChange = false;
101
+ /*
102
+ * Whether a 5-way directional key is being held.
103
+ *
104
+ * @type {Boolean}
105
+ * @default false
106
+ */
107
+
108
+ var _5WayKeyHold = false;
109
+ /*
110
+ * Whether to set focus during the next window focus event
111
+ *
112
+ * @type {Boolean}
113
+ * @default false
114
+ */
115
+
116
+ var _spotOnWindowFocus = false;
117
+ /*
118
+ * `true` when a pointer move event occurs during a keypress. Used to short circuit key down
119
+ * handling until the next keyup occurs.
120
+ *
121
+ * @type {Boolean}
122
+ * @default false
123
+ */
124
+
125
+ var _pointerMoveDuringKeyPress = false;
126
+ /*
127
+ * protected methods
128
+ */
129
+
130
+ function preventDefault(evt) {
131
+ evt.preventDefault();
132
+ evt.stopPropagation();
133
+ return false;
134
+ }
135
+
136
+ function shouldPreventNavigation() {
137
+ return (0, _Pause.isPaused)() || (0, _container.getAllContainerIds)().length === 0;
138
+ }
139
+
140
+ function _getCurrent() {
141
+ if (!(0, _snapshot.isWindowReady)()) return;
142
+ var activeElement = document.activeElement;
143
+
144
+ if (activeElement && activeElement !== document.body) {
145
+ return activeElement;
146
+ }
147
+ } // An extension point for updating pointer mode based on the current platform.
148
+ // Currently only webOS
149
+
150
+
151
+ function setPlatformPointerMode() {
152
+ var palmSystem = window.PalmSystem;
153
+
154
+ if (palmSystem && palmSystem.cursor) {
155
+ (0, _pointer.setPointerMode)(palmSystem.cursor.visibility);
156
+ }
157
+ }
158
+
159
+ function focusElement(elem, containerIds, fromPointer) {
160
+ if (!elem) {
161
+ return false;
162
+ }
163
+
164
+ if ((0, _pointer.getPointerMode)() && !fromPointer) {
165
+ (0, _container.setContainerLastFocusedElement)(elem, containerIds);
166
+ return false;
167
+ }
168
+
169
+ var currentFocusedElement = _getCurrent();
170
+
171
+ if (elem === currentFocusedElement) {
172
+ return true;
173
+ }
174
+
175
+ var focusOptions = (0, _container.isWithinOverflowContainer)(elem, containerIds) ? {
176
+ preventScroll: true
177
+ } : null;
178
+
179
+ var silentFocus = function silentFocus() {
180
+ elem.focus(focusOptions);
181
+ focusChanged(elem, containerIds);
182
+ };
183
+
184
+ if (_duringFocusChange) {
185
+ silentFocus();
186
+ return true;
187
+ }
188
+
189
+ _duringFocusChange = true;
190
+
191
+ if ((0, _Pause.isPaused)()) {
192
+ silentFocus();
193
+ _duringFocusChange = false;
194
+ return true;
195
+ }
196
+
197
+ elem.focus(focusOptions);
198
+ _duringFocusChange = false;
199
+ focusChanged(elem, containerIds);
200
+ return true;
201
+ }
202
+
203
+ function focusChanged(elem, containerIds) {
204
+ if (!containerIds || !containerIds.length) {
205
+ containerIds = (0, _container.getContainersForNode)(elem);
206
+ }
207
+
208
+ var containerId = (0, _last["default"])(containerIds);
209
+
210
+ if (containerId) {
211
+ (0, _container.setContainerLastFocusedElement)(elem, containerIds);
212
+ (0, _container.setLastContainer)(containerId);
213
+ }
214
+
215
+ if (process.env.NODE_ENV !== "production") {
216
+ assignFocusPreview(elem);
217
+ }
218
+ }
219
+
220
+ function restoreFocus() {
221
+ var lastContainerId = (0, _container.getLastContainer)();
222
+ var next;
223
+
224
+ if (lastContainerId) {
225
+ var position = (0, _pointer.getLastPointerPosition)(); // walk up the chain of containers from the last to attempt to find a target
226
+
227
+ next = (0, _container.getContainersForNode)((0, _container.getContainerNode)(lastContainerId)).reverse(); // only prepend last focused if it exists so that Spotlight.focus() doesn't receive
228
+ // a falsy target
229
+
230
+ var lastFocusedElement = (0, _container.getContainerLastFocusedElement)(lastContainerId);
231
+
232
+ while ((0, _container.isContainer)(lastFocusedElement)) {
233
+ var _getContainerConfig = (0, _container.getContainerConfig)(lastFocusedElement);
234
+
235
+ lastFocusedElement = _getContainerConfig.lastFocusedElement;
236
+ }
237
+
238
+ var lastContainerNode = (0, _container.getContainerNode)(lastContainerId);
239
+
240
+ if (!lastFocusedElement || lastContainerNode.getBoundingClientRect && lastFocusedElement.getBoundingClientRect && !(0, _utils.contains)(lastContainerNode.getBoundingClientRect(), lastFocusedElement.getBoundingClientRect())) {
241
+ lastFocusedElement = (0, _container.getContainerConfig)(lastContainerId).overflow && (0, _target.getNearestTargetFromPosition)(position, lastContainerId);
242
+ }
243
+
244
+ if (lastFocusedElement) {
245
+ next.unshift(lastFocusedElement);
246
+ }
247
+ } else {
248
+ next = [_container.rootContainerId];
249
+ } // attempt to find a target starting with the last focused element in the last
250
+ // container, followed by the last container, and finally the root container
251
+
252
+
253
+ return next.reduce(function (focused, target) {
254
+ return focused || Spotlight.focus(target);
255
+ }, false);
256
+ } // The below should be gated on non-production environment only.
257
+
258
+
259
+ function assignFocusPreview(elem) {
260
+ var directions = ['up', 'right', 'down', 'left'],
261
+ nextClassBase = _Spottable.spottableClass + '-next-'; // Remove all previous targets
262
+
263
+ directions.forEach(function (dir) {
264
+ var nextClass = nextClassBase + dir,
265
+ prevElems = (0, _utils.parseSelector)('.' + nextClass);
266
+
267
+ if (prevElems && prevElems.length !== 0) {
268
+ prevElems.forEach(function (prevElem) {
269
+ return prevElem.classList.remove(nextClass);
270
+ });
271
+ }
272
+ }); // Find all next targets and identify them
273
+
274
+ directions.forEach(function (dir) {
275
+ var nextElem = (0, _target.getTargetByDirectionFromElement)(dir, elem);
276
+
277
+ if (nextElem) {
278
+ nextElem.classList.add(nextClassBase + dir);
279
+ }
280
+ });
281
+ }
282
+
283
+ function spotNextFromPoint(direction, position) {
284
+ var containerId = Spotlight.getActiveContainer();
285
+ var next = (0, _target.getTargetByDirectionFromPosition)(direction, position, containerId);
286
+
287
+ if (next) {
288
+ (0, _container.setContainerPreviousTarget)(containerId, direction, next, (0, _container.getContainerLastFocusedElement)(containerId));
289
+ return focusElement(next, (0, _container.getContainersForNode)(next));
290
+ }
291
+
292
+ return false;
293
+ }
294
+
295
+ function spotNext(direction, currentFocusedElement, currentContainerIds) {
296
+ var next = (0, _target.getTargetByDirectionFromElement)(direction, currentFocusedElement);
297
+
298
+ if (next) {
299
+ var currentContainerId = (0, _last["default"])(currentContainerIds);
300
+ var nextContainerIds = (0, _container.getContainersForNode)(next); // prevent focus if 5-way is being held and the next element isn't wrapped by
301
+ // the current element's immediate container
302
+
303
+ if (_5WayKeyHold && nextContainerIds.indexOf(currentContainerId) < 0 && !(0, _container.isContainer5WayHoldable)(currentContainerId)) {
304
+ return false;
305
+ }
306
+
307
+ (0, _container.notifyLeaveContainer)(direction, currentFocusedElement, currentContainerIds, next, nextContainerIds);
308
+ (0, _container.setContainerPreviousTarget)(currentContainerId, direction, next, currentFocusedElement);
309
+ var focused = focusElement(next, nextContainerIds);
310
+ (0, _container.notifyEnterContainer)(direction, currentFocusedElement, currentContainerIds, next, nextContainerIds);
311
+ return focused;
312
+ }
313
+
314
+ (0, _container.notifyLeaveContainerFail)(direction, currentFocusedElement, currentContainerIds);
315
+ return false;
316
+ }
317
+
318
+ function onAcceleratedKeyDown(evt) {
319
+ var direction = getDirection(evt.keyCode);
320
+ if (!direction) return;
321
+
322
+ var currentFocusedElement = _getCurrent();
323
+
324
+ var currentContainerIds = (0, _container.getContainersForNode)(currentFocusedElement);
325
+ spotNext(direction, currentFocusedElement, currentContainerIds);
326
+ }
327
+
328
+ function onBlur() {
329
+ var current = _getCurrent();
330
+
331
+ if (current) {
332
+ current.blur();
333
+ }
334
+
335
+ Spotlight.setPointerMode(false);
336
+ _spotOnWindowFocus = true;
337
+ _pointerMoveDuringKeyPress = false;
338
+ }
339
+
340
+ function handleWebOSMouseEvent(ev) {
341
+ if (!(0, _Pause.isPaused)() && ev && ev.detail && ev.detail.type === 'Leave') {
342
+ onBlur();
343
+ }
344
+ }
345
+
346
+ function handleKeyboardStateChangeEvent(_ref) {
347
+ var visibility = _ref.visibility;
348
+
349
+ if (!visibility) {
350
+ setPlatformPointerMode();
351
+ }
352
+ }
353
+
354
+ function onFocus() {
355
+ // Normally, there isn't focus here unless the window has been blurred above. On webOS, the
356
+ // platform may focus the window after the app has already focused a component so we prevent
357
+ // trying to focus something else (potentially) unless the window was previously blurred
358
+ if (_spotOnWindowFocus) {
359
+ setPlatformPointerMode(); // If the window was previously blurred while in pointer mode, the last active containerId may
360
+ // not have yet set focus to its spottable elements. For this reason we can't rely on setting focus
361
+ // to the last focused element of the last active containerId, so we use rootContainerId instead
362
+
363
+ var lastFocusedElement = (0, _container.getContainerLastFocusedElement)(_container.rootContainerId);
364
+
365
+ while ((0, _container.isContainer)(lastFocusedElement)) {
366
+ var _getContainerConfig2 = (0, _container.getContainerConfig)(lastFocusedElement);
367
+
368
+ lastFocusedElement = _getContainerConfig2.lastFocusedElement;
369
+ }
370
+
371
+ if (!Spotlight.focus(lastFocusedElement)) {
372
+ // If the last focused element was previously also disabled (or no longer exists), we
373
+ // need to set focus somewhere
374
+ Spotlight.focus();
375
+ }
376
+
377
+ _spotOnWindowFocus = false;
378
+ }
379
+ }
380
+
381
+ function onKeyUp(evt) {
382
+ _pointerMoveDuringKeyPress = false;
383
+ var keyCode = evt.keyCode;
384
+
385
+ if (getDirection(keyCode) || isEnter(keyCode)) {
386
+ SpotlightAccelerator.reset();
387
+ _5WayKeyHold = false;
388
+ }
389
+ }
390
+
391
+ function handlePointerHide() {
392
+ if (!_getCurrent()) {
393
+ restoreFocus();
394
+ }
395
+ }
396
+
397
+ function onKeyDown(evt) {
398
+ if (shouldPreventNavigation()) {
399
+ (0, _pointer.notifyKeyDown)(evt.keyCode);
400
+ return;
401
+ }
402
+
403
+ var keyCode = evt.keyCode;
404
+ var direction = getDirection(keyCode);
405
+ var pointerHandled = (0, _pointer.notifyKeyDown)(keyCode, handlePointerHide);
406
+
407
+ if (pointerHandled || !(direction || isEnter(keyCode))) {
408
+ return;
409
+ }
410
+
411
+ if (!(0, _Pause.isPaused)() && !_pointerMoveDuringKeyPress) {
412
+ if (_getCurrent()) {
413
+ SpotlightAccelerator.processKey(evt, onAcceleratedKeyDown);
414
+ } else if (!spotNextFromPoint(direction, (0, _pointer.getLastPointerPosition)())) {
415
+ restoreFocus();
416
+ }
417
+
418
+ _5WayKeyHold = true;
419
+ }
420
+
421
+ if (direction) {
422
+ preventDefault(evt);
423
+ }
424
+ }
425
+
426
+ function onMouseMove(_ref2) {
427
+ var target = _ref2.target,
428
+ clientX = _ref2.clientX,
429
+ clientY = _ref2.clientY;
430
+
431
+ if (shouldPreventNavigation()) {
432
+ (0, _pointer.notifyPointerMove)(null, target, clientX, clientY);
433
+ return;
434
+ }
435
+
436
+ var current = _getCurrent();
437
+
438
+ var update = (0, _pointer.notifyPointerMove)(current, target, clientX, clientY);
439
+
440
+ if (update) {
441
+ if (_5WayKeyHold) {
442
+ _pointerMoveDuringKeyPress = true;
443
+ }
444
+
445
+ var next = (0, _target.getNavigableTarget)(target); // TODO: Consider encapsulating this work within focusElement
446
+
447
+ if (next !== current) {
448
+ if (next) {
449
+ focusElement(next, (0, _container.getContainersForNode)(next), true);
450
+ return true;
451
+ } else if (current) {
452
+ current.blur();
453
+ (0, _container.setLastContainerFromTarget)(current, target);
454
+ }
455
+ }
456
+ }
457
+ }
458
+
459
+ function onMouseOver(evt) {
460
+ if (shouldPreventNavigation()) return;
461
+ var target = evt.target;
462
+
463
+ if ((0, _pointer.getPointerMode)() && (0, _pointer.hasPointerMoved)(evt.clientX, evt.clientY)) {
464
+ var next = (0, _target.getNavigableTarget)(target); // account for child controls
465
+
466
+ if (next && next !== _getCurrent()) {
467
+ focusElement(next, (0, _container.getContainersForNode)(next), true);
468
+ return true;
469
+ }
470
+
471
+ preventDefault(evt);
472
+ }
473
+ }
474
+
475
+ function onTouchEnd(evt) {
476
+ var current = _getCurrent();
477
+
478
+ if (current && !current.contains(evt.target)) {
479
+ current.blur();
480
+ }
481
+ }
482
+ /*
483
+ * public methods
484
+ */
485
+
486
+
487
+ var exports =
488
+ /** @lends spotlight.Spotlight */
489
+ {
490
+ // eslint-disable-line no-shadow
491
+
492
+ /**
493
+ * Initializes Spotlight. This is generally handled by
494
+ * {@link spotlight/SpotlightRootDecorator.SpotlightRootDecorator}.
495
+ *
496
+ * @param {Object} containerDefaults Default configuration for new spotlight containers
497
+ * @returns {undefined}
498
+ * @public
499
+ */
500
+ initialize: function initialize(containerDefaults) {
501
+ if (!_initialized) {
502
+ window.addEventListener('blur', onBlur);
503
+ window.addEventListener('focus', onFocus);
504
+ window.addEventListener('keydown', onKeyDown);
505
+ window.addEventListener('keyup', onKeyUp);
506
+ window.addEventListener('mouseover', onMouseOver);
507
+ window.addEventListener('mousemove', onMouseMove);
508
+
509
+ if (_platform["default"].touch) {
510
+ window.addEventListener('touchend', onTouchEnd);
511
+ }
512
+
513
+ if (_platform["default"].webos) {
514
+ window.top.document.addEventListener('webOSMouse', handleWebOSMouseEvent);
515
+ window.top.document.addEventListener('keyboardStateChange', handleKeyboardStateChangeEvent);
516
+ }
517
+
518
+ (0, _container.setLastContainer)(_container.rootContainerId);
519
+ (0, _container.configureDefaults)(containerDefaults);
520
+ (0, _container.configureContainer)(_container.rootContainerId); // by default, pointer mode is off but the platform's current state will override that
521
+
522
+ (0, _pointer.setPointerMode)(false);
523
+ setPlatformPointerMode();
524
+ _initialized = true;
525
+ }
526
+ },
527
+
528
+ /**
529
+ * Terminates Spotlight. This is generally handled by {@link spotlight.SpotlightRootDecorator}.
530
+ *
531
+ * @public
532
+ */
533
+ terminate: function terminate() {
534
+ window.removeEventListener('blur', onBlur);
535
+ window.removeEventListener('focus', onFocus);
536
+ window.removeEventListener('keydown', onKeyDown);
537
+ window.removeEventListener('keyup', onKeyUp);
538
+ window.removeEventListener('mouseover', onMouseOver);
539
+ window.removeEventListener('mousemove', onMouseMove);
540
+
541
+ if (_platform["default"].touch) {
542
+ window.removeEventListener('touchend', onTouchEnd);
543
+ }
544
+
545
+ if (_platform["default"].webos) {
546
+ window.top.document.removeEventListener('webOSMouse', handleWebOSMouseEvent);
547
+ window.top.document.removeEventListener('keyboardStateChange', handleKeyboardStateChangeEvent);
548
+ }
549
+
550
+ Spotlight.clear();
551
+ _initialized = false;
552
+ },
553
+
554
+ /**
555
+ * Resets spotlight container information
556
+ *
557
+ * @private
558
+ */
559
+ clear: function clear() {
560
+ (0, _container.removeAllContainers)();
561
+ (0, _container.setDefaultContainer)();
562
+ (0, _container.setLastContainer)();
563
+ _duringFocusChange = false;
564
+ },
565
+ // set(<config>);
566
+ // set(<containerId>, <config>);
567
+
568
+ /**
569
+ * Sets the config for spotlight or the specified containerID
570
+ *
571
+ * @function
572
+ * @param {String|Object} containerIdOrConfig Configuration object or container ID
573
+ * @param {Object} [config] Configuration object if container ID supplied
574
+ * in `containerIdOrConfig`
575
+ * @returns {undefined}
576
+ * @public
577
+ */
578
+ set: _container.configureContainer,
579
+ // add(<config>);
580
+ // add(<containerId>, <config>);
581
+
582
+ /**
583
+ * Adds the config for a new container. The container ID may be passed in the configuration
584
+ * object. If no container ID is supplied, a new container ID will be generated.
585
+ *
586
+ * @function
587
+ * @param {String|Object} containerIdOrConfig Configuration object or container ID
588
+ * @param {Object} [config] Configuration object if container ID supplied
589
+ * in `containerIdOrConfig`
590
+ * @returns {String} The container ID of the container
591
+ * @public
592
+ */
593
+ add: _container.addContainer,
594
+ unmount: function unmount(containerId) {
595
+ if (!containerId || typeof containerId !== 'string') {
596
+ throw new Error('Please assign the "containerId"!');
597
+ }
598
+
599
+ (0, _container.unmountContainer)(containerId);
600
+ },
601
+
602
+ /**
603
+ * Removes a container from Spotlight
604
+ *
605
+ * @param {String} containerId Container ID to remove
606
+ * @returns {Boolean} `true` if container removed, `false` if container does not exist
607
+ * @public
608
+ */
609
+ remove: function remove(containerId) {
610
+ if (!containerId || typeof containerId !== 'string') {
611
+ throw new Error('Please assign the "containerId"!');
612
+ }
613
+
614
+ if ((0, _container.getContainerConfig)(containerId)) {
615
+ (0, _container.removeContainer)(containerId);
616
+
617
+ if ((0, _container.getLastContainer)() === containerId) {
618
+ Spotlight.setActiveContainer(null);
619
+ }
620
+
621
+ return true;
622
+ }
623
+
624
+ return false;
625
+ },
626
+
627
+ /**
628
+ * Disables the selector rules of the specified container
629
+ *
630
+ * @param {String} containerId Container ID selector rules to disable
631
+ * @returns {Boolean} `true` if container's selector rules are disabled, `false` if container does not exist
632
+ * @public
633
+ */
634
+ disableSelector: function disableSelector(containerId) {
635
+ if ((0, _container.isContainer)(containerId)) {
636
+ (0, _container.configureContainer)(containerId, {
637
+ selectorDisabled: false
638
+ });
639
+ return true;
640
+ }
641
+
642
+ return false;
643
+ },
644
+
645
+ /**
646
+ * Enables the selector rules of the specified container
647
+ *
648
+ * @param {String} containerId Container ID selector rules to enable
649
+ * @returns {Boolean} `true` if container's selector rules are enabled, `false` if container does not exist
650
+ * @public
651
+ */
652
+ enableSelector: function enableSelector(containerId) {
653
+ if ((0, _container.isContainer)(containerId)) {
654
+ (0, _container.configureContainer)(containerId, {
655
+ selectorDisabled: false
656
+ });
657
+ return true;
658
+ }
659
+
660
+ return false;
661
+ },
662
+
663
+ /**
664
+ * Pauses Spotlight
665
+ *
666
+ * @function
667
+ * @returns {undefined}
668
+ * @public
669
+ */
670
+ pause: _Pause.pause,
671
+
672
+ /**
673
+ * Resumes Spotlight
674
+ *
675
+ * @function
676
+ * @returns {undefined}
677
+ * @public
678
+ */
679
+ resume: _Pause.resume,
680
+ // focus()
681
+ // focus(<containerId>)
682
+ // focus(<extSelector>)
683
+
684
+ /**
685
+ * Focuses the specified component ID, container ID, element selector, or the default
686
+ * container.
687
+ *
688
+ * If Spotlight is in pointer mode, focus is not changed but `elem` will be set as the last
689
+ * focused element of its spotlight containers.
690
+ *
691
+ * @param {String|Node} [elem] The spotlight ID or selector for either a spottable
692
+ * component or a spotlight container, or spottable node. If not supplied, the default
693
+ * container will be focused.
694
+ * @returns {Boolean} `true` if focus successful, `false` if not.
695
+ * @public
696
+ */
697
+ focus: function focus(elem) {
698
+ var target = elem;
699
+ var wasContainerId = false;
700
+
701
+ if (!elem) {
702
+ target = (0, _target.getTargetByContainer)();
703
+ } else if (typeof elem === 'string') {
704
+ if ((0, _container.getContainerConfig)(elem)) {
705
+ target = (0, _target.getTargetByContainer)(elem);
706
+ wasContainerId = true;
707
+ } else if (/^[\w\d-]+$/.test(elem)) {
708
+ // support component IDs consisting of alphanumeric, dash, or underscore
709
+ target = (0, _target.getTargetBySelector)("[data-spotlight-id=".concat(elem, "]"));
710
+ } else {
711
+ target = (0, _target.getTargetBySelector)(elem);
712
+ }
713
+ } else if ((0, _container.isContainer)(elem)) {
714
+ target = (0, _target.getTargetByContainer)((0, _container.getContainerId)(elem));
715
+ }
716
+
717
+ var nextContainerIds = (0, _container.getContainersForNode)(target);
718
+ var nextContainerId = (0, _last["default"])(nextContainerIds);
719
+
720
+ if ((0, _container.isNavigable)(target, nextContainerId, true)) {
721
+ var focused = focusElement(target, nextContainerIds);
722
+
723
+ if (!focused && wasContainerId) {
724
+ (0, _container.setLastContainer)(elem);
725
+ }
726
+
727
+ return focused;
728
+ } else if (wasContainerId) {
729
+ // if we failed to find a spottable target within the provided container, we'll set
730
+ // it as the active container to allow it to focus itself if its contents change
731
+ (0, _container.setLastContainer)(elem);
732
+ }
733
+
734
+ return false;
735
+ },
736
+ // move(<direction>)
737
+ // move(<direction>, <selector>)
738
+
739
+ /**
740
+ * Moves focus to the next spottable control in the direction specified. Optionally, a source
741
+ * element selector may be supplied as the starting point.
742
+ *
743
+ * @param {String} direction Direction to move, one of `'left'`, `'right'`, `'up'` or `'down'`
744
+ * @param {String|undefined} selector If supplied, the element to move from. If not supplied,
745
+ * the currently focused item will be used.
746
+ * @returns {Boolean} `true` if focus successful, `false` if not.
747
+ * @public
748
+ */
749
+ move: function move(direction, selector) {
750
+ direction = direction.toLowerCase();
751
+
752
+ if (direction !== 'up' && direction !== 'down' && direction !== 'left' && direction !== 'right') {
753
+ return false;
754
+ }
755
+
756
+ var elem = selector ? (0, _utils.parseSelector)(selector)[0] : _getCurrent();
757
+
758
+ if (!elem) {
759
+ return false;
760
+ }
761
+
762
+ var containerIds = (0, _container.getContainersForNode)(elem);
763
+
764
+ if (!containerIds.length) {
765
+ return false;
766
+ }
767
+
768
+ return spotNext(direction, elem, containerIds);
769
+ },
770
+
771
+ /**
772
+ * Sets or clears the default container that will receive focus.
773
+ *
774
+ * @function
775
+ * @param {String} [containerId] The container ID or a falsy value to clear default
776
+ * container
777
+ * @returns {undefined}
778
+ * @public
779
+ */
780
+ setDefaultContainer: _container.setDefaultContainer,
781
+
782
+ /**
783
+ * Gets the currently active container.
784
+ *
785
+ * @returns {String} The id of the currently active container
786
+ * @public
787
+ */
788
+ getActiveContainer: function getActiveContainer() {
789
+ return (0, _container.getLastContainer)() || _container.rootContainerId;
790
+ },
791
+
792
+ /**
793
+ * Sets the currently active container.
794
+ *
795
+ * Note: If the current container is restricted to 'self-only' and `containerId` is not
796
+ * contained within the current container then the active container will not be updated.
797
+ *
798
+ * @param {String} [containerId] The id of the currently active container. If this is not
799
+ * provided, the root container is set as the currently active container.
800
+ * @public
801
+ */
802
+ setActiveContainer: function setActiveContainer(containerId) {
803
+ if ((0, _container.mayActivateContainer)(containerId)) {
804
+ (0, _container.setLastContainer)(containerId || _container.rootContainerId);
805
+ }
806
+ },
807
+
808
+ /**
809
+ * Gets the current pointer mode
810
+ *
811
+ * @function
812
+ * @returns {Boolean} `true` if spotlight is in pointer mode
813
+ * @public
814
+ */
815
+ getPointerMode: _pointer.getPointerMode,
816
+
817
+ /**
818
+ * Sets the current pointer mode
819
+ *
820
+ * @function
821
+ * @param {Boolean} pointerMode The value of the pointer mode. This determines how
822
+ * spotlight manages focus change behaviors.
823
+ * @public
824
+ */
825
+ setPointerMode: _pointer.setPointerMode,
826
+
827
+ /**
828
+ * Gets the muted mode value of a spottable element.
829
+ *
830
+ * @param {Object} elem The dom element used to determine the muted status.
831
+ * @returns {Boolean} `true` if the passed-in control is in muted mode.
832
+ * @public
833
+ */
834
+ isMuted: function isMuted(elem) {
835
+ if (!elem) {
836
+ return false;
837
+ }
838
+
839
+ return (0, _utils.matchSelector)('[data-spotlight-container-muted="true"] .' + _Spottable.spottableClass, elem);
840
+ },
841
+
842
+ /**
843
+ * Determines whether Spotlight is currently paused.
844
+ *
845
+ * @function
846
+ * @returns {Boolean} `true` if Spotlight is currently paused.
847
+ * @public
848
+ */
849
+ isPaused: _Pause.isPaused,
850
+
851
+ /**
852
+ * Determines whether an element is spottable.
853
+ *
854
+ * @param {Object} elem The dom element used to determine the spottable status.
855
+ * @returns {Boolean} `true` if the element being evaluated is currently spottable.
856
+ * @public
857
+ */
858
+ isSpottable: function isSpottable(elem) {
859
+ if (!elem) {
860
+ return false;
861
+ }
862
+
863
+ return (0, _target.isFocusable)(elem);
864
+ },
865
+
866
+ /**
867
+ * Returns the currently spotted control.
868
+ *
869
+ * @returns {Node} The control that currently has focus, if available
870
+ * @public
871
+ */
872
+ getCurrent: function getCurrent() {
873
+ return _getCurrent();
874
+ },
875
+
876
+ /**
877
+ * Returns a list of spottable elements wrapped by the supplied container.
878
+ *
879
+ * @param {String} containerId The id of the container used to determine the list of spottable elements
880
+ * @returns {Node[]} The spottable elements that are wrapped by the supplied container
881
+ * @public
882
+ */
883
+ getSpottableDescendants: function getSpottableDescendants(containerId) {
884
+ if (!containerId || typeof containerId !== 'string') {
885
+ throw new Error('Please assign the "containerId"!');
886
+ }
887
+
888
+ return (0, _container.getSpottableDescendants)(containerId);
889
+ }
890
+ };
891
+ return exports;
892
+ }();
893
+
894
+ exports.Spotlight = Spotlight;
895
+ var _default = Spotlight;
896
+ exports["default"] = _default;