Improve output: distinguish between failed assertions (failures) and unexpected exceptions (errors), and print a filtered stack trace for any exception.

This commit is contained in:
jeb228 2010-01-29 22:13:57 +00:00
commit 4f2e303079
1839 changed files with 235630 additions and 0 deletions

View file

@ -0,0 +1,175 @@
/*
Copyright (c) 2004-2006, The Dojo Foundation
All Rights Reserved.
Licensed under the Academic Free License version 2.1 or above OR the
modified BSD license. For more information on Dojo licensing, see:
http://dojotoolkit.org/community/licensing.shtml
*/
dojo.require("dojo.lang");
dojo.provide("dojo.dnd.DragSource");
dojo.provide("dojo.dnd.DropTarget");
dojo.provide("dojo.dnd.DragObject");
dojo.provide("dojo.dnd.DragAndDrop");
dojo.dnd.DragSource = function(){
var dm = dojo.dnd.dragManager;
if(dm["registerDragSource"]){ // side-effect prevention
dm.registerDragSource(this);
}
}
dojo.lang.extend(dojo.dnd.DragSource, {
type: "",
onDragEnd: function(){
},
onDragStart: function(){
},
/*
* This function gets called when the DOM element was
* selected for dragging by the HtmlDragAndDropManager.
*/
onSelected: function(){
},
unregister: function(){
dojo.dnd.dragManager.unregisterDragSource(this);
},
reregister: function(){
dojo.dnd.dragManager.registerDragSource(this);
}
});
dojo.dnd.DragObject = function(){
var dm = dojo.dnd.dragManager;
if(dm["registerDragObject"]){ // side-effect prevention
dm.registerDragObject(this);
}
}
dojo.lang.extend(dojo.dnd.DragObject, {
type: "",
onDragStart: function(){
// gets called directly after being created by the DragSource
// default action is to clone self as icon
},
onDragMove: function(){
// this changes the UI for the drag icon
// "it moves itself"
},
onDragOver: function(){
},
onDragOut: function(){
},
onDragEnd: function(){
},
// normal aliases
onDragLeave: this.onDragOut,
onDragEnter: this.onDragOver,
// non-camel aliases
ondragout: this.onDragOut,
ondragover: this.onDragOver
});
dojo.dnd.DropTarget = function(){
if (this.constructor == dojo.dnd.DropTarget) { return; } // need to be subclassed
this.acceptedTypes = [];
dojo.dnd.dragManager.registerDropTarget(this);
}
dojo.lang.extend(dojo.dnd.DropTarget, {
acceptsType: function(type){
if(!dojo.lang.inArray(this.acceptedTypes, "*")){ // wildcard
if(!dojo.lang.inArray(this.acceptedTypes, type)) { return false; }
}
return true;
},
accepts: function(dragObjects){
if(!dojo.lang.inArray(this.acceptedTypes, "*")){ // wildcard
for (var i = 0; i < dragObjects.length; i++) {
if (!dojo.lang.inArray(this.acceptedTypes,
dragObjects[i].type)) { return false; }
}
}
return true;
},
onDragOver: function(){
},
onDragOut: function(){
},
onDragMove: function(){
},
onDropStart: function(){
},
onDrop: function(){
},
onDropEnd: function(){
}
});
// NOTE: this interface is defined here for the convenience of the DragManager
// implementor. It is expected that in most cases it will be satisfied by
// extending a native event (DOM event in HTML and SVG).
dojo.dnd.DragEvent = function(){
this.dragSource = null;
this.dragObject = null;
this.target = null;
this.eventStatus = "success";
//
// can be one of:
// [ "dropSuccess", "dropFailure", "dragMove",
// "dragStart", "dragEnter", "dragLeave"]
//
}
dojo.dnd.DragManager = function(){
/*
* The DragManager handles listening for low-level events and dispatching
* them to higher-level primitives like drag sources and drop targets. In
* order to do this, it must keep a list of the items.
*/
}
dojo.lang.extend(dojo.dnd.DragManager, {
selectedSources: [],
dragObjects: [],
dragSources: [],
registerDragSource: function(){},
dropTargets: [],
registerDropTarget: function(){},
lastDragTarget: null,
currentDragTarget: null,
onKeyDown: function(){},
onMouseOut: function(){},
onMouseMove: function(){},
onMouseUp: function(){}
});
// NOTE: despite the existance of the DragManager class, there will be a
// singleton drag manager provided by the renderer-specific D&D support code.
// It is therefore sane for us to assign instance variables to the DragManager
// prototype
// The renderer-specific file will define the following object:
// dojo.dnd.dragManager = null;

View file

