-
Notifications
You must be signed in to change notification settings - Fork 4
/
ractive.draggable.js
64 lines (56 loc) · 1.94 KB
/
ractive.draggable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* Drag N' Drop Ractive Event
*
* @param {Object} node DOM Node
* @param {Function} fire Method to fire back data to ractive.on
* @return {Object} Teardown method
* @author Nijiko Yonskai
* @copyright 2013
*/
Ractive.eventDefinitions.draggable = function ( node, fire ) {
var $self = Ractive.eventDefinitions.draggable;
var foreach = function (n, next) {
if (n.length) Array.prototype.forEach.call(n, next);
};
var Drag = {
event: function (name) {
return function (event) {
if (name === 'drag_start')
$self.current = node;
fire({
node: $self.current || node,
source: node,
name: name,
type: name.split('_')[1],
target: this,
original: event,
previous: $self.previous
});
if (name === 'drag_enter')
$self.previous = node;
if (name === 'drag_end')
$self.current = null;
};
}
};
node.draggable = true;
node.addEventListener('dragstart', Drag.event('drag_start'));
node.addEventListener('dragenter', Drag.event('drag_enter'));
node.addEventListener('dragover', Drag.event('drag_over'));
node.addEventListener('dragleave', Drag.event('drag_leave'));
node.addEventListener('drop', Drag.event('drag_drop'));
node.addEventListener('dragend', Drag.event('drag_end'));
return {
teardown: function () {
node.draggable = true;
node.removeEventListener('dragstart', Drag.event('drag_start'));
node.removeEventListener('dragenter', Drag.event('drag_enter'));
node.removeEventListener('dragover', Drag.event('drag_over'));
node.removeEventListener('dragleave', Drag.event('drag_leave'));
node.removeEventListener('drop', Drag.event('drag_drop'));
node.removeEventListener('dragend', Drag.event('drag_end'));
}
};
};
Ractive.eventDefinitions.draggable.current = null;
Ractive.eventDefinitions.draggable.previous = null;