Skip to content

Commit cc99251

Browse files
Correct index bug on clone and add
1 parent 8f3896d commit cc99251

10 files changed

+52
-44
lines changed

bower.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "vue.dragable.for",
33
"authors": ["desmaisons_david <[email protected]>"],
4-
"description": "vue dragable for directive",
5-
"main": "src/vuedragablefor.js",
4+
"description": "vue vuedraggable component",
5+
"main": "src/vuedraggable.js",
66
"keywords": [
77
"vue",
88
"list",

dist/vuedraggable.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/Two_Lists.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ <h2>List 2 v-for</h2>
4747
<script type="text/javascript" src="..\libs\vue\dist\vue.js"></script>
4848
<script type="text/javascript" src="..\libs\lodash\lodash.js"></script>
4949
<script type="text/javascript" src="..\libs\Sortable\Sortable.js"></script>
50-
<script type="text/javascript" src="src\vuedragablefor.js"></script>
50+
<script type="text/javascript" src="src\vuedraggable.js"></script>
5151
<script type="text/javascript" src="script\complex.js"></script>
5252
</body>
5353
</html>

examples/Two_Lists_Clone.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ <h2>List 2 v-for</h2>
4949
<script type="text/javascript" src="..\libs\vue\dist\vue.js"></script>
5050
<script type="text/javascript" src="..\libs\lodash\lodash.js"></script>
5151
<script type="text/javascript" src="..\libs\Sortable\Sortable.js"></script>
52-
<script type="text/javascript" src="src\vuedragablefor.js"></script>
52+
<script type="text/javascript" src="src\vuedraggable.js"></script>
5353
<script type="text/javascript" src="script\complex.js"></script>
5454

5555
</body>

examples/Two_Lists_Clone_If.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ <h2>List 2 v-for</h2>
5050
<script type="text/javascript" src="..\libs\vue\dist\vue.js"></script>
5151
<script type="text/javascript" src="..\libs\lodash\lodash.js"></script>
5252
<script type="text/javascript" src="..\libs\Sortable\Sortable.js"></script>
53-
<script type="text/javascript" src="src\vuedragablefor.js"></script>
53+
<script type="text/javascript" src="src\vuedraggable.js"></script>
5454
<script type="text/javascript" src="script\complex.js"></script>
5555
</body>
5656
</html>

examples/Two_Lists_If.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ <h2>List 2 v-for</h2>
4949
<script type="text/javascript" src="..\libs\vue\dist\vue.js"></script>
5050
<script type="text/javascript" src="..\libs\lodash\lodash.js"></script>
5151
<script type="text/javascript" src="..\libs\Sortable\Sortable.js"></script>
52-
<script type="text/javascript" src="src\vuedragablefor.js"></script>
52+
<script type="text/javascript" src="src\vuedraggable.js"></script>
5353
<script type="text/javascript" src="script\complex.js"></script>
5454

5555
</body>

examples/Two_Lists_NoClone_If.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ <h2>List 2 v-for</h2>
5050
<script type="text/javascript" src="..\libs\vue\dist\vue.js"></script>
5151
<script type="text/javascript" src="..\libs\lodash\lodash.js"></script>
5252
<script type="text/javascript" src="..\libs\Sortable\Sortable.js"></script>
53-
<script type="text/javascript" src="src\vuedragablefor.js"></script>
53+
<script type="text/javascript" src="src\vuedraggable.js"></script>
5454
<script type="text/javascript" src="script\complex.js"></script>
5555

5656
</body>

examples/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ <h2>Normal v-for</h2>
4141

4242
<script type="text/javascript" src="..\libs\vue\dist\vue.js"></script>
4343
<script type="text/javascript" src="..\libs\Sortable\Sortable.js"></script>
44-
<script type="text/javascript" src="src\vuedragablefor.js"></script>
44+
<script type="text/javascript" src="src\vuedraggable.js"></script>
4545
<script type="text/javascript" src="script\main.js"></script>
4646
</body>
4747
</html>

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"name": "vuedragablefor",
33
"version": "2.0.0",
44
"description": "draggable component for vue",
5-
"main": "src/vuedragablefor.js",
5+
"main": "src/vuedraggable.js",
66
"files": [
7-
"src/index.js"
7+
"src/vuedraggable.js"
88
],
99
"keywords": [
1010
"vue",

src/vuedragablefor.js renamed to src/vuedraggable.js

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,42 @@
22
"use strict";
33

44
function buildDraggable(Sortable) {
5+
function removeNode (node) {
6+
node.parentElement.removeChild(node)
7+
}
58

6-
function removeNode (node) {
7-
node.parentElement.removeChild(node)
8-
}
9-
10-
function insertNodeAt (fatherNode, node, position) {
11-
if (position < fatherNode.children.length) {
12-
fatherNode.insertBefore(node, fatherNode.children[position])
13-
} else {
14-
fatherNode.appendChild(node)
15-
}
9+
function insertNodeAt (fatherNode, node, position) {
10+
if (position < fatherNode.children.length) {
11+
fatherNode.insertBefore(node, fatherNode.children[position])
12+
} else {
13+
fatherNode.appendChild(node)
1614
}
15+
}
1716

18-
function computeVmIndex (vnodes, element) {
19-
return vnodes.map(elt => elt.elm).indexOf(element)
20-
}
17+
function computeVmIndex (vnodes, element) {
18+
return vnodes.map(elt => elt.elm).indexOf(element)
19+
}
2120

22-
function updatePosition (collection, oldIndex, newIndex) {
23-
if (collection) {
24-
collection.splice(newIndex, 0, collection.splice(oldIndex, 1)[0])
25-
}
21+
function updatePosition (collection, oldIndex, newIndex) {
22+
if (collection) {
23+
collection.splice(newIndex, 0, collection.splice(oldIndex, 1)[0])
2624
}
25+
}
2726

28-
function computeIndexes (slots, children) {
29-
return Array.prototype.map.call(children, elt => computeVmIndex(slots, elt))
30-
}
27+
function computeIndexes (slots, children) {
28+
return Array.prototype.map.call(children, elt => computeVmIndex(slots, elt))
29+
}
3130

32-
function merge (target, source) {
33-
var output = Object(target)
34-
for (var nextKey in source) {
35-
if (source.hasOwnProperty(nextKey)) {
36-
output[nextKey] = source[nextKey]
37-
}
31+
function merge (target, source) {
32+
var output = Object(target)
33+
for (var nextKey in source) {
34+
if (source.hasOwnProperty(nextKey)) {
35+
output[nextKey] = source[nextKey]
3836
}
39-
return output
4037
}
41-
38+
return output
39+
}
40+
4241
function install (Vue) {
4342
const props = {
4443
options: Object,
@@ -60,19 +59,25 @@
6059

6160
const options = merge(this.options, optionsAdded)
6261
this._sortable = new Sortable(this.$el, options)
62+
this.computeIndexes()
6363
},
6464

6565
methods: {
6666

67+
computeIndexes () {
68+
this.$nextTick( () =>{
69+
const slots = this.$slots.default
70+
this.visibleIndexes = computeIndexes(slots, this.$el.children)
71+
})
72+
},
73+
6774
onDragStart (evt) {
6875
if (!this.list) {
6976
return
70-
}
71-
const slots = this.$slots.default
72-
const currentIndex = computeVmIndex(slots, evt.item)
77+
}
78+
const currentIndex = computeVmIndex(this.$slots.default, evt.item)
7379
const element = this.list[currentIndex]
7480
this.context = {
75-
visibleIndexes: computeIndexes(slots, this.$el.children),
7681
currentIndex,
7782
element
7883
}
@@ -85,11 +90,12 @@
8590
return
8691
}
8792
removeNode(evt.item)
88-
const indexes = computeIndexes(this.$slots.default, this.$el.children)
93+
const indexes = this.visibleIndexes
8994
const domNewIndex = evt.newIndex
9095
const numberIndexes = indexes.length
9196
const newIndex = (domNewIndex > numberIndexes - 1) ? numberIndexes : indexes[domNewIndex]
9297
this.list.splice(newIndex, 0, element)
98+
this.computeIndexes()
9399
},
94100

95101
onDragRemove (evt) {
@@ -113,11 +119,12 @@
113119
removeNode(evt.item)
114120
insertNodeAt(evt.from, evt.item, evt.oldIndex)
115121
const oldIndexVM = this.context.currentIndex
116-
const newIndexVM = this.context.visibleIndexes[evt.newIndex]
122+
const newIndexVM = this.visibleIndexes[evt.newIndex]
117123
updatePosition(this.list, oldIndexVM, newIndexVM)
118124
},
119125

120126
onDragEnd (evt) {
127+
this.computeIndexes()
121128
}
122129
}
123130
}

0 commit comments

Comments
 (0)