@ -0,0 +1,475 @@
/*
Copyright (c) 2004-2006, The Dojo Foundation
All Rights Reserved.
Licensed under the Academic Free License version 2.1 or above OR the
modified BSD license. For more information on Dojo licensing, see:
http://dojotoolkit.org/community/licensing.shtml
*/
dojo.provide("dojo.dnd.HtmlDragAndDrop");
dojo.provide("dojo.dnd.HtmlDragSource");
dojo.provide("dojo.dnd.HtmlDropTarget");
dojo.provide("dojo.dnd.HtmlDragObject");
dojo.require("dojo.dnd.HtmlDragManager");
dojo.require("dojo.dnd.DragAndDrop");
dojo.require("dojo.dom");
dojo.require("dojo.style");
dojo.require("dojo.html");
dojo.require("dojo.html.extras");
dojo.require("dojo.lang.extras");
dojo.require("dojo.lfx.*");
dojo.require("dojo.event");
dojo.dnd.HtmlDragSource = function(node, type){
node = dojo.byId(node);
this.dragObjects = [];
this.constrainToContainer = false;
if(node){
this.domNode = node;
this.dragObject = node;
// register us
dojo.dnd.DragSource.call(this);
// set properties that might have been clobbered by the mixin
this.type = (type)||(this.domNode.nodeName.toLowerCase());
}
}
dojo.inherits(dojo.dnd.HtmlDragSource, dojo.dnd.DragSource);
dojo.lang.extend(dojo.dnd.HtmlDragSource, {
dragClass: "", // CSS classname(s) applied to node when it is being dragged
onDragStart: function(){
var dragObj = new dojo.dnd.HtmlDragObject(this.dragObject, this.type);
if(this.dragClass) { dragObj.dragClass = this.dragClass; }
if (this.constrainToContainer) {
dragObj.constrainTo(this.constrainingContainer || this.domNode.parentNode);
}
return dragObj;
},
setDragHandle: function(node){
node = dojo.byId(node);
dojo.dnd.dragManager.unregisterDragSource(this);
this.domNode = node;
dojo.dnd.dragManager.registerDragSource(this);
},
setDragTarget: function(node){
this.dragObject = node;
},
constrainTo: function(container) {
this.constrainToContainer = true;
if (container) {
this.constrainingContainer = container;
}
},
/*
*
* see dojo.dnd.DragSource.onSelected
*/
onSelected: function() {
for (var i=0; i<this.dragObjects.length; i++) {
dojo.dnd.dragManager.selectedSources.push(new dojo.dnd.HtmlDragSource(this.dragObjects[i]));
}
},
/**
* Register elements that should be dragged along with
* the actual DragSource.
*
* Example usage:
* var dragSource = new dojo.dnd.HtmlDragSource(...);
* // add a single element
* dragSource.addDragObjects(dojo.byId('id1'));
* // add multiple elements to drag along
* dragSource.addDragObjects(dojo.byId('id2'), dojo.byId('id3'));
*
* el A dom node to add to the drag list.
*/
addDragObjects: function(/*DOMNode*/ el) {
for (var i=0; i<arguments.length; i++) {
this.dragObjects.push(arguments[i]);
}
}
});
dojo.dnd.HtmlDragObject = function(node, type){
this.domNode = dojo.byId(node);
this.type = type;
this.constrainToContainer = false;
this.dragSource = null;
}
dojo.inherits(dojo.dnd.HtmlDragObject, dojo.dnd.DragObject);
dojo.lang.extend(dojo.dnd.HtmlDragObject, {
dragClass: "",
opacity: 0.5,
createIframe: true, // workaround IE6 bug
// if true, node will not move in X and/or Y direction
disableX: false,
disableY: false,
createDragNode: function() {
var node = this.domNode.cloneNode(true);
if(this.dragClass) { dojo.html.addClass(node, this.dragClass); }
if(this.opacity < 1) { dojo.style.setOpacity(node, this.opacity); }
if(node.tagName.toLowerCase() == "tr"){
// dojo.debug("Dragging table row")
// Create a table for the cloned row
var doc = this.domNode.ownerDocument;
var table = doc.createElement("table");
var tbody = doc.createElement("tbody");
tbody.appendChild(node);
table.appendChild(tbody);
// Set a fixed width to the cloned TDs
var domTds = this.domNode.childNodes;
var cloneTds = node.childNodes;
for(var i = 0; i < domTds.length; i++){
if((cloneTds[i])&&(cloneTds[i].style)){
cloneTds[i].style.width = dojo.style.getContentWidth(domTds[i]) + "px";
}
}
node = table;
}
if((dojo.render.html.ie55||dojo.render.html.ie60) && this.createIframe){
with(node.style) {
top="0px";
left="0px";
}
var outer = document.createElement("div");
outer.appendChild(node);
this.bgIframe = new dojo.html.BackgroundIframe(outer);
outer.appendChild(this.bgIframe.iframe);
node = outer;
}
node.style.zIndex = 999;
return node;
},
onDragStart: function(e){
dojo.html.clearSelection();
this.scrollOffset = dojo.html.getScrollOffset();
this.dragStartPosition = dojo.style.getAbsolutePosition(this.domNode, true);
this.dragOffset = {y: this.dragStartPosition.y - e.pageY,
x: this.dragStartPosition.x - e.pageX};
this.dragClone = this.createDragNode();
this.containingBlockPosition = this.domNode.offsetParent ?
dojo.style.getAbsolutePosition(this.domNode.offsetParent) : {x:0, y:0};
if (this.constrainToContainer) {
this.constraints = this.getConstraints();
}
// set up for dragging
with(this.dragClone.style){
position = "absolute";
top = this.dragOffset.y + e.pageY + "px";
left = this.dragOffset.x + e.pageX + "px";
}
document.body.appendChild(this.dragClone);
dojo.event.topic.publish('dragStart', { source: this } );
},
/** Return min/max x/y (relative to document.body) for this object) **/
getConstraints: function() {
if (this.constrainingContainer.nodeName.toLowerCase() == 'body') {
var width = dojo.html.getViewportWidth();
var height = dojo.html.getViewportHeight();
var x = 0;
var y = 0;
} else {
width = dojo.style.getContentWidth(this.constrainingContainer);
height = dojo.style.getContentHeight(this.constrainingContainer);
x =
this.containingBlockPosition.x +
dojo.style.getPixelValue(this.constrainingContainer, "padding-left", true) +
dojo.style.getBorderExtent(this.constrainingContainer, "left");
y =
this.containingBlockPosition.y +
dojo.style.getPixelValue(this.constrainingContainer, "padding-top", true) +
dojo.style.getBorderExtent(this.constrainingContainer, "top");
}
return {
minX: x,
minY: y,
maxX: x + width - dojo.style.getOuterWidth(this.domNode),
maxY: y + height - dojo.style.getOuterHeight(this.domNode)
}
},
updateDragOffset: function() {
var scroll = dojo.html.getScrollOffset();
if(scroll.y != this.scrollOffset.y) {
var diff = scroll.y - this.scrollOffset.y;
this.dragOffset.y += diff;
this.scrollOffset.y = scroll.y;
}
if(scroll.x != this.scrollOffset.x) {
var diff = scroll.x - this.scrollOffset.x;
this.dragOffset.x += diff;
this.scrollOffset.x = scroll.x;
}
},
/** Moves the node to follow the mouse */
onDragMove: function(e){
this.updateDragOffset();
var x = this.dragOffset.x + e.pageX;
var y = this.dragOffset.y + e.pageY;
if (this.constrainToContainer) {
if (x < this.constraints.minX) { x = this.constraints.minX; }
if (y < this.constraints.minY) { y = this.constraints.minY; }
if (x > this.constraints.maxX) { x = this.constraints.maxX; }
if (y > this.constraints.maxY) { y = this.constraints.maxY; }
}
this.setAbsolutePosition(x, y);
dojo.event.topic.publish('dragMove', { source: this } );
},
/**
* Set the position of the drag clone. (x,y) is relative to <body>.
*/
setAbsolutePosition: function(x, y){
// The drag clone is attached to document.body so this is trivial
if(!this.disableY) { this.dragClone.style.top = y + "px"; }
if(!this.disableX) { this.dragClone.style.left = x + "px"; }
},
/**
* If the drag operation returned a success we reomve the clone of
* ourself from the original position. If the drag operation returned
* failure we slide back over to where we came from and end the operation
* with a little grace.
*/
onDragEnd: function(e){
switch(e.dragStatus){
case "dropSuccess":
dojo.dom.removeNode(this.dragClone);
this.dragClone = null;
break;
case "dropFailure": // slide back to the start
var startCoords = dojo.style.getAbsolutePosition(this.dragClone, true);
// offset the end so the effect can be seen
var endCoords = [this.dragStartPosition.x + 1,
this.dragStartPosition.y + 1];
// animate
var line = new dojo.lfx.Line(startCoords, endCoords);
var anim = new dojo.lfx.Animation(500, line, dojo.lfx.easeOut);
var dragObject = this;
dojo.event.connect(anim, "onAnimate", function(e) {
dragObject.dragClone.style.left = e[0] + "px";
dragObject.dragClone.style.top = e[1] + "px";
});
dojo.event.connect(anim, "onEnd", function (e) {
// pause for a second (not literally) and disappear
dojo.lang.setTimeout(function() {
dojo.dom.removeNode(dragObject.dragClone);
// Allow drag clone to be gc'ed
dragObject.dragClone = null;
},
200);
});
anim.play();
break;
}
// shortly the browser will fire an onClick() event,
// but since this was really a drag, just squelch it
dojo.event.connect(this.domNode, "onclick", this, "squelchOnClick");
dojo.event.topic.publish('dragEnd', { source: this } );
},
squelchOnClick: function(e){
// squelch this onClick() event because it's the result of a drag (it's not a real click)
e.preventDefault();
// but if a real click comes along, allow it
dojo.event.disconnect(this.domNode, "onclick", this, "squelchOnClick");
},
constrainTo: function(container) {
this.constrainToContainer=true;
if (container) {
this.constrainingContainer = container;
} else {
this.constrainingContainer = this.domNode.parentNode;
}
}
});
dojo.dnd.HtmlDropTarget = function(node, types){
if (arguments.length == 0) { return; }
this.domNode = dojo.byId(node);
dojo.dnd.DropTarget.call(this);
if(types && dojo.lang.isString(types)) {
types = [types];
}
this.acceptedTypes = types || [];
}
dojo.inherits(dojo.dnd.HtmlDropTarget, dojo.dnd.DropTarget);
dojo.lang.extend(dojo.dnd.HtmlDropTarget, {
onDragOver: function(e){
if(!this.accepts(e.dragObjects)){ return false; }
// cache the positions of the child nodes
this.childBoxes = [];
for (var i = 0, child; i < this.domNode.childNodes.length; i++) {
child = this.domNode.childNodes[i];
if (child.nodeType != dojo.dom.ELEMENT_NODE) { continue; }
var pos = dojo.style.getAbsolutePosition(child, true);
var height = dojo.style.getInnerHeight(child);
var width = dojo.style.getInnerWidth(child);
this.childBoxes.push({top: pos.y, bottom: pos.y+height,
left: pos.x, right: pos.x+width, node: child});
}
// TODO: use dummy node
return true;
},
_getNodeUnderMouse: function(e){
// find the child
for (var i = 0, child; i < this.childBoxes.length; i++) {
with (this.childBoxes[i]) {
if (e.pageX >= left && e.pageX <= right &&
e.pageY >= top && e.pageY <= bottom) { return i; }
}
}
return -1;
},
createDropIndicator: function() {
this.dropIndicator = document.createElement("div");
with (this.dropIndicator.style) {
position = "absolute";
zIndex = 999;
borderTopWidth = "1px";
borderTopColor = "black";
borderTopStyle = "solid";
width = dojo.style.getInnerWidth(this.domNode) + "px";
left = dojo.style.getAbsoluteX(this.domNode, true) + "px";
}
},
onDragMove: function(e, dragObjects){
var i = this._getNodeUnderMouse(e);
if(!this.dropIndicator){
this.createDropIndicator();
}
if(i < 0) {
if(this.childBoxes.length) {
var before = (dojo.html.gravity(this.childBoxes[0].node, e) & dojo.html.gravity.NORTH);
} else {
var before = true;
}
} else {
var child = this.childBoxes[i];
var before = (dojo.html.gravity(child.node, e) & dojo.html.gravity.NORTH);
}
this.placeIndicator(e, dragObjects, i, before);
if(!dojo.html.hasParent(this.dropIndicator)) {
document.body.appendChild(this.dropIndicator);
}
},
/**
* Position the horizontal line that indicates "insert between these two items"
*/
placeIndicator: function(e, dragObjects, boxIndex, before) {
with(this.dropIndicator.style){
if (boxIndex < 0) {
if (this.childBoxes.length) {
top = (before ? this.childBoxes[0].top
: this.childBoxes[this.childBoxes.length - 1].bottom) + "px";
} else {
top = dojo.style.getAbsoluteY(this.domNode, true) + "px";
}
} else {
var child = this.childBoxes[boxIndex];
top = (before ? child.top : child.bottom) + "px";
}
}
},
onDragOut: function(e) {
if(this.dropIndicator) {
dojo.dom.removeNode(this.dropIndicator);
delete this.dropIndicator;
}
},
/**
* Inserts the DragObject as a child of this node relative to the
* position of the mouse.
*
* @return true if the DragObject was inserted, false otherwise
*/
onDrop: function(e){
this.onDragOut(e);
var i = this._getNodeUnderMouse(e);
if (i < 0) {
if (this.childBoxes.length) {
if (dojo.html.gravity(this.childBoxes[0].node, e) & dojo.html.gravity.NORTH) {
return this.insert(e, this.childBoxes[0].node, "before");
} else {
return this.insert(e, this.childBoxes[this.childBoxes.length-1].node, "after");
}
}
return this.insert(e, this.domNode, "append");
}
var child = this.childBoxes[i];
if (dojo.html.gravity(child.node, e) & dojo.html.gravity.NORTH) {
return this.insert(e, child.node, "before");
} else {
return this.insert(e, child.node, "after");
}
},
insert: function(e, refNode, position) {
var node = e.dragObject.domNode;
if(position == "before") {
return dojo.html.insertBefore(node, refNode);
} else if(position == "after") {
return dojo.html.insertAfter(node, refNode);
} else if(position == "append") {
refNode.appendChild(node);
return true;
}
return false;
}
});

