@enact/spotlight 4.5.2 → 4.5.4
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.
- package/Accelerator/Accelerator.js +11 -31
- package/Accelerator/tests/Accelerrator-specs.js +62 -0
- package/CHANGELOG.md +10 -0
- package/Pause/Pause.js +8 -18
- package/Pause/tests/Pause-specs.js +6 -3
- package/SpotlightContainerDecorator/SpotlightContainer.js +19 -45
- package/SpotlightContainerDecorator/SpotlightContainerDecorator.js +14 -58
- package/SpotlightContainerDecorator/tests/Element.prototype.closest.js +5 -10
- package/SpotlightContainerDecorator/tests/SpotlightContainerDecorator-specs.js +17 -47
- package/SpotlightContainerDecorator/tests/useSpotlightContainer-specs.js +16 -53
- package/SpotlightContainerDecorator/useSpotlightContainer.js +4 -15
- package/SpotlightRootDecorator/SpotlightRootDecorator.js +4 -58
- package/SpotlightRootDecorator/tests/SpotlightRootDecorator-specs.js +38 -0
- package/Spottable/Spottable.js +25 -84
- package/Spottable/SpottableCore.js +32 -71
- package/Spottable/tests/Spottable-specs.js +8 -31
- package/Spottable/tests/useSpot-specs.js +27 -105
- package/Spottable/useSpottable.js +7 -21
- package/package.json +8 -2
- package/src/container.js +94 -219
- package/src/navigate.js +5 -74
- package/src/pointer.js +16 -31
- package/src/spotlight.js +42 -171
- package/src/target.js +61 -139
- package/src/tests/container-specs.js +5 -11
- package/src/tests/navigate-specs.js +2 -10
- package/src/tests/pointer-specs.js +1 -3
- package/src/tests/spotlight-specs.js +42 -24
- package/src/tests/target-specs.js +23 -47
- package/src/tests/utils-specs.js +54 -0
- package/src/tests/utils.js +2 -27
- package/src/utils.js +18 -37
|
@@ -1,23 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _container4 = require("../container");
|
|
4
|
-
|
|
5
4
|
var _spotlight = _interopRequireDefault(require("../spotlight"));
|
|
6
|
-
|
|
7
5
|
var _utils = require("./utils");
|
|
8
|
-
|
|
9
6
|
var _container, _container2, _container3;
|
|
10
|
-
|
|
11
7
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
12
|
-
|
|
13
8
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
14
|
-
|
|
15
9
|
var nonSpottable = function nonSpottable() {
|
|
16
10
|
return (0, _utils.node)({
|
|
17
11
|
className: 'other'
|
|
18
12
|
});
|
|
19
13
|
};
|
|
20
|
-
|
|
21
14
|
var scenarios = {
|
|
22
15
|
complexTree: (0, _utils.join)((0, _utils.spottable)({
|
|
23
16
|
'data-spotlight-id': 's1'
|
|
@@ -27,22 +20,18 @@ var scenarios = {
|
|
|
27
20
|
id: 'child-of-third'
|
|
28
21
|
}))), _container)))), _container2)))), _container3)))
|
|
29
22
|
};
|
|
30
|
-
|
|
31
23
|
var setupContainers = function setupContainers() {
|
|
32
24
|
_spotlight["default"].setPointerMode(false);
|
|
33
|
-
|
|
34
25
|
(0, _container4.configureDefaults)({
|
|
35
26
|
selector: '.spottable'
|
|
36
27
|
});
|
|
37
28
|
(0, _container4.configureContainer)(_container4.rootContainerId);
|
|
38
29
|
(0, _container4.setLastContainer)(_container4.rootContainerId);
|
|
39
30
|
};
|
|
40
|
-
|
|
41
31
|
var teardownContainers = function teardownContainers() {
|
|
42
32
|
// clean up any containers we create for safe tests
|
|
43
33
|
(0, _container4.removeAllContainers)();
|
|
44
34
|
};
|
|
45
|
-
|
|
46
35
|
var mockFocus = function mockFocus(n) {
|
|
47
36
|
var fn = jest.fn().mockImplementation(function () {
|
|
48
37
|
return true;
|
|
@@ -54,59 +43,88 @@ var mockFocus = function mockFocus(n) {
|
|
|
54
43
|
});
|
|
55
44
|
return fn;
|
|
56
45
|
};
|
|
57
|
-
|
|
58
46
|
describe('Spotlight', function () {
|
|
59
47
|
describe('#focus', function () {
|
|
60
48
|
beforeEach(setupContainers);
|
|
61
49
|
afterEach(teardownContainers);
|
|
62
50
|
test('should focus spottable by id', (0, _utils.testScenario)(scenarios.complexTree, function (root) {
|
|
63
51
|
var fn = mockFocus(root.querySelector('[data-spotlight-id="s1"]'));
|
|
64
|
-
|
|
65
52
|
_spotlight["default"].focus('s1');
|
|
66
|
-
|
|
67
53
|
expect(fn).toBeCalled();
|
|
68
54
|
}));
|
|
69
55
|
test('should focus container by id', (0, _utils.testScenario)(scenarios.complexTree, function (root) {
|
|
70
56
|
(0, _container4.configureContainer)('first-container');
|
|
71
57
|
var fn = mockFocus(root.querySelector('[data-spotlight-id="first-container"] > .spottable'));
|
|
72
|
-
|
|
73
58
|
_spotlight["default"].focus('first-container');
|
|
74
|
-
|
|
75
59
|
expect(fn).toBeCalled();
|
|
76
60
|
}));
|
|
77
61
|
test('should focus spottable by node', (0, _utils.testScenario)(scenarios.complexTree, function (root) {
|
|
78
62
|
var n = root.querySelector('[data-spotlight-id="s1"]');
|
|
79
63
|
var fn = mockFocus(n);
|
|
80
|
-
|
|
81
64
|
_spotlight["default"].focus(n);
|
|
82
|
-
|
|
83
65
|
expect(fn).toBeCalled();
|
|
84
66
|
}));
|
|
85
67
|
test('should focus container by node', (0, _utils.testScenario)(scenarios.complexTree, function (root) {
|
|
86
68
|
(0, _container4.configureContainer)('first-container');
|
|
87
69
|
var n = root.querySelector('[data-spotlight-id="first-container"]');
|
|
88
70
|
var fn = mockFocus(n.querySelector('.spottable'));
|
|
89
|
-
|
|
90
71
|
_spotlight["default"].focus(n);
|
|
91
|
-
|
|
92
72
|
expect(fn).toBeCalled();
|
|
93
73
|
}));
|
|
94
74
|
test('should focus spottable by selector', (0, _utils.testScenario)(scenarios.complexTree, function (root) {
|
|
95
75
|
var n = root.querySelector('[data-spotlight-id="s1"]');
|
|
96
76
|
var fn = mockFocus(n);
|
|
97
|
-
|
|
98
77
|
_spotlight["default"].focus('[data-spotlight-id="s1"]');
|
|
99
|
-
|
|
100
78
|
expect(fn).toBeCalled();
|
|
101
79
|
}));
|
|
102
80
|
test('should focus container by selector', (0, _utils.testScenario)(scenarios.complexTree, function (root) {
|
|
103
81
|
(0, _container4.configureContainer)('first-container');
|
|
104
82
|
var n = root.querySelector('[data-spotlight-id="first-container"]');
|
|
105
83
|
var fn = mockFocus(n.querySelector('.spottable'));
|
|
106
|
-
|
|
107
84
|
_spotlight["default"].focus('[data-spotlight-id="first-container"]');
|
|
108
|
-
|
|
109
85
|
expect(fn).toBeCalled();
|
|
110
86
|
}));
|
|
111
87
|
});
|
|
88
|
+
describe('#move', function () {
|
|
89
|
+
beforeEach(setupContainers);
|
|
90
|
+
afterEach(teardownContainers);
|
|
91
|
+
test('should return false when the direction is not one of \'left\', \'right\', \'up\' or \'down\'', (0, _utils.testScenario)(scenarios.complexTree, function () {
|
|
92
|
+
var actual = _spotlight["default"].move('leeeft');
|
|
93
|
+
expect(actual).toBe(false);
|
|
94
|
+
}));
|
|
95
|
+
});
|
|
96
|
+
describe('#initialize', function () {
|
|
97
|
+
beforeEach(function () {
|
|
98
|
+
jest.spyOn(window, 'addEventListener').mockImplementationOnce(function () {});
|
|
99
|
+
});
|
|
100
|
+
afterEach(function () {
|
|
101
|
+
window.addEventListener.mockRestore();
|
|
102
|
+
});
|
|
103
|
+
test('should register event listener of several event types', function () {
|
|
104
|
+
_spotlight["default"].initialize();
|
|
105
|
+
expect(window.addEventListener).toBeCalledWith('blur', expect.any(Function));
|
|
106
|
+
expect(window.addEventListener).toBeCalledWith('focus', expect.any(Function));
|
|
107
|
+
expect(window.addEventListener).toBeCalledWith('keydown', expect.any(Function));
|
|
108
|
+
expect(window.addEventListener).toBeCalledWith('keyup', expect.any(Function));
|
|
109
|
+
expect(window.addEventListener).toBeCalledWith('mouseover', expect.any(Function));
|
|
110
|
+
expect(window.addEventListener).toBeCalledWith('mousemove', expect.any(Function));
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
describe('#terminate', function () {
|
|
114
|
+
beforeEach(function () {
|
|
115
|
+
jest.spyOn(window, 'removeEventListener').mockImplementationOnce(function () {});
|
|
116
|
+
});
|
|
117
|
+
afterEach(function () {
|
|
118
|
+
window.removeEventListener.mockRestore();
|
|
119
|
+
});
|
|
120
|
+
test('should remove event listener of several event types', function () {
|
|
121
|
+
_spotlight["default"].terminate();
|
|
122
|
+
expect(window.removeEventListener).toBeCalledWith('blur', expect.any(Function));
|
|
123
|
+
expect(window.removeEventListener).toBeCalledWith('focus', expect.any(Function));
|
|
124
|
+
expect(window.removeEventListener).toBeCalledWith('keydown', expect.any(Function));
|
|
125
|
+
expect(window.removeEventListener).toBeCalledWith('keyup', expect.any(Function));
|
|
126
|
+
expect(window.removeEventListener).toBeCalledWith('mouseover', expect.any(Function));
|
|
127
|
+
expect(window.removeEventListener).toBeCalledWith('mousemove', expect.any(Function));
|
|
128
|
+
});
|
|
129
|
+
});
|
|
112
130
|
});
|
|
@@ -1,38 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _container12 = require("../container");
|
|
4
|
-
|
|
5
4
|
var _target = require("../target");
|
|
6
|
-
|
|
7
5
|
var _utils = require("./utils");
|
|
8
|
-
|
|
9
6
|
var _container2, _container3, _container4, _container5, _container6, _container7, _container8, _container9, _container10, _container11;
|
|
10
|
-
|
|
11
7
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
12
|
-
|
|
13
8
|
var nonSpottable = function nonSpottable() {
|
|
14
9
|
return (0, _utils.node)({
|
|
15
10
|
className: 'other'
|
|
16
11
|
});
|
|
17
12
|
};
|
|
18
|
-
|
|
19
13
|
var position = function position(top, left) {
|
|
20
14
|
return "position: absolute; top: ".concat(top, "px; left: ").concat(left, "px; height: 10px; width: 10px;");
|
|
21
15
|
};
|
|
22
|
-
|
|
23
16
|
var positionedSpottable = function positionedSpottable(id, top, left) {
|
|
24
17
|
return (0, _utils.spottable)({
|
|
25
18
|
id: id,
|
|
26
19
|
style: position(top, left)
|
|
27
20
|
});
|
|
28
21
|
};
|
|
29
|
-
|
|
30
22
|
var grid = function grid() {
|
|
31
23
|
var _container;
|
|
32
|
-
|
|
33
24
|
return (0, _utils.container)((_container = {}, _defineProperty(_container, _container12.containerAttribute, 'grid'), _defineProperty(_container, "style", 'position: relative; height: 30px; width: 30px;'), _defineProperty(_container, "children", (0, _utils.join)(positionedSpottable('top-left', 0, 0), positionedSpottable('top-center', 0, 10), positionedSpottable('top-right', 0, 20), positionedSpottable('middle-left', 10, 0), positionedSpottable('middle-center', 10, 10), positionedSpottable('middle-right', 10, 20), positionedSpottable('bottom-left', 20, 0), positionedSpottable('bottom-center', 20, 10), positionedSpottable('bottom-right', 20, 20))), _container));
|
|
34
25
|
};
|
|
35
|
-
|
|
36
26
|
var scenarios = {
|
|
37
27
|
complexTree: (0, _utils.join)((0, _utils.spottable)(nonSpottable()), (0, _utils.container)((_container4 = {}, _defineProperty(_container4, _container12.containerAttribute, 'first-container'), _defineProperty(_container4, "children", (0, _utils.join)((0, _utils.someSpottables)(2), (0, _utils.container)((_container3 = {}, _defineProperty(_container3, _container12.containerAttribute, 'second-container'), _defineProperty(_container3, "children", (0, _utils.join)((0, _utils.someSpottables)(3), (0, _utils.container)((_container2 = {}, _defineProperty(_container2, _container12.containerAttribute, 'third-container'), _defineProperty(_container2, 'data-spotlight-container-disabled', true), _defineProperty(_container2, "children", (0, _utils.join)((0, _utils.someSpottables)(4), (0, _utils.node)({
|
|
38
28
|
id: 'child-of-third'
|
|
@@ -88,29 +78,26 @@ var scenarios = {
|
|
|
88
78
|
emptyContainer: (0, _utils.join)(positionedSpottable('above', 0, 10), (0, _utils.container)((_container10 = {}, _defineProperty(_container10, _container12.containerAttribute, 'empty-container'), _defineProperty(_container10, "style", position(10, 10)), _container10)), positionedSpottable('below', 30, 0)),
|
|
89
79
|
emptyContainerOverlap: (0, _utils.join)(positionedSpottable('above', 5, 10), (0, _utils.container)((_container11 = {}, _defineProperty(_container11, _container12.containerAttribute, 'empty-container'), _defineProperty(_container11, "style", position(10, 10)), _container11)), positionedSpottable('below', 30, 0))
|
|
90
80
|
};
|
|
91
|
-
|
|
92
81
|
var safeTarget = function safeTarget(n, fn) {
|
|
93
82
|
return n ? fn(n) : 'NOT FOUND';
|
|
94
83
|
};
|
|
95
|
-
|
|
96
84
|
var setupContainers = function setupContainers() {
|
|
97
85
|
(0, _container12.configureDefaults)({
|
|
98
86
|
selector: '.spottable'
|
|
99
87
|
});
|
|
100
88
|
(0, _container12.configureContainer)(_container12.rootContainerId);
|
|
101
89
|
};
|
|
102
|
-
|
|
103
90
|
var teardownContainers = function teardownContainers() {
|
|
104
91
|
// clean up any containers we create for safe tests
|
|
105
92
|
(0, _container12.getAllContainerIds)().forEach(_container12.removeContainer);
|
|
106
93
|
(0, _container12.setDefaultContainer)();
|
|
107
|
-
};
|
|
108
|
-
|
|
94
|
+
};
|
|
109
95
|
|
|
96
|
+
// NOTE: Skipping most tests because JSDOM does not support measurments
|
|
110
97
|
describe('target', function () {
|
|
111
98
|
beforeEach(setupContainers);
|
|
112
99
|
afterEach(teardownContainers);
|
|
113
|
-
describe
|
|
100
|
+
describe('#getNavigableTarget', function () {
|
|
114
101
|
test('should find spottable parent', (0, _utils.testScenario)(scenarios.complexTree, function (root) {
|
|
115
102
|
var other = root.querySelector('.other');
|
|
116
103
|
var expected = other.parentNode;
|
|
@@ -257,8 +244,9 @@ describe('target', function () {
|
|
|
257
244
|
expect(actual).toBe(expected);
|
|
258
245
|
}));
|
|
259
246
|
test('should return null for an empty selectors', (0, _utils.testScenario)(scenarios.nonSpottableInContainer, function () {
|
|
260
|
-
var expected = null;
|
|
247
|
+
var expected = null;
|
|
261
248
|
|
|
249
|
+
// eslint-disable-next-line no-undefined
|
|
262
250
|
expect((0, _target.getTargetBySelector)(undefined)).toBe(expected);
|
|
263
251
|
expect((0, _target.getTargetBySelector)(null)).toBe(expected);
|
|
264
252
|
expect((0, _target.getTargetBySelector)('')).toBe(expected);
|
|
@@ -494,11 +482,9 @@ describe('target', function () {
|
|
|
494
482
|
defaultElement: '#bottom-right'
|
|
495
483
|
});
|
|
496
484
|
var overlap = root.querySelector('#over-middle-center');
|
|
497
|
-
|
|
498
485
|
var _overlap$getBoundingC = overlap.getBoundingClientRect(),
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
486
|
+
x = _overlap$getBoundingC.left,
|
|
487
|
+
y = _overlap$getBoundingC.top;
|
|
502
488
|
expect(safeTarget((0, _target.getTargetByDirectionFromPosition)('down', {
|
|
503
489
|
x: x,
|
|
504
490
|
y: y
|
|
@@ -511,11 +497,9 @@ describe('target', function () {
|
|
|
511
497
|
overflow: true
|
|
512
498
|
});
|
|
513
499
|
var element = root.querySelector('#outside-overflow');
|
|
514
|
-
|
|
515
500
|
var _element$getBoundingC = element.getBoundingClientRect(),
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
501
|
+
x = _element$getBoundingC.left,
|
|
502
|
+
y = _element$getBoundingC.top;
|
|
519
503
|
expect(safeTarget((0, _target.getTargetByDirectionFromPosition)('down', {
|
|
520
504
|
x: x,
|
|
521
505
|
y: y
|
|
@@ -531,11 +515,9 @@ describe('target', function () {
|
|
|
531
515
|
enterTo: null
|
|
532
516
|
});
|
|
533
517
|
var element = root.querySelector('#outside-overflow');
|
|
534
|
-
|
|
535
518
|
var _element$getBoundingC2 = element.getBoundingClientRect(),
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
519
|
+
x = _element$getBoundingC2.left,
|
|
520
|
+
y = _element$getBoundingC2.top;
|
|
539
521
|
expect(safeTarget((0, _target.getTargetByDirectionFromPosition)('down', {
|
|
540
522
|
x: x,
|
|
541
523
|
y: y
|
|
@@ -548,13 +530,11 @@ describe('target', function () {
|
|
|
548
530
|
overflow: true
|
|
549
531
|
});
|
|
550
532
|
var element = root.querySelector('#overflow-within');
|
|
551
|
-
|
|
552
533
|
var _element$getBoundingC3 = element.getBoundingClientRect(),
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
534
|
+
left = _element$getBoundingC3.left,
|
|
535
|
+
width = _element$getBoundingC3.width,
|
|
536
|
+
top = _element$getBoundingC3.top,
|
|
537
|
+
height = _element$getBoundingC3.height;
|
|
558
538
|
var x = left + width / 2;
|
|
559
539
|
var y = top + height / 2;
|
|
560
540
|
expect(safeTarget((0, _target.getTargetByDirectionFromPosition)('down', {
|
|
@@ -573,13 +553,11 @@ describe('target', function () {
|
|
|
573
553
|
test('should ignore empty containers', (0, _utils.testScenario)(scenarios.emptyContainer, function (root) {
|
|
574
554
|
(0, _container12.configureContainer)('empty-container');
|
|
575
555
|
var element = root.querySelector('#above');
|
|
576
|
-
|
|
577
556
|
var _element$getBoundingC4 = element.getBoundingClientRect(),
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
557
|
+
left = _element$getBoundingC4.left,
|
|
558
|
+
width = _element$getBoundingC4.width,
|
|
559
|
+
top = _element$getBoundingC4.top,
|
|
560
|
+
height = _element$getBoundingC4.height;
|
|
583
561
|
var x = left + width / 2;
|
|
584
562
|
var y = top + height - 1; // just inside the bottom of 'above'
|
|
585
563
|
|
|
@@ -593,13 +571,11 @@ describe('target', function () {
|
|
|
593
571
|
test('should ignore overlapping empty containers', (0, _utils.testScenario)(scenarios.emptyContainer, function (root) {
|
|
594
572
|
(0, _container12.configureContainer)('empty-container');
|
|
595
573
|
var element = root.querySelector('#above');
|
|
596
|
-
|
|
597
574
|
var _element$getBoundingC5 = element.getBoundingClientRect(),
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
575
|
+
left = _element$getBoundingC5.left,
|
|
576
|
+
width = _element$getBoundingC5.width,
|
|
577
|
+
top = _element$getBoundingC5.top,
|
|
578
|
+
height = _element$getBoundingC5.height;
|
|
603
579
|
var x = left + width / 2;
|
|
604
580
|
var y = top + height + 1; // just inside the empty container
|
|
605
581
|
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _utils = require("../utils");
|
|
4
|
+
describe('utils', function () {
|
|
5
|
+
describe('#getPointRect', function () {
|
|
6
|
+
test('should return an rect value calculated based on given position', function () {
|
|
7
|
+
var expected = {
|
|
8
|
+
left: 10,
|
|
9
|
+
top: 20,
|
|
10
|
+
width: 0,
|
|
11
|
+
height: 0,
|
|
12
|
+
right: 10,
|
|
13
|
+
bottom: 20,
|
|
14
|
+
center: {
|
|
15
|
+
x: 10,
|
|
16
|
+
y: 20,
|
|
17
|
+
left: 10,
|
|
18
|
+
right: 10,
|
|
19
|
+
top: 20,
|
|
20
|
+
bottom: 20
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
var actual = (0, _utils.getPointRect)({
|
|
24
|
+
x: 10,
|
|
25
|
+
y: 20
|
|
26
|
+
});
|
|
27
|
+
expect(actual).toEqual(expected);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
describe('#getContainerRect', function () {
|
|
31
|
+
test('should return an viewPort rect value when container is document', function () {
|
|
32
|
+
var innerWidth = window.innerWidth;
|
|
33
|
+
var innerHeight = window.innerHeight;
|
|
34
|
+
var actual = (0, _utils.getContainerRect)('spotlightRootDecorator');
|
|
35
|
+
var expected = {
|
|
36
|
+
left: 0,
|
|
37
|
+
top: 0,
|
|
38
|
+
width: innerWidth,
|
|
39
|
+
height: innerHeight,
|
|
40
|
+
right: innerWidth,
|
|
41
|
+
bottom: innerHeight,
|
|
42
|
+
center: {
|
|
43
|
+
x: innerWidth / 2,
|
|
44
|
+
y: innerHeight / 2,
|
|
45
|
+
left: innerWidth / 2,
|
|
46
|
+
right: innerWidth / 2,
|
|
47
|
+
top: innerHeight / 2,
|
|
48
|
+
bottom: innerHeight / 2
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
expect(actual).toEqual(expected);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
});
|
package/src/tests/utils.js
CHANGED
|
@@ -4,49 +4,34 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.testScenario = exports.spottable = exports.someSpottablesAndContainers = exports.someSpottables = exports.someNodes = exports.someContainers = exports.node = exports.join = exports.generateContainerId = exports.container = void 0;
|
|
7
|
-
|
|
8
7
|
var _ramda = _interopRequireDefault(require("ramda"));
|
|
9
|
-
|
|
10
8
|
var _container = require("../container");
|
|
11
|
-
|
|
12
9
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
13
|
-
|
|
14
10
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
15
|
-
|
|
16
11
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
17
|
-
|
|
18
12
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
19
|
-
|
|
20
13
|
var join = _ramda["default"].unapply(_ramda["default"].join('\n'));
|
|
21
|
-
|
|
22
14
|
exports.join = join;
|
|
23
|
-
|
|
24
15
|
var testScenario = function testScenario(scenario, callback) {
|
|
25
16
|
return function () {
|
|
26
17
|
var rootId = 'test-root';
|
|
27
18
|
var html = join("<div id=\"".concat(rootId, "\">"), scenario, '</div>');
|
|
28
19
|
document.body.innerHTML = html;
|
|
29
20
|
var root = document.getElementById(rootId);
|
|
30
|
-
|
|
31
21
|
try {
|
|
32
22
|
callback(root);
|
|
33
23
|
} catch (e) {
|
|
34
24
|
console.log(html); // eslint-disable-line no-console
|
|
35
|
-
|
|
36
25
|
throw e;
|
|
37
26
|
}
|
|
38
27
|
};
|
|
39
28
|
};
|
|
40
|
-
|
|
41
29
|
exports.testScenario = testScenario;
|
|
42
30
|
var _id = 1;
|
|
43
|
-
|
|
44
31
|
var generateContainerId = function generateContainerId() {
|
|
45
32
|
return "".concat(_container.containerAttribute, "=").concat(_id++);
|
|
46
33
|
};
|
|
47
|
-
|
|
48
34
|
exports.generateContainerId = generateContainerId;
|
|
49
|
-
|
|
50
35
|
var coerceProps = function coerceProps(v) {
|
|
51
36
|
if (typeof v === 'object') {
|
|
52
37
|
return v;
|
|
@@ -56,7 +41,6 @@ var coerceProps = function coerceProps(v) {
|
|
|
56
41
|
};
|
|
57
42
|
}
|
|
58
43
|
};
|
|
59
|
-
|
|
60
44
|
var node = function node(props) {
|
|
61
45
|
var children = '';
|
|
62
46
|
var attributes = '';
|
|
@@ -71,34 +55,25 @@ var node = function node(props) {
|
|
|
71
55
|
});
|
|
72
56
|
return "<div ".concat(attributes, ">").concat(children, "</div>");
|
|
73
57
|
};
|
|
74
|
-
|
|
75
58
|
exports.node = node;
|
|
76
|
-
|
|
77
59
|
var spottable = function spottable(props) {
|
|
78
60
|
return node(_objectSpread({
|
|
79
61
|
className: 'spottable'
|
|
80
62
|
}, coerceProps(props)));
|
|
81
63
|
};
|
|
82
|
-
|
|
83
64
|
exports.spottable = spottable;
|
|
84
|
-
|
|
85
65
|
var container = function container(props) {
|
|
86
66
|
var _objectSpread2;
|
|
87
|
-
|
|
88
67
|
return node(_objectSpread((_objectSpread2 = {}, _defineProperty(_objectSpread2, _container.containerAttribute, _id++), _defineProperty(_objectSpread2, 'data-spotlight-container', true), _objectSpread2), coerceProps(props)));
|
|
89
|
-
};
|
|
90
|
-
|
|
68
|
+
};
|
|
91
69
|
|
|
70
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
92
71
|
exports.container = container;
|
|
93
|
-
|
|
94
72
|
var someNodes = _ramda["default"].useWith(_ramda["default"].compose(_ramda["default"].join('\n'), _ramda["default"].map), [_ramda["default"].identity, _ramda["default"].range(0)]);
|
|
95
|
-
|
|
96
73
|
exports.someNodes = someNodes;
|
|
97
74
|
var someSpottables = someNodes(spottable);
|
|
98
75
|
exports.someSpottables = someSpottables;
|
|
99
76
|
var someContainers = someNodes(container);
|
|
100
77
|
exports.someContainers = someContainers;
|
|
101
|
-
|
|
102
78
|
var someSpottablesAndContainers = _ramda["default"].converge(_ramda["default"].concat, [someSpottables, someContainers]);
|
|
103
|
-
|
|
104
79
|
exports.someSpottablesAndContainers = someSpottablesAndContainers;
|
package/src/utils.js
CHANGED
|
@@ -10,24 +10,17 @@ exports.getRect = getRect;
|
|
|
10
10
|
exports.getRects = getRects;
|
|
11
11
|
exports.matchSelector = exports.intersects = void 0;
|
|
12
12
|
exports.parseSelector = parseSelector;
|
|
13
|
-
|
|
14
13
|
var _curry = _interopRequireDefault(require("ramda/src/curry"));
|
|
15
|
-
|
|
16
14
|
var _warning = _interopRequireDefault(require("warning"));
|
|
17
|
-
|
|
18
15
|
var _container = require("./container");
|
|
19
|
-
|
|
20
16
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
21
|
-
|
|
22
17
|
var elementMatchesSelector = function elementMatchesSelector(selector) {
|
|
23
18
|
var matchedNodes = (this.parentNode || this.document).querySelectorAll(selector);
|
|
24
19
|
return [].slice.call(matchedNodes).indexOf(this) >= 0;
|
|
25
20
|
};
|
|
26
|
-
|
|
27
21
|
if (typeof window === 'object') {
|
|
28
22
|
elementMatchesSelector = window.Element.prototype.matches || window.Element.prototype.matchesSelector || window.Element.prototype.mozMatchesSelector || window.Element.prototype.webkitMatchesSelector || window.Element.prototype.msMatchesSelector || window.Element.prototype.oMatchesSelector || elementMatchesSelector;
|
|
29
23
|
}
|
|
30
|
-
|
|
31
24
|
var matchSelector = (0, _curry["default"])(function (selector, elem) {
|
|
32
25
|
if (typeof selector === 'string') {
|
|
33
26
|
return elementMatchesSelector.call(elem, selector);
|
|
@@ -36,14 +29,11 @@ var matchSelector = (0, _curry["default"])(function (selector, elem) {
|
|
|
36
29
|
} else if (typeof selector === 'object' && selector.nodeType === 1) {
|
|
37
30
|
return elem === selector;
|
|
38
31
|
}
|
|
39
|
-
|
|
40
32
|
return false;
|
|
41
33
|
});
|
|
42
34
|
exports.matchSelector = matchSelector;
|
|
43
|
-
|
|
44
35
|
function parseSelector(selector) {
|
|
45
36
|
var result = [];
|
|
46
|
-
|
|
47
37
|
try {
|
|
48
38
|
if (typeof selector === 'string') {
|
|
49
39
|
result = [].slice.call(document.querySelectorAll(selector));
|
|
@@ -55,36 +45,36 @@ function parseSelector(selector) {
|
|
|
55
45
|
} catch (ex) {
|
|
56
46
|
process.env.NODE_ENV !== "production" ? (0, _warning["default"])(true, "parseSelector failed for selector: ".concat(selector)) : void 0;
|
|
57
47
|
}
|
|
58
|
-
|
|
59
48
|
return result;
|
|
60
49
|
}
|
|
61
|
-
|
|
62
50
|
var testIntersection = function testIntersection(type, containerRect, elementRect) {
|
|
63
51
|
var L = containerRect.left,
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
52
|
+
R = containerRect.right,
|
|
53
|
+
T = containerRect.top,
|
|
54
|
+
B = containerRect.bottom;
|
|
67
55
|
var l = elementRect.left,
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
56
|
+
r = elementRect.right,
|
|
57
|
+
t = elementRect.top,
|
|
58
|
+
b = elementRect.bottom;
|
|
72
59
|
if (type === 'intersects') {
|
|
73
60
|
// Test intersection by eliminating the area of the element that is outside of the container
|
|
74
61
|
return !(b < T || t > B || r < L || l > R);
|
|
75
62
|
} else if (type === 'contains') {
|
|
76
|
-
var epsilon = 1;
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
63
|
+
var epsilon = 1;
|
|
64
|
+
|
|
65
|
+
// Test whether all bounds are within the container
|
|
66
|
+
return r > L - epsilon && r < R + epsilon &&
|
|
67
|
+
// right
|
|
68
|
+
l > L - epsilon && l < R + epsilon &&
|
|
69
|
+
// left
|
|
70
|
+
t > T - epsilon && t < B + epsilon &&
|
|
71
|
+
// top
|
|
81
72
|
b > T - epsilon && b < B + epsilon // bottom
|
|
82
73
|
;
|
|
83
74
|
}
|
|
84
75
|
|
|
85
76
|
return true;
|
|
86
77
|
};
|
|
87
|
-
|
|
88
78
|
var intersects = (0, _curry["default"])(function (containerRect, elementRect) {
|
|
89
79
|
return testIntersection('intersects', containerRect, elementRect);
|
|
90
80
|
});
|
|
@@ -93,7 +83,6 @@ var contains = (0, _curry["default"])(function (containerRect, elementRect) {
|
|
|
93
83
|
return testIntersection('contains', containerRect, elementRect);
|
|
94
84
|
});
|
|
95
85
|
exports.contains = contains;
|
|
96
|
-
|
|
97
86
|
function getRect(elem) {
|
|
98
87
|
var cr = elem.getBoundingClientRect();
|
|
99
88
|
var rect = {
|
|
@@ -113,10 +102,9 @@ function getRect(elem) {
|
|
|
113
102
|
rect.center.top = rect.center.bottom = rect.center.y;
|
|
114
103
|
return rect;
|
|
115
104
|
}
|
|
116
|
-
|
|
117
105
|
function getPointRect(position) {
|
|
118
106
|
var x = position.x,
|
|
119
|
-
|
|
107
|
+
y = position.y;
|
|
120
108
|
return {
|
|
121
109
|
left: x,
|
|
122
110
|
top: y,
|
|
@@ -134,19 +122,16 @@ function getPointRect(position) {
|
|
|
134
122
|
}
|
|
135
123
|
};
|
|
136
124
|
}
|
|
137
|
-
|
|
138
125
|
function getRects(candidates) {
|
|
139
126
|
if (candidates && candidates.length > 0) {
|
|
140
127
|
return candidates.map(getRect);
|
|
141
128
|
}
|
|
142
|
-
|
|
143
129
|
return [];
|
|
144
130
|
}
|
|
145
|
-
|
|
146
131
|
function getViewportRect() {
|
|
147
132
|
var _window = window,
|
|
148
|
-
|
|
149
|
-
|
|
133
|
+
width = _window.innerWidth,
|
|
134
|
+
height = _window.innerHeight;
|
|
150
135
|
var x = width / 2;
|
|
151
136
|
var y = height / 2;
|
|
152
137
|
return {
|
|
@@ -166,17 +151,13 @@ function getViewportRect() {
|
|
|
166
151
|
}
|
|
167
152
|
};
|
|
168
153
|
}
|
|
169
|
-
|
|
170
154
|
function getContainerRect(containerId) {
|
|
171
155
|
var containerNode = (0, _container.getContainerNode)(containerId);
|
|
172
|
-
|
|
173
156
|
if (!containerNode) {
|
|
174
157
|
return null;
|
|
175
158
|
}
|
|
176
|
-
|
|
177
159
|
if (containerNode === document) {
|
|
178
160
|
return getViewportRect();
|
|
179
161
|
}
|
|
180
|
-
|
|
181
162
|
return getRect(containerNode);
|
|
182
163
|
}
|