Skip to content

Commit 709b801

Browse files
Continuing refactoring
1 parent d268d23 commit 709b801

File tree

3 files changed

+52
-87
lines changed

3 files changed

+52
-87
lines changed

src/core/componentStructure.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { nextTick } from "vue";
12
import { isTransition as isTransitionName } from "../util/tags";
23

34
const getHtmlElementFromNode = ({ el }) => el;
@@ -40,6 +41,18 @@ class ComponentStructure {
4041
this._checkCoherence();
4142
}
4243

44+
setHtmlRoot($el) {
45+
if (!$el) {
46+
return;
47+
}
48+
this.$el = $el;
49+
this.rootContainer = getRootContainer(this);
50+
nextTick(() => {
51+
this.visibleIndexes = this._computeIndexes();
52+
});
53+
return this;
54+
}
55+
4356
get _domChildrenFromNodes() {
4457
return this._getChildrenNodes().map(getHtmlElementFromNode);
4558
}
@@ -79,7 +92,7 @@ class ComponentStructure {
7992
.filter(node => !!node.transition);
8093
}
8194

82-
computeIndexes() {
95+
_computeIndexes() {
8396
const {
8497
_domChildrenFromNodes,
8598
transitionMode,
@@ -96,17 +109,16 @@ class ComponentStructure {
96109
return transitionMode ? rawIndexes.filter(ind => ind !== -1) : rawIndexes;
97110
}
98111

99-
setHtmlRoot($el) {
100-
if (!$el) {
101-
return;
102-
}
103-
this.$el = $el;
104-
this.rootContainer = getRootContainer(this);
105-
return this;
112+
computeVmIndex(domElement) {
113+
return this._domChildrenFromNodes.indexOf(domElement);
106114
}
107115

108-
computeVmIndex(htmlElement) {
109-
return this._domChildrenFromNodes.indexOf(htmlElement);
116+
getVmIndexFromDomIndex(domIndex) {
117+
const { visibleIndexes } = this;
118+
const numberIndexes = visibleIndexes.length;
119+
return domIndex > numberIndexes - 1
120+
? numberIndexes
121+
: visibleIndexes[domIndex];
110122
}
111123
}
112124

src/vuedraggable.js

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,9 @@ const draggableComponent = defineComponent({
9898
manage: event => manage.call(this, event)
9999
}
100100
});
101-
const {
102-
componentStructure: { rootContainer }
103-
} = this;
101+
const { rootContainer } = componentStructure;
104102
this._sortable = new Sortable(rootContainer, sortableOptions);
105103
rootContainer.__draggable_component__ = this;
106-
this.computeIndexes();
107104
},
108105

109106
beforeUnmount() {
@@ -112,42 +109,26 @@ const draggableComponent = defineComponent({
112109

113110
computed: {
114111
realList() {
115-
return this.list ? this.list : this.modelValue;
112+
const { list } = this;
113+
return list ? list : this.modelValue;
116114
}
117115
},
118116

119117
watch: {
120118
$attrs: {
121119
handler(newOptionValue) {
122-
this.updateOptions(newOptionValue);
123-
},
124-
deep: true
125-
},
126-
127-
realList: {
128-
handler() {
129-
this.computeIndexes();
120+
const { _sortable } = this;
121+
getValidSortableEntries(newOptionValue).forEach(([key, value]) => {
122+
_sortable.option(key, value);
123+
});
130124
},
131125
deep: true
132126
}
133127
},
134128

135129
methods: {
136-
updateOptions(newOptionValue) {
137-
const { _sortable } = this;
138-
getValidSortableEntries(newOptionValue).forEach(([key, value]) => {
139-
_sortable.option(key, value);
140-
});
141-
},
142-
143-
computeIndexes() {
144-
nextTick(() => {
145-
this.visibleIndexes = this.componentStructure.computeIndexes();
146-
});
147-
},
148-
149-
getUnderlyingVm(htmlElement) {
150-
const index = this.componentStructure.computeVmIndex(htmlElement);
130+
getUnderlyingVm(domElement) {
131+
const index = this.componentStructure.computeVmIndex(domElement);
151132
if (index === -1) {
152133
//Edge case during move callback: related element might be
153134
//an element different from collection
@@ -163,9 +144,7 @@ const draggableComponent = defineComponent({
163144
},
164145

165146
emitChanges(evt) {
166-
nextTick(() => {
167-
this.$emit("change", evt);
168-
});
147+
nextTick(() => this.$emit("change", evt));
169148
},
170149

171150
alterList(onList) {
@@ -204,9 +183,7 @@ const draggableComponent = defineComponent({
204183
},
205184

206185
getVmIndexFromDomIndex(domIndex) {
207-
const indexes = this.visibleIndexes;
208-
const numberIndexes = indexes.length;
209-
return domIndex > numberIndexes - 1 ? numberIndexes : indexes[domIndex];
186+
return this.componentStructure.getVmIndexFromDomIndex(domIndex);
210187
},
211188

212189
onDragStart(evt) {
@@ -223,7 +200,6 @@ const draggableComponent = defineComponent({
223200
removeNode(evt.item);
224201
const newIndex = this.getVmIndexFromDomIndex(evt.newIndex);
225202
this.spliceList(newIndex, 0, element);
226-
this.computeIndexes();
227203
const added = { element, newIndex };
228204
this.emitChanges({ added });
229205
},
@@ -291,7 +267,6 @@ const draggableComponent = defineComponent({
291267
},
292268

293269
onDragEnd() {
294-
this.computeIndexes();
295270
draggingElement = null;
296271
}
297272
}

tests/unit/vuedraggable.spec.js

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ function resetMocks() {
4848
}
4949

5050
describe("draggable.vue when initialized with list", () => {
51-
let computeIndexesSpy;
5251
beforeEach(() => {
5352
resetMocks();
54-
computeIndexesSpy = jest.spyOn(draggable.methods, "computeIndexes");
5553

5654
items = ["a", "b", "c"];
5755
wrapper = mount(draggable, {
@@ -206,16 +204,7 @@ describe("draggable.vue when initialized with list", () => {
206204

207205
it("computes indexes", async () => {
208206
await nextTick();
209-
expect(vm.visibleIndexes).toEqual([-1, 0, 1, 2, 3]);
210-
});
211-
212-
it("update indexes", async () => {
213-
await nextTick();
214-
215-
computeIndexesSpy.mockClear();
216-
wrapper.setProps({ list: ["c", "d", "e", "f", "g"] });
217-
await nextTick();
218-
expect(computeIndexesSpy).toHaveBeenCalledTimes(1);
207+
expect(vm.componentStructure.visibleIndexes).toEqual([-1, 0, 1, 2, 3]);
219208
});
220209

221210
it("set realList", () => {
@@ -658,25 +647,26 @@ describe("draggable.vue when initialized with list", () => {
658647
expect(wrapper.emitted().end).toEqual([[endEvt]]);
659648
});
660649
});
650+
});
661651

662-
describe("when re-rendering", () => {
663-
const updatedRender= "<div><header></header><div>a</div><div>b</div><div>c</div><div>d</div><footer></footer></div>";
664-
beforeEach(async () => {
665-
items.push("d");
666-
wrapper.setProps({
667-
list: [...items]
668-
});
669-
await nextTick();
652+
describe("when re-rendering", () => {
653+
const updatedRender= "<div><header></header><div>a</div><div>b</div><div>c</div><div>d</div><footer></footer></div>";
654+
beforeEach(async () => {
655+
items.push("d");
656+
wrapper.setProps({
657+
list: [...items]
670658
});
659+
vm.$forceUpdate();
660+
await nextTick();
661+
});
671662

672-
it("updates the rendered elements", () => {
673-
expect(wrapper.html()).toEqual(updatedRender);
674-
});
663+
it("updates the rendered elements", () => {
664+
expect(wrapper.html()).toEqual(updatedRender);
665+
});
675666

676-
it("updates indexes", async () => {
677-
await nextTick();
678-
expect(vm.visibleIndexes).toEqual([-1, 0, 1, 2, 3, 4]);
679-
});
667+
it("updates indexes", async () => {
668+
await nextTick();
669+
expect(vm.componentStructure.visibleIndexes).toEqual([-1, 0, 1, 2, 3, 4]);
680670
});
681671
});
682672

@@ -948,11 +938,8 @@ describe("draggable.vue when initialized with list", () => {
948938
});
949939

950940
describe("draggable.vue when initialized with modelValue", () => {
951-
let computeIndexesSpy;
952-
953941
beforeEach(() => {
954942
Sortable.mockClear();
955-
computeIndexesSpy = jest.spyOn(draggable.methods, "computeIndexes");
956943

957944
items = ["a", "b", "c"];
958945
wrapper = mount(draggable, {
@@ -970,22 +957,13 @@ describe("draggable.vue when initialized with modelValue", () => {
970957

971958
it("computes indexes", async () => {
972959
await nextTick();
973-
expect(vm.visibleIndexes).toEqual([0, 1, 2]);
960+
expect(vm.componentStructure.visibleIndexes).toEqual([0, 1, 2]);
974961
});
975962

976963
it("renders correctly", () => {
977964
expectHTML(wrapper, initialRenderRaw);
978965
});
979966

980-
it("update indexes", async () => {
981-
await nextTick();
982-
computeIndexesSpy.mockClear();
983-
984-
wrapper.setProps({ modelValue: ["c", "d", "e", "f", "g"] });
985-
await nextTick();
986-
expect(computeIndexesSpy).toHaveBeenCalledTimes(1);
987-
});
988-
989967
it("set realList", () => {
990968
expect(vm.realList).toEqual(["a", "b", "c"]);
991969
});
@@ -1138,7 +1116,7 @@ describe("draggable.vue when initialized with a transition group", () => {
11381116

11391117
it("computes indexes", async () => {
11401118
await nextTick();
1141-
expect(vm.visibleIndexes).toEqual([0, 1, 2]);
1119+
expect(vm.componentStructure.visibleIndexes).toEqual([0, 1, 2]);
11421120
});
11431121

11441122
it("set realList", () => {

0 commit comments

Comments
 (0)