-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.eNestedSortable.js
232 lines (194 loc) · 7.38 KB
/
jquery.eNestedSortable.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
/*
* jQuery Evented Nested Sortable
* v 0.0.6 / 2014-04-15
* https://github.com/andersonlevita/eNestedSortable
*
* Copyright (c) 2014-2014 Anderson Nogueira
* Licensed under the MIT License
* http://www.opensource.org/licenses/mit-license.php
*/
(function($) {
var defaults = {
opacity: "0.5",
nestleSize: 40,
ulSelector: ".e-nested-sortable-ul",
liSelector: ".e-nested-sortable-li",
isAllowed: function(data){ return true; } //data is {element: dragEl, parent: newParent, index: newIndex}
};
function Plugin(el, options){
this.options = $.extend({}, defaults, options);
this.el = $(el);
this.ulClass = "e-nested-sortable-ul";
this.liClass = "e-nested-sortable-li";
this.placeHolderClass = "e-nested-sortable-placeholder";
this.placeHolderErrorClass = "e-nested-sortable-placeholder-error";
this.placeHolerSelector = "." + this.placeHolderClass;
this.init();
}
Plugin.prototype.init = function(){
this.isHandle = false;
this.isValid = true;
this.mouseStart = {x: 0, y: 0};
this.offsetStart = {left: 0, top: 0};
this.fixIndex = 0;
var that = this;
function addSelectors(ul) {
var ulTag = ul[0].tagName;
ul.hasClass(that.ulClass) || ul.addClass(that.ulClass);
ul.children("li").each(function(){
var li = $(this);
li.hasClass(that.liClass) || li.addClass(that.liClass);
li.children(ulTag).each(function(){
addSelectors($(this));
});
});
}
function onDragStart(e) {
that.dragStart(e);
}
function onMove(e) {
that.move(e);
}
function onDrop(e) {
that.drop(e);
}
if(this.options.ulSelector == defaults.ulSelector || this.options.liSelector == defaults.liSelector)
addSelectors(this.el);
this.el.on("mousedown", this.options.liSelector, onDragStart);
$(window).mouseup(onDrop).mousemove(onMove);
};
Plugin.prototype.dragStart = function(e){
e.preventDefault();
e.stopPropagation();
this.reset();
this.dragEl = $(e.target).closest(this.options.liSelector);
this.firstStyle = this.dragEl.attr("style") || "";
this.dragEl.css({"position": "absolute", "opacity": this.options.opacity, "z-index": "-1"});
this.isHandle = true;
this.mouseStart = {x: e.pageX, y: e.pageY};
this.offsetStart = this.dragEl.offset();
this.index = this.oldIndex = this.dragEl.index();
this.parent = this.oldParent = this.dragEl.closest(this.options.ulSelector);
this.attachPlaceHolder(this.index, this.parent, this.dragEl.outerHeight());
this.dragEl.offset(this.offsetStart);
this.el.trigger("drag", {element: this.dragEl, parent: this.parent, index: this.index});
};
Plugin.prototype.attachPlaceHolder = function(index, parent, height){
if (!this.placeholder)
this.placeholder = $("<div>").css({
"height": height + "px"
}).addClass(this.placeHolderClass);
else if (height !== null)
this.placeholder.css("height", height);
if (index === 0)
parent.prepend(this.placeholder);
else
parent.children(this.options.liSelector + ":nth-child(" + index + ")").after(this.placeholder);
this.index = index;
this.parent = parent;
};
Plugin.prototype.move = function(e){
if (!this.isHandle)
return;
var diffX = e.pageX - this.mouseStart.x;
var diffY = e.pageY - this.mouseStart.y;
var offset = {
left: this.offsetStart.left + diffX,
top: this.offsetStart.top + diffY
};
this.dragEl.offset(offset);
e.preventDefault();
var hoverEl = $(window.document.elementFromPoint(e.pageX, e.pageY)).closest(this.options.ulSelector + "," + this.options.liSelector + "," + this.placeHolerSelector);
var hoverIndex = hoverEl.index();
var hoverParent = hoverEl.closest(this.options.ulSelector);
var isNesting = this.nestle(hoverEl, hoverIndex, hoverParent);
if (isNesting){
hoverIndex = isNesting.hoverIndex;
hoverParent = isNesting.hoverParent;
}
if (hoverEl.length === 0 || hoverEl[0] == this.dragEl[0] || //Not Valid
(hoverIndex == this.index && hoverParent[0] == this.parent[0])) //Not Modified
return;
this.placeholder.detach();
this.attachPlaceHolder(hoverIndex, hoverParent);
this.fixIndex = hoverParent[0] == this.oldParent[0] && hoverIndex > this.oldIndex ? 1 : 0; //Fix diff index caused by placeholder element
var retObj = {
element: this.dragEl,
parent: this.parent,
index: this.index - this.fixIndex
};
if (this.options.isAllowed(retObj)){
this.el.trigger("move", retObj);
this.placeholder.removeClass(this.placeHolderErrorClass);
this.isValid = true;
}else{
this.placeholder.addClass(this.placeHolderErrorClass);
this.isValid = false;
}
};
Plugin.prototype.nestle = function(hoverEl, hoverIndex, hoverParent){
if (hoverEl.hasClass(this.placeHolderClass)){
if (hoverIndex > 0){ //Nestle
var aboveEl = hoverParent.children(this.options.liSelector + ":nth-child(" + hoverIndex + ")");
if (aboveEl.length > 0 && this.dragEl.offset().left - aboveEl.offset().left > this.options.nestleSize){
var aboveGroup = aboveEl.children(this.options.ulSelector).first();
if (aboveGroup.length !== 0 && aboveGroup.is(":visible") && aboveGroup.children(this.options.liSelector).length === 0){
return {
hoverParent: aboveGroup,
hoverIndex: 0
};
}
}
}else if (hoverParent.children(this.options.liSelector).length === 0 && this.dragEl.offset().left - hoverParent.closest(this.options.liSelector).offset().left <= this.options.nestleSize){ //Leave nestle
var el = hoverParent.closest(this.options.liSelector);
return {
hoverParent: el,
hoverIndex: el.index()
};
}
}
return false;
};
Plugin.prototype.drop = function(){
this.detachPlaceholder();
this.index -= this.fixIndex;
if (this.dragEl && this.parent && this.isValid && (this.parent[0] != this.oldParent[0] || this.index != this.oldIndex))
this.el.trigger("drop", {element: this.dragEl, newParent: this.parent, newIndex: this.index, oldParent: this.oldParent, oldIndex: this.oldIndex});
this.reset();
};
Plugin.prototype.reset = function(){
this.isHandle = false;
this.isValid = true;
this.fixIndex = 0;
if (this.dragEl){
this.dragEl.attr("style", this.firstStyle);
this.dragEl = null;
}
this.detachPlaceholder();
};
Plugin.prototype.detachPlaceholder = function(){
if (this.placeholder){
this.placeholder.detach();
this.placeholder.removeClass(this.placeHolderErrorClass);
}
};
// Plugin definition
// =================
var old = $.fn.eNestedSortable;
$.fn.eNestedSortable = function(option, params) {
return this.each(function() {
var $this = $(this);
var plugin = $this.data('eNestedSortable');
var options = typeof option == 'object' && option;
if (!plugin) $this.data('eNestedSortable', (plugin = new Plugin(this, options)));
if (typeof option == 'string') plugin[option].apply(plugin, Array.prototype.slice.call(arguments, 1));
});
};
$.fn.eNestedSortable.Constructor = Plugin;
// eNestedSortable no conflict
// ===========================
$.fn.eNestedSortable.noConflict = function() {
$.fn.eNestedSortable = old;
return this;
};
}(jQuery));