@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
@@ -1,896 +0,0 @@
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
-
32
- /*
33
- * A javascript-based implementation of Spatial Navigation.
34
- *
35
- * Copyright (c) 2016 Luke Chang.
36
- * https://github.com/luke-chang/js-spatial-navigation
37
- *
38
- * Licensed under the MPL license.
39
- */
40
-
41
- /**
42
- * Exports the {@link spotlight.Spotlight} object used for controlling spotlight behavior and the
43
- * {@link spotlight.Spotlight.getDirection} function for mapping a keycode to a spotlight direction.
44
- *
45
- * The default export is {@link spotlight.Spotlight}.
46
- *
47
- * @module spotlight
48
- * @exports default Spotlight
49
- * @exports getDirection
50
- */
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');
56
- /**
57
- * Translates keyCodes into 5-way direction descriptions (e.g. `'down'`)
58
- *
59
- * @function
60
- * @memberof spotlight
61
- * @param {Number} keyCode - Key code to analyze
62
- * @returns {String|false} - One of `'up'`, `'down'`, `'left'`, `'right'` or `false` if not a direction key
63
- * @public
64
- */
65
-
66
- var getDirection = function getDirection(keyCode) {
67
- return isDown(keyCode) && 'down' || isLeft(keyCode) && 'left' || isRight(keyCode) && 'right' || isUp(keyCode) && 'up';
68
- };
69
-
70
- exports.getDirection = getDirection;
71
- var SpotlightAccelerator = new _Accelerator["default"]();
72
- /**
73
- * Provides 5-way navigation and focus support
74
- *
75
- * ```
76
- * import Spotlight from '@enact/Spotlight';
77
- *
78
- * // get the currently focused component
79
- * const current = Spotlight.getCurrent();
80
- *
81
- * // focus an element by CSS selector
82
- * Spotlight.focus('.my-custom-class');
83
- *
84
- * // is `current` focusable?
85
- * const isFocused = Spotlight.isSpottable(current);
86
- * ```
87
- *
88
- * @type {Object}
89
- * @memberof spotlight
90
- * @public
91
- */
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;