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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
// Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
//
// Element.Class part Copyright (c) 2005 by Rick Olson
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Element.Class = {
// Element.toggleClass(element, className) toggles the class being on/off
// Element.toggleClass(element, className1, className2) toggles between both classes,
// defaulting to className1 if neither exist
toggle: function(element, className) {
if(Element.Class.has(element, className)) {
Element.Class.remove(element, className);
if(arguments.length == 3) Element.Class.add(element, arguments[2]);
} else {
Element.Class.add(element, className);
if(arguments.length == 3) Element.Class.remove(element, arguments[2]);
}
},
// gets space-delimited classnames of an element as an array
get: function(element) {
element = $(element);
return element.className.split(' ');
},
// functions adapted from original functions by Gavin Kistner
remove: function(element) {
element = $(element);
var regEx;
for(var i = 1; i < arguments.length; i++) {
regEx = new RegExp("^" + arguments[i] + "\\b\\s*|\\s*\\b" + arguments[i] + "\\b", 'g');
element.className = element.className.replace(regEx, '')
}
},
add: function(element) {
element = $(element);
for(var i = 1; i < arguments.length; i++) {
Element.Class.remove(element, arguments[i]);
element.className += (element.className.length > 0 ? ' ' : '') + arguments[i];
}
},
// returns true if all given classes exist in said element
has: function(element) {
element = $(element);
if(!element || !element.className) return false;
var regEx;
for(var i = 1; i < arguments.length; i++) {
regEx = new RegExp("\\b" + arguments[i] + "\\b");
if(!regEx.test(element.className)) return false;
}
return true;
},
// expects arrays of strings and/or strings as optional paramters
// Element.Class.has_any(element, ['classA','classB','classC'], 'classD')
has_any: function(element) {
element = $(element);
if(!element || !element.className) return false;
var regEx;
for(var i = 1; i < arguments.length; i++) {
if((typeof arguments[i] == 'object') &&
(arguments[i].constructor == Array)) {
for(var j = 0; j < arguments[i].length; j++) {
regEx = new RegExp("\\b" + arguments[i][j] + "\\b");
if(regEx.test(element.className)) return true;
}
} else {
regEx = new RegExp("\\b" + arguments[i] + "\\b");
if(regEx.test(element.className)) return true;
}
}
return false;
},
childrenWith: function(element, className) {
var children = $(element).getElementsByTagName('*');
var elements = new Array();
for (var i = 0; i < children.length; i++) {
if (Element.Class.has(children[i], className)) {
elements.push(children[i]);
break;
}
}
return elements;
}
}
/*--------------------------------------------------------------------------*/
var Droppables = {
drops: false,
add: function(element) {
var element = $(element);
var options = {
greedy: true,
hoverclass: null
}.extend(arguments[1] || {});
// cache containers
if(options.containment) {
options._containers = new Array();
var containment = options.containment;
if((typeof containment == 'object') &&
(containment.constructor == Array)) {
for(var i=0; i<containment.length; i++)
options._containers.push($(containment[i]));
} else {
options._containers.push($(containment));
}
options._containers_length =
options._containers.length-1;
}
if(element.style.position=='') //fix IE
element.style.position = 'relative';
// activate the droppable
element.droppable = options;
if(!this.drops) this.drops = [];
this.drops.push(element);
},
is_contained: function(element, drop) {
var containers = drop.droppable._containers;
var parentNode = element.parentNode;
var i = drop.droppable._containers_length;
do { if(parentNode==containers[i]) return true; } while (i--);
return false;
},
is_affected: function(pX, pY, element, drop) {
return (
(drop!=element) &&
((!drop.droppable._containers) ||
this.is_contained(element, drop)) &&
((!drop.droppable.accept) ||
(Element.Class.has_any(element, drop.droppable.accept))) &&
Position.within(drop, pX, pY) );
},
deactivate: function(drop) {
Element.Class.remove(drop, drop.droppable.hoverclass);
this.last_active = null;
},
activate: function(drop) {
if(this.last_active) this.deactivate(this.last_active);
if(drop.droppable.hoverclass) {
Element.Class.add(drop, drop.droppable.hoverclass);
this.last_active = drop;
}
},
show: function(event, element) {
if(!this.drops) return;
var pX = Event.pointerX(event);
var pY = Event.pointerY(event);
Position.prepare();
var i = this.drops.length-1; do {
var drop = this.drops[i];
if(this.is_affected(pX, pY, element, drop)) {
if(drop.droppable.onHover)
drop.droppable.onHover(
element, drop, Position.overlap(drop.droppable.overlap, drop));
if(drop.droppable.greedy) {
this.activate(drop);
return;
}
}
} while (i--);
},
fire: function(event, element) {
if(!this.last_active) return;
Position.prepare();
if (this.is_affected(Event.pointerX(event), Event.pointerY(event), element, this.last_active))
if (this.last_active.droppable.onDrop)
this.last_active.droppable.onDrop(element, this.last_active);
},
reset: function() {
if(this.last_active)
this.deactivate(this.last_active);
}
}
Draggables = {
observers: new Array(),
addObserver: function(observer) {
this.observers.push(observer);
},
notify: function(eventName, draggable) { // 'onStart', 'onEnd'
for(var i = 0; i < this.observers.length; i++)
this.observers[i][eventName](draggable);
}
}
/*--------------------------------------------------------------------------*/
Draggable = Class.create();
Draggable.prototype = {
initialize: function(element) {
var options = {
handle: false,
starteffect: function(element) {
new Effect.Opacity(element, {duration:0.2, from:1.0, to:0.7});
},
reverteffect: function(element, top_offset, left_offset) {
new Effect.MoveBy(element, -top_offset, -left_offset, {duration:0.4});
},
endeffect: function(element) {
new Effect.Opacity(element, {duration:0.2, from:0.7, to:1.0});
},
zindex: 1000,
revert: false
}.extend(arguments[1] || {});
this.element = $(element);
this.handle = options.handle ? $(options.handle) : this.element;
// fix IE
if(!this.element.style.position)
this.element.style.position = 'relative';
this.offsetX = 0;
this.offsetY = 0;
this.originalLeft = this.currentLeft();
this.originalTop = this.currentTop();
this.originalX = this.element.offsetLeft;
this.originalY = this.element.offsetTop;
this.originalZ = parseInt(this.element.style.zIndex || "0");
this.options = options;
this.active = false;
this.dragging = false;
Event.observe(this.handle, "mousedown", this.startDrag.bindAsEventListener(this));
Event.observe(document, "mouseup", this.endDrag.bindAsEventListener(this));
Event.observe(document, "mousemove", this.update.bindAsEventListener(this));
Event.observe(document, "keypress", this.keyPress.bindAsEventListener(this));
},
currentLeft: function() {
return parseInt(this.element.style.left || '0');
},
currentTop: function() {
return parseInt(this.element.style.top || '0')
},
startDrag: function(event) {
if(Event.isLeftClick(event)) {
this.active = true;
var style = this.element.style;
this.originalY = this.element.offsetTop - this.currentTop() - this.originalTop;
this.originalX = this.element.offsetLeft - this.currentLeft() - this.originalLeft;
this.offsetY = event.clientY - this.originalY - this.originalTop;
this.offsetX = event.clientX - this.originalX - this.originalLeft;
Event.stop(event);
}
},
finishDrag: function(event, success) {
this.active = false;
this.dragging = false;
if(success) Droppables.fire(event, this.element);
Draggables.notify('onEnd', this);
var revert = this.options.revert;
if(revert && typeof revert == 'function') revert = revert(this.element);
if(revert && this.options.reverteffect) {
this.options.reverteffect(this.element,
this.currentTop()-this.originalTop,
this.currentLeft()-this.originalLeft);
} else {
this.originalLeft = this.currentLeft();
this.originalTop = this.currentTop();
}
this.element.style.zIndex = this.originalZ;
if(this.options.endeffect)
this.options.endeffect(this.element);
Droppables.reset();
},
keyPress: function(event) {
if(this.active) {
if(event.keyCode==Event.KEY_ESC) {
this.finishDrag(event, false);
Event.stop(event);
}
}
},
endDrag: function(event) {
if(this.active && this.dragging) {
this.finishDrag(event, true);
Event.stop(event);
}
this.active = false;
this.dragging = false;
},
draw: function(event) {
var style = this.element.style;
this.originalX = this.element.offsetLeft - this.currentLeft() - this.originalLeft;
this.originalY = this.element.offsetTop - this.currentTop() - this.originalTop;
if((!this.options.constraint) || (this.options.constraint=='horizontal'))
style.left = ((event.clientX - this.originalX) - this.offsetX) + "px";
if((!this.options.constraint) || (this.options.constraint=='vertical'))
style.top = ((event.clientY - this.originalY) - this.offsetY) + "px";
if(style.visibility=="hidden") style.visibility = ""; // fix gecko rendering
},
update: function(event) {
if(this.active) {
if(!this.dragging) {
var style = this.element.style;
this.dragging = true;
if(style.position=="") style.position = "relative";
style.zIndex = this.options.zindex;
Draggables.notify('onStart', this);
if(this.options.starteffect) this.options.starteffect(this.element);
}
Droppables.show(event, this.element);
this.draw(event);
if(this.options.change) this.options.change(this);
// fix AppleWebKit rendering
if(navigator.appVersion.indexOf('AppleWebKit')>0) window.scrollBy(0,0);
Event.stop(event);
}
}
}
/*--------------------------------------------------------------------------*/
SortableObserver = Class.create();
SortableObserver.prototype = {
initialize: function(element, observer) {
this.element = $(element);
this.observer = observer;
this.lastValue = Sortable.serialize(this.element);
},
onStart: function() {
this.lastValue = Sortable.serialize(this.element);
},
onEnd: function() {
if(this.lastValue != Sortable.serialize(this.element))
this.observer(this.element)
}
}
Sortable = {
create: function(element) {
var element = $(element);
var options = {
tag: 'li', // assumes li children, override with tag: 'tagname'
overlap: 'vertical', // one of 'vertical', 'horizontal'
constraint: 'vertical', // one of 'vertical', 'horizontal', false
containment: element, // also takes array of elements (or id's); or false
handle: false, // or a CSS class
only: false,
hoverclass: null,
onChange: function() {},
onUpdate: function() {}
}.extend(arguments[1] || {});
element.sortable = options;
// build options for the draggables
var options_for_draggable = {
revert: true,
constraint: options.constraint,
handle: handle };
if(options.starteffect)
options_for_draggable.starteffect = options.starteffect;
if(options.reverteffect)
options_for_draggable.reverteffect = options.reverteffect;
if(options.endeffect)
options_for_draggable.endeffect = options.endeffect;
if(options.zindex)
options_for_draggable.zindex = options.zindex;
// build options for the droppables
var options_for_droppable = {
overlap: options.overlap,
containment: options.containment,
hoverclass: options.hoverclass,
onHover: function(element, dropon, overlap) {
if(overlap>0.5) {
if(dropon.previousSibling != element) {
var oldParentNode = element.parentNode;
element.style.visibility = "hidden"; // fix gecko rendering
dropon.parentNode.insertBefore(element, dropon);
if(dropon.parentNode!=oldParentNode && oldParentNode.sortable)
oldParentNode.sortable.onChange(element);
if(dropon.parentNode.sortable)
dropon.parentNode.sortable.onChange(element);
}
} else {
var nextElement = dropon.nextSibling || null;
if(nextElement != element) {
var oldParentNode = element.parentNode;
element.style.visibility = "hidden"; // fix gecko rendering
dropon.parentNode.insertBefore(element, nextElement);
if(dropon.parentNode!=oldParentNode && oldParentNode.sortable)
oldParentNode.sortable.onChange(element);
if(dropon.parentNode.sortable)
dropon.parentNode.sortable.onChange(element);
}
}
}
}
// fix for gecko engine
Element.cleanWhitespace(element);
// for onupdate
Draggables.addObserver(new SortableObserver(element, options.onUpdate));
// make it so
var elements = element.childNodes;
for (var i = 0; i < elements.length; i++)
if(elements[i].tagName && elements[i].tagName==options.tag.toUpperCase() &&
(!options.only || (Element.Class.has(elements[i], options.only)))) {
// handles are per-draggable
var handle = options.handle ?
Element.Class.childrenWith(elements[i], options.handle)[0] : elements[i];
new Draggable(elements[i], options_for_draggable.extend({ handle: handle }));
Droppables.add(elements[i], options_for_droppable);
}
},
serialize: function(element) {
var element = $(element);
var options = {
tag: element.sortable.tag,
only: element.sortable.only,
name: element.id
}.extend(arguments[1] || {});
var items = $(element).childNodes;
var queryComponents = new Array();
for(var i=0; i<items.length; i++)
if(items[i].tagName && items[i].tagName==options.tag.toUpperCase() &&
(!options.only || (Element.Class.has(items[i], options.only))))
queryComponents.push(
encodeURIComponent(options.name) + "[]=" +
encodeURIComponent(items[i].id.split("_")[1]));
return queryComponents.join("&");
}
}
|