|
95 | 95 | }); |
96 | 96 | } |
97 | 97 |
|
| 98 | + function _withPrevItem(evt, item) { |
| 99 | + evt.preventDefault(); |
| 100 | + var prev = item.previousElementSibling; |
| 101 | + if (prev) { |
| 102 | + item.parentNode.insertBefore(item, prev); |
| 103 | + evt.target.focus(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + function _withNextItem(evt, item) { |
| 108 | + var next = item.nextElementSibling; |
| 109 | + if (next) { |
| 110 | + item.parentNode.insertBefore(next, item); |
| 111 | + evt.target.focus(); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + function _onKeyDown(evt) { |
| 116 | + var item = evt.target.closest(el.children[0].tagName); |
| 117 | + if (item) { |
| 118 | + evt.oldIndex = Array.prototype.indexOf.call(item.parentNode.children, item); |
| 119 | + } |
| 120 | + |
| 121 | + var direction = sortable.options.direction || 'vertical'; |
| 122 | + |
| 123 | + if (direction === 'vertical') { |
| 124 | + if (evt.key === 'ArrowUp' || evt.keyCode === 38) { |
| 125 | + _withPrevItem(evt, item); |
| 126 | + } |
| 127 | + |
| 128 | + if (evt.key === 'ArrowDown' || evt.keyCode === 40) { |
| 129 | + _withNextItem(evt, item); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if (direction === 'horizontal') { |
| 134 | + if (evt.key === 'ArrowLeft' || evt.keyCode === 37) { |
| 135 | + _withPrevItem(evt, item); |
| 136 | + } |
| 137 | + |
| 138 | + if (evt.key === 'ArrowRight' || evt.keyCode === 39) { |
| 139 | + _withNextItem(evt, item); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + if (item) { |
| 144 | + evt.newIndex = Array.prototype.indexOf.call(item.parentNode.children, item); |
| 145 | + } |
| 146 | + |
| 147 | + _emitEvent(evt); |
| 148 | + scope.$apply(); |
| 149 | + } |
98 | 150 |
|
99 | 151 | function _sync(/**Event*/evt) { |
100 | 152 | var items = getSource(); |
|
185 | 237 | }, |
186 | 238 | onSort: function (/**Event*/evt) { |
187 | 239 | _emitEvent(evt); |
188 | | - } |
| 240 | + }, |
189 | 241 | })); |
190 | 242 |
|
| 243 | + el.addEventListener('keydown', function(evt) { |
| 244 | + _onKeyDown.call(sortable, evt); |
| 245 | + }); |
| 246 | + |
191 | 247 | // Create watchers for `options` |
192 | 248 | angular.forEach([ |
193 | 249 | 'sort', 'disabled', 'draggable', 'handle', 'animation', 'group', 'ghostClass', 'filter', |
194 | | - 'onStart', 'onEnd', 'onAdd', 'onUpdate', 'onRemove', 'onSort', 'onMove', 'onClone', 'setData', |
| 250 | + 'onStart', 'onEnd', 'onAdd', 'onUpdate', 'onRemove', 'onSort', 'onMove', 'onClone', 'onKeyDown', 'setData', |
195 | 251 | 'delay', 'animation', 'forceFallback' |
196 | 252 | ], function (name) { |
197 | 253 | watchers.push(scope.$watch('ngSortable.' + name, function (value) { |
|
0 commit comments