-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzepto.dragswap.js
278 lines (242 loc) · 10.6 KB
/
zepto.dragswap.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*!
* Zepto HTML5 Drag and Drop Sortable
* Author: James Doyle(@james2doyle) http://ohdoylerules.com
* Repository: https://github.com/james2doyle/zepto-dragswap
* Licensed under the MIT license
*/
$.fn.dragswap = function (options) {
var dragSrcEl;
function getPrefix() {
var el = document.createElement('p'),
getPre, transforms = {
'webkitAnimation': '-webkit-animation',
'OAnimation': '-o-animation',
'msAnimation': '-ms-animation',
'MozAnimation': '-moz-animation',
'animation': 'animation'
};
document.body.insertBefore(el, null);
for (var t in transforms) {
if (el.style[t] !== undefined) {
el.style[t] = "translate3d(1px,1px,1px)";
getPre = window.getComputedStyle(el).getPropertyValue(transforms[t]);
// return the successful prefix
return t;
}
}
document.body.removeChild(el);
}
this.defaultOptions = {
element: 'li',
overClass: 'over',
moveClass: 'moving',
dropClass: 'drop',
dropAnimation: false,
exclude: '.disabled',
prefix: getPrefix(),
dropComplete: function (dragSrcEl, originalParent) {
return;
}
};
function excludePattern(elem) {
return elem.is(settings.excludePatt);
}
function onAnimEnd(elem) {
var $elem = $(elem);
$elem.addClass(settings.dropClass);
// add an event for when the animation has finished
$elem.on(settings.prefix + 'End', function () {
// remove the class now that the animation is done
$elem.removeClass(settings.dropClass);
// $elem.removeClass(settings.moveClass);
}, false);
window.setTimeout(function() {
$elem.removeClass(settings.dropClass);
}, 1000);
}
function handleDragStart(e) {
if (!excludePattern($(this))) {
e.preventDefault();
e.stopPropagation();
return false;
}
$(this).parent().addClass(settings.moveClass);
// get the dragging element
dragSrcEl = $(this).parent();
// it is moving
//console.log(e);
if (e.originalEvent.dataTransfer) {
var dt = e.originalEvent.dataTransfer;
dt.effectAllowed = 'move';
dt.setData('text', $(this).parent().html());
dt.setDragImage(dragSrcEl[0], 50, 50);
}
else if(e.dataTransfer){
var dt = e.dataTransfer;
dt.effectAllowed = 'move';
dt.setData('text', $(this).parent().html());
dt.setDragImage(dragSrcEl[0], 50, 50);
}
}
var count = 1;
function handleDragEnter(e) {
// this / e.target is the current hover target.
$(this).addClass(settings.overClass);
}
function handleDragLeave(e) {
$(this).removeClass(settings.overClass); // this / e.target is previous target element.
}
function handleDragOver(e) {
if (e.preventDefault) {
e.preventDefault(); // Necessary. Allows us to drop.
}
if (e.originalEvent.dataTransfer) {
e.originalEvent.dataTransfer.dropEffect = 'move'; // See the section on the DataTransfer object.
}
else if(e.dataTransfer){
e.dataTransfer.dropEffect = 'move'; // See the section on the DataTransfer object.
}
return false;
}
function handleDrop(e) {
var originalParent = $(this);
originalParent = originalParent.attr("draggable") === "true" ? originalParent.parent() : originalParent;
// this / e.target is current target element.
if (e.stopPropagation) {
e.stopPropagation(); // Stops some browsers from redirecting.
}
if (!excludePattern($(this))) {
console.log('prevent drop');
return false;
}
// Don't do anything if dropping the same column we're draggi.
if (dragSrcEl.attr("id") != originalParent.parent().attr("id")) {
// Set the source column's HTML to the HTML of the column dropped on.
// var content = originalParent.attr("draggable") === "true" ? originalParent.parent().html() : originalParent.html();
var oldEl = {
html: originalParent.html(),
id: originalParent.attr("id"),
mode: originalParent.attr("data-mode")
};
var newEl = {
html: dragSrcEl.html(),
id: dragSrcEl.attr("id"),
mode: dragSrcEl.attr("data-mode")
};
// swap all the data
// console.log("old--", oldEl);
// console.log("new--", newEl);
// console.log("dragged--", dragSrcEl[0]);
$(dragSrcEl).html(oldEl.html);
dragSrcEl.attr("id", oldEl.id);
dragSrcEl.attr("data-mode", oldEl.mode);
if (settings.dropAnimation) {
onAnimEnd(originalParent);
onAnimEnd(dragSrcEl);
}
originalParent.removeAttr('draggable');
// originalParent.siblings().filter(settings.excludePatt).attr('draggable', true);
originalParent.html(newEl.html);
originalParent.attr("id", newEl.id);
originalParent.attr("data-mode", newEl.mode);
dragSrcEl.removeClass(settings.moveClass);
originalParent.removeClass(settings.moveClass);
originalParent.removeClass(settings.overClass);
// this/e.target is the source node.
//console.log('handleDragEnd');
$elem = originalParent.parent().find(settings.element);
// console.log($elem);
$elem.each(function (index, item) {
// console.log(item);
$(item).removeClass(settings.overClass);
$(item).removeClass(settings.moveClass);
});
settings.dropComplete(dragSrcEl, originalParent);
}
return false;
}
var settings = $.extend({}, this.defaultOptions, options);
if (settings.exclude) {
if (typeof settings.exclude != 'string') {
var excludePatt = '';
for (var i = 0; i < settings.exclude.length; i++) {
excludePatt += ':not(' + settings.exclude[i] + ')';
}
settings.excludePatt = excludePatt;
}
else {
settings.excludePatt = ':not(' + settings.exclude + ')';
}
}
var method = String(options);
var items = [];
// check for the methods
if (/^(toArray|toJSON)$/.test(method)) {
if (method == 'toArray') {
$(this).find(settings.element).each(function (index, elem) {
items.push(this.id);
});
return items;
} else if (method == 'toJSON') {
$(this).find(settings.element).each(function (index, elem) {
items[index] = {
id: this.id
};
});
return JSON.stringify(items);
}
return;
}
return this.each(function (index, item) {
var $this = $(this);
// select all but the disabled things
var $elem = $this.find(settings.element);
var target = this;
var config = { childList: true };
// var observer = new MutationObserver(function (mutations) {
// console.log(mutations);
// for(var i=0; i<mutations.length; i++){
// if(mutations[i].addedNodes.length != 0){
// for(var j=0; j<mutations[i].addedNodes.length; j++){
// $(mutations[i].addedNodes[j]).siblings().removeAttr('draggable');
// $(mutations[i].addedNodes[j]).siblings().filter(settings.excludePatt).attr('draggable', true);
// }
// }
// }
// });
// observer.observe(target, config);
function handleDragEnd(e) {
// $this.removeClass(settings.moveClass);
$(this).parent().removeClass(settings.moveClass);
// this/e.target is the source node.
//console.log('handleDragEnd');
$elem = $(this).parent().parent().find(settings.element);
// console.log($elem);
$elem.each(function (index, item) {
// console.log(item);
$(item).parent().removeClass(settings.overClass);
$(item).parent().removeClass(settings.moveClass);
});
}
// set the items to draggable
$elem.filter(settings.excludePatt).attr('draggable', true);
$this.off('dragstart');
$this.off('dragenter');
$this.off('dragover');
$this.off('dragleave');
$this.off('drop');
$this.off('dragend');
$this.on('dragstart', settings.element, handleDragStart);
//$this.on('dragenter', settings.element, handleDragEnter);
//$this.on('dragover', settings.element, handleDragOver);
$this.on('dragleave', settings.element, handleDragLeave);
$this.on('drop', settings.element, handleDrop);
$this.on('dragend', settings.element, handleDragEnd);
//$this.on('dragstart', ".item", handleDragStart);
$this.on('dragenter', settings.dropzone, handleDragEnter);
$this.on('dragover', settings.dropzone, handleDragOver);
$this.on('dragleave', settings.dropzone, handleDragLeave);
$this.on('drop', settings.dropzone, handleDrop);
// $this.on('dragend', settings.dropzone, handleDragEnd);
});
};