-
Notifications
You must be signed in to change notification settings - Fork 0
/
dropTo.js
executable file
·353 lines (295 loc) · 10.8 KB
/
dropTo.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*!require:*/
/*
JQueryPlugin: jQuery.fn.dropTo
Drop an element relatively to another element.
Usage:
$(eleA).dropTo(eleB).left()
Options:
target - the object of reference for positioning.
*/
(function($){
var dataKey = 'jQuery.fn.dropTo.Data';
var defaultOptions = {
target: null,
animCacheProp: null
};
var settingsHolder = {};
function init(i, dom){
var el = $(dom);
var settings = {};
$.extend(settings, settingsHolder);
// make sure it is jquery obj
settings.target = $(settings.target);
var offset = null;
el.css('position', 'absolute');
// we'd better not change the hierachical structure of dom,
// if el and target don't share same parent,
// we need to caculate the position diff between them
if (el.parent().get(0) == settings.target.parent().get(0) &&
// temporary fix for jQuery bug 8945
// http://bugs.jquery.com/ticket/8945
$.browser.webkit == null)
offset = settings.target.position();
else{
var diff = {
left: settings.target.offset().left -
settings.target.position().left -
(el.offset().left - el.position().left),
top: settings.target.offset().top -
settings.target.position().top -
(el.offset().top - el.position().top)
}
offset = settings.target.position();
offset.top += diff.top;
offset.left += diff.left;
//el.appendTo(document.body);
}
var data = {
settings: settings,
offset: offset
};
el.data(dataKey, data);
}
/**
* @function setBidiLeft Set the css('left') by default, but if it is in a rtl element,
* will calculate corresponding 'right distance' and user css('right') instead of left.
* @private
*/
function setBidiLeft(el, left, offset, animOptions){
var parent = el.offsetParent();
var cssProp = 'left', value = 0;
// if offsetparent is body and it is a non-positioned element
// that means the positioning element is <html />
if (parent[0].tagName.toUpperCase() == 'BODY' &&
!(/absolute|relative|fixed/i.test(parent.css('position')))){
parent = $(document.documentElement);
}
if (parent.length <= 0 || el.css('direction') != 'ltr'){
// right to left layout
var right = parent.outerWidth() - left - el.outerWidth();
if (offset != null && !isNaN(offset * 1)){
right -= offset;
}
cssProp = 'right';
value = right;
}
else{
if (offset != null && !isNaN(offset * 1)){
left += offset;
}
// left to right layout (default)
cssProp = 'left';
value = left;
}
setTargetPos(el, cssProp, value, animOptions);
};
/**
* @function setTop Set css('top') and plus offset
* @private
*/
function setTop(el, top, offset, animOptions){
var cssProp = 'left', value = 0;
if (offset != null && !isNaN(offset * 1)){
top += offset;
}
cssProp = 'top';
value = top;
setTargetPos(el, cssProp, value, animOptions);
};
/**
* @function setTargetPos
* set target position or move target to position with animation
**/
function setTargetPos(el, cssProp, value, animOptions){
var animProp = {};
if (!animOptions || animOptions === false){
el.css(cssProp, value);
}
else{
if (animOptions === true){
animOptions = {};
}
// if animCache enabled, it means user wants to do all
// animation all together.
if (animOptions.dtDelay === true){
delete animOptions.dtDelay;
if (el.data(dataKey).animCacheProp == null){
el.data(dataKey).animCacheProp = {};
}
el.data(dataKey).animCacheProp[cssProp] = value;
}
else{
// if there are delayed animation
if (el.data(dataKey).animCacheProp){
animProp = el.data(dataKey).animCacheProp;
el.data(dataKey).animCacheProp = null;
}
animProp[cssProp] = value;
el.animate(animProp, animOptions);
}
}
}
/**
* Helpers
**/
/**
* @function curry Curry helper function, return a function that calls
* 'func' and pass remaining arguments to 'func'.
* @private
*/
function curry(func){
var args = Array.prototype.slice.apply(arguments);
args.shift();
return function(){
var innerArgs = Array.prototype.slice.apply(arguments);
innerArgs = innerArgs.concat(args);
return func.apply(this, innerArgs);
};
};
function callInnerFunc(context, innerFunc, outerArguments){
var args = [];
args.push(innerFunc);
var outerArgs = Array.prototype.slice.call(outerArguments);
args = args.concat(outerArgs);
return context.each(curry.apply(context, args));
};
function innerFuncArgumentConcater(funcToCall, args, outerArguments, amongToRemove){
var outerArgs = Array.prototype.slice.call(outerArguments);
outerArgs.splice(0,amongToRemove);
args = args.concat(outerArgs);
funcToCall.apply(this, args);
};
/**
* Targetting Methods
**/
function left(i, dom, offset){
var el = $(dom);
var data = el.data(dataKey);
if (data == null) return;
var left = data.offset.left - el.outerWidth();
innerFuncArgumentConcater(setBidiLeft, [el, left], arguments, 2);
};
function top(i, dom, offset){
var el = $(dom);
var data = el.data(dataKey);
if (data == null) return;
var top = data.offset.top - el.outerHeight();
innerFuncArgumentConcater(setTop, [el, top], arguments, 2);
};
function right(i, dom, offset){
var el = $(dom);
var data = el.data(dataKey);
if (data == null) return;
var target = data.settings.target;
var left = data.offset.left + target.outerWidth();
innerFuncArgumentConcater(setBidiLeft, [el, left], arguments, 2);
}
function bottom(i, dom, offset){
var el = $(dom);
var data = el.data(dataKey);
if (data == null) return;
var target = data.settings.target;
var top = data.offset.top + target.outerHeight();
innerFuncArgumentConcater(setTop, [el, top], arguments, 2);
}
function insideTop(i, dom, offset){
var el = $(dom);
var data = el.data(dataKey);
if (data == null) return;
var target = data.settings.target;
var top = data.offset.top;
innerFuncArgumentConcater(setTop, [el, top], arguments, 2);
}
function insideLeft(i, dom, offset){
var el = $(dom);
var data = el.data(dataKey);
if (data == null) return;
var target = data.settings.target;
var left = data.offset.left;
innerFuncArgumentConcater(setBidiLeft, [el, left], arguments, 2);
}
function insideRight(i, dom, offset){
var el = $(dom);
var data = el.data(dataKey);
if (data == null) return;
var target = data.settings.target;
var left = data.offset.left + target.outerWidth() - el.outerWidth();
innerFuncArgumentConcater(setBidiLeft, [el, left], arguments, 2);
}
function insideBottom(i, dom, offset){
var el = $(dom);
var data = el.data(dataKey);
if (data == null) return;
var target = data.settings.target;
var top = data.offset.top + target.outerHeight() - el.outerHeight();
innerFuncArgumentConcater(setTop, [el, top], arguments, 2);
}
function center(i, dom, offset){
var el = $(dom);
var data = el.data(dataKey);
if (data == null) return;
var target = data.settings.target;
var left = data.offset.left + (target.outerWidth() - el.outerWidth()) / 2;
innerFuncArgumentConcater(setBidiLeft, [el, left], arguments, 2);
}
function middle(i, dom, offset){
var el = $(dom);
var data = el.data(dataKey);
if (data == null) return;
var target = data.settings.target;
var top = data.offset.top + (target.outerHeight() - el.outerHeight()) / 2;
innerFuncArgumentConcater(setTop, [el, top], arguments, 2);
}
$.fn.extend({
dropTo: function(another, options){
// another can be option or jQuery obj or jQuery selector
if (Object.prototype.toString.call(another).toLowerCase().indexOf('string') > 0 ||
another.length != null && another.attr != null){
if (options == null){
options = {};
}
options.target = $(another);
}
else {
options = another;
}
$.extend(settingsHolder, defaultOptions, options);
return this.each(init);
},
/*
* @function outerLeft, outerTop, outerRight, outerBottom...
* @param offset
* @param animationOptions
*/
outerLeft: function(){
return callInnerFunc(this, left, arguments);
},
outerTop: function(){
return callInnerFunc(this, top, arguments);
},
outerRight: function(){
return callInnerFunc(this, right, arguments);
},
outerBottom: function(){
return callInnerFunc(this, bottom, arguments);
},
innerLeft: function(){
return callInnerFunc(this, insideLeft, arguments);
},
innerTop: function(){
return callInnerFunc(this, insideTop, arguments);
},
innerRight: function(){
return callInnerFunc(this, insideRight, arguments);
},
innerBottom: function(){
return callInnerFunc(this, insideBottom, arguments);
},
atCenter: function(){
return callInnerFunc(this, center, arguments);
},
atMiddle: function(){
return callInnerFunc(this, middle, arguments);
}
});
})(jQuery);