ember-sortable 2.4.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/CHANGELOG.md +239 -108
  2. package/MIGRATION_GUIDE_MODIFIERS.md +20 -13
  3. package/MIGRATION_GUIDE_V2.md +20 -8
  4. package/README.md +94 -169
  5. package/RELEASE.md +60 -0
  6. package/V2_MIGRATION_RFC.md +188 -222
  7. package/addon/modifiers/sortable-group.js +69 -58
  8. package/addon/modifiers/sortable-handle.js +22 -19
  9. package/addon/modifiers/sortable-item.js +126 -82
  10. package/addon/services/ember-sortable.js +3 -6
  11. package/addon/system/scroll-parent.js +1 -1
  12. package/addon/utils/coordinate.js +1 -1
  13. package/addon/utils/css-calculation.js +2 -2
  14. package/addon/utils/defaults.js +4 -3
  15. package/addon/utils/keyboard.js +3 -12
  16. package/addon-test-support/helpers/drag.js +3 -11
  17. package/addon-test-support/helpers/index.js +1 -4
  18. package/addon-test-support/helpers/reorder.js +4 -6
  19. package/addon-test-support/utils/keyboard.js +3 -12
  20. package/addon-test-support/utils/offset.js +6 -6
  21. package/app/services/ember-sortable-internal-state.js +1 -0
  22. package/config/environment.js +2 -2
  23. package/index.js +0 -20
  24. package/package.json +56 -33
  25. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -25
  26. package/.github/workflows/github_ci.yml +0 -69
  27. package/addon/components/sortable-group.js +0 -741
  28. package/addon/components/sortable-handle.js +0 -30
  29. package/addon/components/sortable-item.js +0 -770
  30. package/addon/styles/components/sortable-group.css +0 -9
  31. package/addon/templates/components/sortable-group.hbs +0 -16
  32. package/addon/templates/components/sortable-handle.hbs +0 -1
  33. package/addon/templates/components/sortable-item.hbs +0 -2
  34. package/addon/utils/transitionend.js +0 -22
  35. package/app/components/sortable-group.js +0 -3
  36. package/app/components/sortable-handle.js +0 -4
  37. package/app/components/sortable-item.js +0 -3
  38. package/vendor/polyfills/closest.js +0 -21
