Skip to content

Commit e3e9a38

Browse files
author
Jihye Hong
authored
Merge pull request w3c#109 from jihyerish/polyfill-setting-starting-point
Modify the way to set the spatial navigation starting point
2 parents 4337307 + ca3b54b commit e3e9a38

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

demo/sample/heuristic_non_scrollable_iframe.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<script src="../../polyfill/spatnav-heuristic.js"></script>
1313
</head>
1414
<body>
15-
<iframe src="heuristic_non_scrollable_iframe_element.html" tabindex="0" class="container c2"></iframe>
15+
<iframe src="heuristic_non_scrollable_iframe_element.html" class="container c2"></iframe>
1616
<div tabindex="0" class="box b1" style="left: 70px; top: -180px;"></div>
1717
<div tabindex="0" class="box b1" style="left: 180px; top: -120px;"></div>
1818
<div tabindex="0" class="box b1" style="left: 20px; top: -90px;"></div>

polyfill/spatnav-heuristic.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,17 @@
6868
navigate(dir);
6969

7070
spatNavManager.startingPosition = null;
71-
}
71+
}
7272
}
73-
73+
7474
if (e.keyCode === TAB_KEY_CODE)
7575
spatNavManager.startingPosition = null;
7676
});
7777

7878
/**
7979
* mouseup EventListener :
8080
* If the mouse click a point in the page, the point will be the starting point.
81+
* *NOTE: Let UA set the spatial navigation starting point based on click
8182
*/
8283
document.addEventListener('mouseup', function(e) {
8384
spatNavManager.startingPosition = {xPosition: e.clientX, yPosition: e.clientY};
@@ -95,19 +96,25 @@
9596
// spatial navigation steps
9697

9798
// 1
98-
const startingPoint = findStartingPoint();
99-
100-
// 2 Optional step, not handled
101-
// UA defined starting point
99+
let startingPoint = findStartingPoint();
100+
let eventTarget = null;
101+
let elementFromPosition = null;
102102

103-
// 3
104-
let eventTarget = startingPoint;
105-
106-
// 3-2 : the mouse clicked position will be come the starting point
103+
// 2 Optional step, UA defined starting point
107104
if (spatNavManager.startingPosition) {
108-
eventTarget = document.elementFromPoint(spatNavManager.startingPosition.xPosition, spatNavManager.startingPosition.yPosition);
105+
elementFromPosition = document.elementFromPoint(spatNavManager.startingPosition.xPosition, spatNavManager.startingPosition.yPosition);
106+
}
109107

108+
if (elementFromPosition && startingPoint.contains(elementFromPosition)) {
109+
startingPoint = spatNavManager.startingPosition;
110110
spatNavManager.startingPosition = null;
111+
112+
// 3
113+
eventTarget = elementFromPosition;
114+
}
115+
else {
116+
// 3
117+
eventTarget = startingPoint;
111118
}
112119

113120
// 4
@@ -310,7 +317,7 @@
310317
// find the best candidate within startingPoint
311318
if (candidates && candidates.length > 0) {
312319
if ((isContainer(targetElement) || targetElement.nodeName === 'BODY') && !(targetElement.nodeName === 'INPUT')) {
313-
if (candidates.every(x => targetElement.focusableAreas().includes(x))) {
320+
if (candidates.every(x => targetElement.focusableAreas().includes(x))) {
314321
// if candidates are contained in the targetElement, then the focus moves inside the targetElement
315322
bestCandidate = selectBestCandidateFromEdge(targetElement, candidates, dir);
316323
}
@@ -320,9 +327,9 @@
320327
}
321328
else {
322329
bestCandidate = selectBestCandidate(targetElement, candidates, dir);
323-
}
330+
}
324331
}
325-
332+
326333
return bestCandidate;
327334
}
328335

@@ -647,7 +654,7 @@
647654
}
648655
}
649656

650-
/**
657+
/**
651658
* isOverflow
652659
* Whether this element is overflow or not
653660
* @function
@@ -737,7 +744,6 @@
737744
return false;
738745
else
739746
return ((!element.parentElement) ||
740-
(element.nodeName === 'IFRAME') ||
741747
(element.tabIndex >= 0) ||
742748
(isScrollable(element) && isOverflow(element)));
743749
}
@@ -1116,7 +1122,7 @@
11161122
}
11171123

11181124
window.addEventListener('load', function() {
1119-
1125+
11201126
// load SpatNav polyfill
11211127
focusNavigationHeuristics();
11221128
});

0 commit comments

Comments
 (0)