Skip to content

Commit 4f4c609

Browse files
Improve way to compute added element. Partial Fix for issue#8
1 parent 2d82216 commit 4f4c609

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

dist/vuedragablefor.min.js

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

examples/script/complex.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
var vm = new Vue({
22
el: "#main",
33
data: {
4-
list1:[{name:"John"},
5-
{name:"Joao"},
6-
{name:"Jean"} ],
7-
list2:[{name:"Juan"},
8-
{name:"Edgard"},
9-
{name:"Johnson"} ]
4+
list1:[{name:"John", id:1},
5+
{name:"Joao", id:2},
6+
{name:"Jean", id:3},
7+
{name:"Gerard", id:4} ],
8+
list2:[{name:"Juan", id:5},
9+
{name:"Edgard", id:6},
10+
{name:"Johnson", id:7} ]
1011
},
1112
methods:{
1213
add: function(){

src/vuedragablefor.js

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@
1010
};
1111
});
1212
}
13+
14+
function getFragment(elt){
15+
return elt.__v_frag;
16+
}
17+
18+
function getCollectionFragment(fr){
19+
if (!fr || !!fr.forId){
20+
return fr;
21+
}
22+
return getCollectionFragment(fr.parentFrag);
23+
}
24+
25+
function getVmObject(evt){
26+
var fragment = getCollectionFragment(getFragment(evt.item));
27+
return fragment.raw;
28+
}
29+
30+
function updatePosition(collection, newIndex, oldIndex ){
31+
if (!!collection){
32+
collection.splice(newIndex, 0, collection.splice(oldIndex, 1)[0] );
33+
}
34+
}
1335

1436
var vueDragFor = {
1537
install : function(Vue) {
@@ -24,14 +46,13 @@
2446
options = _.isString(options)? JSON.parse(options) : options;
2547
options = _.merge(options,{
2648
onUpdate: function (evt) {
27-
var collection = ctx.collection;
28-
if (!!collection)
29-
collection.splice(evt.newIndex, 0, collection.splice(evt.oldIndex, 1)[0] );
49+
updatePosition(ctx.collection, evt.newIndex, evt.oldIndex);
3050
},
3151
onAdd: function (evt) {
32-
var directive = evt.from.__directive;
33-
if ((!!directive) && (!!ctx.collection))
34-
ctx.collection.splice(evt.newIndex, 0, directive.collection[evt.oldIndex]);
52+
if (!!ctx.collection){
53+
var addElement= getVmObject(evt);
54+
ctx.collection.splice(evt.newIndex, 0, addElement);
55+
}
3556
},
3657
onRemove: function (evt) {
3758
var collection = ctx.collection;
@@ -40,17 +61,15 @@
4061
if (!!evt.clone){
4162
//if cloning mode: replace cloned element by orginal element (with original vue binding information)+
4263
//re-order element as sortablejs may re-order without sending events
43-
var newIndex = Array.prototype.indexOf.call(evt.from.children, evt.clone), oldIndex = evt.oldIndex;
64+
var newIndex = _.indexOf(evt.from.children, evt.clone), oldIndex = evt.oldIndex;
4465
evt.from.replaceChild(evt.item, evt.clone);
45-
if (!!collection && (newIndex != oldIndex)){
46-
var item = collection.splice(oldIndex, 1);
47-
collection.splice(newIndex, 0, item[0]);
66+
if (newIndex != oldIndex){
67+
updatePosition(collection, newIndex, oldIndex);
4868
}
4969
}
5070
}
5171
});
5272
var parent = (!!this.params.root) ? document.getElementById(this.params.root) : this.el.parentElement;
53-
parent.__directive = this;
5473
this._sortable = new Sortable(parent, options);
5574
},
5675
update : function (value){

0 commit comments

Comments
 (0)