View file

@ -0,0 +1,475 @@
/*
Copyright (c) 2004-2006, The Dojo Foundation
All Rights Reserved.
Licensed under the Academic Free License version 2.1 or above OR the
modified BSD license. For more information on Dojo licensing, see:
http://dojotoolkit.org/community/licensing.shtml
*/
dojo.provide("dojo.dnd.HtmlDragManager");
dojo.require("dojo.dnd.DragAndDrop");
dojo.require("dojo.event.*");
dojo.require("dojo.lang.array");
dojo.require("dojo.html");
dojo.require("dojo.style");
// NOTE: there will only ever be a single instance of HTMLDragManager, so it's
// safe to use prototype properties for book-keeping.
dojo.dnd.HtmlDragManager = function(){
}
dojo.inherits(dojo.dnd.HtmlDragManager, dojo.dnd.DragManager);
dojo.lang.extend(dojo.dnd.HtmlDragManager, {
/**
* There are several sets of actions that the DnD code cares about in the
* HTML context:
* 1.) mouse-down ->
* (draggable selection)
* (dragObject generation)
* mouse-move ->
* (draggable movement)
* (droppable detection)
* (inform droppable)
* (inform dragObject)
* mouse-up
* (inform/destroy dragObject)
* (inform draggable)
* (inform droppable)
* 2.) mouse-down -> mouse-down
* (click-hold context menu)
* 3.) mouse-click ->
* (draggable selection)
* shift-mouse-click ->
* (augment draggable selection)
* mouse-down ->
* (dragObject generation)
* mouse-move ->
* (draggable movement)
* (droppable detection)
* (inform droppable)
* (inform dragObject)
* mouse-up
* (inform draggable)
* (inform droppable)
* 4.) mouse-up
* (clobber draggable selection)
*/
disabled: false, // to kill all dragging!
nestedTargets: false,
mouseDownTimer: null, // used for click-hold operations
dsCounter: 0,
dsPrefix: "dojoDragSource",
// dimension calculation cache for use durring drag
dropTargetDimensions: [],
currentDropTarget: null,
// currentDropTargetPoints: null,
previousDropTarget: null,
_dragTriggered: false,
selectedSources: [],
dragObjects: [],
// mouse position properties
currentX: null,
currentY: null,
lastX: null,
lastY: null,
mouseDownX: null,
mouseDownY: null,
threshold: 7,
dropAcceptable: false,
cancelEvent: function(e){ e.stopPropagation(); e.preventDefault();},
// method over-rides
registerDragSource: function(ds){
if(ds["domNode"]){
// FIXME: dragSource objects SHOULD have some sort of property that
// references their DOM node, we shouldn't just be passing nodes and
// expecting it to work.
var dp = this.dsPrefix;
var dpIdx = dp+"Idx_"+(this.dsCounter++);
ds.dragSourceId = dpIdx;
this.dragSources[dpIdx] = ds;
ds.domNode.setAttribute(dp, dpIdx);
// so we can drag links
if(dojo.render.html.ie){
dojo.event.connect(ds.domNode, "ondragstart", this.cancelEvent);
}
}
},
unregisterDragSource: function(ds){
if (ds["domNode"]){
var dp = this.dsPrefix;
var dpIdx = ds.dragSourceId;
delete ds.dragSourceId;
delete this.dragSources[dpIdx];
ds.domNode.setAttribute(dp, null);
}
if(dojo.render.html.ie){
dojo.event.disconnect(ds.domNode, "ondragstart", this.cancelEvent );
}
},
registerDropTarget: function(dt){
this.dropTargets.push(dt);
},
unregisterDropTarget: function(dt){
var index = dojo.lang.find(this.dropTargets, dt, true);
if (index>=0) {
this.dropTargets.splice(index, 1);
}
},
/**
* Get the DOM element that is meant to drag.
* Loop through the parent nodes of the event target until
* the element is found that was created as a DragSource and
* return it.
*
* @param event object The event for which to get the drag source.
*/
getDragSource: function(e){
var tn = e.target;
if(tn === document.body){ return; }
var ta = dojo.html.getAttribute(tn, this.dsPrefix);
while((!ta)&&(tn)){
tn = tn.parentNode;
if((!tn)||(tn === document.body)){ return; }
ta = dojo.html.getAttribute(tn, this.dsPrefix);
}
return this.dragSources[ta];
},
onKeyDown: function(e){
},
onMouseDown: function(e){
if(this.disabled) { return; }
// only begin on left click
if(dojo.render.html.ie) {
if(e.button != 1) { return; }
} else if(e.which != 1) {
return;
}
var target = e.target.nodeType == dojo.dom.TEXT_NODE ?
e.target.parentNode : e.target;
// do not start drag involvement if the user is interacting with
// a form element.
if(dojo.html.isTag(target, "button", "textarea", "input", "select", "option")) {
return;
}
// find a selection object, if one is a parent of the source node
var ds = this.getDragSource(e);
// this line is important. if we aren't selecting anything then
// we need to return now, so preventDefault() isn't called, and thus
// the event is propogated to other handling code
if(!ds){ return; }
if(!dojo.lang.inArray(this.selectedSources, ds)){
this.selectedSources.push(ds);
ds.onSelected();
}
this.mouseDownX = e.pageX;
this.mouseDownY = e.pageY;
// Must stop the mouse down from being propogated, or otherwise can't
// drag links in firefox.
// WARNING: preventing the default action on all mousedown events
// prevents user interaction with the contents.
e.preventDefault();
dojo.event.connect(document, "onmousemove", this, "onMouseMove");
},
onMouseUp: function(e, cancel){
// if we aren't dragging then ignore the mouse-up
// (in particular, don't call preventDefault(), because other
// code may need to process this event)
if(this.selectedSources.length==0){
return;
}
this.mouseDownX = null;
this.mouseDownY = null;
this._dragTriggered = false;
// e.preventDefault();
e.dragSource = this.dragSource;
if((!e.shiftKey)&&(!e.ctrlKey)){
if(this.currentDropTarget) {
this.currentDropTarget.onDropStart();
}
dojo.lang.forEach(this.dragObjects, function(tempDragObj){
var ret = null;
if(!tempDragObj){ return; }
if(this.currentDropTarget) {
e.dragObject = tempDragObj;
// NOTE: we can't get anything but the current drop target
// here since the drag shadow blocks mouse-over events.
// This is probelematic for dropping "in" something
var ce = this.currentDropTarget.domNode.childNodes;
if(ce.length > 0){
e.dropTarget = ce[0];
while(e.dropTarget == tempDragObj.domNode){
e.dropTarget = e.dropTarget.nextSibling;
}
}else{
e.dropTarget = this.currentDropTarget.domNode;
}
if(this.dropAcceptable){
ret = this.currentDropTarget.onDrop(e);
}else{
this.currentDropTarget.onDragOut(e);
}
}
e.dragStatus = this.dropAcceptable && ret ? "dropSuccess" : "dropFailure";
// decouple the calls for onDragEnd, so they don't block the execution here
// ie. if the onDragEnd would call an alert, the execution here is blocked until the
// user has confirmed the alert box and then the rest of the dnd code is executed
// while the mouse doesnt "hold" the dragged object anymore ... and so on
dojo.lang.delayThese([
function() {
// in FF1.5 this throws an exception, see
// http://dojotoolkit.org/pipermail/dojo-interest/2006-April/006751.html
try{
tempDragObj.dragSource.onDragEnd(e)
} catch(err) {
// since the problem seems passing e, we just copy all
// properties and try the copy ...
var ecopy = {};
for (var i in e) {
if (i=="type") { // the type property contains the exception, no idea why...
ecopy.type = "mouseup";
continue;
}
ecopy[i] = e[i];
}
tempDragObj.dragSource.onDragEnd(ecopy);
}
}
, function() {tempDragObj.onDragEnd(e)}]);
}, this);
this.selectedSources = [];
this.dragObjects = [];
this.dragSource = null;
if(this.currentDropTarget) {
this.currentDropTarget.onDropEnd();
}
}
dojo.event.disconnect(document, "onmousemove", this, "onMouseMove");
this.currentDropTarget = null;
},
onScroll: function(){
for(var i = 0; i < this.dragObjects.length; i++) {
if(this.dragObjects[i].updateDragOffset) {
this.dragObjects[i].updateDragOffset();
}
}
// TODO: do not recalculate, only adjust coordinates
this.cacheTargetLocations();
},
_dragStartDistance: function(x, y){
if((!this.mouseDownX)||(!this.mouseDownX)){
return;
}
var dx = Math.abs(x-this.mouseDownX);
var dx2 = dx*dx;
var dy = Math.abs(y-this.mouseDownY);
var dy2 = dy*dy;
return parseInt(Math.sqrt(dx2+dy2), 10);
},
cacheTargetLocations: function(){
this.dropTargetDimensions = [];
dojo.lang.forEach(this.dropTargets, function(tempTarget){
var tn = tempTarget.domNode;
if(!tn){ return; }
var ttx = dojo.style.getAbsoluteX(tn, true);
var tty = dojo.style.getAbsoluteY(tn, true);
this.dropTargetDimensions.push([
[ttx, tty], // upper-left
// lower-right
[ ttx+dojo.style.getInnerWidth(tn), tty+dojo.style.getInnerHeight(tn) ],
tempTarget
]);
//dojo.debug("Cached for "+tempTarget)
}, this);
//dojo.debug("Cache locations")
},
onMouseMove: function(e){
if((dojo.render.html.ie)&&(e.button != 1)){
// Oooops - mouse up occurred - e.g. when mouse was not over the
// window. I don't think we can detect this for FF - but at least
// we can be nice in IE.
this.currentDropTarget = null;
this.onMouseUp(e, true);
return;
}
// if we've got some sources, but no drag objects, we need to send
// onDragStart to all the right parties and get things lined up for
// drop target detection
if( (this.selectedSources.length)&&
(!this.dragObjects.length) ){
var dx;
var dy;
if(!this._dragTriggered){
this._dragTriggered = (this._dragStartDistance(e.pageX, e.pageY) > this.threshold);
if(!this._dragTriggered){ return; }
dx = e.pageX - this.mouseDownX;
dy = e.pageY - this.mouseDownY;
}
// the first element is always our dragSource, if there are multiple
// selectedSources (elements that move along) then the first one is the master
// and for it the events will be fired etc.
this.dragSource = this.selectedSources[0];
dojo.lang.forEach(this.selectedSources, function(tempSource){
if(!tempSource){ return; }
var tdo = tempSource.onDragStart(e);
if(tdo){
tdo.onDragStart(e);
// "bump" the drag object to account for the drag threshold
tdo.dragOffset.top += dy;
tdo.dragOffset.left += dx;
tdo.dragSource = tempSource;
this.dragObjects.push(tdo);
}
}, this);
/* clean previous drop target in dragStart */
this.previousDropTarget = null;
this.cacheTargetLocations();
}
// FIXME: we need to add dragSources and dragObjects to e
dojo.lang.forEach(this.dragObjects, function(dragObj){
if(dragObj){ dragObj.onDragMove(e); }
});
// if we have a current drop target, check to see if we're outside of
// it. If so, do all the actions that need doing.
if(this.currentDropTarget){
//dojo.debug(dojo.dom.hasParent(this.currentDropTarget.domNode))
var c = dojo.style.toCoordinateArray(this.currentDropTarget.domNode, true);
// var dtp = this.currentDropTargetPoints;
var dtp = [
[c[0],c[1]], [c[0]+c[2], c[1]+c[3]]
];
}
if((!this.nestedTargets)&&(dtp)&&(this.isInsideBox(e, dtp))){
if(this.dropAcceptable){
this.currentDropTarget.onDragMove(e, this.dragObjects);
}
}else{
// FIXME: need to fix the event object!
// see if we can find a better drop target
var bestBox = this.findBestTarget(e);
if(bestBox.target === null){
if(this.currentDropTarget){
this.currentDropTarget.onDragOut(e);
this.previousDropTarget = this.currentDropTarget;
this.currentDropTarget = null;
// this.currentDropTargetPoints = null;
}
this.dropAcceptable = false;
return;
}
if(this.currentDropTarget !== bestBox.target){
if(this.currentDropTarget){
this.previousDropTarget = this.currentDropTarget;
this.currentDropTarget.onDragOut(e);
}
this.currentDropTarget = bestBox.target;
// this.currentDropTargetPoints = bestBox.points;
e.dragObjects = this.dragObjects;
this.dropAcceptable = this.currentDropTarget.onDragOver(e);
}else{
if(this.dropAcceptable){
this.currentDropTarget.onDragMove(e, this.dragObjects);
}
}
}
},
findBestTarget: function(e) {
var _this = this;
var bestBox = new Object();
bestBox.target = null;
bestBox.points = null;
dojo.lang.every(this.dropTargetDimensions, function(tmpDA) {
if(!_this.isInsideBox(e, tmpDA))
return true;
bestBox.target = tmpDA[2];
bestBox.points = tmpDA;
// continue iterating only if _this.nestedTargets == true
return Boolean(_this.nestedTargets);
});
return bestBox;
},
isInsideBox: function(e, coords){
if( (e.pageX > coords[0][0])&&
(e.pageX < coords[1][0])&&
(e.pageY > coords[0][1])&&
(e.pageY < coords[1][1]) ){
return true;
}
return false;
},
onMouseOver: function(e){
},
onMouseOut: function(e){
}
});
dojo.dnd.dragManager = new dojo.dnd.HtmlDragManager();
// global namespace protection closure
(function(){
var d = document;
var dm = dojo.dnd.dragManager;
// set up event handlers on the document
dojo.event.connect(d, "onkeydown", dm, "onKeyDown");
dojo.event.connect(d, "onmouseover", dm, "onMouseOver");
dojo.event.connect(d, "onmouseout", dm, "onMouseOut");
dojo.event.connect(d, "onmousedown", dm, "onMouseDown");
dojo.event.connect(d, "onmouseup", dm, "onMouseUp");
// TODO: process scrolling of elements, not only window
dojo.event.connect(window, "onscroll", dm, "onScroll");
})();

