|
1 | 1 | (function () {
|
| 2 | + var classNone = 'none'; |
| 3 | + var classHeader = 'header'; |
| 4 | + |
| 5 | + function enableKeyboardNavigate() { |
| 6 | + if ( |
| 7 | + !document.querySelector || |
| 8 | + !document.addEventListener || |
| 9 | + !document.body.classList || |
| 10 | + !document.body.parentElement |
| 11 | + ) { |
| 12 | + return; |
| 13 | + } |
| 14 | + |
| 15 | + var pathList = document.body.querySelector('.path-list'); |
| 16 | + var itemList = document.body.querySelector('.item-list'); |
| 17 | + if (!pathList && !itemList) { |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + function getFocusableSibling(container, isPrev) { |
| 22 | + if (!container) { |
| 23 | + return |
| 24 | + } |
| 25 | + var focusA = container.querySelector(':focus'); |
| 26 | + var focusLI = focusA; |
| 27 | + while (focusLI && focusLI.tagName !== 'LI') { |
| 28 | + focusLI = focusLI.parentElement; |
| 29 | + } |
| 30 | + if (!focusLI) { |
| 31 | + if (isPrev) { |
| 32 | + focusLI = container.firstElementChild; |
| 33 | + } else { |
| 34 | + focusLI = container.lastElementChild; |
| 35 | + } |
| 36 | + } |
| 37 | + if (!focusLI) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + var siblingLI = focusLI; |
| 42 | + do { |
| 43 | + if (isPrev) { |
| 44 | + siblingLI = siblingLI.previousElementSibling; |
| 45 | + if (!siblingLI) { |
| 46 | + siblingLI = container.lastElementChild; |
| 47 | + } |
| 48 | + } else { |
| 49 | + siblingLI = siblingLI.nextElementSibling; |
| 50 | + if (!siblingLI) { |
| 51 | + siblingLI = container.firstElementChild; |
| 52 | + } |
| 53 | + } |
| 54 | + } while (siblingLI !== focusLI && ( |
| 55 | + siblingLI.classList.contains(classNone) || |
| 56 | + siblingLI.classList.contains(classHeader) |
| 57 | + )); |
| 58 | + |
| 59 | + if (siblingLI) { |
| 60 | + var newFocusA = siblingLI.querySelector('a'); |
| 61 | + return newFocusA; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + var UP = 'Up'; |
| 66 | + var DOWN = 'Down'; |
| 67 | + var LEFT = 'Left'; |
| 68 | + var RIGHT = 'Right'; |
| 69 | + |
| 70 | + var ARROW_UP = 'ArrowUp'; |
| 71 | + var ARROW_DOWN = 'ArrowDown'; |
| 72 | + var ARROW_LEFT = 'ArrowLeft'; |
| 73 | + var ARROW_RIGHT = 'ArrowRight'; |
| 74 | + |
| 75 | + var ARROW_UP_CODE = 38; |
| 76 | + var ARROW_DOWN_CODE = 40; |
| 77 | + var ARROW_LEFT_CODE = 37; |
| 78 | + var ARROW_RIGHT_CODE = 39; |
| 79 | + |
| 80 | + var skipTags = ['INPUT', 'BUTTON', 'TEXTAREA']; |
| 81 | + |
| 82 | + document.addEventListener('keydown', function (e) { |
| 83 | + if ( |
| 84 | + e.ctrlKey || |
| 85 | + e.altKey || |
| 86 | + e.shiftKey || |
| 87 | + e.metaKey || |
| 88 | + skipTags.indexOf(e.target.tagName) >= 0 |
| 89 | + ) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + var newFocusEl; |
| 94 | + if (e.key) { |
| 95 | + switch (e.key) { |
| 96 | + case LEFT: |
| 97 | + case ARROW_LEFT: |
| 98 | + newFocusEl = getFocusableSibling(pathList, true); |
| 99 | + break; |
| 100 | + case RIGHT: |
| 101 | + case ARROW_RIGHT: |
| 102 | + newFocusEl = getFocusableSibling(pathList, false); |
| 103 | + break; |
| 104 | + case UP: |
| 105 | + case ARROW_UP: |
| 106 | + newFocusEl = getFocusableSibling(itemList, true); |
| 107 | + break; |
| 108 | + case DOWN: |
| 109 | + case ARROW_DOWN: |
| 110 | + newFocusEl = getFocusableSibling(itemList, false); |
| 111 | + break; |
| 112 | + } |
| 113 | + } else if (e.keyCode) { |
| 114 | + switch (e.keyCode) { |
| 115 | + case ARROW_LEFT_CODE: |
| 116 | + newFocusEl = getFocusableSibling(pathList, true); |
| 117 | + break; |
| 118 | + case ARROW_RIGHT_CODE: |
| 119 | + newFocusEl = getFocusableSibling(pathList, false); |
| 120 | + break; |
| 121 | + case ARROW_UP_CODE: |
| 122 | + newFocusEl = getFocusableSibling(itemList, true); |
| 123 | + break; |
| 124 | + case ARROW_DOWN_CODE: |
| 125 | + newFocusEl = getFocusableSibling(itemList, false); |
| 126 | + break; |
| 127 | + } |
| 128 | + } |
| 129 | + if (newFocusEl) { |
| 130 | + e.preventDefault(); |
| 131 | + newFocusEl.focus(); |
| 132 | + } |
| 133 | + }); |
| 134 | + } |
| 135 | + |
2 | 136 | function enableDragUpload() {
|
3 | 137 | if (!document.querySelector || !document.addEventListener || !document.body.classList) {
|
4 | 138 | return;
|
|
71 | 205 | return;
|
72 | 206 | }
|
73 | 207 |
|
| 208 | + var selectorNone = '.' + classNone; |
| 209 | + var selectorNotNone = ':not(' + selectorNone + ')'; |
| 210 | + |
74 | 211 | // event handler
|
75 | 212 | var timeoutId;
|
76 | 213 | var lastFilterText = '';
|
|
80 | 217 | return;
|
81 | 218 | }
|
82 | 219 |
|
83 |
| - var itemsSelector = '.item-list > li:not(.header):not(.parent)'; |
| 220 | + var itemsSelector = '.item-list > li:not(.' + classHeader + '):not(.parent)'; |
84 | 221 | var items, i;
|
85 | 222 |
|
86 | 223 | if (!filterText) { // filter cleared, show all items
|
87 |
| - itemsSelector += '.none'; |
| 224 | + itemsSelector += selectorNone; |
88 | 225 | items = document.body.querySelectorAll(itemsSelector);
|
89 | 226 | for (i = items.length - 1; i >= 0; i--) {
|
90 |
| - items[i].classList.remove('none'); |
| 227 | + items[i].classList.remove(classNone); |
91 | 228 | }
|
92 | 229 | } else {
|
93 | 230 | if (filterText.indexOf(lastFilterText) >= 0) { // increment search, find in visible items
|
94 |
| - itemsSelector += ':not(.none)'; |
| 231 | + itemsSelector += selectorNotNone; |
95 | 232 | } else if (lastFilterText.indexOf(filterText) >= 0) { // decrement search, find in hidden items
|
96 |
| - itemsSelector += '.none'; |
| 233 | + itemsSelector += selectorNone; |
97 | 234 | }
|
98 | 235 |
|
99 | 236 | items = document.body.querySelectorAll(itemsSelector);
|
100 | 237 | for (i = items.length - 1; i >= 0; i--) {
|
101 | 238 | var item = items[i];
|
102 | 239 | var name = item.querySelector('.name');
|
103 | 240 | if (name && name.textContent.toLowerCase().indexOf(filterText) < 0) {
|
104 |
| - item.classList.add('none'); |
| 241 | + item.classList.add(classNone); |
105 | 242 | } else {
|
106 |
| - item.classList.remove('none'); |
| 243 | + item.classList.remove(classNone); |
107 | 244 | }
|
108 | 245 | }
|
109 | 246 | }
|
|
205 | 342 | }, false);
|
206 | 343 | }
|
207 | 344 |
|
| 345 | + enableKeyboardNavigate(); |
208 | 346 | enableDragUpload();
|
209 | 347 | enableFilter();
|
210 | 348 | enableNonRefreshDelete();
|
|
0 commit comments