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