Skip to content

Commit

Permalink
Merge pull request #156 from joankaradimov/use-is-equal-util
Browse files Browse the repository at this point in the history
Use Ember's util/isEqual in sortable components
  • Loading branch information
dgavey authored Nov 7, 2019
2 parents 5b9f259 + 4edfb2c commit 576e840
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions addon/services/drag-coordinator.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import Service from '@ember/service';
import { alias } from '@ember/object/computed';
import { A } from '@ember/array';
import { isEqual } from '@ember/utils';


function indexOf(items, a) {
return items.findIndex(function (element) {
return isEqual(element, a);
});
}

function swapInPlace(items, a, b) {
const aPos = items.indexOf(a);
const bPos = items.indexOf(b);
const aPos = indexOf(items, a);
const bPos = indexOf(items, b);

items.replace(aPos, 1, [ b ]);
items.replace(bPos, 1, [ a ]);
}

function shiftInPlace(items, a, b) {
const aPos = items.indexOf(a);
const bPos = items.indexOf(b);
const aPos = indexOf(items, a);
const bPos = indexOf(items, b);

items.removeAt(aPos);
items.insertAt(bPos, a);
Expand Down Expand Up @@ -104,12 +112,12 @@ export default Service.extend({
moveObjectPositions(a, b, sortComponents) {
const aSortable = sortComponents.find((component) => {
return component.get('sortableObjectList').find((sortable) => {
return sortable === a;
return isEqual(sortable, a);
});
});
const bSortable = sortComponents.find((component) => {
return component.get('sortableObjectList').find((sortable) => {
return sortable === b;
return isEqual(sortable, b);
});
});
const swap = aSortable === bSortable;
Expand Down Expand Up @@ -138,7 +146,7 @@ export default Service.extend({

// Remove from aList and insert into bList
aList.removeObject(a);
bList.insertAt(bList.indexOf(b), a);
bList.insertAt(indexOf(bList, b), a);
}
},

Expand Down

0 comments on commit 576e840

Please sign in to comment.