|
1 | 1 | (function(){
|
2 | 2 |
|
3 |
| - var forDirective = Vue.directive('for'); |
4 |
| - var dragableForDirective = _.clone(forDirective); |
5 |
| - dragableForDirective.params = dragableForDirective.params.concat('root', 'options'); |
6 |
| - |
7 | 3 | function mix(source, functions){
|
8 | 4 | _.forEach(['bind', 'update', 'unbind'],function(value){
|
9 | 5 | var original = source[value];
|
|
14 | 10 | });
|
15 | 11 | }
|
16 | 12 |
|
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) { |
24 | 26 | var collection = ctx.collection;
|
25 | 27 | if (!!collection)
|
26 | 28 | collection.splice(evt.newIndex, 0, collection.splice(evt.oldIndex, 1)[0] );
|
27 |
| - }, |
28 |
| - onAdd: function (evt) { |
| 29 | + }, |
| 30 | + onAdd: function (evt) { |
29 | 31 | var directive = evt.from.__directive;
|
30 | 32 | if ((!!directive) && (!!ctx.collection))
|
31 | 33 | 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 | + }); |
45 | 54 |
|
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 | + } |
54 | 67 | })();
|
0 commit comments