|
13 | 13 | 'drag-enter', |
14 | 14 | 'drag-leave', |
15 | 15 | 'drag-end', |
16 | | - 'drop' |
| 16 | + 'drop', |
| 17 | + 'draggable', |
| 18 | + 'droppable' |
17 | 19 | ], |
18 | 20 | bind: function () { |
19 | 21 | // use the VM so we only have 1 dragging item per app |
20 | 22 | this.vm._dragSrcEl = null; |
| 23 | + |
| 24 | + if(this.params.draggable === undefined){ |
| 25 | + this.params.draggable = true; |
| 26 | + } |
| 27 | + if(this.params.droppable === undefined){ |
| 28 | + this.params.droppable = true; |
| 29 | + } |
| 30 | + // transfer "false" => false, "true" => true |
| 31 | + var blooeanMaps = { |
| 32 | + true: true, |
| 33 | + false: false |
| 34 | + } |
| 35 | + |
| 36 | + var draggable = blooeanMaps[this.params.draggable]; |
| 37 | + var droppable = blooeanMaps[this.params.droppable]; |
| 38 | + var emptyFn = function(){}; |
| 39 | + |
21 | 40 | this.handleDragStart = function (e) { |
22 | 41 | e.target.classList.add('dragging'); |
23 | 42 | this.vm._dragSrcEl = e.target; |
24 | 43 | e.dataTransfer.effectAllowed = 'move'; |
25 | 44 | // Need to set to something or else drag doesn't start |
26 | 45 | e.dataTransfer.setData('text', '*'); |
27 | 46 | if (typeof(this.vm[this.params.dragStart]) === 'function') { |
28 | | - this.vm[this.params.dragStart].call(this, e.target); |
| 47 | + this.vm[this.params.dragStart].call(this, e.target, e); |
29 | 48 | } |
30 | 49 | }.bind(this); |
31 | 50 | this.handleDragOver = function(e) { |
|
36 | 55 | e.dataTransfer.dropEffect = 'move'; |
37 | 56 | e.target.classList.add('drag-over'); |
38 | 57 | if (typeof(this.vm[this.params.dragOver]) === 'function') { |
39 | | - this.vm[this.params.dragOver].call(this, e.target); |
| 58 | + this.vm[this.params.dragOver].call(this, e.target, e); |
40 | 59 | } |
41 | 60 | return false; |
42 | 61 | }.bind(this); |
43 | 62 | this.handleDragEnter = function(e) { |
44 | 63 | if (typeof(this.vm[this.params.dragEnter]) === 'function') { |
45 | | - this.vm[this.params.dragEnter].call(this, e.target); |
| 64 | + this.vm[this.params.dragEnter].call(this, e.target, e); |
46 | 65 | } |
47 | 66 | e.target.classList.add('drag-enter'); |
48 | 67 | }.bind(this); |
49 | 68 | this.handleDragLeave = function(e) { |
50 | 69 | if (typeof(this.vm[this.params.dragLeave]) === 'function') { |
51 | | - this.vm[this.params.dragLeave].call(this, e.target); |
| 70 | + this.vm[this.params.dragLeave].call(this, e.target, e); |
52 | 71 | } |
53 | 72 | e.target.classList.remove('drag-enter'); |
54 | 73 | }.bind(this); |
55 | 74 | this.handleDrag = function(e) { |
56 | 75 | if (typeof(this.vm[this.params.drag]) === 'function') { |
57 | | - this.vm[this.params.drag].call(this, e.target); |
| 76 | + this.vm[this.params.drag].call(this, e.target, e); |
58 | 77 | } |
59 | 78 | }.bind(this); |
60 | 79 | this.handleDragEnd = function(e) { |
61 | 80 | e.target.classList.remove('dragging', 'drag-over', 'drag-enter'); |
62 | 81 | if (typeof(this.vm[this.params.dragEnd]) === 'function') { |
63 | | - this.vm[this.params.dragEnd].call(this, e.target); |
| 82 | + this.vm[this.params.dragEnd].call(this, e.target, e); |
64 | 83 | } |
65 | 84 | }.bind(this); |
66 | 85 | this.handleDrop = function(e) { |
|
72 | 91 | // Don't do anything if dropping the same column we're dragging. |
73 | 92 | if (this.vm._dragSrcEl != e.target) { |
74 | 93 | if (typeof(this.vm[this.params.drop]) === 'function') { |
75 | | - var el = (e.target.draggable) ? e.target : e.target.parentElement; |
76 | | - this.vm[this.params.drop].call(this, this.vm._dragSrcEl, el); |
| 94 | + var el = (e.target.draggable || draggable) ? e.target : e.target.parentElement; |
| 95 | + this.vm[this.params.drop].call(this, this.vm._dragSrcEl, el, e); |
77 | 96 | } |
78 | 97 | } |
79 | 98 | return false; |
80 | 99 | }.bind(this); |
| 100 | + |
| 101 | + if(!draggable){ |
| 102 | + this.handleDragStart = emptyFn; |
| 103 | + this.handleDragEnter = emptyFn; |
| 104 | + this.handleDrag = emptyFn; |
| 105 | + this.handleDragLeave = emptyFn; |
| 106 | + this.handleDragEnd = emptyFn; |
| 107 | + } |
| 108 | + if(!droppable){ |
| 109 | + this.handleDrop = emptyFn; |
| 110 | + } |
81 | 111 | // setup the listeners |
82 | | - this.el.setAttribute('draggable', 'true'); |
| 112 | + draggable && this.el.setAttribute('draggable', 'true'); |
83 | 113 | this.el.addEventListener('dragstart', this.handleDragStart, false); |
84 | 114 | this.el.addEventListener('dragenter', this.handleDragEnter, false); |
85 | 115 | this.el.addEventListener('dragover', this.handleDragOver, false); |
86 | 116 | this.el.addEventListener('drag', this.handleDrag, false); |
87 | 117 | this.el.addEventListener('dragleave', this.handleDragLeave, false); |
88 | | - this.el.addEventListener('drop', this.handleDrop, false); |
89 | 118 | this.el.addEventListener('dragend', this.handleDragEnd, false); |
| 119 | + this.el.addEventListener('drop', this.handleDrop, false); |
90 | 120 | }, |
91 | 121 | update: function (newValue, oldValue) { |
92 | 122 | // console.log(this); |
|
0 commit comments