|
1 | 1 | (function(){ |
| 2 | + function buildVueDragFor(_){ |
2 | 3 |
|
3 | | - function mix(source, functions){ |
4 | | - _.forEach(['bind', 'update', 'unbind'],function(value){ |
5 | | - var original = source[value]; |
6 | | - source[value] = function(){ |
7 | | - functions[value].apply(this, arguments); |
8 | | - return original.apply(this, arguments); |
9 | | - }; |
10 | | - }); |
11 | | - } |
| 4 | + function mix(source, functions){ |
| 5 | + _.forEach(['bind', 'update', 'unbind'],function(value){ |
| 6 | + var original = source[value]; |
| 7 | + source[value] = function(){ |
| 8 | + functions[value].apply(this, arguments); |
| 9 | + return original.apply(this, arguments); |
| 10 | + }; |
| 11 | + }); |
| 12 | + } |
| 13 | + |
| 14 | + var vueDragFor = { |
| 15 | + install : function(Vue) { |
| 16 | + var forDirective = Vue.directive('for'); |
| 17 | + var dragableForDirective = _.clone(forDirective); |
| 18 | + dragableForDirective.params = dragableForDirective.params.concat('root', 'options'); |
12 | 19 |
|
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'); |
| 20 | + mix(dragableForDirective, { |
| 21 | + bind : function () { |
| 22 | + var ctx = this; |
| 23 | + var options = this.params.options; |
| 24 | + options = (typeof options === "string") ? JSON.parse(options) : options; |
| 25 | + options = _.merge(options,{ |
| 26 | + onUpdate: function (evt) { |
| 27 | + var collection = ctx.collection; |
| 28 | + if (!!collection) |
| 29 | + collection.splice(evt.newIndex, 0, collection.splice(evt.oldIndex, 1)[0] ); |
| 30 | + }, |
| 31 | + 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]); |
| 35 | + }, |
| 36 | + onRemove: function (evt) { |
| 37 | + if (!!ctx.collection) |
| 38 | + ctx.collection.splice(evt.oldIndex, 1); |
| 39 | + } |
| 40 | + }); |
| 41 | + var parent = (!!this.params.root) ? document.getElementById(this.params.root) : this.el.parentElement; |
| 42 | + parent.__directive = this; |
| 43 | + this.sortable = new Sortable(parent, options); |
| 44 | + }, |
| 45 | + update : function (value){ |
| 46 | + if ((!!value) && (!Array.isArray(value))) |
| 47 | + throw new Error('should received an Array'); |
18 | 48 |
|
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) { |
26 | | - var collection = ctx.collection; |
27 | | - if (!!collection) |
28 | | - collection.splice(evt.newIndex, 0, collection.splice(evt.oldIndex, 1)[0] ); |
29 | | - }, |
30 | | - onAdd: function (evt) { |
31 | | - var directive = evt.from.__directive; |
32 | | - if ((!!directive) && (!!ctx.collection)) |
33 | | - ctx.collection.splice(evt.newIndex, 0, directive.collection[evt.oldIndex]); |
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'); |
| 49 | + this.collection = value; |
| 50 | + }, |
| 51 | + unbind : function (){ |
| 52 | + this.sortable.destroy(); |
| 53 | + } |
| 54 | + }); |
47 | 55 |
|
48 | | - this.collection = value; |
49 | | - }, |
50 | | - unbind : function (){ |
51 | | - this.sortable.destroy(); |
52 | | - } |
53 | | - }); |
54 | | - |
55 | | - Vue.directive('dragable-for', dragableForDirective); |
56 | | - } |
57 | | - }; |
| 56 | + Vue.directive('dragable-for', dragableForDirective); |
| 57 | + } |
| 58 | + }; |
| 59 | + return vueDragFor; |
| 60 | + } |
58 | 61 |
|
59 | 62 | if (typeof exports == "object") { |
60 | | - module.exports = vueDragFor; |
| 63 | + var _ = require("lodash.js"); |
| 64 | + module.exports = buildVueDragFor(_); |
61 | 65 | } 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 | + define(['lodash'], function(_){ return buildVueDragFor(_); }); |
| 67 | + } else if ((window.Vue) && (window._)) { |
| 68 | + window.vueDragFor = buildVueDragFor(window._); |
| 69 | + Vue.use(window.vueDragFor); |
66 | 70 | } |
67 | 71 | })(); |
0 commit comments