1 // Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
2 // (c) 2005 Sammi Williams (http://www.oriontransfer.co.nz, sammi@oriontransfer.co.nz)
4 // See scriptaculous.js for full license.
6 /*--------------------------------------------------------------------------*/
11 remove: function(element) {
12 this.drops = this.drops.reject(function(d) { return d.element==$(element) });
15 add: function(element) {
17 var options = Object.extend({
21 }, arguments[1] || {});
24 if(options.containment) {
25 options._containers = [];
26 var containment = options.containment;
27 if((typeof containment == 'object') &&
28 (containment.constructor == Array)) {
29 containment.each( function(c) { options._containers.push($(c)) });
31 options._containers.push($(containment));
35 if(options.accept) options.accept = [options.accept].flatten();
37 Element.makePositioned(element); // fix IE
38 options.element = element;
40 this.drops.push(options);
43 findDeepestChild: function(drops) {
46 for (i = 1; i < drops.length; ++i)
47 if (Element.isParent(drops[i].element, deepest.element))
53 isContained: function(element, drop) {
56 containmentNode = element.treeNode;
58 containmentNode = element.parentNode;
60 return drop._containers.detect(function(c) { return containmentNode == c });
63 isAffected: function(point, element, drop) {
65 (drop.element!=element) &&
66 ((!drop._containers) ||
67 this.isContained(element, drop)) &&
69 (Element.classNames(element).detect(
70 function(v) { return drop.accept.include(v) } ) )) &&
71 Position.within(drop.element, point[0], point[1]) );
74 deactivate: function(drop) {
76 Element.removeClassName(drop.element, drop.hoverclass);
77 this.last_active = null;
80 activate: function(drop) {
82 Element.addClassName(drop.element, drop.hoverclass);
83 this.last_active = drop;
86 show: function(point, element) {
87 if(!this.drops.length) return;
90 if(this.last_active) this.deactivate(this.last_active);
91 this.drops.each( function(drop) {
92 if(Droppables.isAffected(point, element, drop))
96 if(affected.length>0) {
97 drop = Droppables.findDeepestChild(affected);
98 Position.within(drop.element, point[0], point[1]);
100 drop.onHover(element, drop.element, Position.overlap(drop.overlap, drop.element));
102 Droppables.activate(drop);
106 fire: function(event, element) {
107 if(!this.last_active) return;
110 if (this.isAffected([Event.pointerX(event), Event.pointerY(event)], element, this.last_active))
111 if (this.last_active.onDrop)
112 this.last_active.onDrop(element, this.last_active.element, event);
117 this.deactivate(this.last_active);
125 register: function(draggable) {
126 if(this.drags.length == 0) {
127 this.eventMouseUp = this.endDrag.bindAsEventListener(this);
128 this.eventMouseMove = this.updateDrag.bindAsEventListener(this);
129 this.eventKeypress = this.keyPress.bindAsEventListener(this);
131 Event.observe(document, "mouseup", this.eventMouseUp);
132 Event.observe(document, "mousemove", this.eventMouseMove);
133 Event.observe(document, "keypress", this.eventKeypress);
135 this.drags.push(draggable);
138 unregister: function(draggable) {
139 this.drags = this.drags.reject(function(d) { return d==draggable });
140 if(this.drags.length == 0) {
141 Event.stopObserving(document, "mouseup", this.eventMouseUp);
142 Event.stopObserving(document, "mousemove", this.eventMouseMove);
143 Event.stopObserving(document, "keypress", this.eventKeypress);
147 activate: function(draggable) {
148 window.focus(); // allows keypress events if window isn't currently focused, fails for Safari
149 this.activeDraggable = draggable;
152 deactivate: function() {
153 this.activeDraggable = null;
156 updateDrag: function(event) {
157 if(!this.activeDraggable) return;
158 var pointer = [Event.pointerX(event), Event.pointerY(event)];
159 // Mozilla-based browsers fire successive mousemove events with
160 // the same coordinates, prevent needless redrawing (moz bug?)
161 if(this._lastPointer && (this._lastPointer.inspect() == pointer.inspect())) return;
162 this._lastPointer = pointer;
163 this.activeDraggable.updateDrag(event, pointer);
166 endDrag: function(event) {
167 if(!this.activeDraggable) return;
168 this._lastPointer = null;
169 this.activeDraggable.endDrag(event);
170 this.activeDraggable = null;
173 keyPress: function(event) {
174 if(this.activeDraggable)
175 this.activeDraggable.keyPress(event);
178 addObserver: function(observer) {
179 this.observers.push(observer);
180 this._cacheObserverCallbacks();
183 removeObserver: function(element) { // element instead of observer fixes mem leaks
184 this.observers = this.observers.reject( function(o) { return o.element==element });
185 this._cacheObserverCallbacks();
188 notify: function(eventName, draggable, event) { // 'onStart', 'onEnd', 'onDrag'
189 if(this[eventName+'Count'] > 0)
190 this.observers.each( function(o) {
191 if(o[eventName]) o[eventName](eventName, draggable, event);
195 _cacheObserverCallbacks: function() {
196 ['onStart','onEnd','onDrag'].each( function(eventName) {
197 Draggables[eventName+'Count'] = Draggables.observers.select(
198 function(o) { return o[eventName]; }
204 /*--------------------------------------------------------------------------*/
206 var Draggable = Class.create();
207 Draggable.prototype = {
208 initialize: function(element) {
209 var options = Object.extend({
211 starteffect: function(element) {
212 element._opacity = Element.getOpacity(element);
213 new Effect.Opacity(element, {duration:0.2, from:element._opacity, to:0.7});
215 reverteffect: function(element, top_offset, left_offset) {
216 var dur = Math.sqrt(Math.abs(top_offset^2)+Math.abs(left_offset^2))*0.02;
217 element._revert = new Effect.Move(element, { x: -left_offset, y: -top_offset, duration: dur});
219 endeffect: function(element) {
220 var toOpacity = typeof element._opacity == 'number' ? element._opacity : 1.0
221 new Effect.Opacity(element, {duration:0.2, from:0.7, to:toOpacity});
226 scrollSensitivity: 20,
228 snap: false // false, or xy or [x,y] or function(x,y){ return [x,y] }
229 }, arguments[1] || {});
231 this.element = $(element);
233 if(options.handle && (typeof options.handle == 'string')) {
234 var h = Element.childrenWithClassName(this.element, options.handle, true);
235 if(h.length>0) this.handle = h[0];
237 if(!this.handle) this.handle = $(options.handle);
238 if(!this.handle) this.handle = this.element;
240 if(options.scroll && !options.scroll.scrollTo && !options.scroll.outerHTML)
241 options.scroll = $(options.scroll);
243 Element.makePositioned(this.element); // fix IE
245 this.delta = this.currentDelta();
246 this.options = options;
247 this.dragging = false;
249 this.eventMouseDown = this.initDrag.bindAsEventListener(this);
250 Event.observe(this.handle, "mousedown", this.eventMouseDown);
252 Draggables.register(this);
255 destroy: function() {
256 Event.stopObserving(this.handle, "mousedown", this.eventMouseDown);
257 Draggables.unregister(this);
260 currentDelta: function() {
262 parseInt(Element.getStyle(this.element,'left') || '0'),
263 parseInt(Element.getStyle(this.element,'top') || '0')]);
266 initDrag: function(event) {
267 if(Event.isLeftClick(event)) {
268 // abort on form elements, fixes a Firefox issue
269 var src = Event.element(event);
271 src.tagName=='INPUT' ||
272 src.tagName=='SELECT' ||
273 src.tagName=='OPTION' ||
274 src.tagName=='BUTTON' ||
275 src.tagName=='TEXTAREA')) return;
277 if(this.element._revert) {
278 this.element._revert.cancel();
279 this.element._revert = null;
282 var pointer = [Event.pointerX(event), Event.pointerY(event)];
283 var pos = Position.cumulativeOffset(this.element);
284 this.offset = [0,1].map( function(i) { return (pointer[i] - pos[i]) });
286 Draggables.activate(this);
291 startDrag: function(event) {
292 this.dragging = true;
294 if(this.options.zindex) {
295 this.originalZ = parseInt(Element.getStyle(this.element,'z-index') || 0);
296 this.element.style.zIndex = this.options.zindex;
299 if(this.options.ghosting) {
300 this._clone = this.element.cloneNode(true);
301 Position.absolutize(this.element);
302 this.element.parentNode.insertBefore(this._clone, this.element);
305 if(this.options.scroll) {
306 if (this.options.scroll == window) {
307 var where = this._getWindowScroll(this.options.scroll);
308 this.originalScrollLeft = where.left;
309 this.originalScrollTop = where.top;
311 this.originalScrollLeft = this.options.scroll.scrollLeft;
312 this.originalScrollTop = this.options.scroll.scrollTop;
316 Draggables.notify('onStart', this, event);
317 if(this.options.starteffect) this.options.starteffect(this.element);
320 updateDrag: function(event, pointer) {
321 if(!this.dragging) this.startDrag(event);
323 Droppables.show(pointer, this.element);
324 Draggables.notify('onDrag', this, event);
326 if(this.options.change) this.options.change(this);
328 if(this.options.scroll) {
329 this.stopScrolling();
332 if (this.options.scroll == window) {
333 with(this._getWindowScroll(this.options.scroll)) { p = [ left, top, left+width, top+height ]; }
335 p = Position.page(this.options.scroll);
336 p[0] += this.options.scroll.scrollLeft;
337 p[1] += this.options.scroll.scrollTop;
338 p.push(p[0]+this.options.scroll.offsetWidth);
339 p.push(p[1]+this.options.scroll.offsetHeight);
342 if(pointer[0] < (p[0]+this.options.scrollSensitivity)) speed[0] = pointer[0]-(p[0]+this.options.scrollSensitivity);
343 if(pointer[1] < (p[1]+this.options.scrollSensitivity)) speed[1] = pointer[1]-(p[1]+this.options.scrollSensitivity);
344 if(pointer[0] > (p[2]-this.options.scrollSensitivity)) speed[0] = pointer[0]-(p[2]-this.options.scrollSensitivity);
345 if(pointer[1] > (p[3]-this.options.scrollSensitivity)) speed[1] = pointer[1]-(p[3]-this.options.scrollSensitivity);
346 this.startScrolling(speed);
349 // fix AppleWebKit rendering
350 if(navigator.appVersion.indexOf('AppleWebKit')>0) window.scrollBy(0,0);
355 finishDrag: function(event, success) {
356 this.dragging = false;
358 if(this.options.ghosting) {
359 Position.relativize(this.element);
360 Element.remove(this._clone);
364 if(success) Droppables.fire(event, this.element);
365 Draggables.notify('onEnd', this, event);
367 var revert = this.options.revert;
368 if(revert && typeof revert == 'function') revert = revert(this.element);
370 var d = this.currentDelta();
371 if(revert && this.options.reverteffect) {
372 this.options.reverteffect(this.element,
373 d[1]-this.delta[1], d[0]-this.delta[0]);
378 if(this.options.zindex)
379 this.element.style.zIndex = this.originalZ;
381 if(this.options.endeffect)
382 this.options.endeffect(this.element);
384 Draggables.deactivate(this);
388 keyPress: function(event) {
389 if(event.keyCode!=Event.KEY_ESC) return;
390 this.finishDrag(event, false);
394 endDrag: function(event) {
395 if(!this.dragging) return;
396 this.stopScrolling();
397 this.finishDrag(event, true);
401 draw: function(point) {
402 var pos = Position.cumulativeOffset(this.element);
403 var d = this.currentDelta();
404 pos[0] -= d[0]; pos[1] -= d[1];
406 if(this.options.scroll && (this.options.scroll != window)) {
407 pos[0] -= this.options.scroll.scrollLeft-this.originalScrollLeft;
408 pos[1] -= this.options.scroll.scrollTop-this.originalScrollTop;
411 var p = [0,1].map(function(i){
412 return (point[i]-pos[i]-this.offset[i])
415 if(this.options.snap) {
416 if(typeof this.options.snap == 'function') {
417 p = this.options.snap(p[0],p[1],this);
419 if(this.options.snap instanceof Array) {
420 p = p.map( function(v, i) {
421 return Math.round(v/this.options.snap[i])*this.options.snap[i] }.bind(this))
423 p = p.map( function(v) {
424 return Math.round(v/this.options.snap)*this.options.snap }.bind(this))
428 var style = this.element.style;
429 if((!this.options.constraint) || (this.options.constraint=='horizontal'))
430 style.left = p[0] + "px";
431 if((!this.options.constraint) || (this.options.constraint=='vertical'))
432 style.top = p[1] + "px";
433 if(style.visibility=="hidden") style.visibility = ""; // fix gecko rendering
436 stopScrolling: function() {
437 if(this.scrollInterval) {
438 clearInterval(this.scrollInterval);
439 this.scrollInterval = null;
440 Draggables._lastScrollPointer = null;
444 startScrolling: function(speed) {
445 this.scrollSpeed = [speed[0]*this.options.scrollSpeed,speed[1]*this.options.scrollSpeed];
446 this.lastScrolled = new Date();
447 this.scrollInterval = setInterval(this.scroll.bind(this), 10);
451 var current = new Date();
452 var delta = current - this.lastScrolled;
453 this.lastScrolled = current;
454 if(this.options.scroll == window) {
455 with (this._getWindowScroll(this.options.scroll)) {
456 if (this.scrollSpeed[0] || this.scrollSpeed[1]) {
457 var d = delta / 1000;
458 this.options.scroll.scrollTo( left + d*this.scrollSpeed[0], top + d*this.scrollSpeed[1] );
462 this.options.scroll.scrollLeft += this.scrollSpeed[0] * delta / 1000;
463 this.options.scroll.scrollTop += this.scrollSpeed[1] * delta / 1000;
467 Droppables.show(Draggables._lastPointer, this.element);
468 Draggables.notify('onDrag', this);
469 Draggables._lastScrollPointer = Draggables._lastScrollPointer || $A(Draggables._lastPointer);
470 Draggables._lastScrollPointer[0] += this.scrollSpeed[0] * delta / 1000;
471 Draggables._lastScrollPointer[1] += this.scrollSpeed[1] * delta / 1000;
472 if (Draggables._lastScrollPointer[0] < 0)
473 Draggables._lastScrollPointer[0] = 0;
474 if (Draggables._lastScrollPointer[1] < 0)
475 Draggables._lastScrollPointer[1] = 0;
476 this.draw(Draggables._lastScrollPointer);
478 if(this.options.change) this.options.change(this);
481 _getWindowScroll: function(w) {
484 if (w.document.documentElement && documentElement.scrollTop) {
485 T = documentElement.scrollTop;
486 L = documentElement.scrollLeft;
487 } else if (w.document.body) {
494 } else if (w.document.documentElement && documentElement.clientWidth) {
495 W = documentElement.clientWidth;
496 H = documentElement.clientHeight;
498 W = body.offsetWidth;
499 H = body.offsetHeight
502 return { top: T, left: L, width: W, height: H };
506 /*--------------------------------------------------------------------------*/
508 var SortableObserver = Class.create();
509 SortableObserver.prototype = {
510 initialize: function(element, observer) {
511 this.element = $(element);
512 this.observer = observer;
513 this.lastValue = Sortable.serialize(this.element);
516 onStart: function() {
517 this.lastValue = Sortable.serialize(this.element);
522 if(this.lastValue != Sortable.serialize(this.element))
523 this.observer(this.element)
530 _findRootElement: function(element) {
531 while (element.tagName != "BODY") {
532 if(element.id && Sortable.sortables[element.id]) return element;
533 element = element.parentNode;
537 options: function(element) {
538 element = Sortable._findRootElement($(element));
540 return Sortable.sortables[element.id];
543 destroy: function(element){
544 var s = Sortable.options(element);
547 Draggables.removeObserver(s.element);
548 s.droppables.each(function(d){ Droppables.remove(d) });
549 s.draggables.invoke('destroy');
551 delete Sortable.sortables[s.element.id];
555 create: function(element) {
556 element = $(element);
557 var options = Object.extend({
559 tag: 'li', // assumes li children, override with tag: 'tagname'
563 overlap: 'vertical', // one of 'vertical', 'horizontal'
564 constraint: 'vertical', // one of 'vertical', 'horizontal', false
565 containment: element, // also takes array of elements (or id's); or false
566 handle: false, // or a CSS class
571 scrollSensitivity: 20,
573 format: /^[^_]*_(.*)$/,
574 onChange: Prototype.emptyFunction,
575 onUpdate: Prototype.emptyFunction
576 }, arguments[1] || {});
578 // clear any old sortable with same element
579 this.destroy(element);
581 // build options for the draggables
582 var options_for_draggable = {
584 scroll: options.scroll,
585 scrollSpeed: options.scrollSpeed,
586 scrollSensitivity: options.scrollSensitivity,
587 ghosting: options.ghosting,
588 constraint: options.constraint,
589 handle: options.handle };
591 if(options.starteffect)
592 options_for_draggable.starteffect = options.starteffect;
594 if(options.reverteffect)
595 options_for_draggable.reverteffect = options.reverteffect;
597 if(options.ghosting) options_for_draggable.reverteffect = function(element) {
598 element.style.top = 0;
599 element.style.left = 0;
602 if(options.endeffect)
603 options_for_draggable.endeffect = options.endeffect;
606 options_for_draggable.zindex = options.zindex;
608 // build options for the droppables
609 var options_for_droppable = {
610 overlap: options.overlap,
611 containment: options.containment,
613 hoverclass: options.hoverclass,
614 onHover: Sortable.onHover
615 //greedy: !options.dropOnEmpty
618 var options_for_tree = {
619 onHover: Sortable.onEmptyHover,
620 overlap: options.overlap,
621 containment: options.containment,
622 hoverclass: options.hoverclass
625 // fix for gecko engine
626 Element.cleanWhitespace(element);
628 options.draggables = [];
629 options.droppables = [];
631 // drop on empty handling
632 if(options.dropOnEmpty || options.tree) {
633 Droppables.add(element, options_for_tree);
634 options.droppables.push(element);
637 (this.findElements(element, options) || []).each( function(e) {
638 // handles are per-draggable
639 var handle = options.handle ?
640 Element.childrenWithClassName(e, options.handle)[0] : e;
641 options.draggables.push(
642 new Draggable(e, Object.extend(options_for_draggable, { handle: handle })));
643 Droppables.add(e, options_for_droppable);
644 if(options.tree) e.treeNode = element;
645 options.droppables.push(e);
649 (Sortable.findTreeElements(element, options) || []).each( function(e) {
650 Droppables.add(e, options_for_tree);
651 e.treeNode = element;
652 options.droppables.push(e);
657 this.sortables[element.id] = options;
660 Draggables.addObserver(new SortableObserver(element, options.onUpdate));
664 // return all suitable-for-sortable elements in a guaranteed order
665 findElements: function(element, options) {
666 return Element.findChildren(
667 element, options.only, options.tree ? true : false, options.tag);
670 findTreeElements: function(element, options) {
671 return Element.findChildren(
672 element, options.only, options.tree ? true : false, options.treeTag);
675 onHover: function(element, dropon, overlap) {
676 if(Element.isParent(dropon, element)) return;
678 if(overlap > .33 && overlap < .66 && Sortable.options(dropon).tree) {
680 } else if(overlap>0.5) {
681 Sortable.mark(dropon, 'before');
682 if(dropon.previousSibling != element) {
683 var oldParentNode = element.parentNode;
684 element.style.visibility = "hidden"; // fix gecko rendering
685 dropon.parentNode.insertBefore(element, dropon);
686 if(dropon.parentNode!=oldParentNode)
687 Sortable.options(oldParentNode).onChange(element);
688 Sortable.options(dropon.parentNode).onChange(element);
691 Sortable.mark(dropon, 'after');
692 var nextElement = dropon.nextSibling || null;
693 if(nextElement != element) {
694 var oldParentNode = element.parentNode;
695 element.style.visibility = "hidden"; // fix gecko rendering
696 dropon.parentNode.insertBefore(element, nextElement);
697 if(dropon.parentNode!=oldParentNode)
698 Sortable.options(oldParentNode).onChange(element);
699 Sortable.options(dropon.parentNode).onChange(element);
704 onEmptyHover: function(element, dropon, overlap) {
705 var oldParentNode = element.parentNode;
706 var droponOptions = Sortable.options(dropon);
708 if(!Element.isParent(dropon, element)) {
711 var children = Sortable.findElements(dropon, {tag: droponOptions.tag});
715 var offset = Element.offsetSize(dropon, droponOptions.overlap) * (1.0 - overlap);
717 for (index = 0; index < children.length; index += 1) {
718 if (offset - Element.offsetSize (children[index], droponOptions.overlap) >= 0) {
719 offset -= Element.offsetSize (children[index], droponOptions.overlap);
720 } else if (offset - (Element.offsetSize (children[index], droponOptions.overlap) / 2) >= 0) {
721 child = index + 1 < children.length ? children[index + 1] : null;
724 child = children[index];
730 dropon.insertBefore(element, child);
732 Sortable.options(oldParentNode).onChange(element);
733 droponOptions.onChange(element);
738 if(Sortable._marker) Element.hide(Sortable._marker);
741 mark: function(dropon, position) {
742 // mark on ghosting only
743 var sortable = Sortable.options(dropon.parentNode);
744 if(sortable && !sortable.ghosting) return;
746 if(!Sortable._marker) {
747 Sortable._marker = $('dropmarker') || document.createElement('DIV');
748 Element.hide(Sortable._marker);
749 Element.addClassName(Sortable._marker, 'dropmarker');
750 Sortable._marker.style.position = 'absolute';
751 document.getElementsByTagName("body").item(0).appendChild(Sortable._marker);
753 var offsets = Position.cumulativeOffset(dropon);
754 Sortable._marker.style.left = offsets[0] + 'px';
755 Sortable._marker.style.top = offsets[1] + 'px';
757 if(position=='after')
758 if(sortable.overlap == 'horizontal')
759 Sortable._marker.style.left = (offsets[0]+dropon.clientWidth) + 'px';
761 Sortable._marker.style.top = (offsets[1]+dropon.clientHeight) + 'px';
763 Element.show(Sortable._marker);
766 _tree: function(element, options, parent) {
767 var children = Sortable.findElements(element, options) || [];
769 for (var i = 0; i < children.length; ++i) {
770 var match = children[i].id.match(options.format);
772 if (!match) continue;
775 id: encodeURIComponent(match ? match[1] : null),
779 position: parent.children.length,
780 container: Sortable._findChildrenElement(children[i], options.treeTag.toUpperCase())
783 /* Get the element containing the children and recurse over it */
785 this._tree(child.container, options, child)
787 parent.children.push (child);
793 /* Finds the first element of the given tag type within a parent element.
794 Used for finding the first LI[ST] within a L[IST]I[TEM].*/
795 _findChildrenElement: function (element, containerTag) {
796 if (element && element.hasChildNodes)
797 for (var i = 0; i < element.childNodes.length; ++i)
798 if (element.childNodes[i].tagName == containerTag)
799 return element.childNodes[i];
804 tree: function(element) {
805 element = $(element);
806 var sortableOptions = this.options(element);
807 var options = Object.extend({
808 tag: sortableOptions.tag,
809 treeTag: sortableOptions.treeTag,
810 only: sortableOptions.only,
812 format: sortableOptions.format
813 }, arguments[1] || {});
823 return Sortable._tree (element, options, root);
826 /* Construct a [i] index for a particular node */
827 _constructIndex: function(node) {
830 if (node.id) index = '[' + node.position + ']' + index;
831 } while ((node = node.parent) != null);
835 sequence: function(element) {
836 element = $(element);
837 var options = Object.extend(this.options(element), arguments[1] || {});
839 return $(this.findElements(element, options) || []).map( function(item) {
840 return item.id.match(options.format) ? item.id.match(options.format)[1] : '';
844 setSequence: function(element, new_sequence) {
845 element = $(element);
846 var options = Object.extend(this.options(element), arguments[2] || {});
849 this.findElements(element, options).each( function(n) {
850 if (n.id.match(options.format))
851 nodeMap[n.id.match(options.format)[1]] = [n, n.parentNode];
852 n.parentNode.removeChild(n);
855 new_sequence.each(function(ident) {
856 var n = nodeMap[ident];
858 n[1].appendChild(n[0]);
859 delete nodeMap[ident];
864 serialize: function(element) {
865 element = $(element);
866 var options = Object.extend(Sortable.options(element), arguments[1] || {});
867 var name = encodeURIComponent(
868 (arguments[1] && arguments[1].name) ? arguments[1].name : element.id);
871 return Sortable.tree(element, arguments[1]).children.map( function (item) {
872 return [name + Sortable._constructIndex(item) + "=" +
873 encodeURIComponent(item.id)].concat(item.children.map(arguments.callee));
874 }).flatten().join('&');
876 return Sortable.sequence(element, arguments[1]).map( function(item) {
877 return name + "[]=" + encodeURIComponent(item);
883 /* Returns true if child is contained within element */
884 Element.isParent = function(child, element) {
885 if (!child.parentNode || child == element) return false;
887 if (child.parentNode == element) return true;
889 return Element.isParent(child.parentNode, element);
892 Element.findChildren = function(element, only, recursive, tagName) {
893 if(!element.hasChildNodes()) return null;
894 tagName = tagName.toUpperCase();
895 if(only) only = [only].flatten();
897 $A(element.childNodes).each( function(e) {
898 if(e.tagName && e.tagName.toUpperCase()==tagName &&
899 (!only || (Element.classNames(e).detect(function(v) { return only.include(v) }))))
902 var grandchildren = Element.findChildren(e, only, recursive, tagName);
903 if(grandchildren) elements.push(grandchildren);
907 return (elements.length>0 ? elements.flatten() : []);
910 Element.offsetSize = function (element, type) {
911 if (type == 'vertical' || type == 'height')
912 return element.offsetHeight;
914 return element.offsetWidth;