Skip to content

Commit 8feb7e8

Browse files
Clean coding
1 parent 742b19c commit 8feb7e8

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

src/vuedraggable.js

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,25 @@ import { isReadOnlyEvent } from "./core/sortableEvents";
1111

1212
import { h, defineComponent, nextTick, resolveComponent } from "vue";
1313

14-
function computeVmIndex(vnodes, element, mainNode) {
15-
const index = vnodes.map(({ el }) => el).indexOf(element);
14+
function computeVmIndex(vNodes, element) {
15+
const domElements = vNodes.map(({ el }) => el);
16+
const index = domElements.indexOf(element);
1617
if (index === -1) {
1718
throw new Error("node not found", {
18-
nodes: vnodes.map(({ el }) => el),
19+
nodes: domElements,
1920
element,
20-
index,
21-
mainNode
2221
});
2322
}
2423
return index;
2524
}
2625

27-
function computeIndexes(slots, children, isTransition, footerOffset) {
28-
if (!slots) {
29-
return [];
30-
}
31-
32-
const elmFromNodes = slots.map(({ el }) => el);
33-
const footerIndex = children.length - footerOffset;
34-
const rawIndexes = [...children].map((elt, idx) =>
35-
idx >= footerIndex ? elmFromNodes.length : elmFromNodes.indexOf(elt)
26+
function computeIndexes(vNodes, domChildren, isTransition, footerOffset) {
27+
const domChildrenFromNodes = vNodes.map(({ el }) => el);
28+
const footerIndex = domChildren.length - footerOffset;
29+
const rawIndexes = [...domChildren].map((elt, idx) =>
30+
idx >= footerIndex
31+
? domChildrenFromNodes.length
32+
: domChildrenFromNodes.indexOf(elt)
3633
);
3734
return isTransition ? rawIndexes.filter(ind => ind !== -1) : rawIndexes;
3835
}
@@ -281,11 +278,7 @@ const draggableComponent = defineComponent({
281278
},
282279

283280
getUnderlyingVm(htmlElt) {
284-
const index = computeVmIndex(
285-
this.getChildrenNodes() || [],
286-
htmlElt,
287-
this.mainNode
288-
);
281+
const index = computeVmIndex(this.getChildrenNodes() || [], htmlElt);
289282
if (index === -1) {
290283
//Edge case during move callback: related element might be
291284
//an element different from collection
@@ -343,7 +336,7 @@ const draggableComponent = defineComponent({
343336
return context;
344337
},
345338

346-
getVmIndex(domIndex) {
339+
getVmIndexFromDomIndex(domIndex) {
347340
const indexes = this.visibleIndexes;
348341
const numberIndexes = indexes.length;
349342
return domIndex > numberIndexes - 1 ? numberIndexes : indexes[domIndex];
@@ -365,7 +358,7 @@ const draggableComponent = defineComponent({
365358
return;
366359
}
367360
removeNode(evt.item);
368-
const newIndex = this.getVmIndex(evt.newIndex);
361+
const newIndex = this.getVmIndexFromDomIndex(evt.newIndex);
369362
this.spliceList(newIndex, 0, element);
370363
this.computeIndexes();
371364
const added = { element, newIndex };
@@ -388,7 +381,7 @@ const draggableComponent = defineComponent({
388381
removeNode(evt.item);
389382
insertNodeAt(evt.from, evt.item, evt.oldIndex);
390383
const oldIndex = this.context.index;
391-
const newIndex = this.getVmIndex(evt.newIndex);
384+
const newIndex = this.getVmIndexFromDomIndex(evt.newIndex);
392385
this.updatePosition(oldIndex, newIndex);
393386
const moved = { element: this.context.element, oldIndex, newIndex };
394387
this.emitChanges({ moved });
@@ -401,8 +394,10 @@ const draggableComponent = defineComponent({
401394
const domChildren = [...evt.to.children].filter(
402395
el => el.style["display"] !== "none"
403396
);
404-
const currentDOMIndex = domChildren.indexOf(evt.related);
405-
const currentIndex = relatedContext.component.getVmIndex(currentDOMIndex);
397+
const currentDomIndex = domChildren.indexOf(evt.related);
398+
const currentIndex = relatedContext.component.getVmIndexFromDomIndex(
399+
currentDomIndex
400+
);
406401
const draggedInList = domChildren.indexOf(draggingElement) !== -1;
407402
return draggedInList || !evt.willInsertAfter
408403
? currentIndex

0 commit comments

Comments
 (0)