1 // Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
2 // (c) 2005-2007 Sammi Williams (http://www.oriontransfer.co.nz, sammi@oriontransfer.co.nz)
4 // script.aculo.us is freely distributable under the terms of an MIT-style license.
5 // For details, see the script.aculo.us web site: http://script.aculo.us/
8 JSAN.use('Scriptaculous.Effects');
10 if(Object.isUndefined(Effect))
11 throw("DragDrop.js requires including script.aculo.us' Effects.js library");
16 remove: function(element) {
17 this.drops = this.drops.reject(function(d) { return d.element==$(element) });
20 add: function(element) {
22 var options = Object.extend({
26 }, arguments[1] || { });
29 if(options.containment) {
30 options._containers = [];
31 var containment = options.containment;
32 if(Object.isArray(containment)) {
33 containment.each( function(c) { options._containers.push($(c)) });
35 options._containers.push($(containment));
39 if(options.accept) options.accept = [options.accept].flatten();
41 Element.makePositioned(element); // fix IE
42 options.element = element;
44 this.drops.push(options);
47 findDeepestChild: function(drops) {
50 for (i = 1; i < drops.length; ++i)
51 if (Element.isParent(drops[i].element, deepest.element))
57 isContained: function(element, drop) {
60 containmentNode = element.treeNode;
62 containmentNode = element.parentNode;
64 return drop._containers.detect(function(c) { return containmentNode == c });
67 isAffected: function(point, element, drop) {
69 (drop.element!=element) &&
70 ((!drop._containers) ||
71 this.isContained(element, drop)) &&
73 (Element.classNames(element).detect(
74 function(v) { return drop.accept.include(v) } ) )) &&
75 Position.within(drop.element, point[0], point[1]) );
78 deactivate: function(drop) {
80 Element.removeClassName(drop.element, drop.hoverclass);
81 this.last_active = null;
84 activate: function(drop) {
86 Element.addClassName(drop.element, drop.hoverclass);
87 this.last_active = drop;
90 show: function(point, element) {
91 if(!this.drops.length) return;
92 var drop, affected = [];
94 this.drops.each( function(drop) {
95 if(Droppables.isAffected(point, element, drop))
100 drop = Droppables.findDeepestChild(affected);
102 if(this.last_active && this.last_active != drop) this.deactivate(this.last_active);
104 Position.within(drop.element, point[0], point[1]);
106 drop.onHover(element, drop.element, Position.overlap(drop.overlap, drop.element));
108 if (drop != this.last_active) Droppables.activate(drop);
112 fire: function(event, element) {
113 if(!this.last_active) return;
116 if (this.isAffected([Event.pointerX(event), Event.pointerY(event)], element, this.last_active))
117 if (this.last_active.onDrop) {
118 this.last_active.onDrop(element, this.last_active.element, event);
125 this.deactivate(this.last_active);
133 register: function(draggable) {
134 if(this.drags.length == 0) {
135 this.eventMouseUp = this.endDrag.bindAsEventListener(this);
136 this.eventMouseMove = this.updateDrag.bindAsEventListener(this);
137 this.eventKeypress = this.keyPress.bindAsEventListener(this);
139 Event.observe(document, "mouseup", this.eventMouseUp);
140 Event.observe(document, "mousemove", this.eventMouseMove);
141 Event.observe(document, "keypress", this.eventKeypress);
143 this.drags.push(draggable);
146 unregister: function(draggable) {
147 this.drags = this.drags.reject(function(d) { return d==draggable });
148 if(this.drags.length == 0) {
149 Event.stopObserving(document, "mouseup", this.eventMouseUp);
150 Event.stopObserving(document, "mousemove", this.eventMouseMove);
151 Event.stopObserving(document, "keypress", this.eventKeypress);
155 activate: function(draggable) {
156 if(draggable.options.delay) {
157 this._timeout = setTimeout(function() {
158 Draggables._timeout = null;
160 Draggables.activeDraggable = draggable;
161 }.bind(this), draggable.options.delay);
163 window.focus(); // allows keypress events if window isn't currently focused, fails for Safari
164 this.activeDraggable = draggable;
168 deactivate: function() {
169 this.activeDraggable = null;
172 updateDrag: function(event) {
173 if(!this.activeDraggable) return;
174 var pointer = [Event.pointerX(event), Event.pointerY(event)];
175 // Mozilla-based browsers fire successive mousemove events with
176 // the same coordinates, prevent needless redrawing (moz bug?)
177 if(this._lastPointer && (this._lastPointer.inspect() == pointer.inspect())) return;
178 this._lastPointer = pointer;
180 this.activeDraggable.updateDrag(event, pointer);
183 endDrag: function(event) {
185 clearTimeout(this._timeout);
186 this._timeout = null;
188 if(!this.activeDraggable) return;
189 this._lastPointer = null;
190 this.activeDraggable.endDrag(event);
191 this.activeDraggable = null;
194 keyPress: function(event) {
195 if(this.activeDraggable)
196 this.activeDraggable.keyPress(event);
199 addObserver: function(observer) {
200 this.observers.push(observer);
201 this._cacheObserverCallbacks();
204 removeObserver: function(element) { // element instead of observer fixes mem leaks
205 this.observers = this.observers.reject( function(o) { return o.element==element });
206 this._cacheObserverCallbacks();
209 notify: function(eventName, draggable, event) { // 'onStart', 'onEnd', 'onDrag'
210 if(this[eventName+'Count'] > 0)
211 this.observers.each( function(o) {
212 if(o[eventName]) o[eventName](eventName, draggable, event);
214 if(draggable.options[eventName]) draggable.options[eventName](draggable, event);
217 _cacheObserverCallbacks: function() {
218 ['onStart','onEnd','onDrag'].each( function(eventName) {
219 Draggables[eventName+'Count'] = Draggables.observers.select(
220 function(o) { return o[eventName]; }
226 /*--------------------------------------------------------------------------*/
228 var Draggable = Class.create({
229 initialize: function(element) {
232 reverteffect: function(element, top_offset, left_offset) {
233 var dur = Math.sqrt(Math.abs(top_offset^2)+Math.abs(left_offset^2))*0.02;
234 new Effect.Move(element, { x: -left_offset, y: -top_offset, duration: dur,
235 queue: {scope:'_draggable', position:'end'}
238 endeffect: function(element) {
239 var toOpacity = Object.isNumber(element._opacity) ? element._opacity : 1.0;
240 new Effect.Opacity(element, {duration:0.2, from:0.7, to:toOpacity,
241 queue: {scope:'_draggable', position:'end'},
242 afterFinish: function(){
243 Draggable._dragging[element] = false
251 scrollSensitivity: 20,
253 snap: false, // false, or xy or [x,y] or function(x,y){ return [x,y] }
257 if(!arguments[1] || Object.isUndefined(arguments[1].endeffect))
258 Object.extend(defaults, {
259 starteffect: function(element) {
260 element._opacity = Element.getOpacity(element);
261 Draggable._dragging[element] = true;
262 new Effect.Opacity(element, {duration:0.2, from:element._opacity, to:0.7});
266 var options = Object.extend(defaults, arguments[1] || { });
268 this.element = $(element);
270 if(options.handle && Object.isString(options.handle))
271 this.handle = this.element.down('.'+options.handle, 0);
273 if(!this.handle) this.handle = $(options.handle);
274 if(!this.handle) this.handle = this.element;
276 if(options.scroll && !options.scroll.scrollTo && !options.scroll.outerHTML) {
277 options.scroll = $(options.scroll);
278 this._isScrollChild = Element.childOf(this.element, options.scroll);
281 Element.makePositioned(this.element); // fix IE
283 this.options = options;
284 this.dragging = false;
286 this.eventMouseDown = this.initDrag.bindAsEventListener(this);
287 Event.observe(this.handle, "mousedown", this.eventMouseDown);
289 Draggables.register(this);
292 destroy: function() {
293 Event.stopObserving(this.handle, "mousedown", this.eventMouseDown);
294 Draggables.unregister(this);
297 currentDelta: function() {
299 parseInt(Element.getStyle(this.element,'left') || '0'),
300 parseInt(Element.getStyle(this.element,'top') || '0')]);
303 initDrag: function(event) {
304 if(!Object.isUndefined(Draggable._dragging[this.element]) &&
305 Draggable._dragging[this.element]) return;
306 if(Event.isLeftClick(event)) {
307 // abort on form elements, fixes a Firefox issue
308 var src = Event.element(event);
309 if((tag_name = src.tagName.toUpperCase()) && (
311 tag_name=='SELECT' ||
312 tag_name=='OPTION' ||
313 tag_name=='BUTTON' ||
314 tag_name=='TEXTAREA')) return;
316 var pointer = [Event.pointerX(event), Event.pointerY(event)];
317 var pos = Position.cumulativeOffset(this.element);
318 this.offset = [0,1].map( function(i) { return (pointer[i] - pos[i]) });
320 Draggables.activate(this);
325 startDrag: function(event) {
326 this.dragging = true;
328 this.delta = this.currentDelta();
330 if(this.options.zindex) {
331 this.originalZ = parseInt(Element.getStyle(this.element,'z-index') || 0);
332 this.element.style.zIndex = this.options.zindex;
335 if(this.options.ghosting) {
336 this._clone = this.element.cloneNode(true);
337 this.element._originallyAbsolute = (this.element.getStyle('position') == 'absolute');
338 if (!this.element._originallyAbsolute)
339 Position.absolutize(this.element);
340 this.element.parentNode.insertBefore(this._clone, this.element);
343 if(this.options.scroll) {
344 if (this.options.scroll == window) {
345 var where = this._getWindowScroll(this.options.scroll);
346 this.originalScrollLeft = where.left;
347 this.originalScrollTop = where.top;
349 this.originalScrollLeft = this.options.scroll.scrollLeft;
350 this.originalScrollTop = this.options.scroll.scrollTop;
354 Draggables.notify('onStart', this, event);
356 if(this.options.starteffect) this.options.starteffect(this.element);
359 updateDrag: function(event, pointer) {
360 if(!this.dragging) this.startDrag(event);
362 if(!this.options.quiet){
364 Droppables.show(pointer, this.element);
367 Draggables.notify('onDrag', this, event);
370 if(this.options.change) this.options.change(this);
372 if(this.options.scroll) {
373 this.stopScrolling();
376 if (this.options.scroll == window) {
377 with(this._getWindowScroll(this.options.scroll)) { p = [ left, top, left+width, top+height ]; }
379 p = Position.page(this.options.scroll);
380 p[0] += this.options.scroll.scrollLeft + Position.deltaX;
381 p[1] += this.options.scroll.scrollTop + Position.deltaY;
382 p.push(p[0]+this.options.scroll.offsetWidth);
383 p.push(p[1]+this.options.scroll.offsetHeight);
386 if(pointer[0] < (p[0]+this.options.scrollSensitivity)) speed[0] = pointer[0]-(p[0]+this.options.scrollSensitivity);
387 if(pointer[1] < (p[1]+this.options.scrollSensitivity)) speed[1] = pointer[1]-(p[1]+this.options.scrollSensitivity);
388 if(pointer[0] > (p[2]-this.options.scrollSensitivity)) speed[0] = pointer[0]-(p[2]-this.options.scrollSensitivity);
389 if(pointer[1] > (p[3]-this.options.scrollSensitivity)) speed[1] = pointer[1]-(p[3]-this.options.scrollSensitivity);
390 this.startScrolling(speed);
393 // fix AppleWebKit rendering
394 if(Prototype.Browser.WebKit) window.scrollBy(0,0);
399 finishDrag: function(event, success) {
400 this.dragging = false;
402 if(this.options.quiet){
404 var pointer = [Event.pointerX(event), Event.pointerY(event)];
405 Droppables.show(pointer, this.element);
408 if(this.options.ghosting) {
409 if (!this.element._originallyAbsolute)
410 Position.relativize(this.element);
411 delete this.element._originallyAbsolute;
412 Element.remove(this._clone);
418 dropped = Droppables.fire(event, this.element);
419 if (!dropped) dropped = false;
421 if(dropped && this.options.onDropped) this.options.onDropped(this.element);
422 Draggables.notify('onEnd', this, event);
424 var revert = this.options.revert;
425 if(revert && Object.isFunction(revert)) revert = revert(this.element);
427 var d = this.currentDelta();
428 if(revert && this.options.reverteffect) {
429 if (dropped == 0 || revert != 'failure')
430 this.options.reverteffect(this.element,
431 d[1]-this.delta[1], d[0]-this.delta[0]);
436 if(this.options.zindex)
437 this.element.style.zIndex = this.originalZ;
439 if(this.options.endeffect)
440 this.options.endeffect(this.element);
442 Draggables.deactivate(this);
446 keyPress: function(event) {
447 if(event.keyCode!=Event.KEY_ESC) return;
448 this.finishDrag(event, false);
452 endDrag: function(event) {
453 if(!this.dragging) return;
454 this.stopScrolling();
455 this.finishDrag(event, true);
459 draw: function(point) {
460 var pos = Position.cumulativeOffset(this.element);
461 if(this.options.ghosting) {
462 var r = Position.realOffset(this.element);
463 pos[0] += r[0] - Position.deltaX; pos[1] += r[1] - Position.deltaY;
466 var d = this.currentDelta();
467 pos[0] -= d[0]; pos[1] -= d[1];
469 if(this.options.scroll && (this.options.scroll != window && this._isScrollChild)) {
470 pos[0] -= this.options.scroll.scrollLeft-this.originalScrollLeft;
471 pos[1] -= this.options.scroll.scrollTop-this.originalScrollTop;
474 var p = [0,1].map(function(i){
475 return (point[i]-pos[i]-this.offset[i])
478 if(this.options.snap) {
479 if(Object.isFunction(this.options.snap)) {
480 p = this.options.snap(p[0],p[1],this);
482 if(Object.isArray(this.options.snap)) {
483 p = p.map( function(v, i) {
484 return (v/this.options.snap[i]).round()*this.options.snap[i] }.bind(this))
486 p = p.map( function(v) {
487 return (v/this.options.snap).round()*this.options.snap }.bind(this))
491 var style = this.element.style;
492 if((!this.options.constraint) || (this.options.constraint=='horizontal'))
493 style.left = p[0] + "px";
494 if((!this.options.constraint) || (this.options.constraint=='vertical')){
495 style.top = p[1] + "px";
498 if(style.visibility=="hidden") style.visibility = ""; // fix gecko rendering
501 stopScrolling: function() {
502 if(this.scrollInterval) {
503 clearInterval(this.scrollInterval);
504 this.scrollInterval = null;
505 Draggables._lastScrollPointer = null;
509 startScrolling: function(speed) {
510 if(!(speed[0] || speed[1])) return;
511 this.scrollSpeed = [speed[0]*this.options.scrollSpeed,speed[1]*this.options.scrollSpeed];
512 this.lastScrolled = new Date();
513 this.scrollInterval = setInterval(this.scroll.bind(this), 10);
517 var current = new Date();
518 var delta = current - this.lastScrolled;
519 this.lastScrolled = current;
520 if(this.options.scroll == window) {
521 with (this._getWindowScroll(this.options.scroll)) {
522 if (this.scrollSpeed[0] || this.scrollSpeed[1]) {
523 var d = delta / 1000;
524 this.options.scroll.scrollTo( left + d*this.scrollSpeed[0], top + d*this.scrollSpeed[1] );
528 this.options.scroll.scrollLeft += this.scrollSpeed[0] * delta / 1000;
529 this.options.scroll.scrollTop += this.scrollSpeed[1] * delta / 1000;
533 Droppables.show(Draggables._lastPointer, this.element);
534 Draggables.notify('onDrag', this);
535 if (this._isScrollChild) {
536 Draggables._lastScrollPointer = Draggables._lastScrollPointer || $A(Draggables._lastPointer);
537 Draggables._lastScrollPointer[0] += this.scrollSpeed[0] * delta / 1000;
538 Draggables._lastScrollPointer[1] += this.scrollSpeed[1] * delta / 1000;
539 if (Draggables._lastScrollPointer[0] < 0)
540 Draggables._lastScrollPointer[0] = 0;
541 if (Draggables._lastScrollPointer[1] < 0)
542 Draggables._lastScrollPointer[1] = 0;
543 this.draw(Draggables._lastScrollPointer);
546 if(this.options.change) this.options.change(this);
549 _getWindowScroll: function(w) {
552 if (w.document.documentElement && documentElement.scrollTop) {
553 T = documentElement.scrollTop;
554 L = documentElement.scrollLeft;
555 } else if (w.document.body) {
562 } else if (w.document.documentElement && documentElement.clientWidth) {
563 W = documentElement.clientWidth;
564 H = documentElement.clientHeight;
566 W = body.offsetWidth;
567 H = body.offsetHeight
570 return { top: T, left: L, width: W, height: H };
574 Draggable._dragging = { };
576 /*--------------------------------------------------------------------------*/
578 var SortableObserver = Class.create({
579 initialize: function(element, observer) {
580 this.element = $(element);
581 this.observer = observer;
582 this.lastValue = Sortable.serialize(this.element);
585 onStart: function() {
586 this.lastValue = Sortable.serialize(this.element);
591 if(this.lastValue != Sortable.serialize(this.element))
592 this.observer(this.element)
597 SERIALIZE_RULE: /^[^_\-](?:[A-Za-z0-9\-\_]*)[_](.*)$/,
601 _findRootElement: function(element) {
602 while (element.tagName.toUpperCase() != "BODY") {
603 if(element.id && Sortable.sortables[element.id]) return element;
604 element = element.parentNode;
608 options: function(element) {
609 element = Sortable._findRootElement($(element));
611 return Sortable.sortables[element.id];
614 destroy: function(element){
615 var s = Sortable.options(element);
618 Draggables.removeObserver(s.element);
619 s.droppables.each(function(d){ Droppables.remove(d) });
620 s.draggables.invoke('destroy');
622 delete Sortable.sortables[s.element.id];
626 create: function(element) {
627 element = $(element);
628 var options = Object.extend({
630 tag: 'li', // assumes li children, override with tag: 'tagname'
634 overlap: 'vertical', // one of 'vertical', 'horizontal'
635 constraint: 'vertical', // one of 'vertical', 'horizontal', false
636 containment: element, // also takes array of elements (or id's); or false
637 handle: false, // or a CSS class
644 scrollSensitivity: 20,
646 format: this.SERIALIZE_RULE,
648 // these take arrays of elements or ids and can be
649 // used for better initialization performance
653 onChange: Prototype.emptyFunction,
654 onUpdate: Prototype.emptyFunction
655 }, arguments[1] || { });
657 // clear any old sortable with same element
658 this.destroy(element);
660 // build options for the draggables
661 var options_for_draggable = {
663 quiet: options.quiet,
664 scroll: options.scroll,
665 scrollSpeed: options.scrollSpeed,
666 scrollSensitivity: options.scrollSensitivity,
667 delay: options.delay,
668 ghosting: options.ghosting,
669 constraint: options.constraint,
670 handle: options.handle };
672 if(options.starteffect)
673 options_for_draggable.starteffect = options.starteffect;
675 if(options.reverteffect)
676 options_for_draggable.reverteffect = options.reverteffect;
678 if(options.ghosting) options_for_draggable.reverteffect = function(element) {
679 element.style.top = 0;
680 element.style.left = 0;
683 if(options.endeffect)
684 options_for_draggable.endeffect = options.endeffect;
687 options_for_draggable.zindex = options.zindex;
689 // build options for the droppables
690 var options_for_droppable = {
691 overlap: options.overlap,
692 containment: options.containment,
694 hoverclass: options.hoverclass,
695 onHover: Sortable.onHover
698 var options_for_tree = {
699 onHover: Sortable.onEmptyHover,
700 overlap: options.overlap,
701 containment: options.containment,
702 hoverclass: options.hoverclass
705 // fix for gecko engine
706 Element.cleanWhitespace(element);
708 options.draggables = [];
709 options.droppables = [];
711 // drop on empty handling
712 if(options.dropOnEmpty || options.tree) {
713 Droppables.add(element, options_for_tree);
714 options.droppables.push(element);
717 (options.elements || this.findElements(element, options) || []).each( function(e,i) {
718 var handle = options.handles ? $(options.handles[i]) :
719 (options.handle ? $(e).select('.' + options.handle)[0] : e);
720 options.draggables.push(
721 new Draggable(e, Object.extend(options_for_draggable, { handle: handle })));
722 Droppables.add(e, options_for_droppable);
723 if(options.tree) e.treeNode = element;
724 options.droppables.push(e);
728 (Sortable.findTreeElements(element, options) || []).each( function(e) {
729 Droppables.add(e, options_for_tree);
730 e.treeNode = element;
731 options.droppables.push(e);
736 this.sortables[element.id] = options;
739 Draggables.addObserver(new SortableObserver(element, options.onUpdate));
743 // return all suitable-for-sortable elements in a guaranteed order
744 findElements: function(element, options) {
745 return Element.findChildren(
746 element, options.only, options.tree ? true : false, options.tag);
749 findTreeElements: function(element, options) {
750 return Element.findChildren(
751 element, options.only, options.tree ? true : false, options.treeTag);
754 onHover: function(element, dropon, overlap) {
755 if(Element.isParent(dropon, element)) return;
757 if(overlap > .33 && overlap < .66 && Sortable.options(dropon).tree) {
759 } else if(overlap>0.5) {
760 Sortable.mark(dropon, 'before');
761 if(dropon.previousSibling != element) {
762 var oldParentNode = element.parentNode;
763 element.style.visibility = "hidden"; // fix gecko rendering
764 dropon.parentNode.insertBefore(element, dropon);
765 if(dropon.parentNode!=oldParentNode)
766 Sortable.options(oldParentNode).onChange(element);
767 Sortable.options(dropon.parentNode).onChange(element);
770 Sortable.mark(dropon, 'after');
771 var nextElement = dropon.nextSibling || null;
772 if(nextElement != element) {
773 var oldParentNode = element.parentNode;
774 element.style.visibility = "hidden"; // fix gecko rendering
775 dropon.parentNode.insertBefore(element, nextElement);
776 if(dropon.parentNode!=oldParentNode)
777 Sortable.options(oldParentNode).onChange(element);
778 Sortable.options(dropon.parentNode).onChange(element);
783 onEmptyHover: function(element, dropon, overlap) {
784 var oldParentNode = element.parentNode;
785 var droponOptions = Sortable.options(dropon);
787 if(!Element.isParent(dropon, element)) {
790 var children = Sortable.findElements(dropon, {tag: droponOptions.tag, only: droponOptions.only});
794 var offset = Element.offsetSize(dropon, droponOptions.overlap) * (1.0 - overlap);
796 for (index = 0; index < children.length; index += 1) {
797 if (offset - Element.offsetSize (children[index], droponOptions.overlap) >= 0) {
798 offset -= Element.offsetSize (children[index], droponOptions.overlap);
799 } else if (offset - (Element.offsetSize (children[index], droponOptions.overlap) / 2) >= 0) {
800 child = index + 1 < children.length ? children[index + 1] : null;
803 child = children[index];
809 dropon.insertBefore(element, child);
811 Sortable.options(oldParentNode).onChange(element);
812 droponOptions.onChange(element);
817 if(Sortable._marker) Sortable._marker.hide();
820 mark: function(dropon, position) {
821 // mark on ghosting only
822 var sortable = Sortable.options(dropon.parentNode);
823 if(sortable && !sortable.ghosting) return;
825 if(!Sortable._marker) {
827 ($('dropmarker') || Element.extend(document.createElement('DIV'))).
828 hide().addClassName('dropmarker').setStyle({position:'absolute'});
829 document.getElementsByTagName("body").item(0).appendChild(Sortable._marker);
831 var offsets = Position.cumulativeOffset(dropon);
832 Sortable._marker.setStyle({left: offsets[0]+'px', top: offsets[1] + 'px'});
834 if(position=='after')
835 if(sortable.overlap == 'horizontal')
836 Sortable._marker.setStyle({left: (offsets[0]+dropon.clientWidth) + 'px'});
838 Sortable._marker.setStyle({top: (offsets[1]+dropon.clientHeight) + 'px'});
840 Sortable._marker.show();
843 _tree: function(element, options, parent) {
844 var children = Sortable.findElements(element, options) || [];
846 for (var i = 0; i < children.length; ++i) {
847 var match = children[i].id.match(options.format);
849 if (!match) continue;
852 id: encodeURIComponent(match ? match[1] : null),
856 position: parent.children.length,
857 container: $(children[i]).down(options.treeTag)
860 /* Get the element containing the children and recurse over it */
862 this._tree(child.container, options, child)
864 parent.children.push (child);
870 tree: function(element) {
871 element = $(element);
872 var sortableOptions = this.options(element);
873 var options = Object.extend({
874 tag: sortableOptions.tag,
875 treeTag: sortableOptions.treeTag,
876 only: sortableOptions.only,
878 format: sortableOptions.format
879 }, arguments[1] || { });
889 return Sortable._tree(element, options, root);
892 /* Construct a [i] index for a particular node */
893 _constructIndex: function(node) {
896 if (node.id) index = '[' + node.position + ']' + index;
897 } while ((node = node.parent) != null);
901 sequence: function(element) {
902 element = $(element);
903 var options = Object.extend(this.options(element), arguments[1] || { });
905 return $(this.findElements(element, options) || []).map( function(item) {
906 return item.id.match(options.format) ? item.id.match(options.format)[1] : '';
910 setSequence: function(element, new_sequence) {
911 element = $(element);
912 var options = Object.extend(this.options(element), arguments[2] || { });
915 this.findElements(element, options).each( function(n) {
916 if (n.id.match(options.format))
917 nodeMap[n.id.match(options.format)[1]] = [n, n.parentNode];
918 n.parentNode.removeChild(n);
921 new_sequence.each(function(ident) {
922 var n = nodeMap[ident];
924 n[1].appendChild(n[0]);
925 delete nodeMap[ident];
930 serialize: function(element) {
931 element = $(element);
932 var options = Object.extend(Sortable.options(element), arguments[1] || { });
933 var name = encodeURIComponent(
934 (arguments[1] && arguments[1].name) ? arguments[1].name : element.id);
937 return Sortable.tree(element, arguments[1]).children.map( function (item) {
938 return [name + Sortable._constructIndex(item) + "[id]=" +
939 encodeURIComponent(item.id)].concat(item.children.map(arguments.callee));
940 }).flatten().join('&');
942 return Sortable.sequence(element, arguments[1]).map( function(item) {
943 return name + "[]=" + encodeURIComponent(item);
949 // Returns true if child is contained within element
950 Element.isParent = function(child, element) {
951 if (!child.parentNode || child == element) return false;
952 if (child.parentNode == element) return true;
953 return Element.isParent(child.parentNode, element);
956 Element.findChildren = function(element, only, recursive, tagName) {
957 if(!element.hasChildNodes()) return null;
958 tagName = tagName.toUpperCase();
959 if(only) only = [only].flatten();
961 $A(element.childNodes).each( function(e) {
962 if(e.tagName && e.tagName.toUpperCase()==tagName &&
963 (!only || (Element.classNames(e).detect(function(v) { return only.include(v) }))))
966 var grandchildren = Element.findChildren(e, only, recursive, tagName);
967 if(grandchildren) elements.push(grandchildren);
971 return (elements.length>0 ? elements.flatten() : []);
974 Element.offsetSize = function (element, type) {
975 return element['offset' + ((type=='vertical' || type=='height') ? 'Height' : 'Width')];