📄 dhtmlxgrid_drag.js
字号:
//v.1.6 build 80512/*
Copyright DHTMLX LTD. http://www.dhtmlx.com
You allowed to use this component or parts of it under GPL terms
To use it on other terms or get Professional edition of the component please contact us at sales@dhtmlx.com
*//**
* @desc: enable/disable drag-and-drop
* @type: public
* @param: mode - enabled/disabled [ can be true/false/temporary_disabled - last value mean that tree can be D-n-D can be switched to true later ]
* @topic: 0
*/
dhtmlXGridObject.prototype.enableDragAndDrop=function(mode){
if (mode=="temporary_disabled"){
this.dADTempOff=false;
mode=true; }
else
this.dADTempOff=true;
this.dragAndDropOff=convertStringToBoolean(mode);
};
/**
* @desc: set Drag-And-Drop behavior (child - drop as chils, sibling - drop as sibling
* @type: public
* @param: mode - behavior name (child,sibling,complex)
* @topic: 0
*/
dhtmlXGridObject.prototype.setDragBehavior=function(mode){
this.dadmodec=this.dadmodefix=0;
switch (mode) {
case "child": this.dadmode=0; this._sbmod=false; break;
case "sibling": this.dadmode=1; this._sbmod=false; break;
case "sibling-next": this.dadmode=1; this._sbmod=true; break;
case "complex": this.dadmode=2; this._sbmod=false; break;
case "complex-next": this.dadmode=2; this._sbmod=true; break;
} };
/**
* @desc: switch to mode when draged item, droped in target location in same order as they was in source grid
* @type: public
* @param: mode - true/false to enable/disable mode
* @topic: 0
*/
dhtmlXGridObject.prototype.enableDragOrder=function(mode){
this._dndorder=convertStringToBoolean(mode);
};
dhtmlXGridObject.prototype._checkParent=function(row,ids){
var z=this._h2.get[row.idd].parent;
if (!z.parent) return;
for (var i=0; i<ids.length; i++)
if (ids[i]==z.id) return true;
return this._checkParent(this.rowsAr[z.id],ids);
}
/**
* @desc: create html element for dragging
* @type: private
* @param: htmlObject - html node object
* @topic: 1
*/
dhtmlXGridObject.prototype._createDragNode=function(htmlObject,e){
this.editStop();
if (window.dhtmlDragAndDrop.dragNode) return null;
if (!this.dADTempOff) return null;
htmlObject.parentObject=new Object();
htmlObject.parentObject.treeNod=this;
if (!this.callEvent("onBeforeDrag",[htmlObject.parentNode.idd])) return null;
var z=new Array();
z[this.selMultiRows?z.length:0]=htmlObject.parentNode.idd;
//remove child in case of treeGrid
if (this.isTreeGrid()){
for (var i=z.length-1; i>=0; i--)
if (this._checkParent(this.rowsAr[z[i]],z)) z.splice(i,1);
}
var self=this;
if (z.length && this._dndorder)
z.sort(function(a,b){ return (self.rowsAr[a].rowIndex>self.rowsAr[b].rowIndex?1:-1); });
var el = this.getFirstParentOfType(_isIE?e.srcElement:e.target,"TD");
if (el) this._dndExtra=el._cellIndex;
this._dragged=new Array();
for (var i=0; i<z.length; i++)
if (this.rowsAr[z[i]]){
this._dragged[this._dragged.length]=this.rowsAr[z[i]];
this.rowsAr[z[i]].treeNod=this;
}
htmlObject.parentObject.parentNode=htmlObject.parentNode;
var dragSpan=document.createElement('div');
dragSpan.innerHTML=this.rowToDragElement(htmlObject.parentNode.idd);
dragSpan.style.position="absolute";
dragSpan.className="dragSpanDiv";
return dragSpan;
}
/**
* @desc: create a drag visual marker
* @type: private
*/
dhtmlXGridObject.prototype._createSdrgc=function(){
this._sdrgc=document.createElement("DIV");
this._sdrgc.innerHTML=" ";
this._sdrgc.className="gridDragLine";
this.objBox.appendChild(this._sdrgc);
}
/**
* @desc: create a drag context object
* @type: private
*/
function dragContext(a,b,c,d,e,f,j,h,k,l){
this.source=a||"grid";
this.target=b||"grid";
this.mode=c||"move";
this.dropmode=d||"child";
this.sid=e||0;
this.tid=f||window.unknown;
this.sobj=j||null;
this.tobj=h||null;
this.sExtra=k||null;
this.tExtra=l||null;
return this;
}
/**
* @desc: check is operation possible
* @type: private
*/
dragContext.prototype.valid=function(){
if (this.sobj!=this.tobj) return true;
if (this.sid==this.tid) return false;
if (this.target=="treeGrid"){
var z=this.tid
while (z = this.tobj.getParentId(z) ){
if (this.sid==z) return false;
}
}
return true;
}
/**
* @desc: close context
* @type: private
*/
dragContext.prototype.close=function(){
this.sobj=null;
this.tobj=null;
}
/**
* @desc: return copy of context
* @type: private
*/
dragContext.prototype.copy=function(){
return new dragContext(this.source,this.target,this.mode,this.dropmode,this.sid,this.tid,this.sobj,this.tobj,this.sExtra,this.tExtra);
}
/**
* @desc: set a lue of context attribute
* @type: private
*/
dragContext.prototype.set=function(a,b){
this[a]=b;
return this;
}
/**
* @desc: generate an Id for new node
* @type: private
*/
dragContext.prototype.uid=function(a,b){
this.nid=this.sid;
while (this.tobj.rowsAr[this.nid])
this.nid=this.nid+((new Date()).valueOf());
return this;
}
/**
* @desc: get data array for grid row
* @type: private
*/
dragContext.prototype.data=function(){
if (this.sobj==this.tobj)
return this.sobj._getRowArray(this.sobj.rowsAr[this.sid]);
if (this.source=="tree")
return this.tobj.treeToGridElement(this.sobj,this.sid,this.tid);
else
return this.tobj.gridToGrid(this.sid,this.sobj,this.tobj);
}
dragContext.prototype.childs=function(){
if (this.source=="treeGrid")
return this.sobj._h2.get[this.sid]._xml_await?this.sobj._h2.get[this.sid].has_kids:null;
return null;
}
/**
* @desc: return parent id for row in context
* @type: private
*/
dragContext.prototype.pid=function(){
if (this.tid==window.unknown) return 0;
if (!this.tobj._h2) return 0;
if (this.target=="treeGrid")
if (this.dropmode=="child")
return this.tid;
else{
var z=this.tobj.rowsAr[this.tid];
var apid=this.tobj._h2.get[z.idd].parent.id;
if ((this.alfa)&&(this.tobj._sbmod)&&(z.nextSibling)){
var zpid=this.tobj._h2.get[z.nextSibling.idd].parent.id;
if (zpid==this.tid)
return this.tid;
if (zpid!=apid)
return zpid;
}
return apid;
}
}
/**
* @desc: get index of target position
* @type: private
*/
dragContext.prototype.ind=function(){
if (this.tid==window.unknown) return 0;
if (this.target=="treeGrid"){
if (this.dropmode=="child")
this.tobj.openItem(this.tid);
else
this.tobj.openItem(this.tobj.getParentId(this.tid));
}
var ind=this.tobj.rowsCol._dhx_find(this.tobj.rowsAr[this.tid]);
if ((this.alfa)&&(this.tobj._sbmod)&&(this.dropmode=="sibling")){
var z=this.tobj.rowsAr[this.tid];
if ((z.nextSibling)&&(this._h2.get[z.nextSibling.idd].parent.id==this.tid))
return ind+1;
}
return (ind+1+((this.target=="treeGrid" && ind>=0 && this.tobj._h2.get[this.tobj.rowsCol[ind].idd].state=="minus")?this.tobj._getOpenLenght(this.tobj.rowsCol[ind].idd,0):0));
}
/**
* @desc: get row related image
* @type: private
*/
dragContext.prototype.img=function(){
if ((this.target!="grid")&&(this.sobj._h2))
return this.sobj.getItemImage(this.sid);
else return null;
}
/**
* @desc: return list of rows in context
* @type: private
*/
dragContext.prototype.slist=function(){
var res=new Array();
for (var i=0; i<this.sid.length; i++)
res[res.length]=this.sid[i][(this.source=="tree")?"id":"idd"];
return res.join(",");
}
/**
* @desc: drag entry point
* @type: private
*/
dhtmlXGridObject.prototype._drag=function(sourceHtmlObject,dhtmlObject,targetHtmlObject,lastLanding){
var z=(this.lastLanding)
//close unfinished tasks
if (this._autoOpenTimer) window.clearTimeout(this._autoOpenTimer);
//detect details
var r1=targetHtmlObject.parentNode;
var r2=sourceHtmlObject.parentObject;
//drop on header
if (!r1.idd) { r1.grid=this; this.dadmodefix=0; }
var c=new dragContext(0,0,0,(r1.grid.dadmodec?"sibling":"child"));
if (r2 && r2.childNodes)
c.set("source","tree").set("sobj",r2.treeNod).set("sid",c.sobj._dragged);
else{
if (r2.treeNod.isTreeGrid()) c.set("source","treeGrid");
c.set("sobj",r2.treeNod).set("sid",c.sobj._dragged);
}
if (r1.grid.isTreeGrid())
c.set("target","treeGrid");
else
c.set("dropmode","sibling");
c.set("tobj",r1.grid).set("tid",r1.idd);
var el = this.getFirstParentOfType(lastLanding,"TD")
if (el) c.set("tExtra",el._cellIndex);
if (el) c.set("sExtra",c.sobj._dndExtra);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -