Skip to content

Commit 3eb6160

Browse files
transform to a Vuejs plugin
1 parent 1d8cb45 commit 3eb6160

File tree

2 files changed

+48
-35
lines changed

2 files changed

+48
-35
lines changed

build/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.

src/vuedragablefor.js

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
(function(){
22

3-
var forDirective = Vue.directive('for');
4-
var dragableForDirective = _.clone(forDirective);
5-
dragableForDirective.params = dragableForDirective.params.concat('root', 'options');
6-
73
function mix(source, functions){
84
_.forEach(['bind', 'update', 'unbind'],function(value){
95
var original = source[value];
@@ -14,41 +10,58 @@
1410
});
1511
}
1612

17-
mix(dragableForDirective, {
18-
bind : function () {
19-
var ctx = this;
20-
var options = this.params.options;
21-
options = (typeof options === "string") ? JSON.parse(options) : options;
22-
options = _.merge(options,{
23-
onUpdate: function (evt) {
13+
var vueDragFor = {
14+
install : function(Vue) {
15+
var forDirective = Vue.directive('for');
16+
var dragableForDirective = _.clone(forDirective);
17+
dragableForDirective.params = dragableForDirective.params.concat('root', 'options');
18+
19+
mix(dragableForDirective, {
20+
bind : function () {
21+
var ctx = this;
22+
var options = this.params.options;
23+
options = (typeof options === "string") ? JSON.parse(options) : options;
24+
options = _.merge(options,{
25+
onUpdate: function (evt) {
2426
var collection = ctx.collection;
2527
if (!!collection)
2628
collection.splice(evt.newIndex, 0, collection.splice(evt.oldIndex, 1)[0] );
27-
},
28-
onAdd: function (evt) {
29+
},
30+
onAdd: function (evt) {
2931
var directive = evt.from.__directive;
3032
if ((!!directive) && (!!ctx.collection))
3133
ctx.collection.splice(evt.newIndex, 0, directive.collection[evt.oldIndex]);
32-
},
33-
onRemove: function (evt) {
34-
if (!!ctx.collection)
35-
ctx.collection.splice(evt.oldIndex, 1);
36-
}
37-
});
38-
var parent = (!!this.params.root) ? document.getElementById(this.params.root) : this.el.parentElement;
39-
parent.__directive = this;
40-
this.sortable = new Sortable(parent, options);
41-
},
42-
update : function (value){
43-
if ((!!value) && (!Array.isArray(value)))
44-
throw new Error('should received an Array');
34+
},
35+
onRemove: function (evt) {
36+
if (!!ctx.collection)
37+
ctx.collection.splice(evt.oldIndex, 1);
38+
}
39+
});
40+
var parent = (!!this.params.root) ? document.getElementById(this.params.root) : this.el.parentElement;
41+
parent.__directive = this;
42+
this.sortable = new Sortable(parent, options);
43+
},
44+
update : function (value){
45+
if ((!!value) && (!Array.isArray(value)))
46+
throw new Error('should received an Array');
47+
48+
this.collection = value;
49+
},
50+
unbind : function (){
51+
this.sortable.destroy();
52+
}
53+
});
4554

46-
this.collection = value;
47-
},
48-
unbind : function (){
49-
this.sortable.destroy();
50-
}
51-
});
52-
53-
Vue.directive('dragable-for', dragableForDirective);
55+
Vue.directive('dragable-for', dragableForDirective);
56+
}
57+
};
58+
59+
if (typeof exports == "object") {
60+
module.exports = vueDragFor;
61+
} else if (typeof define == "function" && define.amd) {
62+
define([], function(){ return vueDragFor; });
63+
} else if (window.Vue) {
64+
window.vueDragFor = vueDragFor;
65+
Vue.use(vueDragFor);
66+
}
5467
})();

0 commit comments

Comments
 (0)