Skip to content

Commit 851e215

Browse files
Making v-if and index works!! Definitive fix for issue #8. + cleaning examples
1 parent 68352ed commit 851e215

File tree

8 files changed

+23
-76
lines changed

8 files changed

+23
-76
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/Two_Lists.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ <h2>List 2 v-for</h2>
4242

4343
<a href="index.html">See basic example</a>
4444
<a href="Two_Lists_Clone.html">See clone element example</a>
45+
<a href="Two_Lists_NoClone_If.html">See v-if element example</a>
4546
<a href="Two_Lists_Clone_If.html">See clone v-if element example</a>
4647

4748
<script type="text/javascript" src="..\libs\vue\dist\vue.js"></script>

examples/Two_Lists_Clone.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ <h2>List 2 v-for</h2>
4242

4343
<a href="index.html">See basic example</a>
4444
<a href="Two_Lists.html">See 2 lists example</a>
45+
<a href="Two_Lists_NoClone_If.html">See v-if element example</a>
4546
<a href="Two_Lists_Clone_If.html">See clone v-if element example</a>
4647

4748
<script type="text/javascript" src="..\libs\vue\dist\vue.js"></script>

examples/Two_Lists_Clone_If.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ <h2>List 2 v-for</h2>
4343

4444
<a href="index.html">See basic example</a>
4545
<a href="Two_Lists.html">See 2 lists example</a>
46-
<a href="Two_Lists.html">See 2 lists example</a>
46+
<a href="Two_Lists_Clone.html">See clone element example</a>
47+
<a href="Two_Lists_NoClone_If.html">See v-if element example</a>
4748

4849
<script type="text/javascript" src="..\libs\vue\dist\vue.js"></script>
4950
<script type="text/javascript" src="..\libs\lodash\lodash.js"></script>

examples/Two_Lists_If.html

Lines changed: 0 additions & 55 deletions
This file was deleted.

examples/Two_Lists_NoClone_If.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ <h2>List 2 v-for</h2>
4242

4343
<a href="index.html">See basic example</a>
4444
<a href="Two_Lists.html">See 2 lists example</a>
45+
<a href="Two_Lists_Clone.html">See clone element example</a>
4546
<a href="Two_Lists_Clone_If.html">See clone v-if element example</a>
4647

4748
<script type="text/javascript" src="..\libs\vue\dist\vue.js"></script>

examples/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ <h2>Normal v-for</h2>
3535

3636
<a href="Two_Lists.html">See 2 lists example</a>
3737
<a href="Two_Lists_Clone.html">See clone element example</a>
38+
<a href="Two_Lists_NoClone_If.html">See v-if element example</a>
3839
<a href="Two_Lists_Clone_If.html">See clone v-if element example</a>
3940

41+
4042
<script type="text/javascript" src="..\libs\vue\dist\vue.js"></script>
4143
<script type="text/javascript" src="..\libs\lodash\lodash.js"></script>
4244
<script type="text/javascript" src="..\libs\Sortable\Sortable.js"></script>

src/vuedragablefor.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
node.parentElement.removeChild(node);
3636
}
3737

38-
function insertNodeAt(fatherNode, node, position){
39-
if (position<fatherNode.children.length)
40-
fatherNode.insertBefore(node, fatherNode.children[position]);
38+
function insertNodeBefore(fatherNode, node, beforeNode){
39+
if (!!beforeNode)
40+
fatherNode.insertBefore(node, beforeNode);
4141
else
4242
fatherNode.appendChild(node);
4343
}
@@ -51,6 +51,12 @@
5151
var forDirective = Vue.directive('for');
5252
var dragableForDirective = _.clone(forDirective);
5353
dragableForDirective.params = dragableForDirective.params.concat('root', 'options');
54+
var rightElement;
55+
56+
function insertAtOriginalPlace(evt){
57+
removeNode(evt.item);
58+
insertNodeBefore(evt.from, evt.item, rightElement)
59+
}
5460

5561
mix(dragableForDirective, {
5662
bind : function () {
@@ -68,16 +74,15 @@
6874
options = _.isString(options)? JSON.parse(options) : options;
6975
options = _.merge(options,{
7076
onStart: function (evt) {
77+
rightElement = evt.item.nextSibling;
7178
indexes = computeIndexes(_.chain(evt.from.children));
7279
},
7380
onUpdate: function (evt) {
7481
updatePosition(ctx.collection, evt.newIndex, evt.oldIndex);
75-
removeNode(evt.item);
76-
insertNodeAt(evt.from, evt.item, evt.oldIndex)
82+
insertAtOriginalPlace(evt);
7783
},
7884
onAdd: function (evt) {
7985
indexes = computeIndexes(_.chain(evt.to.children).filter(function(elt){return elt!==evt.item;}));
80-
console.log(indexes);
8186
if (!!ctx.collection){
8287
var addElement= getVmObject(evt.item);
8388
var length = indexes.length;
@@ -88,27 +93,18 @@
8893
var newIndex = indexes[evt.newIndex];
8994
ctx.collection.splice(newIndex, 0, addElement);
9095
}
91-
removeNode(evt.item);
92-
insertNodeAt(evt.from, evt.item, evt.oldIndex)
96+
insertAtOriginalPlace(evt);
9397
}
9498
},
9599
onRemove: function (evt) {
96-
var collection = ctx.collection;
97-
if (!!collection && !evt.clone){
100+
var collection = ctx.collection, cloning = !!evt.clone;
101+
if (!!collection && !cloning){
98102
var realOld = indexes[evt.oldIndex];
99103
collection.splice(realOld, 1);
100104
}
101-
if (!!evt.clone){
105+
if (cloning){
102106
removeNode(evt.clone);
103-
insertNodeAt(evt.from, evt.item, evt.oldIndex)
104107
}
105-
else{
106-
//remove added node: Vue will add it for us
107-
var elt = evt.item
108-
if (!!getFragment(elt).parentFrag){
109-
removeNode(elt);
110-
}
111-
}
112108
}
113109
});
114110
var parent = (!!this.params.root) ? document.getElementById(this.params.root) : this.el.parentElement;

0 commit comments

Comments
 (0)