@@ -1,741 +0,0 @@
1
- import { A } from '@ember/array';
2
- import Component from '@ember/component';
3
- import { set, get, computed, defineProperty } from '@ember/object';
4
- import { next, schedule, scheduleOnce, later } from '@ember/runloop';
5
- import layout from '../templates/components/sortable-group';
6
- import {
7
- isEnterKey,
8
- isSpaceKey,
9
- isEscapeKey,
10
- isDownArrowKey,
11
- isUpArrowKey,
12
- isLeftArrowKey,
13
- isRightArrowKey,
14
- } from '../utils/keyboard';
15
- import { ANNOUNCEMENT_ACTION_TYPES } from '../utils/constant';
16
- import { deprecate } from '@ember/debug';
17
-
18
- const a = A;
19
- const NO_MODEL = {};
20
-
21
- /**
22
- * This component supports re-ordering items in a group via drag-drop and keyboard navigation.
23
- * The component is built with accessibility in mind.
24
- *
25
- * @param {Ember.Object} groupModel The group of models to be rearranged.
26
- * @param {Ember.Array} model The array of models.
27
- * @param {String} a11yItemName A name for each model, used for creating more meaningful a11y announcements.
28
- * @param {Object} a11yAnnouncementConfig A map of action to function to build meaningful a11y announcements.
29
- * @param {String} itemVisualClass A class for styling visual indicators on the yielded `sortable-item`.
30
- * @param {Object} handleVisualClass An object for styling visual indicators on the yielded `sortable-handle` on different `move`.
31
- * @param {Function} [onChange] An optional callback for when position rearrangements are confirmed.
32
- *
33
- * @module drag-drop/draggable-group
34
- * @example
35
- * {{#sortable-group data-test-vertical-demo-group tagName="ol" a11yAnnouncementConfig=a11yAnnouncementConfig a11yItemName="spanish number" itemVisualClass=itemVisualClass handleVisualClass=handleVisualClass onChange=(action "update") model=model.items as |group|}}
36
- * {{#each group.model as |item|}}
37
- * {{#group.item data-test-vertical-demo-item tagName="li" model=item as |groupItem|}}
38
- * {{item}}
39
- * {{#groupItem.handle data-test-vertical-demo-handle class="handle"}}
40
- * <span data-item={{item}}>
41
- * <span>&vArr;</span>
42
- * </span>
43
- * {{/groupItem.handle}}
44
- * {{/group.item}}
45
- * {{/each}}
46
- * {{/sortable-group}}
47
- **/
48
- export default Component.extend({
49
- layout,
50
- tagName: 'ol',
51
-
52
- attributeBindings: ['data-test-selector', 'tabindex', 'role'],
53
-
54
- /**
55
- Called when order of items has been changed
56
- @property onChange
57
- @type Action
58
- @param object group model (omitted if not set)
59
- @param array item models in their new order
60
- @param object item model just dragged
61
- @default null
62
- */
63
- onChange: function() {},
64
-
65
- /**
66
- @property direction
67
- @type string
68
- @default y
69
- */
70
- direction: 'y',
71
-
72
- /**
73
- @property model
74
- @type Any
75
- @default null
76
- */
77
- model: NO_MODEL,
78
-
79
- /**
80
- * @property a group associated with the model
81
- * @type Any
82
- * @default null
83
- */
84
- groupModel: NO_MODEL,
85
-
86
- /**
87
- * @property an object containing different classes for visual indicators
88
- * @type
89
- * @default null
90
- * @example
91
- * {
92
- * UP: 'up'
93
- * DOWN: 'down',
94
- * LEFT: 'left',
95
- * RIGHT: 'right',
96
- * }
97
- */
98
- handleVisualClass: NO_MODEL,
99
-
100
- /**
101
- * @property an object containing functions for producing screen reader announcements
102
- * @type
103
- * @default null
104
- * @example
105
- * {
106
- * MOVE: function() {},
107
- * ACTIVATE: function() {},
108
- * CONFIRM: function() {},
109
- * CANCEL: function() {},
110
- * }
111
- */
112
- a11yAnnouncementConfig: NO_MODEL,
113
-
114
- /** Primary keyboard utils */
115
- // Tracks the currently selected item
116
- _selectedItem: null,
117
- // Tracks the current move
118
- move: null,
119
- // Tracks the status of keyboard reorder mode
120
- isKeyboardReorderModeEnabled: false,
121
- // Tracks if we're still performing a programmatical focus.
122
- isRetainingFocus: false,
123
- /** End of keyboard utils */
124
-
125
- /**
126
- @property items
127
- @type Ember.NativeArray
128
- */
129
- items: computed(() => a()),
130
-
131
- init() {
132
- this._super(...arguments);
133
-
134
- deprecate(
135
- 'The <SortableGroup> component is deprecated. Please use the modifier version {{sortable-group}}.',
136
- false,
137
- {
138
- id: 'ember-sortable:sortable-group-component-deprecated',
139
- until: '3.0.0',
140
- for: 'ember-sortable',
141
- since: {
142
- available: '2.2.6',
143
- enabled: '2.2.6',
144
- },
145
- }
146
- );
147
-
148
- this._setGetterSetters();
149
- this.set('moves', []);
150
- },
151
-
152
- didInsertElement() {
153
- this._super(...arguments);
154
-
155
- // Adding announcer inside an ordered list violates a11y guidelines, so we insert it after our list.
156
- const announcer = this._createAnnouncer();
157
- this.set('announcer', announcer);
158
- this.element.insertAdjacentElement('afterend', announcer);
159
- },
160
-
161
- /**
162
- * Explanation
163
- * 1. `KeyboardReorderMode` is disabled: users can activate it via ENTER or SPACE.
164
- * 2. `KeyboardReorderMode` is enabled: users can reorder via UP or DOWN arrow keys. TODO: Expand to more keys, e.g LEFT, RIGHT
165
- * 3. `KeyboardReorderMode` is enabled: users can finalize/save the reordering via ENTER or SPACE.
166
- * 4. `KeyboardReorderMode` is enabled: users can discard the reordering via ESC.
167
- *
168
- * @param {Event} evt a DOM event
169
- */
170
- keyDown(event) {
171
- if (!this.get('isKeyDownEnabled')) {
172
- return;
173
- }
174
- // Note: If handle is specified, we need to target the keyDown on the handle
175
- const isKeyboardReorderModeEnabled = this.get('isKeyboardReorderModeEnabled');
176
- const _selectedItem = this.get('_selectedItem');
177
-
178
- if (!isKeyboardReorderModeEnabled && (isEnterKey(event) || isSpaceKey(event))) {
179
- this.prepareKeyboardReorderMode();
180
- this._announceAction(ANNOUNCEMENT_ACTION_TYPES.ACTIVATE);
181
- this._updateItemVisualIndicators(_selectedItem, true);
182
- this._updateHandleVisualIndicators(_selectedItem, true);
183
-
184
- this.set('isRetainingFocus', true);
185
-
186
- scheduleOnce('render', () => {
187
- this.element.focus();
188
- this.set('isRetainingFocus', false);
189
- });
190
-
191
- // Prevent the default scroll
192
- event.preventDefault();
193
- return;
194
- }
195
-
196
- if (isKeyboardReorderModeEnabled) {
197
- this._handleKeyboardReorder(event);
198
- event.preventDefault();
199
- }
200
- },
201
-
202
- /**
203
- * Make sure that we cancel any ongoing keyboard operation when the focus is lost from the handle.
204
- * Because this can be fired pre-maturely, effectively cancelling before other keyboard operations,
205
- * we need to wait until other operations are completed, so this will cancel properly.
206
- *
207
- * @param {Event} event a DOM event.
208
- */
209
- focusOut() {
210
- if (!this.get('isRetainingFocus') && !this._isElementWithinHandle(document.activeElement)) {
211
- this.cancelKeyboardSelection();
212
- }
213
- },
214
-
215
- prepareKeyboardReorderMode() {
216
- this._enableKeyboardReorderMode();
217
- this._setupA11yApplicationContainer();
218
- },
219
-
220
- /**
221
- * Moves the item to its new position and adds the move to our history.
222
- *
223
- * @param {Object} item the item to be moved.
224
- * @param {Integer} delta how much to move index-wise.
225
- */
226
- moveItem(item, delta) {
227
- const { sortedItems, moves } = this.getProperties('sortedItems', 'moves');
228
- const sortedIndex = sortedItems.indexOf(item);
229
- const newSortedIndex = sortedIndex + delta;
230
-
231
- // If out of bounds, we don't do anything.
232
- if (newSortedIndex < 0 || newSortedIndex >= sortedItems.length) {
233
- return;
234
- }
235
- this._announceAction(ANNOUNCEMENT_ACTION_TYPES.MOVE, delta);
236
- // Guarantees that the before the UI is fully rendered before we move again.
237
- scheduleOnce('render', () => {
238
- this._move(sortedIndex, newSortedIndex);
239
- this._updateHandleVisualIndicators(item, true);
240
-
241
- moves.push([sortedIndex, newSortedIndex]);
242
- });
243
- },
244
-
245
- /**
246
- * Handles all the necessary operations needed for cancelling the ccurrent keyboard selection.
247
- * 1. Disables keyboard reorder mode.
248
- * 2. Undo all of the tracked moves.
249
- * 3. Tears down the application container, so we are not focus locked within the application.
250
- * 4. Resets the current selected item.
251
- */
252
- cancelKeyboardSelection() {
253
- const _selectedItem = this.get('_selectedItem');
254
- this._disableKeyboardReorderMode();
255
- // Revert the process by reversing the move.
256
- const moves = this.get('moves');
257
- while (moves.length > 0) {
258
- const move = moves.pop();
259
- this._move(move[1], move[0])
260
- }
261
- this._tearDownA11yApplicationContainer();
262
- this._updateItemVisualIndicators(_selectedItem, false);
263
- this._updateHandleVisualIndicators(_selectedItem, false);
264
- this._resetItemSelection();
265
- },
266
-
267
- /**
268
- * Handles all th necessary operations needed for confirming the current keyboard selection.
269
- * 1. Disables keyboard reorder mode.
270
- * 2. Tears down the application container, so we are not focus locked within the container.
271
- * 3. Make sure to update and sync all the internal items and UI.
272
- * 4. Triggers the `onChange` action if provided.
273
- * 5. Resets the currently selected item.
274
- */
275
- confirmKeyboardSelection() {
276
- const items = this.get('sortedItems');
277
- const groupModel = this.get('groupModel');
278
- const _selectedItem = this.get('_selectedItem');
279
- const selectedModel = _selectedItem.get('model');
280
- const itemModels = items.mapBy('model');
281
- this.set('moves', []);
282
- this._disableKeyboardReorderMode();
283
- this._tearDownA11yApplicationContainer();
284
- this._updateItems();
285
- if (groupModel !== NO_MODEL) {
286
- this.onChange(groupModel, itemModels, selectedModel)
287
- } else {
288
- this.onChange(itemModels, selectedModel);
289
- }
290
- this._updateItemVisualIndicators(_selectedItem, false);
291
- this._updateHandleVisualIndicators(_selectedItem, false);
292
- this._resetItemSelection();
293
- },
294
-
295
- /**
296
- * Enables keyboard navigation
297
- */
298
- _activateKeyDown(){
299
- this.set('isKeyDownEnabled', true);
300
- },
301
-
302
- /**
303
- * Disables keyboard navigation
304
- * Currently used to handle keydown events bubbling up from
305
- * elements that aren't meant to invoke keyboard navigation
306
- * by ignoring them.
307
- */
308
- _deactivateKeyDown() {
309
- this.set('isKeyDownEnabled', false);
310
- },
311
-
312
- /**
313
- Register an item with this group.
314
- @method registerItem
315
- @param {SortableItem} [item]
316
- */
317
- _registerItem(item) {
318
- this.get('items').addObject(item);
319
- },
320
-
321
- /**
322
- De-register an item with this group.
323
- @method deregisterItem
324
- @param {SortableItem} [item]
325
- */
326
- _deregisterItem(item) {
327
- this.get('items').removeObject(item);
328
- },
329
-
330
- _setSelectedItem(item) {
331
- this.set('_selectedItem', item);
332
- },
333
-
334
- /**
335
- Prepare for sorting.
336
- Main purpose is to stash the current itemPosition so
337
- we don’t incur expensive re-layouts.
338
- @method _=repare
339
- */
340
- _prepare() {
341
- this._itemPosition = this.get('itemPosition');
342
- },
343
-
344
- /**
345
- Update item positions (relatively to the first element position).
346
- @method update
347
- */
348
- _update() {
349
- let sortedItems = this.get('sortedItems');
350
- // Position of the first element
351
- let position = this._itemPosition;
352
-
353
- // Just in case we haven’t called prepare first.
354
- if (position === undefined) {
355
- position = this.get('itemPosition');
356
- }
357
-
358
- sortedItems.forEach(item => {
359
- let dimension;
360
- let direction = this.get('direction');
361
-
362
- if (!get(item, 'isDragging')) {
363
- set(item, direction, position);
364
- }
365
-
366
- // add additional spacing around active element
367
- if (get(item, 'isBusy')) {
368
- position += get(item, 'spacing') * 2;
369
- }
370
-
371
- if (direction === 'x') {
372
- dimension = 'width';
373
- }
374
- if (direction === 'y') {
375
- dimension = 'height';
376
- }
377
-
378
- position += get(item, dimension);
379
- });
380
- },
381
-
382
- /**
383
- @method _commit
384
- */
385
- _commit() {
386
- const items = this.get('sortedItems');
387
- const groupModel = this.get('groupModel');
388
- const itemModels = items.mapBy('model');
389
- const draggedItem = items.findBy('wasDropped', true);
390
- let draggedModel;
391
-
392
- if (draggedItem) {
393
- set(draggedItem, 'wasDropped', false); // Reset
394
- draggedModel = get(draggedItem, 'model');
395
- }
396
-
397
- this._updateItems();
398
- if (groupModel !== NO_MODEL) {
399
- this.onChange(groupModel, itemModels, draggedModel);
400
- } else {
401
- this.onChange(itemModels, draggedModel);
402
- }
403
- },
404
-
405
- /**
406
- * Keeps the UI in sync with actual changes.
407
- * Needed for drag and keyboard operations.
408
- */
409
- _updateItems() {
410
- const items = this.get('sortedItems');
411
-
412
- delete this._itemPosition;
413
-
414
- schedule('render', () => {
415
- items.invoke('freeze');
416
- });
417
-
418
- schedule('afterRender', () => {
419
- items.invoke('reset');
420
- });
421
-
422
- next(() => {
423
- schedule('render', () => {
424
- items.invoke('thaw');
425
- });
426
- });
427
- },
428
-
429
- /**
430
- * Moves an sortedItem from one index to another index, effectively performing an reorder.
431
- *
432
- * @param {Integer} fromIndex the original index
433
- * @param {Integer} toIndex the new index
434
- */
435
- _move(fromIndex, toIndex) {
436
- const { direction, sortedItems } = this.getProperties('direction', 'sortedItems');
437
- const item = sortedItems[fromIndex];
438
- const nextItem = sortedItems[toIndex];
439
-
440
- // switch direction values to notify sortedItems to update, so it sorts by direction.
441
- let value;
442
- const dimension = direction === "y" ? "height" : "width";
443
- // DOWN or RIGHT
444
- if (toIndex > fromIndex) {
445
- value = item.get(direction);
446
- item.set(direction, nextItem.get(direction) + (nextItem.get(dimension) - item.get(dimension)));
447
- nextItem.set(direction, value);
448
- // UP or LEFT
449
- } else {
450
- value = nextItem.get(direction);
451
- nextItem.set(direction, item.get(direction) + (item.get(dimension) - nextItem.get(dimension)));
452
- item.set(direction, value);
453
- }
454
- },
455
-
456
- /**
457
- * Handles all of the keyboard operations, such as
458
- * 1. Keyboard navigation for UP, DOWN, LEFT, RIGHT
459
- * 2. Confirming reorder
460
- * 3. Discard reorder
461
- * 4. Also handles refocusing the element that triggered the interaction.
462
- *
463
- * @param {Event} event a DOM event.
464
- */
465
- _handleKeyboardReorder(event) {
466
- let { direction, _selectedItem } = this.getProperties('direction', '_selectedItem');
467
-
468
- if (direction === "y" && isDownArrowKey(event)) {
469
- this.moveItem(_selectedItem, 1);
470
- } else if (direction === "y" && isUpArrowKey(event)) {
471
- this.moveItem(_selectedItem, -1);
472
- } else if (direction === "x" && isLeftArrowKey(event)) {
473
- this.moveItem(_selectedItem, -1);
474
- } else if (direction === "x" && isRightArrowKey(event)) {
475
- this.moveItem(_selectedItem, 1);
476
- } else if (isEnterKey(event) || isSpaceKey(event)) {
477
- // confirm will reset the _selectedItem, so caching it here before we remove it.
478
- const itemElement = this.get('_selectedItem.element');
479
- this._announceAction(ANNOUNCEMENT_ACTION_TYPES.CONFIRM);
480
- this.confirmKeyboardSelection();
481
-
482
- this.set('isRetainingFocus', true);
483
- scheduleOnce('render', () => this._focusItem(itemElement));
484
- } else if (isEscapeKey(event)) {
485
- // cancel will reset the _selectedItem, so caching it here before we remove it.
486
- const _selectedItemElement = this.get('_selectedItem.element');
487
- this._announceAction(ANNOUNCEMENT_ACTION_TYPES.CANCEL);
488
- this.cancelKeyboardSelection();
489
-
490
- this.set('isRetainingFocus', true);
491
- scheduleOnce('render', () => {
492
- const moves = this.get('moves');
493
- if (moves && moves.length > 0) {
494
- const sortedItems = this.get('sortedItems');
495
- const itemElement = sortedItems[moves[0].fromIndex].element
496
- this._focusItem(itemElement);
497
- } else {
498
- this._focusItem(_selectedItemElement);
499
- }
500
- this.set('isRetainingFocus', false);
501
- });
502
- }
503
- },
504
-
505
- /**
506
- * Sets focus on the current item or its handle.
507
- *
508
- * @param {Elment} itemElement an DOM element representing an sortable-item.
509
- */
510
- _focusItem(itemElement) {
511
- const handle = itemElement.querySelector('[data-sortable-handle]');
512
- if (handle) {
513
- handle.focus();
514
- } else {
515
- // The consumer did not use a handle, so we set focus back to the item.
516
- itemElement.focus();
517
- }
518
- },
519
-
520
- /**
521
- * Enables keyboard reorder mode.
522
- */
523
- _enableKeyboardReorderMode() {
524
- this.set('isKeyboardReorderModeEnabled', true);
525
- },
526
-
527
- /**
528
- * Disables keyboard reorder mode
529
- */
530
- _disableKeyboardReorderMode() {
531
- this.set('isKeyboardReorderModeEnabled', false);
532
- },
533
-
534
- /**
535
- * Sets up the group as an application and make it programmatically focusable.
536
- */
537
- _setupA11yApplicationContainer() {
538
- this.setProperties({
539
- role: 'application',
540
- tabindex: -1,
541
- });
542
- },
543
-
544
- /**
545
- * Creates a `visually-hidden` `aria-live` announcement region.
546
- */
547
- _createAnnouncer() {
548
- const announcer = document.createElement('span');
549
- announcer.setAttribute('aria-live', 'polite');
550
- announcer.classList.add('visually-hidden');
551
- return announcer;
552
- },
553
-
554
- /**
555
- * Announces the message constructed from `a11yAnnouncementConfig`.
556
- *
557
- * @param {String} type the action type.
558
- * @param {Number} delta how much distance (item-wise) is being moved.
559
- */
560
- _announceAction(type, delta = null) {
561
- const a11yAnnouncementConfig = this.get('a11yAnnouncementConfig');
562
- const a11yItemName = this.get('a11yItemName');
563
-
564
- if (a11yAnnouncementConfig === NO_MODEL || !a11yItemName || !(type in a11yAnnouncementConfig)) {
565
- return;
566
- }
567
-
568
- const sortedItems = this.get('sortedItems');
569
- const _selectedItem = this.get('_selectedItem');
570
- const index = sortedItems.indexOf(_selectedItem);
571
- const announcer = this.get('announcer');
572
-
573
- const config = {
574
- a11yItemName,
575
- index: index,
576
- maxLength : sortedItems.length,
577
- direction: this.get('direction'),
578
- delta,
579
- }
580
-
581
- const message = a11yAnnouncementConfig[type](config);
582
- announcer.textContent = message;
583
-
584
- // Reset the message after the message is announced.
585
- later(() => {
586
- announcer.textContent = '';
587
- }, 1000);
588
- },
589
-
590
- /**
591
- * Tears down the `role=application` container.
592
- */
593
- _tearDownA11yApplicationContainer() {
594
- this.setProperties({
595
- role: undefined,
596
- tabindex: undefined,
597
- });
598
- },
599
-
600
- /**
601
- * Reset the selected item.
602
- */
603
- _resetItemSelection() {
604
- this.set('_selectedItem', null);
605
- },
606
-
607
- /**
608
- * Checks if the given element is a descedant of a handle.
609
- *
610
- * @param {Element} element a DOM element.
611
- */
612
- _isElementWithinHandle(element) {
613
- return element.closest(
614
- `#${this.element.id} [data-sortable-handle]`
615
- );
616
- },
617
-
618
- /**
619
- * Updates the selected item's visual indicators.
620
- *
621
- * @param {Object} item the selected item.
622
- * @param {Boolean} isActive to activate or deactivate the class.
623
- */
624
- _updateItemVisualIndicators(item, isActive) {
625
- const itemVisualClass = this.get('itemVisualClass');
626
-
627
- if (!itemVisualClass || !item) {
628
- return;
629
- }
630
-
631
- if (isActive) {
632
- item.element.classList.add(itemVisualClass);
633
- } else {
634
- item.element.classList.remove(itemVisualClass);
635
- }
636
- },
637
-
638
- /**
639
- * Updates the selected item's handle's visual indicators
640
- *
641
- * @param {Object} item the selected item.
642
- * @param {Boolean} isUpdate to update or not update.
643
- */
644
- _updateHandleVisualIndicators(item, isUpdate) {
645
- const handleVisualClass = this.get('handleVisualClass');
646
-
647
- if (handleVisualClass === NO_MODEL || !item) {
648
- return;
649
- }
650
-
651
- const sortedItems = this.get('sortedItems');
652
- const direction = this.get('direction');
653
- const index = sortedItems.indexOf(item);
654
- const handle = item.element.querySelector('[data-sortable-handle');
655
- const visualHandle = handle ? handle : item.element;
656
- const visualKeys = direction === 'y' ? ['UP', 'DOWN'] : ['LEFT', 'RIGHT'];
657
-
658
- visualKeys.forEach(visualKey => {
659
- visualHandle.classList.remove(handleVisualClass[visualKey]);
660
- });
661
-
662
- if (!isUpdate) {
663
- return;
664
- }
665
-
666
- if (index > 0) {
667
- visualHandle.classList.add(handleVisualClass[visualKeys[0]]);
668
- }
669
-
670
- if (index < sortedItems.length - 1) {
671
- visualHandle.classList.add(handleVisualClass[visualKeys[1]]);
672
- }
673
- },
674
-
675
- /**
676
- * Defining getters and setters to support native getter and setters until we decide to drop support for ember versions below 3.10
677
- */
678
- _setGetterSetters() {
679
- /**
680
- Position for the first item.
681
- If spacing is present, first item's position will have to change as well.
682
- @property itemPosition
683
- @type Number
684
- */
685
- defineProperty(this, 'itemPosition', {
686
- get() {
687
- const direction = this.get('direction');
688
-
689
- return this.get(`sortedItems.firstObject.${direction}`) - this.get('sortedItems.firstObject.spacing');
690
- }
691
- });
692
-
693
- /**
694
- An array of DOM elements.
695
- @property sortedItems
696
- @type Array
697
- */
698
- defineProperty(this, 'sortedItems', {
699
- get() {
700
- const items = a(this.get('items'));
701
- const direction = this.get('direction');
702
-
703
- return a(items.sortBy(direction));
704
- }
705
- })
706
- },
707
-
708
- actions: {
709
- activateKeyDown(){
710
- this._activateKeyDown();
711
- },
712
-
713
- deactivateKeyDown() {
714
- this._deactivateKeyDown();
715
- },
716
-
717
- registerItem(item) {
718
- this._registerItem(item);
719
- },
720
-
721
- deregisterItem(item) {
722
- this._deregisterItem(item);
723
- },
724
-
725
- setSelectedItem(item) {
726
- this._setSelectedItem(item);
727
- },
728
-
729
- prepare() {
730
- this._prepare();
731
- },
732
-
733
- update() {
734
- this._update();
735
- },
736
-
737
- commit() {
738
- this._commit();
739
- },
740
- }
741
- });