View file

@ -0,0 +1,76 @@
/*
Copyright (c) 2004-2006, The Dojo Foundation
All Rights Reserved.
Licensed under the Academic Free License version 2.1 or above OR the
modified BSD license. For more information on Dojo licensing, see:
http://dojotoolkit.org/community/licensing.shtml
*/
dojo.provide("dojo.dnd.HtmlDragMove");
dojo.provide("dojo.dnd.HtmlDragMoveSource");
dojo.provide("dojo.dnd.HtmlDragMoveObject");
dojo.require("dojo.dnd.*");
dojo.dnd.HtmlDragMoveSource = function(node, type){
dojo.dnd.HtmlDragSource.call(this, node, type);
}
dojo.inherits(dojo.dnd.HtmlDragMoveSource, dojo.dnd.HtmlDragSource);
dojo.lang.extend(dojo.dnd.HtmlDragMoveSource, {
onDragStart: function(){
var dragObj = new dojo.dnd.HtmlDragMoveObject(this.dragObject, this.type);
if (this.constrainToContainer) {
dragObj.constrainTo(this.constrainingContainer);
}
return dragObj;
},
/*
* see dojo.dnd.HtmlDragSource.onSelected
*/
onSelected: function() {
for (var i=0; i<this.dragObjects.length; i++) {
dojo.dnd.dragManager.selectedSources.push(new dojo.dnd.HtmlDragMoveSource(this.dragObjects[i]));
}
}
});
dojo.dnd.HtmlDragMoveObject = function(node, type){
dojo.dnd.HtmlDragObject.call(this, node, type);
}
dojo.inherits(dojo.dnd.HtmlDragMoveObject, dojo.dnd.HtmlDragObject);
dojo.lang.extend(dojo.dnd.HtmlDragMoveObject, {
onDragEnd: function(e){
// shortly the browser will fire an onClick() event,
// but since this was really a drag, just squelch it
dojo.event.connect(this.domNode, "onclick", this, "squelchOnClick");
},
onDragStart: function(e){
dojo.html.clearSelection();
this.dragClone = this.domNode;
this.scrollOffset = dojo.html.getScrollOffset();
this.dragStartPosition = dojo.style.getAbsolutePosition(this.domNode, true);
this.dragOffset = {y: this.dragStartPosition.y - e.pageY,
x: this.dragStartPosition.x - e.pageX};
this.containingBlockPosition = this.domNode.offsetParent ?
dojo.style.getAbsolutePosition(this.domNode.offsetParent, true) : {x:0, y:0};
this.dragClone.style.position = "absolute";
if (this.constrainToContainer) {
this.constraints = this.getConstraints();
}
},
/**
* Set the position of the drag node. (x,y) is relative to <body>.
*/
setAbsolutePosition: function(x, y){
// The drag clone is attached to it's constraining container so offset for that
if(!this.disableY) { this.domNode.style.top = (y-this.containingBlockPosition.y) + "px"; }
if(!this.disableX) { this.domNode.style.left = (x-this.containingBlockPosition.x) + "px"; }
}
});

