-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplednd-classes.js
211 lines (195 loc) · 6.3 KB
/
simplednd-classes.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*========== CLASSICAL INHERITANCE ==========*/
"use strict";
function extend(subClass, superClass) {
var F = function() {};
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass;
subClass.superclass = superClass.prototype;
if (superClass.prototype.constructor == Object.prototype.constructor) {
superClass.prototype.constructor = superClass;
}
}
function mixin(receiver, supplier) {
Object.keys(supplier).forEach(function(key) {
receiver[key] = supplier[key];
});
return receiver;
}
var PubSub = {
subscribers: {
any: []
},
on: function(type, fn, context) {
type = type || 'any';
fn = typeof fn === "function" ? fn : context[fn];
if (typeof this.subscribers[type] === "undefined") {
this.subscribers[type] = [];
}
this.subscribers[type].push({
fn: fn,
context: context || this
});
},
off: function(type, fn, context) {
this.visitSubscribers('off', type, fn, context);
},
trigger: function(type, publication) {
this.visitSubscribers('trigger', type, publication);
},
visitSubscribers: function(action, type, arg, context) {
var pubtype = type || 'any',
subscribers = this.subscribers[pubtype],
i,
max = subscribers ? subscribers.length : 0;
for (i = 0; i < max; i += 1) {
if (action === 'trigger') {
subscribers[i].fn.call(subscribers[i].context, arg);
} else {
if (subscribers[i].fn === arg &&
subscribers[i].context === context) {
subscribers.splice(i, 1);
}
}
}
}
};
var Helpers = {
attachEvent: function(el, type, cssClass, callback) {
var self = this;
if (arguments.length === 3) {
callback = arguments[2];
cssClass = false;
}
el.addEventListener(type, function(e) {
var target = self.delegate(el, e, cssClass);
if (!target) {
return false;
}
e.data = {
el: target
};
callback.call(self, e);
}, true);
},
delegate: function(el, e, cssClass) {
var target = e.target;
if (cssClass) {
while (!target.classList.contains(cssClass) && target != el) {
target = target.parentNode;
}
if (target === el) {
return false;
}
}
return target;
}
};
function SimpleDnD(options) {
this.rootEl = options.rootEl;
this.dropZones = options.dropZones;
this.draggable = options.draggable;
this.init(this.rootEl);
}
SimpleDnD.prototype = {
// Current dragging element
dragEl: null,
init: function(rootEl) {
this.attachEvent(rootEl, 'dragenter', this.onDragEnter);
this.attachEvent(rootEl, 'dragover', this.onDragOver);
this.attachEvent(rootEl, 'dragleave', this.onDragLeave);
this.attachEvent(rootEl, 'dragend', this.onDragEnd);
this.attachEvent(rootEl, 'drop', this.onDragStart);
this.attachEvent(rootEl, 'dragstart', 'js-draggable', this.onDragStart);
},
onDragStart: function(e) {
var proto = SimpleDnD.prototype;
var el = e.data.el;
proto.dragEl = el;
console.log(SimpleDnD.prototype.dragEl);
el.classList.add('dragging');
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('Text', e.target.dataset['dragel']);
return true;
},
onDragLeave: function(e) {
var el = e.data.el;
el.classList.remove('dragging-over');
},
onDragEnd: function(e) {
var el = e.data.el;
el.classList.remove('dragging');
},
onDragOver: function(e) {
var dragEl = this.dragEl;
var el = e.data.el;
// console.log(self);
var targetCoord = e.target.getBoundingClientRect();
if (el == this.rootEl && this.rootEl.children.length) {
return false;
}
else if (this.rootEl.children.length === 0) {
return this.rootEl.appendChild(dragEl);
}
if (e.clientY - targetCoord.top < (targetCoord.height / 2)) {
e.target.insertAdjacentElement('beforeBegin', dragEl);
} else {
e.target.insertAdjacentElement('afterEnd', dragEl);
}
e.preventDefault();
return false;
},
onDragEnter: function(e) {
// this.classList.add('dragging-over');
// this.classList.remove('dragging-over');
// var el = e.target;
e.preventDefault();
return false;
},
onDrop: function(e) {
e.stopPropagation();
e.preventDefault();
return false;
},
constructor: SimpleDnD
};
mixin(SimpleDnD.prototype, Helpers);
// List with border
function SimpleDnDListWithBorder(options) {
SimpleDnD.call(this, options);
this.uber = SimpleDnD.prototype;
}
SimpleDnDListWithBorder.prototype = Object.create(SimpleDnD.prototype, {
constructor: {
value: SimpleDnDListWithBorder
}
});
SimpleDnDListWithBorder.prototype.onDragEnd = function(e) {
var rootEl = this.rootEl;
Object.getPrototypeOf(SimpleDnDListWithBorder.prototype).onDragEnd.call(this, e);
rootEl.classList.add('red-border');
setTimeout(function() {
rootEl.classList.remove('red-border');
}, 1000)
};
// List with border and background
function SimpleDnDListWithBorderAndBackground(options) {
SimpleDnDListWithBorder.call(this, options);
this.copyList = options.copyList;
this.uber = SimpleDnDListWithBorder.prototype;
}
SimpleDnDListWithBorderAndBackground.prototype = Object.create(SimpleDnDListWithBorder.prototype, {
constructor: {
value: SimpleDnDListWithBorderAndBackground
}
});
SimpleDnDListWithBorderAndBackground.prototype.onDragEnd = function(e) {
var rootEl = this.rootEl;
Object.getPrototypeOf(SimpleDnDListWithBorderAndBackground.prototype).onDragEnd.call(this, e);
rootEl.classList.add('red-background');
var copy = this.dragEl.cloneNode(true);
this.copyList.appendChild(copy);
setTimeout(function() {
rootEl.classList.remove('red-background');
}, 1000);
};