Skip to content

Commit cb45e4e

Browse files
committed
Fix some eslint errors and modify some funtions name.
1 parent c276a1b commit cb45e4e

File tree

2 files changed

+43
-51
lines changed

2 files changed

+43
-51
lines changed

demo/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
function polyfilEnableChanged() {
4747
let polyfilEnableSwitch = document.getElementById('polyfill-enable-switch');
4848
let polyfilStateDiv = document.getElementById('polyfill-state');
49-
window.spatnavPolyfillOff = !polyfilEnableSwitch.checked;
49+
50+
// toggle spatialNavigation
51+
window.__spatialNavigation__.setKeyMode(window.__spatialNavigation__.getKeyMode()==='NONE'? '':'NONE');
5052

5153
if(polyfilEnableSwitch.checked) {
5254
polyfilStateDiv.classList.remove('deactivate');

polyfill/spatnav-heuristic.js

Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
(function () {
1212

1313
// Indicates global variables for spatnav (starting position)
14-
let spatNavManager = {
14+
const spatNavManager = {
1515
startingPosition: null,
1616
useStandardName: true,
1717
};
@@ -32,7 +32,7 @@
3232

3333
const ARROW_KEY_CODE = {37: 'left', 38: 'up', 39: 'right', 40: 'down'};
3434
const TAB_KEY_CODE = 9;
35-
let shiftArrow = false;
35+
let spatialNaviagtionKeyMode = 'ARROW';
3636

3737
function focusNavigationHeuristics() {
3838

@@ -54,23 +54,28 @@
5454
* If arrow key pressed, get the next focusing element and send it to focusing controller
5555
*/
5656
window.addEventListener('keydown', function(e) {
57-
const spatnavPolyfillOff = window.spatnavPolyfillOff || (parent && parent.spatnavPolyfillOff);
58-
if (!spatnavPolyfillOff && !e.defaultPrevented) {
57+
const currentKeyMode = (parent && parent.__spatialNavigation__.getKeyMode()) || window.__spatialNavigation__.getKeyMode() ;
58+
const eventTarget = document.activeElement;
59+
const dir = ARROW_KEY_CODE[e.keyCode];
60+
61+
if(!currentKeyMode ||
62+
(currentKeyMode === 'NONE') ||
63+
((currentKeyMode === 'SHIFTARROW') && !e.shiftKey) ||
64+
((currentKeyMode === 'ARROW') && e.shiftKey))
65+
return;
66+
67+
if (!e.defaultPrevented) {
5968
let focusNavigableArrowKey = {'left': true, 'up': true, 'right': true, 'down': true};
60-
const eventTarget = document.activeElement;
61-
const dir = ARROW_KEY_CODE[e.keyCode];
6269

63-
if(!shiftArrow || e.shiftKey) {
64-
// Edge case (text input, area) : Don't move focus, just navigate cursor in text area
65-
if ((eventTarget.nodeName === 'INPUT') || eventTarget.nodeName === 'TEXTAREA')
66-
focusNavigableArrowKey = handlingEditableElement(e);
70+
// Edge case (text input, area) : Don't move focus, just navigate cursor in text area
71+
if ((eventTarget.nodeName === 'INPUT') || eventTarget.nodeName === 'TEXTAREA')
72+
focusNavigableArrowKey = handlingEditableElement(e);
6773

68-
if (focusNavigableArrowKey[dir]) {
69-
e.preventDefault();
70-
navigate(dir);
74+
if (focusNavigableArrowKey[dir]) {
75+
e.preventDefault();
76+
navigate(dir);
7177

72-
spatNavManager.startingPosition = null;
73-
}
78+
spatNavManager.startingPosition = null;
7479
}
7580
}
7681

@@ -323,7 +328,7 @@
323328
**/
324329
function spatNavSearch (dir, candidates, container) {
325330
// Let container be the nearest ancestor of eventTarget that is a spatnav container.
326-
let targetElement = this;
331+
const targetElement = this;
327332
let bestCandidate = null;
328333

329334
candidates = spatNavCandidates(targetElement, dir, candidates, container);
@@ -573,7 +578,7 @@
573578
}
574579

575580
function isCSSSpatNavContain(el) {
576-
return (readCssVar(el, 'spatial-navigation-contain') == 'contain') ? true : false;
581+
return readCssVar(el, 'spatial-navigation-contain') == 'contain';
577582
}
578583

579584
/**
@@ -1001,7 +1006,7 @@
10011006
* @returns {Number} euclidian distance between two elements
10021007
**/
10031008
function getEntryAndExitPoints(dir = 'down', rect1, rect2) {
1004-
let points = {entryPoint:[0,0], exitPoint:[0,0]};
1009+
const points = {entryPoint:[0,0], exitPoint:[0,0]};
10051010

10061011
// Set direction
10071012
switch (dir) {
@@ -1146,7 +1151,7 @@
11461151

11471152

11481153
function addNonStandardAPI() {
1149-
function isScrollableElement(container, dir) {
1154+
function canScroll(container, dir) {
11501155
return (isScrollable(container, dir) && !isScrollBoundary(container, dir)) ||
11511156
(!container.parentElement && !isHTMLScrollBoundary(container, dir));
11521157
}
@@ -1179,7 +1184,7 @@
11791184
return bestNextTarget;
11801185
}
11811186
}
1182-
if (isScrollableElement(eventTarget, dir)) {
1187+
if (canScroll(eventTarget, dir)) {
11831188
if(findCandidate) {
11841189
return [];
11851190
} else {
@@ -1221,7 +1226,7 @@
12211226
// If there isn't any candidate and the best candidate among candidate:
12221227
// 1) Scroll or 2) Find candidates of the ancestor container
12231228
// 8 - if
1224-
else if (isScrollableElement(container, dir)) {
1229+
else if (canScroll(container, dir)) {
12251230
if(findCandidate) {
12261231
return [];
12271232
} else {
@@ -1239,12 +1244,10 @@
12391244
eventTarget = window.frameElement;
12401245
container = window.parent.document.documentElement;
12411246
}
1242-
else {
1243-
if(findCandidate) {
1244-
return [];
1245-
} else {
1246-
return null;
1247-
}
1247+
else if(findCandidate) {
1248+
return [];
1249+
} else {
1250+
return null;
12481251
}
12491252

12501253
parentContainer = container.getSpatnavContainer();
@@ -1278,7 +1281,7 @@
12781281
}
12791282
}
12801283

1281-
if (isScrollableElement(container, dir)) {
1284+
if (canScroll(container, dir)) {
12821285
bestNextTarget = eventTarget;
12831286
return bestNextTarget;
12841287
}
@@ -1288,7 +1291,7 @@
12881291
isContainer: isContainer,
12891292
findCandidates: findTarget.bind(null, true),
12901293
findNextTarget: findTarget.bind(null, false),
1291-
getDistance: (element, candidateElement, dir) => {
1294+
getDistanceFromTarget: (element, candidateElement, dir) => {
12921295
if ((isContainer(element) || element.nodeName === 'BODY') && !(element.nodeName === 'INPUT')) {
12931296
if (element.focusableAreas().includes(candidateElement)) {
12941297
return getInnerDistance(element.getBoundingClientRect(), candidateElement.getBoundingClientRect(), dir);
@@ -1297,28 +1300,15 @@
12971300
return getDistance(element.getBoundingClientRect(), candidateElement.getBoundingClientRect(), dir);
12981301
},
12991302

1300-
setPolyfillOnOff: (option) => {
1301-
switch(option) {
1302-
case 'SHIFTARROW':
1303-
window.spatnavPolyfillOff = false;
1304-
if(parent) {
1305-
parent.spatnavPolyfillOff = false;
1306-
}
1307-
shiftArrow = true;
1308-
break;
1309-
case 'OFF':
1310-
window.spatnavPolyfillOff = true;
1311-
break;
1312-
case 'ARROW':
1313-
default:
1314-
window.spatnavPolyfillOff = false;
1315-
if(parent) {
1316-
parent.spatnavPolyfillOff = false;
1317-
}
1318-
shiftArrow = false;
1319-
break;
1303+
setKeyMode : (option) => {
1304+
if(['SHIFTARROW', 'ARROW', 'NONE'].includes(option)) {
1305+
spatialNaviagtionKeyMode = option;
1306+
} else {
1307+
spatialNaviagtionKeyMode = 'ARROW';
13201308
}
1321-
}
1309+
},
1310+
1311+
getKeyMode : () => spatialNaviagtionKeyMode
13221312
};
13231313
}
13241314
addNonStandardAPI();

0 commit comments

Comments
 (0)