|
10 | 10 | };
|
11 | 11 | });
|
12 | 12 | }
|
| 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 | + } |
13 | 35 |
|
14 | 36 | var vueDragFor = {
|
15 | 37 | install : function(Vue) {
|
|
24 | 46 | options = _.isString(options)? JSON.parse(options) : options;
|
25 | 47 | options = _.merge(options,{
|
26 | 48 | 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); |
30 | 50 | },
|
31 | 51 | 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 | + } |
35 | 56 | },
|
36 | 57 | onRemove: function (evt) {
|
37 | 58 | var collection = ctx.collection;
|
|
40 | 61 | if (!!evt.clone){
|
41 | 62 | //if cloning mode: replace cloned element by orginal element (with original vue binding information)+
|
42 | 63 | //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; |
44 | 65 | 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); |
48 | 68 | }
|
49 | 69 | }
|
50 | 70 | }
|
51 | 71 | });
|
52 | 72 | var parent = (!!this.params.root) ? document.getElementById(this.params.root) : this.el.parentElement;
|
53 |
| - parent.__directive = this; |
54 | 73 | this._sortable = new Sortable(parent, options);
|
55 | 74 | },
|
56 | 75 | update : function (value){
|
|
0 commit comments