1 // script.aculo.us dragdrop.js v1.8.1, Thu Jan 03 22:07:12 -0500 2008
3 // Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
4 // (c) 2005-2007 Sammi Williams (http://www.oriontransfer.co.nz, sammi@oriontransfer.co.nz)
6 // script.aculo.us is freely distributable under the terms of an MIT-style license.
7 // For details, see the script.aculo.us web site: http://script.aculo.us/
9 if(Object.isUndefined(Effect))
10 throw("dragdrop.js requires including script.aculo.us' effects.js library");
15 remove: function(element) {
16 this.drops = this.drops.reject(function(d) { return d.element==$(element) });
19 add: function(element) {
21 var options = Object.extend({
25 }, arguments[1] || { });
28 if(options.containment) {
29 options._containers = [];
30 var containment = options.containment;
31 if(Object.isArray(containment)) {
32 containment.each( function(c) { options._containers.push($(c)) });
34 options._containers.push($(containment));
38 if(options.accept) options.accept = [options.accept].flatten();
39 if (Prototype.Browser.IE) {
40 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";
497 if(style.visibility=="hidden") style.visibility = ""; // fix gecko rendering
500 stopScrolling: function() {
501 if(this.scrollInterval) {
502 clearInterval(this.scrollInterval);
503 this.scrollInterval = null;
504 Draggables._lastScrollPointer = null;
508 startScrolling: function(speed) {
509 if(!(speed[0] || speed[1])) return;
510 this.scrollSpeed = [speed[0]*this.options.scrollSpeed,speed[1]*this.options.scrollSpeed];
511 this.lastScrolled = new Date();
512 this.scrollInterval = setInterval(this.scroll.bind(this), 10);
516 var current = new Date();
517 var delta = current - this.lastScrolled;
518 this.lastScrolled = current;
519 if(this.options.scroll == window) {
520 with (this._getWindowScroll(this.options.scroll)) {
521 if (this.scrollSpeed[0] || this.scrollSpeed[1]) {
522 var d = delta / 1000;
523 this.options.scroll.scrollTo( left + d*this.scrollSpeed[0], top + d*this.scrollSpeed[1] );
527 this.options.scroll.scrollLeft += this.scrollSpeed[0] * delta / 1000;
528 this.options.scroll.scrollTop += this.scrollSpeed[1] * delta / 1000;
532 Droppables.show(Draggables._lastPointer, this.element);
533 Draggables.notify('onDrag', this);
534 if (this._isScrollChild) {
535 Draggables._lastScrollPointer = Draggables._lastScrollPointer || $A(Draggables._lastPointer);
536 Draggables._lastScrollPointer[0] += this.scrollSpeed[0] * delta / 1000;
537 Draggables._lastScrollPointer[1] += this.scrollSpeed[1] * delta / 1000;
538 if (Draggables._lastScrollPointer[0] < 0)
539 Draggables._lastScrollPointer[0] = 0;
540 if (Draggables._lastScrollPointer[1] < 0)
541 Draggables._lastScrollPointer[1] = 0;
542 this.draw(Draggables._lastScrollPointer);
545 if(this.options.change) this.options.change(this);
548 _getWindowScroll: function(w) {
551 if (w.document.documentElement && documentElement.scrollTop) {
552 T = documentElement.scrollTop;
553 L = documentElement.scrollLeft;
554 } else if (w.document.body) {
561 } else if (w.document.documentElement && documentElement.clientWidth) {
562 W = documentElement.clientWidth;
563 H = documentElement.clientHeight;
565 W = body.offsetWidth;
566 H = body.offsetHeight
569 return { top: T, left: L, width: W, height: H };
573 Draggable._dragging = { };
575 /*--------------------------------------------------------------------------*/
577 var SortableObserver = Class.create({
578 initialize: function(element, observer) {
579 this.element = $(element);
580 this.observer = observer;
581 this.lastValue = Sortable.serialize(this.element);
584 onStart: function() {
585 this.lastValue = Sortable.serialize(this.element);
590 if(this.lastValue != Sortable.serialize(this.element))
591 this.observer(this.element)
596 SERIALIZE_RULE: /^[^_\-](?:[A-Za-z0-9\-\_]*)[_](.*)$/,
600 _findRootElement: function(element) {
601 while (element.tagName.toUpperCase() != "BODY") {
602 if(element.id && Sortable.sortables[element.id]) return element;
603 element = element.parentNode;
607 options: function(element) {
608 element = Sortable._findRootElement($(element));
610 return Sortable.sortables[element.id];
613 destroy: function(element){
614 var s = Sortable.options(element);
617 Draggables.removeObserver(s.element);
618 s.droppables.each(function(d){ Droppables.remove(d) });
619 s.draggables.invoke('destroy');
621 delete Sortable.sortables[s.element.id];
625 create: function(element) {
626 element = $(element);
627 var options = Object.extend({
629 tag: 'li', // assumes li children, override with tag: 'tagname'
633 overlap: 'vertical', // one of 'vertical', 'horizontal'
634 constraint: 'vertical', // one of 'vertical', 'horizontal', false
635 containment: element, // also takes array of elements (or id's); or false
636 handle: false, // or a CSS class
643 scrollSensitivity: 20,
645 format: this.SERIALIZE_RULE,
647 // these take arrays of elements or ids and can be
648 // used for better initialization performance
652 onChange: Prototype.emptyFunction,
653 onUpdate: Prototype.emptyFunction
654 }, arguments[1] || { });
656 // clear any old sortable with same element
657 this.destroy(element);
659 // build options for the draggables
660 var options_for_draggable = {
662 quiet: options.quiet,
663 scroll: options.scroll,
664 scrollSpeed: options.scrollSpeed,
665 scrollSensitivity: options.scrollSensitivity,
666 delay: options.delay,
667 ghosting: options.ghosting,
668 constraint: options.constraint,
669 handle: options.handle };
671 if(options.starteffect)
672 options_for_draggable.starteffect = options.starteffect;
674 if(options.reverteffect)
675 options_for_draggable.reverteffect = options.reverteffect;
677 if(options.ghosting) options_for_draggable.reverteffect = function(element) {
678 element.style.top = 0;
679 element.style.left = 0;
682 if(options.endeffect)
683 options_for_draggable.endeffect = options.endeffect;
686 options_for_draggable.zindex = options.zindex;
688 // build options for the droppables
689 var options_for_droppable = {
690 overlap: options.overlap,
691 containment: options.containment,
693 hoverclass: options.hoverclass,
694 onHover: Sortable.onHover
697 var options_for_tree = {
698 onHover: Sortable.onEmptyHover,
699 overlap: options.overlap,
700 containment: options.containment,
701 hoverclass: options.hoverclass
704 // fix for gecko engine
705 Element.cleanWhitespace(element);
707 options.draggables = [];
708 options.droppables = [];
710 // drop on empty handling
711 if(options.dropOnEmpty || options.tree) {
712 Droppables.add(element, options_for_tree);
713 options.droppables.push(element);
716 (options.elements || this.findElements(element, options) || []).each( function(e,i) {
717 var handle = options.handles ? $(options.handles[i]) :
718 (options.handle ? $(e).select('.' + options.handle)[0] : e);
719 options.draggables.push(
720 new Draggable(e, Object.extend(options_for_draggable, { handle: handle })));
721 Droppables.add(e, options_for_droppable);
722 if(options.tree) e.treeNode = element;
723 options.droppables.push(e);
727 (Sortable.findTreeElements(element, options) || []).each( function(e) {
728 Droppables.add(e, options_for_tree);
729 e.treeNode = element;
730 options.droppables.push(e);
735 this.sortables[element.id] = options;
738 Draggables.addObserver(new SortableObserver(element, options.onUpdate));
742 // return all suitable-for-sortable elements in a guaranteed order
743 findElements: function(element, options) {
744 return Element.findChildren(
745 element, options.only, options.tree ? true : false, options.tag);
748 findTreeElements: function(element, options) {
749 return Element.findChildren(
750 element, options.only, options.tree ? true : false, options.treeTag);
753 onHover: function(element, dropon, overlap) {
754 if(Element.isParent(dropon, element)) return;
756 if(overlap > .33 && overlap < .66 && Sortable.options(dropon).tree) {
758 } else if(overlap>0.5) {
759 Sortable.mark(dropon, 'before');
760 if(dropon.previousSibling != element) {
761 var oldParentNode = element.parentNode;
762 element.style.visibility = "hidden"; // fix gecko rendering
763 dropon.parentNode.insertBefore(element, dropon);
764 if(dropon.parentNode!=oldParentNode)
765 Sortable.options(oldParentNode).onChange(element);
766 Sortable.options(dropon.parentNode).onChange(element);
769 Sortable.mark(dropon, 'after');
770 var nextElement = dropon.nextSibling || null;
771 if(nextElement != element) {
772 var oldParentNode = element.parentNode;
773 element.style.visibility = "hidden"; // fix gecko rendering
774 dropon.parentNode.insertBefore(element, nextElement);
775 if(dropon.parentNode!=oldParentNode)
776 Sortable.options(oldParentNode).onChange(element);
777 Sortable.options(dropon.parentNode).onChange(element);
782 onEmptyHover: function(element, dropon, overlap) {
783 var oldParentNode = element.parentNode;
784 var droponOptions = Sortable.options(dropon);
786 if(!Element.isParent(dropon, element)) {
789 var children = Sortable.findElements(dropon, {tag: droponOptions.tag, only: droponOptions.only});
793 var offset = Element.offsetSize(dropon, droponOptions.overlap) * (1.0 - overlap);
795 for (index = 0; index < children.length; index += 1) {
796 if (offset - Element.offsetSize (children[index], droponOptions.overlap) >= 0) {
797 offset -= Element.offsetSize (children[index], droponOptions.overlap);
798 } else if (offset - (Element.offsetSize (children[index], droponOptions.overlap) / 2) >= 0) {
799 child = index + 1 < children.length ? children[index + 1] : null;
802 child = children[index];
808 dropon.insertBefore(element, child);
810 Sortable.options(oldParentNode).onChange(element);
811 droponOptions.onChange(element);
816 if(Sortable._marker) Sortable._marker.hide();
819 mark: function(dropon, position) {
820 // mark on ghosting only
821 var sortable = Sortable.options(dropon.parentNode);
822 if(sortable && !sortable.ghosting) return;
824 if(!Sortable._marker) {
826 ($('dropmarker') || Element.extend(document.createElement('DIV'))).
827 hide().addClassName('dropmarker').setStyle({position:'absolute'});
828 document.getElementsByTagName("body").item(0).appendChild(Sortable._marker);
830 var offsets = Position.cumulativeOffset(dropon);
831 Sortable._marker.setStyle({left: offsets[0]+'px', top: offsets[1] + 'px'});
833 if(position=='after')
834 if(sortable.overlap == 'horizontal')
835 Sortable._marker.setStyle({left: (offsets[0]+dropon.clientWidth) + 'px'});
837 Sortable._marker.setStyle({top: (offsets[1]+dropon.clientHeight) + 'px'});
839 Sortable._marker.show();
842 _tree: function(element, options, parent) {
843 var children = Sortable.findElements(element, options) || [];
845 for (var i = 0; i < children.length; ++i) {
846 var match = children[i].id.match(options.format);
848 if (!match) continue;
851 id: encodeURIComponent(match ? match[1] : null),
855 position: parent.children.length,
856 container: $(children[i]).down(options.treeTag)
859 /* Get the element containing the children and recurse over it */
861 this._tree(child.container, options, child)
863 parent.children.push (child);
869 tree: function(element) {
870 element = $(element);
871 var sortableOptions = this.options(element);
872 var options = Object.extend({
873 tag: sortableOptions.tag,
874 treeTag: sortableOptions.treeTag,
875 only: sortableOptions.only,
877 format: sortableOptions.format
878 }, arguments[1] || { });
888 return Sortable._tree(element, options, root);
891 /* Construct a [i] index for a particular node */
892 _constructIndex: function(node) {
895 if (node.id) index = '[' + node.position + ']' + index;
896 } while ((node = node.parent) != null);
900 sequence: function(element) {
901 element = $(element);
902 var options = Object.extend(this.options(element), arguments[1] || { });
904 return $(this.findElements(element, options) || []).map( function(item) {
905 return item.id.match(options.format) ? item.id.match(options.format)[1] : '';
909 setSequence: function(element, new_sequence) {
910 element = $(element);
911 var options = Object.extend(this.options(element), arguments[2] || { });
914 this.findElements(element, options).each( function(n) {
915 if (n.id.match(options.format))
916 nodeMap[n.id.match(options.format)[1]] = [n, n.parentNode];
917 n.parentNode.removeChild(n);
920 new_sequence.each(function(ident) {
921 var n = nodeMap[ident];
923 n[1].appendChild(n[0]);
924 delete nodeMap[ident];
929 serialize: function(element) {
930 element = $(element);
931 var options = Object.extend(Sortable.options(element), arguments[1] || { });
932 var name = encodeURIComponent(
933 (arguments[1] && arguments[1].name) ? arguments[1].name : element.id);
936 return Sortable.tree(element, arguments[1]).children.map( function (item) {
937 return [name + Sortable._constructIndex(item) + "[id]=" +
938 encodeURIComponent(item.id)].concat(item.children.map(arguments.callee));
939 }).flatten().join('&');
941 return Sortable.sequence(element, arguments[1]).map( function(item) {
942 return name + "[]=" + encodeURIComponent(item);
948 // Returns true if child is contained within element
949 Element.isParent = function(child, element) {
950 if (!child.parentNode || child == element) return false;
951 if (child.parentNode == element) return true;
952 return Element.isParent(child.parentNode, element);
955 Element.findChildren = function(element, only, recursive, tagName) {
956 if(!element.hasChildNodes()) return null;
957 tagName = tagName.toUpperCase();
958 if(only) only = [only].flatten();
960 $A(element.childNodes).each( function(e) {
961 if(e.tagName && e.tagName.toUpperCase()==tagName &&
962 (!only || (Element.classNames(e).detect(function(v) { return only.include(v) }))))
965 var grandchildren = Element.findChildren(e, only, recursive, tagName);
966 if(grandchildren) elements.push(grandchildren);
970 return (elements.length>0 ? elements.flatten() : []);
973 Element.offsetSize = function (element, type) {
974 return element['offset' + ((type=='vertical' || type=='height') ? 'Height' : 'Width')];