//>>built
define("dojox/grid/enhanced/plugins/DnD","dojo/_base/kernel,dojo/_base/declare,dojo/_base/connect,dojo/_base/array,dojo/_base/lang,dojo/_base/html,dojo/_base/json,dojo/_base/window,dojo/query,dojo/keys,dojo/dnd/Source,dojo/dnd/Avatar,../_Plugin,../../EnhancedGrid,dojo/dnd/Manager,./Selector,./Rearrange".split(","),function(u,t,x,h,o,f,y,k,v,w,z,A,B,C,D){var E=function(a){a.sort(function(a,b){return a-b});for(var c=[[a[0]]],b=1,d=0;b<a.length;++b)a[b]==a[b-1]+1?c[d].push(a[b]):c[++d]=[a[b]];return c},
q=function(a){for(var c=a[0],b=1;b<a.length;++b)c=c.concat(a[b]);return c},F=t("dojox.grid.enhanced.plugins.GridDnDElement",null,{constructor:function(a){this.plugin=a;this.node=f.create("div");this._items={}},destroy:function(){this.plugin=null;f.destroy(this.node);this._items=this.node=null},createDnDNodes:function(a){this.destroyDnDNodes();var c=["grid/"+a.type+"s"],b=this.plugin.grid.id+"_dndItem";h.forEach(a.selected,function(a,g){var e=b+g;this._items[e]={type:c,data:a,dndPlugin:this.plugin};
this.node.appendChild(f.create("div",{id:e}))},this)},getDnDNodes:function(){return h.map(this.node.childNodes,function(a){return a})},destroyDnDNodes:function(){f.empty(this.node);this._items={}},getItem:function(a){return this._items[a]}}),G=t("dojox.grid.enhanced.plugins.GridDnDSource",z,{accept:["grid/cells","grid/rows","grid/cols"],constructor:function(a,c){this.grid=c.grid;this.dndElem=c.dndElem;this.dndPlugin=c.dnd;this.sourcePlugin=null},destroy:function(){this.inherited(arguments);this.sourcePlugin=
this.dndPlugin=this.dndElem=this.grid=null},getItem:function(a){return this.dndElem.getItem(a)},checkAcceptance:function(a,c){if(this!=a&&c[0]){var b=a.getItem(c[0].id);if(b.dndPlugin)for(var d=b.type,g=0;g<d.length;++g){if(d[g]in this.accept){if(this.dndPlugin._canAccept(b.dndPlugin))this.sourcePlugin=b.dndPlugin;else return!1;break}}else if("grid/rows"in this.accept){var e=[];h.forEach(c,function(b){b=a.getItem(b.id);if(b.data&&0<=h.indexOf(b.type,"grid/rows")){var c=b.data;"string"==typeof b.data&&
(c=y.fromJson(b.data));c&&e.push(c)}});if(e.length)this.sourcePlugin={_dndRegion:{type:"row",selected:[e]}};else return!1}}return this.inherited(arguments)},onDraggingOver:function(){this.dndPlugin.onDraggingOver(this.sourcePlugin)},onDraggingOut:function(){this.dndPlugin.onDraggingOut(this.sourcePlugin)},onDndDrop:function(a,c,b,d){this.onDndCancel();if(this!=a&&this==d)this.dndPlugin.onDragIn(this.sourcePlugin,b)}}),H=t("dojox.grid.enhanced.plugins.GridDnDAvatar",A,{construct:function(){this._itemType=
this.manager._dndPlugin._dndRegion.type;this._itemCount=this._getItemCount();this.isA11y=f.hasClass(k.body(),"dijit_a11y");var a=f.create("table",{border:"0",cellspacing:"0","class":"dojoxGridDndAvatar",style:{position:"absolute",zIndex:"1999",margin:"0px"}}),c=this.manager.source,b=f.create("tbody",null,a),b=f.create("tr",null,b),d=f.create("td",{"class":"dojoxGridDnDIcon"},b);this.isA11y&&f.create("span",{id:"a11yIcon",innerHTML:this.manager.copy?"+":"<"},d);f.create("td",{"class":"dojoxGridDnDItemIcon "+
this._getGridDnDIconClass()},b);d=f.create("td",null,b);f.create("span",{"class":"dojoxGridDnDItemCount",innerHTML:c.generateText?this._generateText():""},d);f.style(b,{opacity:0.9});this.node=a},_getItemCount:function(){var a=this.manager._dndPlugin._dndRegion.selected,c=0;switch(this._itemType){case "cell":var a=a[0],c=this.manager._dndPlugin.grid.layout.cells,b=a.max.col-a.min.col+1,d=a.max.row-a.min.row+1;if(1<b)for(var g=a.min.col;g<=a.max.col;++g)c[g].hidden&&--b;c=b*d;break;case "row":case "col":c=
q(a).length}return c},_getGridDnDIconClass:function(){return{row:["dojoxGridDnDIconRowSingle","dojoxGridDnDIconRowMulti"],col:["dojoxGridDnDIconColSingle","dojoxGridDnDIconColMulti"],cell:["dojoxGridDnDIconCellSingle","dojoxGridDnDIconCellMulti"]}[this._itemType][1==this._itemCount?0:1]},_generateText:function(){return"("+this._itemCount+")"}}),u=t("dojox.grid.enhanced.plugins.DnD",B,{name:"dnd",_targetAnchorBorderWidth:2,_copyOnly:!1,_config:{row:{within:!0,"in":!0,out:!0},col:{within:!0,"in":!0,
out:!0},cell:{within:!0,"in":!0,out:!0}},constructor:function(a,c){this.grid=a;this._config=o.clone(this._config);c=o.isObject(c)?c:{};this.setupConfig(c.dndConfig);this._copyOnly=!!c.copyOnly;this._mixinGrid();this.selector=a.pluginMgr.getPlugin("selector");this.rearranger=a.pluginMgr.getPlugin("rearrange");this.rearranger.setArgs(c);this._clear();this._elem=new F(this);this._source=new G(this._elem.node,{grid:a,dndElem:this._elem,dnd:this});this._container=v(".dojoxGridMasterView",this.grid.domNode)[0];
this._initEvents()},destroy:function(){this.inherited(arguments);this._clear();this._source.destroy();this._elem.destroy();this._config=this.rearranger=this.selector=this.grid=this._container=null},_mixinGrid:function(){this.grid.setupDnDConfig=o.hitch(this,"setupConfig");this.grid.dndCopyOnly=o.hitch(this,"copyOnly")},setupConfig:function(a){if(a&&o.isObject(a)){var c=["row","col","cell"],b=["within","in","out"],d=this._config;h.forEach(c,function(c){if(c in a){var e=a[c];e&&o.isObject(e)?h.forEach(b,
function(a){a in e&&(d[c][a]=!!e[a])}):h.forEach(b,function(a){d[c][a]=!!e})}});h.forEach(b,function(b){if(b in a){var e=a[b];e&&o.isObject(e)?h.forEach(c,function(a){a in e&&(d[a][b]=!!e[a])}):h.forEach(c,function(a){d[a][b]=!!e})}})}},copyOnly:function(a){if("undefined"!=typeof a)this._copyOnly=!!a;return this._copyOnly},_isOutOfGrid:function(a){var c=f.position(this.grid.domNode),b=a.clientX,a=a.clientY;return a<c.y||a>c.y+c.h||b<c.x||b>c.x+c.w},_onMouseMove:function(a){if(this._dndRegion&&!this._dnding&&
!this._externalDnd)this._dnding=!0,this._startDnd(a);else{if(this._isMouseDown&&!this._dndRegion)delete this._isMouseDown,this._oldCursor=f.style(k.body(),"cursor"),f.style(k.body(),"cursor","not-allowed");var c=this._isOutOfGrid(a);if(!this._alreadyOut&&c)this._alreadyOut=!0,this._dnding&&this._destroyDnDUI(!0,!1),this._moveEvent=a,this._source.onOutEvent();else if(this._alreadyOut&&!c)this._alreadyOut=!1,this._dnding&&this._createDnDUI(a,!0),this._moveEvent=a,this._source.onOverEvent()}},_onMouseUp:function(){if(!this._extDnding&&
!this._isSource){var a=this._dnding&&!this._alreadyOut;a&&this._config[this._dndRegion.type].within&&this._rearrange();this._endDnd(a)}f.style(k.body(),"cursor",this._oldCursor||"");delete this._isMouseDown},_initEvents:function(){var a=this.grid,c=this.selector;this.connect(k.doc,"onmousemove","_onMouseMove");this.connect(k.doc,"onmouseup","_onMouseUp");this.connect(a,"onCellMouseOver",function(a){if(!this._dnding&&!c.isSelecting()&&!a.ctrlKey)this._dndReady=c.isSelected("cell",a.rowIndex,a.cell.index),
c.selectEnabled(!this._dndReady)});this.connect(a,"onHeaderCellMouseOver",function(){this._dndReady&&c.selectEnabled(!0)});this.connect(a,"onRowMouseOver",function(a){this._dndReady&&!a.cell&&c.selectEnabled(!0)});this.connect(a,"onCellMouseDown",function(a){if(!a.ctrlKey&&this._dndReady)this._dndRegion=this._getDnDRegion(a.rowIndex,a.cell.index),this._isMouseDown=!0});this.connect(a,"onCellMouseUp",function(a){if(!this._dndReady&&!c.isSelecting()&&a.cell)this._dndReady=c.isSelected("cell",a.rowIndex,
a.cell.index),c.selectEnabled(!this._dndReady)});this.connect(a,"onCellClick",function(a){this._dndReady&&!a.ctrlKey&&!a.shiftKey&&c.select("cell",a.rowIndex,a.cell.index)});this.connect(a,"onEndAutoScroll",function(a,c,g,e,f){this._dnding&&this._markTargetAnchor(f)});this.connect(k.doc,"onkeydown",function(a){if(a.keyCode==w.ESCAPE)this._endDnd(!1);else if(a.keyCode==w.CTRL)c.selectEnabled(!0),this._isCopy=!0});this.connect(k.doc,"onkeyup",function(a){if(a.keyCode==w.CTRL)c.selectEnabled(!this._dndReady),
this._isCopy=!1})},_clear:function(){this._moveEvent=this._target=this._dndRegion=null;this._targetAnchor={};this._extDnding=this._alreadyOut=this._isSource=this._externalDnd=this._dnding=!1},_getDnDRegion:function(a,c){var b=this.selector,d=b._selected,g=!!d.cell.length|!!d.row.length<<1|!!d.col.length<<2,e;switch(g){case 1:e="cell";if(!this._config[e].within&&!this._config[e].out)break;var f=this.grid.layout.cells,g=function(a){for(var b=0,c=a.min.col;c<=a.max.col;++c)f[c].hidden&&++b;return(a.max.row-
a.min.row+1)*(a.max.col-a.min.col+1-b)},i={max:{row:-1,col:-1},min:{row:Infinity,col:Infinity}};h.forEach(d[e],function(a){if(a.row<i.min.row)i.min.row=a.row;if(a.row>i.max.row)i.max.row=a.row;if(a.col<i.min.col)i.min.col=a.col;if(a.col>i.max.col)i.max.col=a.col});if(h.some(d[e],function(b){return b.row==a&&b.col==c})&&g(i)==d[e].length&&h.every(d[e],function(a){return a.row>=i.min.row&&a.row<=i.max.row&&a.col>=i.min.col&&a.col<=i.max.col}))return{type:e,selected:[i],handle:{row:a,col:c}};break;case 2:case 4:e=
2==g?"row":"col";if(!this._config[e].within&&!this._config[e].out)break;d=b.getSelected(e);if(d.length)return{type:e,selected:E(d),handle:2==g?a:c}}return null},_startDnd:function(a){this._createDnDUI(a)},_endDnd:function(a){this._destroyDnDUI(!1,a);this._clear()},_createDnDUI:function(a,c){var b=f.position(this.grid.views.views[0].domNode);f.style(this._container,"height",b.h+"px");try{c||this._createSource(a),this._createMoveable(a),this._oldCursor=f.style(k.body(),"cursor"),f.style(k.body(),"cursor",
"default")}catch(d){}},_destroyDnDUI:function(a,c){try{c&&this._destroySource(),this._unmarkTargetAnchor(),a||this._destroyMoveable(),f.style(k.body(),"cursor",this._oldCursor)}catch(b){}},_createSource:function(a){this._elem.createDnDNodes(this._dndRegion);var c=D.manager(),b=c.makeAvatar;c._dndPlugin=this;c.makeAvatar=function(){var a=new H(c);delete c._dndPlugin;return a};c.startDrag(this._source,this._elem.getDnDNodes(),a.ctrlKey);c.makeAvatar=b;c.onMouseMove(a)},_destroySource:function(){x.publish("/dnd/cancel")},
_createMoveable:function(){if(!this._markTagetAnchorHandler)this._markTagetAnchorHandler=this.connect(k.doc,"onmousemove","_markTargetAnchor")},_destroyMoveable:function(){this.disconnect(this._markTagetAnchorHandler);delete this._markTagetAnchorHandler},_calcColTargetAnchorPos:function(a,c){var b,d,g,e;e=a.clientX;var j=this.grid.layout.cells,i=f._isBodyLtr(),m=this._getVisibleHeaders();for(b=0;b<m.length;++b)if(d=f.position(m[b].node),i?(0===b||e>=d.x)&&e<d.x+d.w:(0===b||e<d.x+d.w)&&e>=d.x){g=d.x+
(i?0:d.w);break}else if(i?b===m.length-1&&e>=d.x+d.w:b===m.length-1&&e<d.x){++b;g=d.x+(i?d.w:0);break}if(b<m.length){if(e=m[b].cell.index,this.selector.isSelected("col",e)&&this.selector.isSelected("col",e-1)){d=this._dndRegion.selected;for(b=0;b<d.length;++b)if(0<=h.indexOf(d[b],e)){e=d[b][0];d=f.position(j[e].getHeaderNode());g=d.x+(i?0:d.w);break}}}else e=j.length;this._target=e;return g-c.x},_calcRowTargetAnchorPos:function(a,c){for(var b=this.grid,d=0,g=b.layout.cells;g[d].hidden;)++d;var e=
b.layout.cells[d],g=b.scroller.firstVisibleRow,d=e.getNode(g);if(!d)return this._target=-1,0;for(var j=f.position(d);j.y+j.h<a.clientY&&!(++g>=b.rowCount);)j=f.position(e.getNode(g));if(g<b.rowCount){if(this.selector.isSelected("row",g)&&this.selector.isSelected("row",g-1)){b=this._dndRegion.selected;for(d=0;d<b.length;++d)if(0<=h.indexOf(b[d],g)){g=b[d][0];j=f.position(e.getNode(g));break}}b=j.y}else b=j.y+j.h;this._target=g;return b-c.y},_calcCellTargetAnchorPos:function(a,c,b){var d=this._dndRegion.selected[0],
g=this._dndRegion.handle,e=this.grid,j=f._isBodyLtr(),i=e.layout.cells,m,h,n,k,o,q,p,r,l;h=g.col-d.min.col;n=d.max.col-g.col;var s;b.childNodes.length?(s=v(".dojoxGridCellBorderLeftTopDIV",b)[0],b=v(".dojoxGridCellBorderRightBottomDIV",b)[0]):(s=f.create("div",{"class":"dojoxGridCellBorderLeftTopDIV"},b),b=f.create("div",{"class":"dojoxGridCellBorderRightBottomDIV"},b));for(l=d.min.col+1;l<g.col;++l)i[l].hidden&&--h;for(l=g.col+1;l<d.max.col;++l)i[l].hidden&&--n;k=this._getVisibleHeaders();for(l=
h;l<k.length-n;++l)if(m=f.position(k[l].node),a.clientX>=m.x&&a.clientX<m.x+m.w||l==h&&(j?a.clientX<m.x:a.clientX>=m.x+m.w)||l==k.length-n-1&&(j?a.clientX>=m.x+m.w:a<m.x)){p=k[l-h];r=k[l+n];h=f.position(p.node);n=f.position(r.node);p=p.cell.index;r=r.cell.index;q=j?h.x:n.x;o=j?n.x+n.w-h.x:h.x+h.w-n.x;break}for(l=0;i[l].hidden;)++l;j=i[l];h=e.scroller.firstVisibleRow;for(n=f.position(j.getNode(h));n.y+n.h<a.clientY;)if(++h<e.rowCount)n=f.position(j.getNode(h));else break;g=h>=g.row-d.min.row?h-g.row+
d.min.row:0;a=g+d.max.row-d.min.row;a>=e.rowCount&&(a=e.rowCount-1,g=a-d.max.row+d.min.row);h=f.position(j.getNode(g));n=f.position(j.getNode(a));d=h.y;e=n.y+n.h-h.y;this._target={min:{row:g,col:p},max:{row:a,col:r}};j=(f.marginBox(s).w-f.contentBox(s).w)/2;p=f.position(i[p].getNode(g));f.style(s,{width:p.w-j+"px",height:p.h-j+"px"});i=f.position(i[r].getNode(a));f.style(b,{width:i.w-j+"px",height:i.h-j+"px"});return{h:e,w:o,l:q-c.x,t:d-c.y}},_markTargetAnchor:function(a){try{var c=this._dndRegion.type;
if(!this._alreadyOut&&!(this._dnding&&!this._config[c].within||this._extDnding&&!this._config[c]["in"])){var b,d,g,e,j=this._targetAnchor[c],i=f.position(this._container);j||(j=this._targetAnchor[c]=f.create("div",{"class":"cell"==c?"dojoxGridCellBorderDIV":"dojoxGridBorderDIV"}),f.style(j,"display","none"),this._container.appendChild(j));switch(c){case "col":b=i.h;d=this._targetAnchorBorderWidth;g=this._calcColTargetAnchorPos(a,i);e=0;break;case "row":b=this._targetAnchorBorderWidth;d=i.w;g=0;e=
this._calcRowTargetAnchorPos(a,i);break;case "cell":var h=this._calcCellTargetAnchorPos(a,i,j);b=h.h;d=h.w;g=h.l;e=h.t}"number"==typeof b&&"number"==typeof d&&"number"==typeof g&&"number"==typeof e?(f.style(j,{height:b+"px",width:d+"px",left:g+"px",top:e+"px"}),f.style(j,"display","")):this._target=null}}catch(k){}},_unmarkTargetAnchor:function(){this._dndRegion&&this._targetAnchor[this._dndRegion.type]&&f.style(this._targetAnchor[this._dndRegion.type],"display","none")},_getVisibleHeaders:function(){return h.map(h.filter(this.grid.layout.cells,
function(a){return!a.hidden}),function(a){return{node:a.getHeaderNode(),cell:a}})},_rearrange:function(){if(null!==this._target){var a=this._dndRegion.type,c=this._dndRegion.selected;if("cell"===a)this.rearranger[this._isCopy||this._copyOnly?"copyCells":"moveCells"](c[0],-1===this._target?null:this._target);else this.rearranger["col"==a?"moveColumns":"moveRows"](q(c),-1===this._target?null:this._target);this._target=null}},onDraggingOver:function(a){if(!this._dnding&&a){this._extDnding=a._isSource=
!0;if(!this._externalDnd)this._externalDnd=!0,this._dndRegion=this._mapRegion(a.grid,a._dndRegion);this._createDnDUI(this._moveEvent,!0);this.grid.pluginMgr.getPlugin("autoScroll").readyForAutoScroll=!0}},_mapRegion:function(a,c){if("cell"===c.type){var b=c.selected[0],d=this.grid.layout.cells,g=a.layout.cells,e,f=0;for(e=b.min.col;e<=b.max.col;++e)g[e].hidden||++f;for(e=0;0<f;++e)d[e].hidden||--f;var i=o.clone(c);i.selected[0].min.col=0;i.selected[0].max.col=e-1;for(e=b.min.col;e<=c.handle.col;++e)g[e].hidden||
++f;for(e=0;0<f;++e)d[e].hidden||--f;i.handle.col=e}return c},onDraggingOut:function(a){if(this._externalDnd&&(this._extDnding=!1,this._destroyDnDUI(!0,!1),a))a._isSource=!1},onDragIn:function(a,c){var b=!1;if(null!==this._target){b=a._dndRegion.selected;switch(a._dndRegion.type){case "cell":this.rearranger.changeCells(a.grid,b[0],this._target);break;case "row":b=q(b),this.rearranger.insertRows(a.grid,b,this._target)}b=!0}this._endDnd(!0);if(a.onDragOut)a.onDragOut(b&&!c)},onDragOut:function(a){if(a&&
!this._copyOnly)switch(a=this._dndRegion.selected,this._dndRegion.type){case "cell":this.rearranger.clearCells(a[0]);break;case "row":this.rearranger.removeRows(q(a))}this._endDnd(!0)},_canAccept:function(a){if(!a)return!1;var c=a._dndRegion,b=c.type;if(!this._config[b]["in"]||!a._config[b].out)return!1;var d=this.grid,g=c.selected,c=h.filter(d.layout.cells,function(a){return!a.hidden}).length,e=d.rowCount,f=!0;switch(b){case "cell":g=g[0],f=d.store.getFeatures()["dojo.data.api.Write"]&&g.max.row-
g.min.row<=e&&h.filter(a.grid.layout.cells,function(a){return a.index>=g.min.col&&a.index<=g.max.col&&!a.hidden}).length<=c;case "row":if(a._allDnDItemsLoaded())return f}return!1},_allDnDItemsLoaded:function(){if(this._dndRegion){var a=this._dndRegion.selected,c=[];switch(this._dndRegion.type){case "cell":for(var b=a[0].min.row,a=a[0].max.row;b<=a;++b)c.push(b);break;case "row":c=q(a);break;default:return!1}var d=this.grid._by_idx;return h.every(c,function(a){return!!d[a]})}return!1}});C.registerPlugin(u,
{dependency:["selector","rearrange"]});return u});