Skip to content

Commit 7a57481

Browse files
Remove prototype extensions and Ember.A usage (#103)
1 parent 8c61359 commit 7a57481

6 files changed

Lines changed: 176 additions & 111 deletions

File tree

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ Here's the reference implementation of the `dragEndAction` action:
143143
dragEndAction ({sourceList, sourceIndex, targetList, targetIndex/* , sourceArgs, targetArgs */}) {
144144
if (sourceList === targetList && sourceIndex === targetIndex) return
145145

146-
const item = sourceList.objectAt(sourceIndex)
146+
const item = sourceList[sourceIndex]
147147

148-
sourceList.removeAt(sourceIndex)
149-
targetList.insertAt(targetIndex, item)
148+
sourceList.splice(sourceIndex, 1)
149+
targetList.splice(targetIndex, 0, item)
150150
}
151151
```
152152

@@ -236,10 +236,10 @@ Or do it by hand:
236236
```js
237237
@action
238238
determineForeignPosition ({draggedItem, items}) {
239-
return Ember.A(items.slice()) // make sure not to mutate the list; `Ember.A()` is typically redundant
240-
.addObject(draggedItem)
241-
.sortBy('name')
242-
.indexOf(draggedItem)
239+
const itemsCopy = items.slice(); // make sure not to mutate the list
240+
itemsCopy.push(draggedItem);
241+
itemsCopy.sort((a, b) => a.name.localeCompare(b.name));
242+
return itemsCopy.indexOf(draggedItem);
243243
}
244244
```
245245

@@ -294,9 +294,9 @@ Now you can access the parent of both source and target lists in the `dragEndAct
294294
dragEndAction({ sourceList, sourceIndex, sourceArgs, targetList, targetIndex, targetArgs }) {
295295
if (sourceModel === targetModel && sourceIndex === targetIndex) return;
296296

297-
const item = sourceList.objectAt(sourceIndex);
298-
sourceList.removeAt(sourceIndex);
299-
targetList.insertAt(targetIndex, item);
297+
const item = sourceList[sourceIndex];
298+
sourceList.splice(sourceIndex, 1);
299+
targetList.splice(targetIndex, 0, item);
300300

301301
// Access the parent via `sourceArgs` and `targetArgs`
302302
sourceArgs.parent.save();
@@ -600,12 +600,12 @@ export default create({
600600
});
601601
```
602602

603-
In a test, list items are available as `sortableList.items`. Item content is available as `sortableList.items.objectAt(index)`.
603+
In a test, list items are available as `sortableList.items`. Item content is available as `sortableList.items[index]`.
604604

605605
For example, to assert the title of the first item in a list, using the page object from the last example, you can do this:
606606

607607
```js
608-
assert.strictEqual(sortableList.items.objectAt(0).item.content.title, "Foo");
608+
assert.strictEqual(sortableList.items[0].item.content.title, "Foo");
609609
```
610610

611611
#### Providing the drag handle selector
@@ -660,7 +660,7 @@ test('sorting a list', async function (assert) {
660660

661661
expectedTitles.forEach((expectedTitle, k) => {
662662
m = `List #0 item #${k} content title`
663-
expect(list.items.objectAt(k).item.title, m).equal(expectedTitle)
663+
expect(list.items[k].item.title, m).equal(expectedTitle)
664664
})
665665
}))
666666
```

addon/components/drag-sort-list.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import Component from '@glimmer/component';
22
import { action } from '@ember/object';
33
import { service } from '@ember/service';
4-
import { A } from '@ember/array';
54
import type DragSort from 'ember-drag-sort/services/drag-sort';
65

76
interface DragSortListSignature<Item extends object> {
@@ -159,17 +158,19 @@ export default class DragSortList<Item extends object> extends Component<
159158
const itemsNodeList = (event.currentTarget as HTMLElement).querySelectorAll(
160159
'.dragSortItem',
161160
);
162-
const draggableItems = A<HTMLElement>(
163-
Array.prototype.slice.call(itemsNodeList),
161+
const draggableItems = Array.from(itemsNodeList) as HTMLElement[];
162+
const positions = draggableItems.map((draggableItem: HTMLElement) =>
163+
draggableItem.getBoundingClientRect(),
164164
);
165-
const positions = A(
166-
draggableItems.map((draggableItem) =>
167-
draggableItem.getBoundingClientRect(),
168-
),
169-
);
170-
const rows = positions.uniqBy('top').mapBy('top').sort();
171-
const currentRowPosition = rows.filter((row) => row < event.clientY).pop();
172-
const closestItem = positions.filterBy('top', currentRowPosition).pop();
165+
const tops = positions.map((pos: DOMRect) => pos.top);
166+
const uniqueTops = [...new Set(tops)];
167+
const rows = uniqueTops.sort();
168+
const currentRowPosition = rows
169+
.filter((row: number) => row < event.clientY)
170+
.pop();
171+
const closestItem = positions
172+
.filter((pos: DOMRect) => pos.top === currentRowPosition)
173+
.pop();
173174

174175
return closestItem ? positions.indexOf(closestItem) : 0;
175176
}

tests/acceptance/index-test.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ function assertListItems(list, expectedTitles, assert, message) {
1010
// List with disabled sorting
1111
expectedTitles.forEach((expectedTitle, i) => {
1212
const m = `${message}: List #0 item #${i} content title`;
13-
assert.strictEqual(
14-
list.items.objectAt(i).item.content.title,
15-
expectedTitle,
16-
m,
17-
);
13+
assert.strictEqual(list.items[i].item.content.title, expectedTitle, m);
1814
});
1915
}
2016

@@ -95,9 +91,9 @@ module('Acceptance | index', function (hooks) {
9591
test('sorting from an unsortable list to a sortable list, and then back into an unsortable list, should not change the position', async function (assert) {
9692
await page.visit();
9793

98-
const item0_0 = page.foreign1.items.objectAt(0).item;
99-
const item0_3 = page.foreign1.items.objectAt(3).item;
100-
const item1_0 = page.foreign2.items.objectAt(0).item;
94+
const item0_0 = page.foreign1.items[0].item;
95+
const item0_3 = page.foreign1.items[3].item;
96+
const item1_0 = page.foreign2.items[0].item;
10197

10298
await item0_3.dragStart();
10399
await page.foreign2.dragEnter();
@@ -120,8 +116,8 @@ module('Acceptance | index', function (hooks) {
120116
test('sorting from an sortable list to an unsortable list should position alphabetically', async function (assert) {
121117
await page.visit();
122118

123-
const item0_0 = page.foreign1.items.objectAt(0).item;
124-
let item1_0 = page.foreign2.items.objectAt(0).item;
119+
const item0_0 = page.foreign1.items[0].item;
120+
let item1_0 = page.foreign2.items[0].item;
125121

126122
await item1_0.dragStart();
127123
await page.foreign1.dragEnter();
@@ -138,7 +134,7 @@ module('Acceptance | index', function (hooks) {
138134
);
139135
assertListItems(page.foreign2, ['Lol'], assert, 'Foreign 2');
140136

141-
item1_0 = page.foreign2.items.objectAt(0).item;
137+
item1_0 = page.foreign2.items[0].item;
142138

143139
await item1_0.dragStart();
144140
await page.foreign1.dragEnter();

0 commit comments

Comments
 (0)