function DragDrop(elem, handles, settings){ this.elem = elem; this.elem.oThis = this; this.handles = handles; this.settings = (settings) ? settings : DragDrop.defaultSettings; if(settings){ for(var i in DragDrop.defaultSettings){ if(typeof this.settings[i] == 'undefined'){ this.settings[i] = DragDrop.defaultSettings[i]; } }//for } this.prevX = 0; this.prevY = 0; this.deltaX = 0; this.deltaY = 0; this.draggedX = this.settings.startPosX; this.draggedY = this.settings.startPosY; this.stopLeft = false; this.stopRight = false; this.stopUp = false; this.stopDown = false; this.isMoving = false; this.onmove = null; this.onstart = null; this.onstop = null; this.init(); } DragDrop.defaultSettings = { dragX: true, dragY: true, moveSpaceX: -1, moveSpaceY: -1, startPosX: 0, startPosY: 0 } DragDrop.prototype.init = function(){ this.initElem(); this.initHandles(); this.setFlags(); this.initMouseDown(); } DragDrop.prototype.initElem = function(){ if(typeof this.elem == 'string'){ this.elem = document.getElementById(this.elem); } } DragDrop.prototype.initHandles = function(){ if(typeof this.handles == 'string'){ var arrHandles = []; var handlesClass = this.handles; var children = getAllChildren(this.elem); var showFunc = function(obj){ if(obj.type == 1){ if(obj.elem.className.indexOf(handlesClass) != -1){ arrHandles.push(obj.elem); } } } showAllChildren(children, showFunc); this.handles = arrHandles; }else if(typeof this.handles == 'object' && typeof this.handles.length == 'undefined'){ var arrHandles = []; arrHandles.push(this.handles); this.handles = arrHandles; } } DragDrop.prototype.setFlags = function(){ if(this.settings.moveSpaceX >= 0){ if(this.settings.moveSpaceX == 0){ this.stopLeft = true; this.stopRight = true; } if(this.draggedX == 0){ this.stopLeft = true; }else if(this.draggedX == this.settings.moveSpaceX){ this.stopRight = true; } } if(this.settings.moveSpaceY >= 0){ if(this.settings.moveSpaceY == 0){ this.stopUp = true; this.stopDown = true; } if(this.draggedY == 0){ this.stopUp = true; }else if(this.draggedY == this.settings.moveSpaceY){ this.stopDown = true; } } } DragDrop.prototype.initMouseDown = function(){ this.isMoving = true; var oThis = this; this.elem.style.position = 'absolute'; for(var i = 0; i < this.handles.length; i++){ var handle = this.handles[i]; handle.onmousedown = this.start; } } DragDrop.prototype.checkMoveSpace = function(deltaX, deltaY){ var dX = deltaX; var dY = deltaY; var retArr = []; //check X //Left if(this.settings.moveSpaceX >= 0){ if(this.stopLeft){ if(deltaX <= 0){ dX = 0; }else{ this.stopLeft = false; } } //Right if(this.stopRight){ if(deltaX >= 0){ dX = 0; }else{ this.stopRight = false; } } if(this.draggedX + deltaX >= this.settings.moveSpaceX){ dX = this.settings.moveSpaceX - this.draggedX; this.stopRight = true; } if(this.draggedX + deltaX <= 0){ dX = -this.draggedX; this.stopLeft = true; } } //checkY if(this.settings.moveSpaceY >= 0){ //Up if(this.stopUp){ if(deltaY <= 0){ dY = 0; }else{ this.stopUp = false; } } if(this.stopDown){ if(deltaY >= 0){ dY = 0; }else{ this.stopDown = false; } } //setting Flags if(this.draggedY + deltaY >= this.settings.moveSpaceY){ dY = this.settings.moveSpaceY - this.draggedY; this.stopDown = true; } if(this.draggedY + deltaY <= 0){ dY = -this.draggedY; this.stopUp = true; } } //X retArr.push(dX); retArr.push(dY); return retArr; } DragDrop.prototype.move = function(currentX, currentY){ var deltaY = currentY - this.prevY; var deltaX = currentX - this.prevX; this.moveBy(deltaX, deltaY); this.prevX = currentX; this.prevY = currentY; } DragDrop.prototype.moveBy = function(deltaX, deltaY){ var newDelta = this.checkMoveSpace(deltaX, deltaY); deltaX = newDelta[0]; deltaY = newDelta[1]; var left = this.elem.offsetLeft + deltaX; var top = this.elem.offsetTop + deltaY; this.elem.style.left = left + 'px'; this.elem.style.top = top + 'px'; this.draggedX += deltaX; this.draggedY += deltaY; this.deltaX = deltaX; this.deltaY = deltaY; //event handlers if(this.onmove){ this.onmove.func.apply(this.onmove.oThis, this.onmove.args); } } DragDrop.prototype.start = function(e){ var oThis = GetThis(this, 'oThis'); var ev = window.event || e; oThis.prevX = ev.clientX; oThis.prevY = ev.clientY; document.onmousemove = function(e){ var ev = window.event || e; var currentX = ev.clientX; var currentY = ev.clientY; oThis.move.apply(oThis, [currentX, currentY]); } document.onmouseup = function(){ oThis.stop.call(oThis); } //event handlers if(oThis.onstart){ oThis.onstart.func.apply(oThis.onstart.oThis, oThis.onstart.args); } } DragDrop.prototype.stop = function(){ document.onmousemove = null; document.onmouseup = null; this.isMoving = false; if(this.onstop){ this.onstop.func.apply(this.onstop.oThis, this.onstop.args); } }