-
Notifications
You must be signed in to change notification settings - Fork 12
/
ui.sortable.js
263 lines (207 loc) · 8.04 KB
/
ui.sortable.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
if (window.Node && Node.prototype && !Node.prototype.contains) {
Node.prototype.contains = function (arg) {
return !!(this.compareDocumentPosition(arg) & 16)
}
}
(function($) {
//Make nodes selectable by expression
$.extend($.expr[':'], { sortable: "(' '+a.className+' ').indexOf(' ui-sortable ')" });
$.fn.sortable = function(o) {
return this.each(function() {
new $.ui.sortable(this,o);
});
}
//Macros for external methods that support chaining
var methods = "destroy,enable,disable,refresh".split(",");
for(var i=0;i<methods.length;i++) {
var cur = methods[i], f;
eval('f = function() { var a = arguments; return this.each(function() { if(jQuery(this).is(".ui-sortable")) jQuery.data(this, "ui-sortable")["'+cur+'"](a); }); }');
$.fn["sortable"+cur.substr(0,1).toUpperCase()+cur.substr(1)] = f;
};
//get instance method
$.fn.sortableInstance = function() {
if($(this[0]).is(".ui-sortable")) return $.data(this[0], "ui-sortable");
return false;
};
$.ui.sortable = function(el,o) {
this.element = el;
this.set = [];
var options = {};
var self = this;
$.data(this.element, "ui-sortable", this);
$(el).addClass("ui-sortable");
$.extend(options, o);
$.extend(options, {
items: options.items || '> li',
cursor: options.cursor || 'move',
smooth: options.smooth != undefined ? options.smooth : true,
helper: 'clone',
containment: options.containment ? (options.containment == 'sortable' ? el : options.containment) : null,
zIndex: options.zIndex || 1000,
_start: function(h,p,c,t,e) {
self.start.apply(t, [self, e]); // Trigger the onStart callback
},
_beforeStop: function(h,p,c,t,e) {
self.stop.apply(t, [self, e]); // Trigger the onStart callback
},
_drag: function(h,p,c,t,e) {
self.drag.apply(t, [self, e]); // Trigger the onStart callback
},
startCondition: function() {
return !self.disabled;
}
});
//Get the items
var items = $(options.items, el);
//Let's determine the floating mode
options.floating = /left|right/.test(items.css('float'));
// cursor
$(el).css('cursor', options.cursor)
//Let's determine the parent's offset
if($(el).css('position') == 'static') $(el).css('position', 'relative');
options.offset = $(el).offset({ border: false });
items.each(function() {
new $.ui.mouseInteraction(this,options);
});
//Add current items to the set
items.each(function() {
self.set.push([this,null]);
});
this.options = options;
}
$.extend($.ui.sortable.prototype, {
plugins: {},
currentTarget: null,
lastTarget: null,
prepareCallbackObj: function(self, that) {
return {
helper: self.helper,
position: { left: self.pos[0], top: self.pos[1] },
offset: self.options.cursorAt,
draggable: self,
current: that,
options: self.options
}
},
refresh: function() {
//Get the items
var self = this;
var items = $(this.options.items, this.element);
var unique = [];
items.each(function() {
old = false;
for(var i=0;i<self.set.length;i++) { if(self.set[i][0] == this) old = true; }
if(!old) unique.push(this);
});
for(var i=0;i<unique.length;i++) {
new $.ui.mouseInteraction(unique[i],self.options);
}
//Add current items to the set
this.set = [];
items.each(function() {
self.set.push([this,null]);
});
},
destroy: function() {
$(this.element).removeClass("ui-sortable").removeClass("ui-sortable-disabled");
$(this.options.items, this.element).mouseInteractionDestroy();
},
enable: function() {
$(this.element).removeClass("ui-sortable-disabled");
this.disabled = false;
},
disable: function() {
$(this.element).addClass("ui-sortable-disabled");
this.disabled = true;
},
start: function(that, e) {
var o = this.options;
if(o.hoverClass) {
that.helper = $('<div class="'+o.hoverClass+'"></div>').appendTo('body').css({
height: this.element.offsetHeight+'px',
width: this.element.offsetWidth+'px',
position: 'absolute'
});
}
if(o.zIndex) {
if($(this.helper).css("zIndex")) o.ozIndex = $(this.helper).css("zIndex");
$(this.helper).css('zIndex', o.zIndex);
}
that.firstSibling = $(this.element).prev()[0];
$(this.element).triggerHandler("sortstart", [e, that.prepareCallbackObj(this)], o.start);
$(this.element).css('visibility', 'hidden');
return false;
},
stop: function(that, e) {
var o = this.options;
var self = this;
var pos = this.pos; // <---- append ------
if(o.smooth) {
var os = $(this.element).offset();
o.beQuietAtEnd = true;
$(this.helper).animate({ left: os.left - o.po.left, top: os.top - o.po.top }, 500, stopIt);
} else {
stopIt();
}
function stopIt() {
self.pos = pos; // <---- append ------
$(self.element).css('visibility', 'visible');
if(that.helper) that.helper.remove();
if(self.helper != self.element) $(self.helper).remove();
if(o.ozIndex)
$(self.helper).css('zIndex', o.ozIndex);
//Let's see if the position in DOM has changed
if($(self.element).prev()[0] != that.firstSibling) {
//alert(o.update);
$(self.element).triggerHandler("sortupdate", [e, that.prepareCallbackObj(self, that)], o.update);
}
$(self.element).triggerHandler("sortstop", [e, that.prepareCallbackObj(self, that)], o.stop);
}
return false;
},
drag: function(that, e) {
var o = this.options;
this.pos = [this.pos[0]-(o.cursorAt.left ? o.cursorAt.left : 0), this.pos[1]-(o.cursorAt.top ? o.cursorAt.top : 0)];
var nv = $(this.element).triggerHandler("sort", [e, that.prepareCallbackObj(this)], o.sort);
var nl = (nv && nv.left) ? nv.left : this.pos[0];
var nt = (nv && nv.top) ? nv.top : this.pos[1];
var m = that.set;
var p = this.pos[1];
for(var i=0;i<m.length;i++) {
var ci = $(m[i][0]); var cio = m[i][0];
if(this.element.contains(cio)) continue;
var cO = ci.offset(); //TODO: Caching
cO = { top: cO.top, left: cO.left };
var mb = function(e) { if(true || o.lba != cio) { ci.before(e); o.lba = cio; } }
var ma = function(e) { if(true || o.laa != cio) { ci.after(e); o.laa = cio; } }
if(o.floating) {
var overlap = ((cO.left - (this.pos[0]+(this.options.po ? this.options.po.left : 0)))/this.helper.offsetWidth);
if(!(cO.top < this.pos[1]+(this.options.po ? this.options.po.top : 0) + cio.offsetHeight/2 && cO.top + cio.offsetHeight > this.pos[1]+(this.options.po ? this.options.po.top : 0) + cio.offsetHeight/2)) continue;
} else {
var overlap = ((cO.top - (this.pos[1]+(this.options.po ? this.options.po.top : 0)))/this.helper.offsetHeight);
if(!(cO.left < this.pos[0]+(this.options.po ? this.options.po.left : 0) + cio.offsetWidth/2 && cO.left + cio.offsetWidth > this.pos[0]+(this.options.po ? this.options.po.left : 0) + cio.offsetWidth/2)) continue;
}
if(overlap >= 0 && overlap <= 0.5) { //Overlapping at top
ci.prev().length ? ma(this.element) : mb(this.element); break;
}
if(overlap < 0 && overlap > -0.5) { //Overlapping at bottom
ci.next()[0] == this.element ? mb(this.element) : ma(this.element); break;
}
}
//Let's see if the position in DOM has changed
if($(this.element).prev()[0] != that.lastSibling) {
$(this.element).triggerHandler("sortchange", [e, that.prepareCallbackObj(this, that)], this.options.change);
that.lastSibling = $(this.element).prev()[0];
}
if(that.helper) { //reposition helper if available
var to = $(this.element).offset();
that.helper.css({
top: to.top+'px',
left: to.left+'px'
});
}
$(this.helper).css('left', nl+'px').css('top', nt+'px'); // Stick the helper to the cursor
return false;
}
});
})(jQuery);