|
| 1 | +import RenderScheduler from "../RenderScheduler.js"; |
1 | 2 | import {
|
2 | 3 | isDown,
|
3 | 4 | isUp,
|
@@ -47,37 +48,19 @@ class ItemNavigation extends EventProvider {
|
47 | 48 | return this.verticalNavigationOn;
|
48 | 49 | }
|
49 | 50 |
|
50 |
| - _onKeyPress(event) { |
51 |
| - const items = this._getItems(); |
52 |
| - if (this.currentIndex >= items.length) { |
53 |
| - if (this.behavior !== ItemNavigationBehavior.Cyclic) { |
54 |
| - if (this.behavior === ItemNavigationBehavior.Paging) { |
55 |
| - this.currentIndex = this.currentIndex - items.length; |
56 |
| - } else { |
57 |
| - this.currentIndex = items.length - 1; |
58 |
| - } |
59 |
| - this.fireEvent(ItemNavigation.BORDER_REACH, { start: false, end: true, offset: this.currentIndex }); |
60 |
| - } else { |
61 |
| - this.currentIndex = this.currentIndex - items.length; |
62 |
| - } |
| 51 | + async _onKeyPress(event) { |
| 52 | + if (this.currentIndex >= this._getItems().length) { |
| 53 | + this.onOverflowBottomEdge(); |
63 | 54 | } else if (this.currentIndex < 0) {
|
64 |
| - if (this.behavior !== ItemNavigationBehavior.Cyclic) { |
65 |
| - if (this.behavior === ItemNavigationBehavior.Paging) { |
66 |
| - this.currentIndex = items.length + this.currentIndex - this.rowSize + (this.rowSize - (this._getItems().length % this.rowSize)); |
67 |
| - } else { |
68 |
| - this.currentIndex = 0; |
69 |
| - } |
70 |
| - this.fireEvent(ItemNavigation.BORDER_REACH, { start: true, end: false, offset: this.currentIndex }); |
71 |
| - } else { |
72 |
| - this.currentIndex = items.length + this.currentIndex; |
73 |
| - } |
| 55 | + this.onOverflowTopEdge(); |
74 | 56 | }
|
75 | 57 |
|
| 58 | + event.preventDefault(); |
| 59 | + |
| 60 | + await RenderScheduler.whenFinished(); |
| 61 | + |
76 | 62 | this.update();
|
77 | 63 | this.focusCurrent();
|
78 |
| - |
79 |
| - // stops browser scrolling with up/down keys |
80 |
| - event.preventDefault(); |
81 | 64 | }
|
82 | 65 |
|
83 | 66 | onkeydown(event) {
|
@@ -227,8 +210,65 @@ class ItemNavigation extends EventProvider {
|
227 | 210 | set current(val) {
|
228 | 211 | this.currentIndex = val;
|
229 | 212 | }
|
| 213 | + |
| 214 | + onOverflowBottomEdge() { |
| 215 | + const items = this._getItems(); |
| 216 | + const rowIndex = this.currentIndex - items.length; |
| 217 | + if (this.behavior === ItemNavigationBehavior.Cyclic) { |
| 218 | + return; |
| 219 | + } |
| 220 | + |
| 221 | + if (this.behavior === ItemNavigationBehavior.Paging) { |
| 222 | + this._handleNextPage(); |
| 223 | + } |
| 224 | + |
| 225 | + this.fireEvent(ItemNavigation.BORDER_REACH, { |
| 226 | + start: false, end: true, offset: this.currentIndex, rowIndex, |
| 227 | + }); |
| 228 | + } |
| 229 | + |
| 230 | + onOverflowTopEdge() { |
| 231 | + const items = this._getItems(); |
| 232 | + const rowIndex = this.currentIndex + this.rowSize; |
| 233 | + |
| 234 | + if (this.behavior === ItemNavigationBehavior.Cyclic) { |
| 235 | + this.currentIndex = items.length + this.currentIndex; |
| 236 | + return; |
| 237 | + } |
| 238 | + |
| 239 | + if (this.behavior === ItemNavigationBehavior.Paging) { |
| 240 | + this._handlePrevPage(); |
| 241 | + } |
| 242 | + |
| 243 | + this.fireEvent(ItemNavigation.BORDER_REACH, { |
| 244 | + start: true, end: false, offset: this.currentIndex, rowIndex, |
| 245 | + }); |
| 246 | + } |
| 247 | + |
| 248 | + _handleNextPage() { |
| 249 | + this.fireEvent(ItemNavigation.PAGE_BOTTOM); |
| 250 | + const items = this._getItems(); |
| 251 | + |
| 252 | + if (!this.hasNextPage) { |
| 253 | + this.currentIndex = items.length - 1; |
| 254 | + } else { |
| 255 | + this.currentIndex = 0; |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + _handlePrevPage() { |
| 260 | + this.fireEvent(ItemNavigation.PAGE_TOP); |
| 261 | + |
| 262 | + if (!this.hasPrevPage) { |
| 263 | + this.currentIndex = 0; |
| 264 | + } else { |
| 265 | + this.currentIndex = 41; |
| 266 | + } |
| 267 | + } |
230 | 268 | }
|
231 | 269 |
|
| 270 | +ItemNavigation.PAGE_TOP = "PageTop"; |
| 271 | +ItemNavigation.PAGE_BOTTOM = "PageBottom"; |
232 | 272 | ItemNavigation.BORDER_REACH = "_borderReach";
|
233 | 273 |
|
234 | 274 | export default ItemNavigation;
|
0 commit comments