michael@0: // script.aculo.us dragdrop.js v1.7.1_beta2, Tue May 15 15:15:45 EDT 2007 michael@0: michael@0: // Copyright (c) 2005-2007 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us) michael@0: // (c) 2005-2007 Sammi Williams (http://www.oriontransfer.co.nz, sammi@oriontransfer.co.nz) michael@0: // michael@0: // script.aculo.us is freely distributable under the terms of an MIT-style license. michael@0: // For details, see the script.aculo.us web site: http://script.aculo.us/ michael@0: michael@0: if(typeof Effect == 'undefined') michael@0: throw("dragdrop.js requires including script.aculo.us' effects.js library"); michael@0: michael@0: var Droppables = { michael@0: drops: [], michael@0: michael@0: remove: function(element) { michael@0: this.drops = this.drops.reject(function(d) { return d.element==$(element) }); michael@0: }, michael@0: michael@0: add: function(element) { michael@0: element = $(element); michael@0: var options = Object.extend({ michael@0: greedy: true, michael@0: hoverclass: null, michael@0: tree: false michael@0: }, arguments[1] || {}); michael@0: michael@0: // cache containers michael@0: if(options.containment) { michael@0: options._containers = []; michael@0: var containment = options.containment; michael@0: if((typeof containment == 'object') && michael@0: (containment.constructor == Array)) { michael@0: containment.each( function(c) { options._containers.push($(c)) }); michael@0: } else { michael@0: options._containers.push($(containment)); michael@0: } michael@0: } michael@0: michael@0: if(options.accept) options.accept = [options.accept].flatten(); michael@0: michael@0: Element.makePositioned(element); // fix IE michael@0: options.element = element; michael@0: michael@0: this.drops.push(options); michael@0: }, michael@0: michael@0: findDeepestChild: function(drops) { michael@0: deepest = drops[0]; michael@0: michael@0: for (i = 1; i < drops.length; ++i) michael@0: if (Element.isParent(drops[i].element, deepest.element)) michael@0: deepest = drops[i]; michael@0: michael@0: return deepest; michael@0: }, michael@0: michael@0: isContained: function(element, drop) { michael@0: var containmentNode; michael@0: if(drop.tree) { michael@0: containmentNode = element.treeNode; michael@0: } else { michael@0: containmentNode = element.parentNode; michael@0: } michael@0: return drop._containers.detect(function(c) { return containmentNode == c }); michael@0: }, michael@0: michael@0: isAffected: function(point, element, drop) { michael@0: return ( michael@0: (drop.element!=element) && michael@0: ((!drop._containers) || michael@0: this.isContained(element, drop)) && michael@0: ((!drop.accept) || michael@0: (Element.classNames(element).detect( michael@0: function(v) { return drop.accept.include(v) } ) )) && michael@0: Position.within(drop.element, point[0], point[1]) ); michael@0: }, michael@0: michael@0: deactivate: function(drop) { michael@0: if(drop.hoverclass) michael@0: Element.removeClassName(drop.element, drop.hoverclass); michael@0: this.last_active = null; michael@0: }, michael@0: michael@0: activate: function(drop) { michael@0: if(drop.hoverclass) michael@0: Element.addClassName(drop.element, drop.hoverclass); michael@0: this.last_active = drop; michael@0: }, michael@0: michael@0: show: function(point, element) { michael@0: if(!this.drops.length) return; michael@0: var affected = []; michael@0: michael@0: if(this.last_active) this.deactivate(this.last_active); michael@0: this.drops.each( function(drop) { michael@0: if(Droppables.isAffected(point, element, drop)) michael@0: affected.push(drop); michael@0: }); michael@0: michael@0: if(affected.length>0) { michael@0: drop = Droppables.findDeepestChild(affected); michael@0: Position.within(drop.element, point[0], point[1]); michael@0: if(drop.onHover) michael@0: drop.onHover(element, drop.element, Position.overlap(drop.overlap, drop.element)); michael@0: michael@0: Droppables.activate(drop); michael@0: } michael@0: }, michael@0: michael@0: fire: function(event, element) { michael@0: if(!this.last_active) return; michael@0: Position.prepare(); michael@0: michael@0: if (this.isAffected([Event.pointerX(event), Event.pointerY(event)], element, this.last_active)) michael@0: if (this.last_active.onDrop) { michael@0: this.last_active.onDrop(element, this.last_active.element, event); michael@0: return true; michael@0: } michael@0: }, michael@0: michael@0: reset: function() { michael@0: if(this.last_active) michael@0: this.deactivate(this.last_active); michael@0: } michael@0: } michael@0: michael@0: var Draggables = { michael@0: drags: [], michael@0: observers: [], michael@0: michael@0: register: function(draggable) { michael@0: if(this.drags.length == 0) { michael@0: this.eventMouseUp = this.endDrag.bindAsEventListener(this); michael@0: this.eventMouseMove = this.updateDrag.bindAsEventListener(this); michael@0: this.eventKeypress = this.keyPress.bindAsEventListener(this); michael@0: michael@0: Event.observe(document, "mouseup", this.eventMouseUp); michael@0: Event.observe(document, "mousemove", this.eventMouseMove); michael@0: Event.observe(document, "keypress", this.eventKeypress); michael@0: } michael@0: this.drags.push(draggable); michael@0: }, michael@0: michael@0: unregister: function(draggable) { michael@0: this.drags = this.drags.reject(function(d) { return d==draggable }); michael@0: if(this.drags.length == 0) { michael@0: Event.stopObserving(document, "mouseup", this.eventMouseUp); michael@0: Event.stopObserving(document, "mousemove", this.eventMouseMove); michael@0: Event.stopObserving(document, "keypress", this.eventKeypress); michael@0: } michael@0: }, michael@0: michael@0: activate: function(draggable) { michael@0: if(draggable.options.delay) { michael@0: this._timeout = setTimeout(function() { michael@0: Draggables._timeout = null; michael@0: window.focus(); michael@0: Draggables.activeDraggable = draggable; michael@0: }.bind(this), draggable.options.delay); michael@0: } else { michael@0: window.focus(); // allows keypress events if window isn't currently focused, fails for Safari michael@0: this.activeDraggable = draggable; michael@0: } michael@0: }, michael@0: michael@0: deactivate: function() { michael@0: this.activeDraggable = null; michael@0: }, michael@0: michael@0: updateDrag: function(event) { michael@0: if(!this.activeDraggable) return; michael@0: var pointer = [Event.pointerX(event), Event.pointerY(event)]; michael@0: // Mozilla-based browsers fire successive mousemove events with michael@0: // the same coordinates, prevent needless redrawing (moz bug?) michael@0: if(this._lastPointer && (this._lastPointer.inspect() == pointer.inspect())) return; michael@0: this._lastPointer = pointer; michael@0: michael@0: this.activeDraggable.updateDrag(event, pointer); michael@0: }, michael@0: michael@0: endDrag: function(event) { michael@0: if(this._timeout) { michael@0: clearTimeout(this._timeout); michael@0: this._timeout = null; michael@0: } michael@0: if(!this.activeDraggable) return; michael@0: this._lastPointer = null; michael@0: this.activeDraggable.endDrag(event); michael@0: this.activeDraggable = null; michael@0: }, michael@0: michael@0: keyPress: function(event) { michael@0: if(this.activeDraggable) michael@0: this.activeDraggable.keyPress(event); michael@0: }, michael@0: michael@0: addObserver: function(observer) { michael@0: this.observers.push(observer); michael@0: this._cacheObserverCallbacks(); michael@0: }, michael@0: michael@0: removeObserver: function(element) { // element instead of observer fixes mem leaks michael@0: this.observers = this.observers.reject( function(o) { return o.element==element }); michael@0: this._cacheObserverCallbacks(); michael@0: }, michael@0: michael@0: notify: function(eventName, draggable, event) { // 'onStart', 'onEnd', 'onDrag' michael@0: if(this[eventName+'Count'] > 0) michael@0: this.observers.each( function(o) { michael@0: if(o[eventName]) o[eventName](eventName, draggable, event); michael@0: }); michael@0: if(draggable.options[eventName]) draggable.options[eventName](draggable, event); michael@0: }, michael@0: michael@0: _cacheObserverCallbacks: function() { michael@0: ['onStart','onEnd','onDrag'].each( function(eventName) { michael@0: Draggables[eventName+'Count'] = Draggables.observers.select( michael@0: function(o) { return o[eventName]; } michael@0: ).length; michael@0: }); michael@0: } michael@0: } michael@0: michael@0: /*--------------------------------------------------------------------------*/ michael@0: michael@0: var Draggable = Class.create(); michael@0: Draggable._dragging = {}; michael@0: michael@0: Draggable.prototype = { michael@0: initialize: function(element) { michael@0: var defaults = { michael@0: handle: false, michael@0: reverteffect: function(element, top_offset, left_offset) { michael@0: var dur = Math.sqrt(Math.abs(top_offset^2)+Math.abs(left_offset^2))*0.02; michael@0: new Effect.Move(element, { x: -left_offset, y: -top_offset, duration: dur, michael@0: queue: {scope:'_draggable', position:'end'} michael@0: }); michael@0: }, michael@0: endeffect: function(element) { michael@0: var toOpacity = typeof element._opacity == 'number' ? element._opacity : 1.0; michael@0: new Effect.Opacity(element, {duration:0.2, from:0.7, to:toOpacity, michael@0: queue: {scope:'_draggable', position:'end'}, michael@0: afterFinish: function(){ michael@0: Draggable._dragging[element] = false michael@0: } michael@0: }); michael@0: }, michael@0: zindex: 1000, michael@0: revert: false, michael@0: quiet: false, michael@0: scroll: false, michael@0: scrollSensitivity: 20, michael@0: scrollSpeed: 15, michael@0: snap: false, // false, or xy or [x,y] or function(x,y){ return [x,y] } michael@0: delay: 0 michael@0: }; michael@0: michael@0: if(!arguments[1] || typeof arguments[1].endeffect == 'undefined') michael@0: Object.extend(defaults, { michael@0: starteffect: function(element) { michael@0: element._opacity = Element.getOpacity(element); michael@0: Draggable._dragging[element] = true; michael@0: new Effect.Opacity(element, {duration:0.2, from:element._opacity, to:0.7}); michael@0: } michael@0: }); michael@0: michael@0: var options = Object.extend(defaults, arguments[1] || {}); michael@0: michael@0: this.element = $(element); michael@0: michael@0: if(options.handle && (typeof options.handle == 'string')) michael@0: this.handle = this.element.down('.'+options.handle, 0); michael@0: michael@0: if(!this.handle) this.handle = $(options.handle); michael@0: if(!this.handle) this.handle = this.element; michael@0: michael@0: if(options.scroll && !options.scroll.scrollTo && !options.scroll.outerHTML) { michael@0: options.scroll = $(options.scroll); michael@0: this._isScrollChild = Element.childOf(this.element, options.scroll); michael@0: } michael@0: michael@0: Element.makePositioned(this.element); // fix IE michael@0: michael@0: this.delta = this.currentDelta(); michael@0: this.options = options; michael@0: this.dragging = false; michael@0: michael@0: this.eventMouseDown = this.initDrag.bindAsEventListener(this); michael@0: Event.observe(this.handle, "mousedown", this.eventMouseDown); michael@0: michael@0: Draggables.register(this); michael@0: }, michael@0: michael@0: destroy: function() { michael@0: Event.stopObserving(this.handle, "mousedown", this.eventMouseDown); michael@0: Draggables.unregister(this); michael@0: }, michael@0: michael@0: currentDelta: function() { michael@0: return([ michael@0: parseInt(Element.getStyle(this.element,'left') || '0'), michael@0: parseInt(Element.getStyle(this.element,'top') || '0')]); michael@0: }, michael@0: michael@0: initDrag: function(event) { michael@0: if(typeof Draggable._dragging[this.element] != 'undefined' && michael@0: Draggable._dragging[this.element]) return; michael@0: if(Event.isLeftClick(event)) { michael@0: // abort on form elements, fixes a Firefox issue michael@0: var src = Event.element(event); michael@0: if((tag_name = src.tagName.toUpperCase()) && ( michael@0: tag_name=='INPUT' || michael@0: tag_name=='SELECT' || michael@0: tag_name=='OPTION' || michael@0: tag_name=='BUTTON' || michael@0: tag_name=='TEXTAREA')) return; michael@0: michael@0: var pointer = [Event.pointerX(event), Event.pointerY(event)]; michael@0: var pos = Position.cumulativeOffset(this.element); michael@0: this.offset = [0,1].map( function(i) { return (pointer[i] - pos[i]) }); michael@0: michael@0: Draggables.activate(this); michael@0: Event.stop(event); michael@0: } michael@0: }, michael@0: michael@0: startDrag: function(event) { michael@0: this.dragging = true; michael@0: michael@0: if(this.options.zindex) { michael@0: this.originalZ = parseInt(Element.getStyle(this.element,'z-index') || 0); michael@0: this.element.style.zIndex = this.options.zindex; michael@0: } michael@0: michael@0: if(this.options.ghosting) { michael@0: this._clone = this.element.cloneNode(true); michael@0: Position.absolutize(this.element); michael@0: this.element.parentNode.insertBefore(this._clone, this.element); michael@0: } michael@0: michael@0: if(this.options.scroll) { michael@0: if (this.options.scroll == window) { michael@0: var where = this._getWindowScroll(this.options.scroll); michael@0: this.originalScrollLeft = where.left; michael@0: this.originalScrollTop = where.top; michael@0: } else { michael@0: this.originalScrollLeft = this.options.scroll.scrollLeft; michael@0: this.originalScrollTop = this.options.scroll.scrollTop; michael@0: } michael@0: } michael@0: michael@0: Draggables.notify('onStart', this, event); michael@0: michael@0: if(this.options.starteffect) this.options.starteffect(this.element); michael@0: }, michael@0: michael@0: updateDrag: function(event, pointer) { michael@0: if(!this.dragging) this.startDrag(event); michael@0: michael@0: if(!this.options.quiet){ michael@0: Position.prepare(); michael@0: Droppables.show(pointer, this.element); michael@0: } michael@0: michael@0: Draggables.notify('onDrag', this, event); michael@0: michael@0: this.draw(pointer); michael@0: if(this.options.change) this.options.change(this); michael@0: michael@0: if(this.options.scroll) { michael@0: this.stopScrolling(); michael@0: michael@0: var p; michael@0: if (this.options.scroll == window) { michael@0: with(this._getWindowScroll(this.options.scroll)) { p = [ left, top, left+width, top+height ]; } michael@0: } else { michael@0: p = Position.page(this.options.scroll); michael@0: p[0] += this.options.scroll.scrollLeft + Position.deltaX; michael@0: p[1] += this.options.scroll.scrollTop + Position.deltaY; michael@0: p.push(p[0]+this.options.scroll.offsetWidth); michael@0: p.push(p[1]+this.options.scroll.offsetHeight); michael@0: } michael@0: var speed = [0,0]; michael@0: if(pointer[0] < (p[0]+this.options.scrollSensitivity)) speed[0] = pointer[0]-(p[0]+this.options.scrollSensitivity); michael@0: if(pointer[1] < (p[1]+this.options.scrollSensitivity)) speed[1] = pointer[1]-(p[1]+this.options.scrollSensitivity); michael@0: if(pointer[0] > (p[2]-this.options.scrollSensitivity)) speed[0] = pointer[0]-(p[2]-this.options.scrollSensitivity); michael@0: if(pointer[1] > (p[3]-this.options.scrollSensitivity)) speed[1] = pointer[1]-(p[3]-this.options.scrollSensitivity); michael@0: this.startScrolling(speed); michael@0: } michael@0: michael@0: // fix AppleWebKit rendering michael@0: if(Prototype.Browser.WebKit) window.scrollBy(0,0); michael@0: michael@0: Event.stop(event); michael@0: }, michael@0: michael@0: finishDrag: function(event, success) { michael@0: this.dragging = false; michael@0: michael@0: if(this.options.quiet){ michael@0: Position.prepare(); michael@0: var pointer = [Event.pointerX(event), Event.pointerY(event)]; michael@0: Droppables.show(pointer, this.element); michael@0: } michael@0: michael@0: if(this.options.ghosting) { michael@0: Position.relativize(this.element); michael@0: Element.remove(this._clone); michael@0: this._clone = null; michael@0: } michael@0: michael@0: var dropped = false; michael@0: if(success) { michael@0: dropped = Droppables.fire(event, this.element); michael@0: if (!dropped) dropped = false; michael@0: } michael@0: if(dropped && this.options.onDropped) this.options.onDropped(this.element); michael@0: Draggables.notify('onEnd', this, event); michael@0: michael@0: var revert = this.options.revert; michael@0: if(revert && typeof revert == 'function') revert = revert(this.element); michael@0: michael@0: var d = this.currentDelta(); michael@0: if(revert && this.options.reverteffect) { michael@0: if (dropped == 0 || revert != 'failure') michael@0: this.options.reverteffect(this.element, michael@0: d[1]-this.delta[1], d[0]-this.delta[0]); michael@0: } else { michael@0: this.delta = d; michael@0: } michael@0: michael@0: if(this.options.zindex) michael@0: this.element.style.zIndex = this.originalZ; michael@0: michael@0: if(this.options.endeffect) michael@0: this.options.endeffect(this.element); michael@0: michael@0: Draggables.deactivate(this); michael@0: Droppables.reset(); michael@0: }, michael@0: michael@0: keyPress: function(event) { michael@0: if(event.keyCode!=Event.KEY_ESC) return; michael@0: this.finishDrag(event, false); michael@0: Event.stop(event); michael@0: }, michael@0: michael@0: endDrag: function(event) { michael@0: if(!this.dragging) return; michael@0: this.stopScrolling(); michael@0: this.finishDrag(event, true); michael@0: Event.stop(event); michael@0: }, michael@0: michael@0: draw: function(point) { michael@0: var pos = Position.cumulativeOffset(this.element); michael@0: if(this.options.ghosting) { michael@0: var r = Position.realOffset(this.element); michael@0: pos[0] += r[0] - Position.deltaX; pos[1] += r[1] - Position.deltaY; michael@0: } michael@0: michael@0: var d = this.currentDelta(); michael@0: pos[0] -= d[0]; pos[1] -= d[1]; michael@0: michael@0: if(this.options.scroll && (this.options.scroll != window && this._isScrollChild)) { michael@0: pos[0] -= this.options.scroll.scrollLeft-this.originalScrollLeft; michael@0: pos[1] -= this.options.scroll.scrollTop-this.originalScrollTop; michael@0: } michael@0: michael@0: var p = [0,1].map(function(i){ michael@0: return (point[i]-pos[i]-this.offset[i]) michael@0: }.bind(this)); michael@0: michael@0: if(this.options.snap) { michael@0: if(typeof this.options.snap == 'function') { michael@0: p = this.options.snap(p[0],p[1],this); michael@0: } else { michael@0: if(this.options.snap instanceof Array) { michael@0: p = p.map( function(v, i) { michael@0: return Math.round(v/this.options.snap[i])*this.options.snap[i] }.bind(this)) michael@0: } else { michael@0: p = p.map( function(v) { michael@0: return Math.round(v/this.options.snap)*this.options.snap }.bind(this)) michael@0: } michael@0: }} michael@0: michael@0: var style = this.element.style; michael@0: if((!this.options.constraint) || (this.options.constraint=='horizontal')) michael@0: style.left = p[0] + "px"; michael@0: if((!this.options.constraint) || (this.options.constraint=='vertical')) michael@0: style.top = p[1] + "px"; michael@0: michael@0: if(style.visibility=="hidden") style.visibility = ""; // fix gecko rendering michael@0: }, michael@0: michael@0: stopScrolling: function() { michael@0: if(this.scrollInterval) { michael@0: clearInterval(this.scrollInterval); michael@0: this.scrollInterval = null; michael@0: Draggables._lastScrollPointer = null; michael@0: } michael@0: }, michael@0: michael@0: startScrolling: function(speed) { michael@0: if(!(speed[0] || speed[1])) return; michael@0: this.scrollSpeed = [speed[0]*this.options.scrollSpeed,speed[1]*this.options.scrollSpeed]; michael@0: this.lastScrolled = new Date(); michael@0: this.scrollInterval = setInterval(this.scroll.bind(this), 10); michael@0: }, michael@0: michael@0: scroll: function() { michael@0: var current = new Date(); michael@0: var delta = current - this.lastScrolled; michael@0: this.lastScrolled = current; michael@0: if(this.options.scroll == window) { michael@0: with (this._getWindowScroll(this.options.scroll)) { michael@0: if (this.scrollSpeed[0] || this.scrollSpeed[1]) { michael@0: var d = delta / 1000; michael@0: this.options.scroll.scrollTo( left + d*this.scrollSpeed[0], top + d*this.scrollSpeed[1] ); michael@0: } michael@0: } michael@0: } else { michael@0: this.options.scroll.scrollLeft += this.scrollSpeed[0] * delta / 1000; michael@0: this.options.scroll.scrollTop += this.scrollSpeed[1] * delta / 1000; michael@0: } michael@0: michael@0: Position.prepare(); michael@0: Droppables.show(Draggables._lastPointer, this.element); michael@0: Draggables.notify('onDrag', this); michael@0: if (this._isScrollChild) { michael@0: Draggables._lastScrollPointer = Draggables._lastScrollPointer || $A(Draggables._lastPointer); michael@0: Draggables._lastScrollPointer[0] += this.scrollSpeed[0] * delta / 1000; michael@0: Draggables._lastScrollPointer[1] += this.scrollSpeed[1] * delta / 1000; michael@0: if (Draggables._lastScrollPointer[0] < 0) michael@0: Draggables._lastScrollPointer[0] = 0; michael@0: if (Draggables._lastScrollPointer[1] < 0) michael@0: Draggables._lastScrollPointer[1] = 0; michael@0: this.draw(Draggables._lastScrollPointer); michael@0: } michael@0: michael@0: if(this.options.change) this.options.change(this); michael@0: }, michael@0: michael@0: _getWindowScroll: function(w) { michael@0: var T, L, W, H; michael@0: with (w.document) { michael@0: if (w.document.documentElement && documentElement.scrollTop) { michael@0: T = documentElement.scrollTop; michael@0: L = documentElement.scrollLeft; michael@0: } else if (w.document.body) { michael@0: T = body.scrollTop; michael@0: L = body.scrollLeft; michael@0: } michael@0: if (w.innerWidth) { michael@0: W = w.innerWidth; michael@0: H = w.innerHeight; michael@0: } else if (w.document.documentElement && documentElement.clientWidth) { michael@0: W = documentElement.clientWidth; michael@0: H = documentElement.clientHeight; michael@0: } else { michael@0: W = body.offsetWidth; michael@0: H = body.offsetHeight michael@0: } michael@0: } michael@0: return { top: T, left: L, width: W, height: H }; michael@0: } michael@0: } michael@0: michael@0: /*--------------------------------------------------------------------------*/ michael@0: michael@0: var SortableObserver = Class.create(); michael@0: SortableObserver.prototype = { michael@0: initialize: function(element, observer) { michael@0: this.element = $(element); michael@0: this.observer = observer; michael@0: this.lastValue = Sortable.serialize(this.element); michael@0: }, michael@0: michael@0: onStart: function() { michael@0: this.lastValue = Sortable.serialize(this.element); michael@0: }, michael@0: michael@0: onEnd: function() { michael@0: Sortable.unmark(); michael@0: if(this.lastValue != Sortable.serialize(this.element)) michael@0: this.observer(this.element) michael@0: } michael@0: } michael@0: michael@0: var Sortable = { michael@0: SERIALIZE_RULE: /^[^_\-](?:[A-Za-z0-9\-\_]*)[_](.*)$/, michael@0: michael@0: sortables: {}, michael@0: michael@0: _findRootElement: function(element) { michael@0: while (element.tagName.toUpperCase() != "BODY") { michael@0: if(element.id && Sortable.sortables[element.id]) return element; michael@0: element = element.parentNode; michael@0: } michael@0: }, michael@0: michael@0: options: function(element) { michael@0: element = Sortable._findRootElement($(element)); michael@0: if(!element) return; michael@0: return Sortable.sortables[element.id]; michael@0: }, michael@0: michael@0: destroy: function(element){ michael@0: var s = Sortable.options(element); michael@0: michael@0: if(s) { michael@0: Draggables.removeObserver(s.element); michael@0: s.droppables.each(function(d){ Droppables.remove(d) }); michael@0: s.draggables.invoke('destroy'); michael@0: michael@0: delete Sortable.sortables[s.element.id]; michael@0: } michael@0: }, michael@0: michael@0: create: function(element) { michael@0: element = $(element); michael@0: var options = Object.extend({ michael@0: element: element, michael@0: tag: 'li', // assumes li children, override with tag: 'tagname' michael@0: dropOnEmpty: false, michael@0: tree: false, michael@0: treeTag: 'ul', michael@0: overlap: 'vertical', // one of 'vertical', 'horizontal' michael@0: constraint: 'vertical', // one of 'vertical', 'horizontal', false michael@0: containment: element, // also takes array of elements (or id's); or false michael@0: handle: false, // or a CSS class michael@0: only: false, michael@0: delay: 0, michael@0: hoverclass: null, michael@0: ghosting: false, michael@0: quiet: false, michael@0: scroll: false, michael@0: scrollSensitivity: 20, michael@0: scrollSpeed: 15, michael@0: format: this.SERIALIZE_RULE, michael@0: michael@0: // these take arrays of elements or ids and can be michael@0: // used for better initialization performance michael@0: elements: false, michael@0: handles: false, michael@0: michael@0: onChange: Prototype.emptyFunction, michael@0: onUpdate: Prototype.emptyFunction michael@0: }, arguments[1] || {}); michael@0: michael@0: // clear any old sortable with same element michael@0: this.destroy(element); michael@0: michael@0: // build options for the draggables michael@0: var options_for_draggable = { michael@0: revert: true, michael@0: quiet: options.quiet, michael@0: scroll: options.scroll, michael@0: scrollSpeed: options.scrollSpeed, michael@0: scrollSensitivity: options.scrollSensitivity, michael@0: delay: options.delay, michael@0: ghosting: options.ghosting, michael@0: constraint: options.constraint, michael@0: handle: options.handle }; michael@0: michael@0: if(options.starteffect) michael@0: options_for_draggable.starteffect = options.starteffect; michael@0: michael@0: if(options.reverteffect) michael@0: options_for_draggable.reverteffect = options.reverteffect; michael@0: else michael@0: if(options.ghosting) options_for_draggable.reverteffect = function(element) { michael@0: element.style.top = 0; michael@0: element.style.left = 0; michael@0: }; michael@0: michael@0: if(options.endeffect) michael@0: options_for_draggable.endeffect = options.endeffect; michael@0: michael@0: if(options.zindex) michael@0: options_for_draggable.zindex = options.zindex; michael@0: michael@0: // build options for the droppables michael@0: var options_for_droppable = { michael@0: overlap: options.overlap, michael@0: containment: options.containment, michael@0: tree: options.tree, michael@0: hoverclass: options.hoverclass, michael@0: onHover: Sortable.onHover michael@0: } michael@0: michael@0: var options_for_tree = { michael@0: onHover: Sortable.onEmptyHover, michael@0: overlap: options.overlap, michael@0: containment: options.containment, michael@0: hoverclass: options.hoverclass michael@0: } michael@0: michael@0: // fix for gecko engine michael@0: Element.cleanWhitespace(element); michael@0: michael@0: options.draggables = []; michael@0: options.droppables = []; michael@0: michael@0: // drop on empty handling michael@0: if(options.dropOnEmpty || options.tree) { michael@0: Droppables.add(element, options_for_tree); michael@0: options.droppables.push(element); michael@0: } michael@0: michael@0: (options.elements || this.findElements(element, options) || []).each( function(e,i) { michael@0: var handle = options.handles ? $(options.handles[i]) : michael@0: (options.handle ? $(e).getElementsByClassName(options.handle)[0] : e); michael@0: options.draggables.push( michael@0: new Draggable(e, Object.extend(options_for_draggable, { handle: handle }))); michael@0: Droppables.add(e, options_for_droppable); michael@0: if(options.tree) e.treeNode = element; michael@0: options.droppables.push(e); michael@0: }); michael@0: michael@0: if(options.tree) { michael@0: (Sortable.findTreeElements(element, options) || []).each( function(e) { michael@0: Droppables.add(e, options_for_tree); michael@0: e.treeNode = element; michael@0: options.droppables.push(e); michael@0: }); michael@0: } michael@0: michael@0: // keep reference michael@0: this.sortables[element.id] = options; michael@0: michael@0: // for onupdate michael@0: Draggables.addObserver(new SortableObserver(element, options.onUpdate)); michael@0: michael@0: }, michael@0: michael@0: // return all suitable-for-sortable elements in a guaranteed order michael@0: findElements: function(element, options) { michael@0: return Element.findChildren( michael@0: element, options.only, options.tree ? true : false, options.tag); michael@0: }, michael@0: michael@0: findTreeElements: function(element, options) { michael@0: return Element.findChildren( michael@0: element, options.only, options.tree ? true : false, options.treeTag); michael@0: }, michael@0: michael@0: onHover: function(element, dropon, overlap) { michael@0: if(Element.isParent(dropon, element)) return; michael@0: michael@0: if(overlap > .33 && overlap < .66 && Sortable.options(dropon).tree) { michael@0: return; michael@0: } else if(overlap>0.5) { michael@0: Sortable.mark(dropon, 'before'); michael@0: if(dropon.previousSibling != element) { michael@0: var oldParentNode = element.parentNode; michael@0: element.style.visibility = "hidden"; // fix gecko rendering michael@0: dropon.parentNode.insertBefore(element, dropon); michael@0: if(dropon.parentNode!=oldParentNode) michael@0: Sortable.options(oldParentNode).onChange(element); michael@0: Sortable.options(dropon.parentNode).onChange(element); michael@0: } michael@0: } else { michael@0: Sortable.mark(dropon, 'after'); michael@0: var nextElement = dropon.nextSibling || null; michael@0: if(nextElement != element) { michael@0: var oldParentNode = element.parentNode; michael@0: element.style.visibility = "hidden"; // fix gecko rendering michael@0: dropon.parentNode.insertBefore(element, nextElement); michael@0: if(dropon.parentNode!=oldParentNode) michael@0: Sortable.options(oldParentNode).onChange(element); michael@0: Sortable.options(dropon.parentNode).onChange(element); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: onEmptyHover: function(element, dropon, overlap) { michael@0: var oldParentNode = element.parentNode; michael@0: var droponOptions = Sortable.options(dropon); michael@0: michael@0: if(!Element.isParent(dropon, element)) { michael@0: var index; michael@0: michael@0: var children = Sortable.findElements(dropon, {tag: droponOptions.tag, only: droponOptions.only}); michael@0: var child = null; michael@0: michael@0: if(children) { michael@0: var offset = Element.offsetSize(dropon, droponOptions.overlap) * (1.0 - overlap); michael@0: michael@0: for (index = 0; index < children.length; index += 1) { michael@0: if (offset - Element.offsetSize (children[index], droponOptions.overlap) >= 0) { michael@0: offset -= Element.offsetSize (children[index], droponOptions.overlap); michael@0: } else if (offset - (Element.offsetSize (children[index], droponOptions.overlap) / 2) >= 0) { michael@0: child = index + 1 < children.length ? children[index + 1] : null; michael@0: break; michael@0: } else { michael@0: child = children[index]; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: dropon.insertBefore(element, child); michael@0: michael@0: Sortable.options(oldParentNode).onChange(element); michael@0: droponOptions.onChange(element); michael@0: } michael@0: }, michael@0: michael@0: unmark: function() { michael@0: if(Sortable._marker) Sortable._marker.hide(); michael@0: }, michael@0: michael@0: mark: function(dropon, position) { michael@0: // mark on ghosting only michael@0: var sortable = Sortable.options(dropon.parentNode); michael@0: if(sortable && !sortable.ghosting) return; michael@0: michael@0: if(!Sortable._marker) { michael@0: Sortable._marker = michael@0: ($('dropmarker') || Element.extend(document.createElement('DIV'))). michael@0: hide().addClassName('dropmarker').setStyle({position:'absolute'}); michael@0: document.getElementsByTagName("body").item(0).appendChild(Sortable._marker); michael@0: } michael@0: var offsets = Position.cumulativeOffset(dropon); michael@0: Sortable._marker.setStyle({left: offsets[0]+'px', top: offsets[1] + 'px'}); michael@0: michael@0: if(position=='after') michael@0: if(sortable.overlap == 'horizontal') michael@0: Sortable._marker.setStyle({left: (offsets[0]+dropon.clientWidth) + 'px'}); michael@0: else michael@0: Sortable._marker.setStyle({top: (offsets[1]+dropon.clientHeight) + 'px'}); michael@0: michael@0: Sortable._marker.show(); michael@0: }, michael@0: michael@0: _tree: function(element, options, parent) { michael@0: var children = Sortable.findElements(element, options) || []; michael@0: michael@0: for (var i = 0; i < children.length; ++i) { michael@0: var match = children[i].id.match(options.format); michael@0: michael@0: if (!match) continue; michael@0: michael@0: var child = { michael@0: id: encodeURIComponent(match ? match[1] : null), michael@0: element: element, michael@0: parent: parent, michael@0: children: [], michael@0: position: parent.children.length, michael@0: container: $(children[i]).down(options.treeTag) michael@0: } michael@0: michael@0: /* Get the element containing the children and recurse over it */ michael@0: if (child.container) michael@0: this._tree(child.container, options, child) michael@0: michael@0: parent.children.push (child); michael@0: } michael@0: michael@0: return parent; michael@0: }, michael@0: michael@0: tree: function(element) { michael@0: element = $(element); michael@0: var sortableOptions = this.options(element); michael@0: var options = Object.extend({ michael@0: tag: sortableOptions.tag, michael@0: treeTag: sortableOptions.treeTag, michael@0: only: sortableOptions.only, michael@0: name: element.id, michael@0: format: sortableOptions.format michael@0: }, arguments[1] || {}); michael@0: michael@0: var root = { michael@0: id: null, michael@0: parent: null, michael@0: children: [], michael@0: container: element, michael@0: position: 0 michael@0: } michael@0: michael@0: return Sortable._tree(element, options, root); michael@0: }, michael@0: michael@0: /* Construct a [i] index for a particular node */ michael@0: _constructIndex: function(node) { michael@0: var index = ''; michael@0: do { michael@0: if (node.id) index = '[' + node.position + ']' + index; michael@0: } while ((node = node.parent) != null); michael@0: return index; michael@0: }, michael@0: michael@0: sequence: function(element) { michael@0: element = $(element); michael@0: var options = Object.extend(this.options(element), arguments[1] || {}); michael@0: michael@0: return $(this.findElements(element, options) || []).map( function(item) { michael@0: return item.id.match(options.format) ? item.id.match(options.format)[1] : ''; michael@0: }); michael@0: }, michael@0: michael@0: setSequence: function(element, new_sequence) { michael@0: element = $(element); michael@0: var options = Object.extend(this.options(element), arguments[2] || {}); michael@0: michael@0: var nodeMap = {}; michael@0: this.findElements(element, options).each( function(n) { michael@0: if (n.id.match(options.format)) michael@0: nodeMap[n.id.match(options.format)[1]] = [n, n.parentNode]; michael@0: n.parentNode.removeChild(n); michael@0: }); michael@0: michael@0: new_sequence.each(function(ident) { michael@0: var n = nodeMap[ident]; michael@0: if (n) { michael@0: n[1].appendChild(n[0]); michael@0: delete nodeMap[ident]; michael@0: } michael@0: }); michael@0: }, michael@0: michael@0: serialize: function(element) { michael@0: element = $(element); michael@0: var options = Object.extend(Sortable.options(element), arguments[1] || {}); michael@0: var name = encodeURIComponent( michael@0: (arguments[1] && arguments[1].name) ? arguments[1].name : element.id); michael@0: michael@0: if (options.tree) { michael@0: return Sortable.tree(element, arguments[1]).children.map( function (item) { michael@0: return [name + Sortable._constructIndex(item) + "[id]=" + michael@0: encodeURIComponent(item.id)].concat(item.children.map(arguments.callee)); michael@0: }).flatten().join('&'); michael@0: } else { michael@0: return Sortable.sequence(element, arguments[1]).map( function(item) { michael@0: return name + "[]=" + encodeURIComponent(item); michael@0: }).join('&'); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Returns true if child is contained within element michael@0: Element.isParent = function(child, element) { michael@0: if (!child.parentNode || child == element) return false; michael@0: if (child.parentNode == element) return true; michael@0: return Element.isParent(child.parentNode, element); michael@0: } michael@0: michael@0: Element.findChildren = function(element, only, recursive, tagName) { michael@0: if(!element.hasChildNodes()) return null; michael@0: tagName = tagName.toUpperCase(); michael@0: if(only) only = [only].flatten(); michael@0: var elements = []; michael@0: $A(element.childNodes).each( function(e) { michael@0: if(e.tagName && e.tagName.toUpperCase()==tagName && michael@0: (!only || (Element.classNames(e).detect(function(v) { return only.include(v) })))) michael@0: elements.push(e); michael@0: if(recursive) { michael@0: var grandchildren = Element.findChildren(e, only, recursive, tagName); michael@0: if(grandchildren) elements.push(grandchildren); michael@0: } michael@0: }); michael@0: michael@0: return (elements.length>0 ? elements.flatten() : []); michael@0: } michael@0: michael@0: Element.offsetSize = function (element, type) { michael@0: return element['offset' + ((type=='vertical' || type=='height') ? 'Height' : 'Width')]; michael@0: }