//>>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(A,y,D,n,t,l,E,q,B,C,F,G,H,I,J){var K=function(a){a.sort(function(f,e){return f-e});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},
v=function(a){for(var c=a[0],b=1;b<a.length;++b)c=c.concat(a[b]);return c},L=y("dojox.grid.enhanced.plugins.GridDnDElement",null,{constructor:function(a){this.plugin=a;this.node=l.create("div");this._items={}},destroy:function(){this.plugin=null;l.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";n.forEach(a.selected,function(d,f){var e=b+f;this._items[e]={type:c,data:d,dndPlugin:this.plugin};
this.node.appendChild(l.create("div",{id:e}))},this)},getDnDNodes:function(){return n.map(this.node.childNodes,function(a){return a})},destroyDnDNodes:function(){l.empty(this.node);this._items={}},getItem:function(a){return this._items[a]}}),M=y("dojox.grid.enhanced.plugins.GridDnDSource",F,{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,f=0;f<d.length;++f){if(d[f]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=[];n.forEach(c,function(g){g=a.getItem(g.id);if(g.data&&0<=n.indexOf(g.type,"grid/rows")){var h=g.data;"string"==typeof g.data&&
(h=E.fromJson(g.data));h&&e.push(h)}});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)}}),N=y("dojox.grid.enhanced.plugins.GridDnDAvatar",G,{construct:function(){this._itemType=
this.manager._dndPlugin._dndRegion.type;this._itemCount=this._getItemCount();this.isA11y=l.hasClass(q.body(),"dijit_a11y");var a=l.create("table",{border:"0",cellspacing:"0","class":"dojoxGridDndAvatar",style:{position:"absolute",zIndex:"1999",margin:"0px"}}),c=this.manager.source,b=l.create("tbody",null,a);b=l.create("tr",null,b);var d=l.create("td",{"class":"dojoxGridDnDIcon"},b);this.isA11y&&l.create("span",{id:"a11yIcon",innerHTML:this.manager.copy?"+":"\x3c"},d);d=l.create("td",{"class":"dojoxGridDnDItemIcon "+
this._getGridDnDIconClass()},b);d=l.create("td",null,b);l.create("span",{"class":"dojoxGridDnDItemCount",innerHTML:c.generateText?this._generateText():""},d);l.style(b,{opacity:.9});this.node=a},_getItemCount:function(){var a=this.manager._dndPlugin._dndRegion.selected,c=0;switch(this._itemType){case "cell":a=a[0];c=this.manager._dndPlugin.grid.layout.cells;var b=a.max.col-a.min.col+1,d=a.max.row-a.min.row+1;if(1<b)for(var f=a.min.col;f<=a.max.col;++f)c[f].hidden&&--b;c=b*d;break;case "row":case "col":c=
v(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+")"}});A=y("dojox.grid.enhanced.plugins.DnD",H,{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=t.clone(this._config);c=t.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 L(this);this._source=new M(this._elem.node,{grid:a,dndElem:this._elem,dnd:this});this._container=B(".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=t.hitch(this,"setupConfig");this.grid.dndCopyOnly=t.hitch(this,"copyOnly")},setupConfig:function(a){if(a&&t.isObject(a)){var c=["row","col","cell"],b=["within","in","out"],d=this._config;n.forEach(c,function(f){if(f in a){var e=a[f];e&&t.isObject(e)?n.forEach(b,
function(g){g in e&&(d[f][g]=!!e[g])}):n.forEach(b,function(g){d[f][g]=!!e})}});n.forEach(b,function(f){if(f in a){var e=a[f];e&&t.isObject(e)?n.forEach(c,function(g){g in e&&(d[g][f]=!!e[g])}):n.forEach(c,function(g){d[g][f]=!!e})}})}},copyOnly:function(a){"undefined"!=typeof a&&(this._copyOnly=!!a);return this._copyOnly},_isOutOfGrid:function(a){var c=l.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._isMouseDown&&!this._dndRegion&&(delete this._isMouseDown,this._oldCursor=l.style(q.body(),"cursor"),l.style(q.body(),"cursor","not-allowed"));var c=this._isOutOfGrid(a);!this._alreadyOut&&c?(this._alreadyOut=!0,this._dnding&&this._destroyDnDUI(!0,!1),this._moveEvent=a,this._source.onOutEvent()):this._alreadyOut&&!c&&(this._alreadyOut=!1,this._dnding&&this._createDnDUI(a,!0),this._moveEvent=a,this._source.onOverEvent())}else this._dnding=!0,this._startDnd(a)},_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)}l.style(q.body(),"cursor",this._oldCursor||"");delete this._isMouseDown},_initEvents:function(){var a=this.grid,c=this.selector;this.connect(q.doc,"onmousemove","_onMouseMove");this.connect(q.doc,"onmouseup","_onMouseUp");this.connect(a,"onCellMouseOver",function(b){this._dnding||c.isSelecting()||b.ctrlKey||(this._dndReady=c.isSelected("cell",b.rowIndex,b.cell.index),
c.selectEnabled(!this._dndReady))});this.connect(a,"onHeaderCellMouseOver",function(b){this._dndReady&&c.selectEnabled(!0)});this.connect(a,"onRowMouseOver",function(b){this._dndReady&&!b.cell&&c.selectEnabled(!0)});this.connect(a,"onCellMouseDown",function(b){!b.ctrlKey&&this._dndReady&&(this._dndRegion=this._getDnDRegion(b.rowIndex,b.cell.index),this._isMouseDown=!0)});this.connect(a,"onCellMouseUp",function(b){this._dndReady||c.isSelecting()||!b.cell||(this._dndReady=c.isSelected("cell",b.rowIndex,
b.cell.index),c.selectEnabled(!this._dndReady))});this.connect(a,"onCellClick",function(b){!this._dndReady||b.ctrlKey||b.shiftKey||c.select("cell",b.rowIndex,b.cell.index)});this.connect(a,"onEndAutoScroll",function(b,d,f,e,g){this._dnding&&this._markTargetAnchor(g)});this.connect(q.doc,"onkeydown",function(b){b.keyCode==C.ESCAPE?this._endDnd(!1):b.keyCode==C.CTRL&&(c.selectEnabled(!0),this._isCopy=!0)});this.connect(q.doc,"onkeyup",function(b){b.keyCode==C.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,f=!!d.cell.length|!!d.row.length<<1|!!d.col.length<<2;switch(f){case 1:var e="cell";if(!this._config[e].within&&!this._config[e].out)break;var g=this.grid.layout.cells;f=function(k){for(var m=0,p=k.min.col;p<=k.max.col;++p)g[p].hidden&&++m;return(k.max.row-
k.min.row+1)*(k.max.col-k.min.col+1-m)};var h={max:{row:-1,col:-1},min:{row:Infinity,col:Infinity}};n.forEach(d[e],function(k){k.row<h.min.row&&(h.min.row=k.row);k.row>h.max.row&&(h.max.row=k.row);k.col<h.min.col&&(h.min.col=k.col);k.col>h.max.col&&(h.max.col=k.col)});if(n.some(d[e],function(k){return k.row==a&&k.col==c})&&f(h)==d[e].length&&n.every(d[e],function(k){return k.row>=h.min.row&&k.row<=h.max.row&&k.col>=h.min.col&&k.col<=h.max.col}))return{type:e,selected:[h],handle:{row:a,col:c}};break;
case 2:case 4:if(e=2==f?"row":"col",this._config[e].within||this._config[e].out)if(d=b.getSelected(e),d.length)return{type:e,selected:K(d),handle:2==f?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=l.position(this.grid.views.views[0].domNode);l.style(this._container,"height",b.h+"px");try{c||this._createSource(a),this._createMoveable(a),this._oldCursor=l.style(q.body(),"cursor"),l.style(q.body(),
"cursor","default")}catch(d){}},_destroyDnDUI:function(a,c){try{c&&this._destroySource(),this._unmarkTargetAnchor(),a||this._destroyMoveable(),l.style(q.body(),"cursor",this._oldCursor)}catch(b){}},_createSource:function(a){this._elem.createDnDNodes(this._dndRegion);var c=J.manager(),b=c.makeAvatar;c._dndPlugin=this;c.makeAvatar=function(){var d=new N(c);delete c._dndPlugin;return d};c.startDrag(this._source,this._elem.getDnDNodes(),a.ctrlKey);c.makeAvatar=b;c.onMouseMove(a)},_destroySource:function(){D.publish("/dnd/cancel")},
_createMoveable:function(a){this._markTagetAnchorHandler||(this._markTagetAnchorHandler=this.connect(q.doc,"onmousemove","_markTargetAnchor"))},_destroyMoveable:function(){this.disconnect(this._markTagetAnchorHandler);delete this._markTagetAnchorHandler},_calcColTargetAnchorPos:function(a,c){var b;var d=a.clientX;var f=this.grid.layout.cells,e=l._isBodyLtr(),g=this._getVisibleHeaders();for(b=0;b<g.length;++b){var h=l.position(g[b].node);if(e?(0===b||d>=h.x)&&d<h.x+h.w:(0===b||d<h.x+h.w)&&d>=h.x){var k=
h.x+(e?0:h.w);break}else if(e?b===g.length-1&&d>=h.x+h.w:b===g.length-1&&d<h.x){++b;k=h.x+(e?h.w:0);break}}if(b<g.length){if(d=g[b].cell.index,this.selector.isSelected("col",d)&&this.selector.isSelected("col",d-1))for(h=this._dndRegion.selected,b=0;b<h.length;++b)if(0<=n.indexOf(h[b],d)){d=h[b][0];h=l.position(f[d].getHeaderNode());k=h.x+(e?0:h.w);break}}else d=f.length;this._target=d;return k-c.x},_calcRowTargetAnchorPos:function(a,c){for(var b=this.grid,d=0,f=b.layout.cells;f[d].hidden;)++d;var e=
b.layout.cells[d];f=b.scroller.firstVisibleRow;d=e.getNode(f);if(!d)return this._target=-1,0;for(var g=l.position(d);g.y+g.h<a.clientY&&!(++f>=b.rowCount);)g=l.position(e.getNode(f));if(f<b.rowCount){if(this.selector.isSelected("row",f)&&this.selector.isSelected("row",f-1))for(b=this._dndRegion.selected,d=0;d<b.length;++d)if(0<=n.indexOf(b[d],f)){f=b[d][0];g=l.position(e.getNode(f));break}b=g.y}else b=g.y+g.h;this._target=f;return b-c.y},_calcCellTargetAnchorPos:function(a,c,b){var d=this._dndRegion.selected[0],
f=this._dndRegion.handle,e=this.grid,g=l._isBodyLtr(),h=e.layout.cells,k;var m=f.col-d.min.col;var p=d.max.col-f.col;if(b.childNodes.length){var z=B(".dojoxGridCellBorderLeftTopDIV",b)[0];b=B(".dojoxGridCellBorderRightBottomDIV",b)[0]}else z=l.create("div",{"class":"dojoxGridCellBorderLeftTopDIV"},b),b=l.create("div",{"class":"dojoxGridCellBorderRightBottomDIV"},b);for(k=d.min.col+1;k<f.col;++k)h[k].hidden&&--m;for(k=f.col+1;k<d.max.col;++k)h[k].hidden&&--p;var w=this._getVisibleHeaders();for(k=m;k<
w.length-p;++k){var r=l.position(w[k].node);if(a.clientX>=r.x&&a.clientX<r.x+r.w||k==m&&(g?a.clientX<r.x:a.clientX>=r.x+r.w)||k==w.length-p-1&&(g?a.clientX>=r.x+r.w:a<r.x)){var u=w[k-m];var x=w[k+p];m=l.position(u.node);p=l.position(x.node);u=u.cell.index;x=x.cell.index;var O=g?m.x:p.x;var P=g?p.x+p.w-m.x:m.x+m.w-p.x;break}}for(k=0;h[k].hidden;)++k;g=h[k];m=e.scroller.firstVisibleRow;for(p=l.position(g.getNode(m));p.y+p.h<a.clientY;)if(++m<e.rowCount)p=l.position(g.getNode(m));else break;f=m>=f.row-
d.min.row?m-f.row+d.min.row:0;a=f+d.max.row-d.min.row;a>=e.rowCount&&(a=e.rowCount-1,f=a-d.max.row+d.min.row);m=l.position(g.getNode(f));p=l.position(g.getNode(a));d=m.y;e=p.y+p.h-m.y;this._target={min:{row:f,col:u},max:{row:a,col:x}};g=(l.marginBox(z).w-l.contentBox(z).w)/2;u=l.position(h[u].getNode(f));l.style(z,{width:u.w-g+"px",height:u.h-g+"px"});h=l.position(h[x].getNode(a));l.style(b,{width:h.w-g+"px",height:h.h-g+"px"});return{h:e,w:P,l:O-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=this._targetAnchor[c],d=l.position(this._container);b||(b=this._targetAnchor[c]=l.create("div",{"class":"cell"==c?"dojoxGridCellBorderDIV":"dojoxGridBorderDIV"}),l.style(b,"display","none"),this._container.appendChild(b));switch(c){case "col":var f=d.h;var e=this._targetAnchorBorderWidth;var g=this._calcColTargetAnchorPos(a,d);var h=0;break;case "row":f=this._targetAnchorBorderWidth;
e=d.w;g=0;h=this._calcRowTargetAnchorPos(a,d);break;case "cell":var k=this._calcCellTargetAnchorPos(a,d,b);f=k.h;e=k.w;g=k.l;h=k.t}"number"==typeof f&&"number"==typeof e&&"number"==typeof g&&"number"==typeof h?(l.style(b,{height:f+"px",width:e+"px",left:g+"px",top:h+"px"}),l.style(b,"display","")):this._target=null}}catch(m){}},_unmarkTargetAnchor:function(){this._dndRegion&&this._targetAnchor[this._dndRegion.type]&&l.style(this._targetAnchor[this._dndRegion.type],"display","none")},_getVisibleHeaders:function(){return n.map(n.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"](v(c),-1===this._target?null:this._target);this._target=null}},onDraggingOver:function(a){!this._dnding&&a&&(this._extDnding=a._isSource=
!0,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,f=a.layout.cells,e,g=0;for(e=b.min.col;e<=b.max.col;++e)f[e].hidden||++g;for(e=0;0<g;++e)d[e].hidden||--g;var h=t.clone(c);h.selected[0].min.col=0;h.selected[0].max.col=e-1;for(e=b.min.col;e<=c.handle.col;++e)f[e].hidden||
++g;for(e=0;0<g;++e)d[e].hidden||--g;h.handle.col=e}return c},onDraggingOut:function(a){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=v(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(v(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,f=c.selected;c=n.filter(d.layout.cells,function(h){return!h.hidden}).length;var e=d.rowCount,g=!0;switch(b){case "cell":f=f[0],g=d.store.getFeatures()["dojo.data.api.Write"]&&f.max.row-
f.min.row<=e&&n.filter(a.grid.layout.cells,function(h){return h.index>=f.min.col&&h.index<=f.max.col&&!h.hidden}).length<=c;case "row":if(a._allDnDItemsLoaded())return g}return!1},_allDnDItemsLoaded:function(){if(this._dndRegion){var a=this._dndRegion.selected,c=[];switch(this._dndRegion.type){case "cell":var b=a[0].min.row;for(a=a[0].max.row;b<=a;++b)c.push(b);break;case "row":c=v(a);break;default:return!1}var d=this.grid._by_idx;return n.every(c,function(f){return!!d[f]})}return!1}});I.registerPlugin(A,
{dependency:["selector","rearrange"]});return A});