View file

@ -0,0 +1,28 @@
/*
Copyright (c) 2004-2006, The Dojo Foundation
All Rights Reserved.
Licensed under the Academic Free License version 2.1 or above OR the
modified BSD license. For more information on Dojo licensing, see:
http://dojotoolkit.org/community/licensing.shtml
*/
dojo.provide("dojo.dnd.Sortable");
dojo.require("dojo.dnd.*");
dojo.dnd.Sortable = function () {}
dojo.lang.extend(dojo.dnd.Sortable, {
ondragstart: function (e) {
var dragObject = e.target;
while (dragObject.parentNode && dragObject.parentNode != this) {
dragObject = dragObject.parentNode;
}
// TODO: should apply HtmlDropTarget interface to self
// TODO: should apply HtmlDragObject interface?
return dragObject;
}
});

View file

@ -0,0 +1,473 @@
/*
Copyright (c) 2004-2006, The Dojo Foundation
All Rights Reserved.
Licensed under the Academic Free License version 2.1 or above OR the
modified BSD license. For more information on Dojo licensing, see:
http://dojotoolkit.org/community/licensing.shtml
*/
/**
* TreeDrag* specialized on managing subtree drags
* It selects nodes and visualises what's going on,
* but delegates real actions upon tree to the controller
*
* This code is considered a part of controller
*/
dojo.provide("dojo.dnd.TreeDragAndDrop");
dojo.provide("dojo.dnd.TreeDragSource");
dojo.provide("dojo.dnd.TreeDropTarget");
dojo.provide("dojo.dnd.TreeDNDController");
dojo.require("dojo.dnd.HtmlDragAndDrop");
dojo.require("dojo.lang.func");
dojo.require("dojo.lang.array");
dojo.require("dojo.lang.extras");
dojo.dnd.TreeDragSource = function(node, syncController, type, treeNode){
this.controller = syncController;
this.treeNode = treeNode;
dojo.dnd.HtmlDragSource.call(this, node, type);
}
dojo.inherits(dojo.dnd.TreeDragSource, dojo.dnd.HtmlDragSource);
dojo.lang.extend(dojo.dnd.TreeDragSource, {
onDragStart: function(){
/* extend adds functions to prototype */
var dragObject = dojo.dnd.HtmlDragSource.prototype.onDragStart.call(this);
//dojo.debugShallow(dragObject)
dragObject.treeNode = this.treeNode;
dragObject.onDragStart = dojo.lang.hitch(dragObject, function(e) {
/* save selection */
this.savedSelectedNode = this.treeNode.tree.selector.selectedNode;
if (this.savedSelectedNode) {
this.savedSelectedNode.unMarkSelected();
}
var result = dojo.dnd.HtmlDragObject.prototype.onDragStart.apply(this, arguments);
/* remove background grid from cloned object */
var cloneGrid = this.dragClone.getElementsByTagName('img');
for(var i=0; i<cloneGrid.length; i++) {
cloneGrid.item(i).style.backgroundImage='url()';
}
return result;
});
dragObject.onDragEnd = function(e) {
/* restore selection */
if (this.savedSelectedNode) {
this.savedSelectedNode.markSelected();
}
//dojo.debug(e.dragStatus);
return dojo.dnd.HtmlDragObject.prototype.onDragEnd.apply(this, arguments);
}
//dojo.debug(dragObject.domNode.outerHTML)
return dragObject;
},
onDragEnd: function(e){
var res = dojo.dnd.HtmlDragSource.prototype.onDragEnd.call(this, e);
return res;
}
});
// .......................................
dojo.dnd.TreeDropTarget = function(domNode, controller, type, treeNode, DNDMode){
this.treeNode = treeNode;
this.controller = controller; // I will sync-ly process drops
this.DNDMode = DNDMode;
dojo.dnd.HtmlDropTarget.apply(this, [domNode, type]);
}
dojo.inherits(dojo.dnd.TreeDropTarget, dojo.dnd.HtmlDropTarget);
dojo.lang.extend(dojo.dnd.TreeDropTarget, {
autoExpandDelay: 1500,
autoExpandTimer: null,
position: null,
indicatorStyle: "2px black solid",
showIndicator: function(position) {
// do not change style too often, cause of blinking possible
if (this.position == position) {
return;
}
//dojo.debug(position)
this.hideIndicator();
this.position = position;
if (position == "before") {
this.treeNode.labelNode.style.borderTop = this.indicatorStyle;
} else if (position == "after") {
this.treeNode.labelNode.style.borderBottom = this.indicatorStyle;
} else if (position == "onto") {
this.treeNode.markSelected();
}
},
hideIndicator: function() {
this.treeNode.labelNode.style.borderBottom="";
this.treeNode.labelNode.style.borderTop="";
this.treeNode.unMarkSelected();
this.position = null;
},
// is the target possibly ok ?
// This function is run on dragOver, but drop possibility is also determined by position over node
// that's why acceptsWithPosition is called
// doesnt take index into account ( can change while moving mouse w/o changing target )
/**
* Coarse (tree-level) access check.
* We can't determine real accepts status w/o position
*/
onDragOver: function(e){
//dojo.debug("onDragOver for "+e);
var accepts = dojo.dnd.HtmlDropTarget.prototype.onDragOver.apply(this, arguments);
//dojo.debug("TreeDropTarget.onDragOver accepts:"+accepts)
if (accepts && this.treeNode.isFolder && !this.treeNode.isExpanded) {
this.setAutoExpandTimer();
}
return accepts;
},
/* Parent.onDragOver calls this function to get accepts status */
accepts: function(dragObjects) {
var accepts = dojo.dnd.HtmlDropTarget.prototype.accepts.apply(this, arguments);
if (!accepts) return false;
var sourceTreeNode = dragObjects[0].treeNode;
if (dojo.lang.isUndefined(sourceTreeNode) || !sourceTreeNode || !sourceTreeNode.isTreeNode) {
dojo.raise("Source is not TreeNode or not found");
}
if (sourceTreeNode === this.treeNode) return false;
return true;
},
setAutoExpandTimer: function() {
// set up autoexpand timer
var _this = this;
var autoExpand = function () {
if (dojo.dnd.dragManager.currentDropTarget === _this) {
_this.controller.expand(_this.treeNode);
}
}
this.autoExpandTimer = dojo.lang.setTimeout(autoExpand, _this.autoExpandDelay);
},
getAcceptPosition: function(e, sourceTreeNode) {
var DNDMode = this.DNDMode;
if (DNDMode & dojo.widget.Tree.prototype.DNDModes.ONTO &&
// check if ONTO is allowed localy
!(
!this.treeNode.actionIsDisabled(dojo.widget.TreeNode.prototype.actions.ADDCHILD) // check dynamically cause may change w/o regeneration of dropTarget
&& sourceTreeNode.parent !== this.treeNode
&& this.controller.canMove(sourceTreeNode, this.treeNode)
)
) {
// disable ONTO if can't move
DNDMode &= ~dojo.widget.Tree.prototype.DNDModes.ONTO;
}
var position = this.getPosition(e, DNDMode);
//dojo.debug(DNDMode & +" : "+position);
// if onto is here => it was allowed before, no accept check is needed
if (position=="onto" ||
(!this.isAdjacentNode(sourceTreeNode, position)
&& this.controller.canMove(sourceTreeNode, this.treeNode.parent)
)
) {
return position;
} else {
return false;
}
},
onDragOut: function(e) {
this.clearAutoExpandTimer();
this.hideIndicator();
},
clearAutoExpandTimer: function() {
if (this.autoExpandTimer) {
clearTimeout(this.autoExpandTimer);
this.autoExpandTimer = null;
}
},
onDragMove: function(e, dragObjects){
var sourceTreeNode = dragObjects[0].treeNode;
var position = this.getAcceptPosition(e, sourceTreeNode);
if (position) {
this.showIndicator(position);
}
},
isAdjacentNode: function(sourceNode, position) {
if (sourceNode === this.treeNode) return true;
if (sourceNode.getNextSibling() === this.treeNode && position=="before") return true;
if (sourceNode.getPreviousSibling() === this.treeNode && position=="after") return true;
return false;
},
/* get DNDMode and see which position e fits */
getPosition: function(e, DNDMode) {
node = dojo.byId(this.treeNode.labelNode);
var mousey = e.pageY || e.clientY + document.body.scrollTop;
var nodey = dojo.html.getAbsoluteY(node);
var height = dojo.html.getInnerHeight(node);
var relY = mousey - nodey;
var p = relY / height;
var position = ""; // "" <=> forbidden
if (DNDMode & dojo.widget.Tree.prototype.DNDModes.ONTO
&& DNDMode & dojo.widget.Tree.prototype.DNDModes.BETWEEN) {
if (p<=0.3) {
position = "before";
} else if (p<=0.7) {
position = "onto";
} else {
position = "after";
}
} else if (DNDMode & dojo.widget.Tree.prototype.DNDModes.BETWEEN) {
if (p<=0.5) {
position = "before";
} else {
position = "after";
}
}
else if (DNDMode & dojo.widget.Tree.prototype.DNDModes.ONTO) {
position = "onto";
}
return position;
},
getTargetParentIndex: function(sourceTreeNode, position) {
var index = position == "before" ? this.treeNode.getParentIndex() : this.treeNode.getParentIndex()+1;
if (this.treeNode.parent === sourceTreeNode.parent
&& this.treeNode.getParentIndex() > sourceTreeNode.getParentIndex()) {
index--; // dragging a node is different for simple move bacause of before-after issues
}
return index;
},
onDrop: function(e){
// onDragOut will clean position
var position = this.position;
//dojo.debug(position);
this.onDragOut(e);
var sourceTreeNode = e.dragObject.treeNode;
if (!dojo.lang.isObject(sourceTreeNode)) {
dojo.raise("TreeNode not found in dragObject")
}
if (position == "onto") {
return this.controller.move(sourceTreeNode, this.treeNode, 0);
} else {
var index = this.getTargetParentIndex(sourceTreeNode, position);
return this.controller.move(sourceTreeNode, this.treeNode.parent, index);
}
//dojo.debug('drop2');
}
});
dojo.dnd.TreeDNDController = function(treeController) {
// I use this controller to perform actions
this.treeController = treeController;
this.dragSources = {};
this.dropTargets = {};
}
dojo.lang.extend(dojo.dnd.TreeDNDController, {
listenTree: function(tree) {
//dojo.debug("Listen tree "+tree);
dojo.event.topic.subscribe(tree.eventNames.createDOMNode, this, "onCreateDOMNode");
dojo.event.topic.subscribe(tree.eventNames.moveFrom, this, "onMoveFrom");
dojo.event.topic.subscribe(tree.eventNames.moveTo, this, "onMoveTo");
dojo.event.topic.subscribe(tree.eventNames.addChild, this, "onAddChild");
dojo.event.topic.subscribe(tree.eventNames.removeNode, this, "onRemoveNode");
dojo.event.topic.subscribe(tree.eventNames.treeDestroy, this, "onTreeDestroy");
},
unlistenTree: function(tree) {
//dojo.debug("Listen tree "+tree);
dojo.event.topic.unsubscribe(tree.eventNames.createDOMNode, this, "onCreateDOMNode");
dojo.event.topic.unsubscribe(tree.eventNames.moveFrom, this, "onMoveFrom");
dojo.event.topic.unsubscribe(tree.eventNames.moveTo, this, "onMoveTo");
dojo.event.topic.unsubscribe(tree.eventNames.addChild, this, "onAddChild");
dojo.event.topic.unsubscribe(tree.eventNames.removeNode, this, "onRemoveNode");
dojo.event.topic.unsubscribe(tree.eventNames.treeDestroy, this, "onTreeDestroy");
},
onTreeDestroy: function(message) {
this.unlistenTree(message.source);
// I'm not widget so don't use destroy() call and dieWithTree
},
onCreateDOMNode: function(message) {
this.registerDNDNode(message.source);
},
onAddChild: function(message) {
this.registerDNDNode(message.child);
},
onMoveFrom: function(message) {
var _this = this;
dojo.lang.forEach(
message.child.getDescendants(),
function(node) { _this.unregisterDNDNode(node); }
);
},
onMoveTo: function(message) {
var _this = this;
dojo.lang.forEach(
message.child.getDescendants(),
function(node) { _this.registerDNDNode(node); }
);
},
/**
* Controller(node model) creates DNDNodes because it passes itself to node for synchroneous drops processing
* I can't process DnD with events cause an event can't return result success/false
*/
registerDNDNode: function(node) {
if (!node.tree.DNDMode) return;
//dojo.debug("registerDNDNode "+node);
/* I drag label, not domNode, because large domNodes are very slow to copy and large to drag */
var source = null;
var target = null;
if (!node.actionIsDisabled(node.actions.MOVE)) {
//dojo.debug("reg source")
var source = new dojo.dnd.TreeDragSource(node.labelNode, this, node.tree.widgetId, node);
this.dragSources[node.widgetId] = source;
}
var target = new dojo.dnd.TreeDropTarget(node.labelNode, this.treeController, node.tree.DNDAcceptTypes, node, node.tree.DNDMode);
this.dropTargets[node.widgetId] = target;
},
unregisterDNDNode: function(node) {
if (this.dragSources[node.widgetId]) {
dojo.dnd.dragManager.unregisterDragSource(this.dragSources[node.widgetId]);
delete this.dragSources[node.widgetId];
}
if (this.dropTargets[node.widgetId]) {
dojo.dnd.dragManager.unregisterDropTarget(this.dropTargets[node.widgetId]);
delete this.dropTargets[node.widgetId];
}
}
});

View file

@ -0,0 +1,16 @@
/*
Copyright (c) 2004-2006, The Dojo Foundation
All Rights Reserved.
Licensed under the Academic Free License version 2.1 or above OR the
modified BSD license. For more information on Dojo licensing, see:
http://dojotoolkit.org/community/licensing.shtml
*/
dojo.kwCompoundRequire({
common: ["dojo.dnd.DragAndDrop"],
browser: ["dojo.dnd.HtmlDragAndDrop"],
dashboard: ["dojo.dnd.HtmlDragAndDrop"]
});
dojo.provide("dojo.dnd.*");