//>>built
define("dojox/grid/enhanced/plugins/Rearrange","dojo/_base/kernel dojo/_base/lang dojo/_base/declare dojo/_base/array dojo/_base/connect ../../EnhancedGrid ../_Plugin ./_RowMapLayer".split(" "),function(z,A,E,p,t,F,G,H){z=E("dojox.grid.enhanced.plugins.Rearrange",G,{name:"rearrange",constructor:function(b,a){this.grid=b;this.setArgs(a);var f=new H(b);dojox.grid.enhanced.plugins.wrap(b,"_storeLayerFetch",f)},setArgs:function(b){this.args=A.mixin(this.args||{},b||{});this.args.setIdentifierForNewItem=
this.args.setIdentifierForNewItem||function(a){return a}},destroy:function(){this.inherited(arguments);this.grid.unwrap("rowmap")},onSetStore:function(b){this.grid.layer("rowmap").clearMapping()},_hasIdentity:function(b){var a=this.grid,f=a.store,g=a.layout.cells;return f.getFeatures()["dojo.data.api.Identity"]&&p.some(b,function(d){return f.getIdentityAttributes(a._by_idx[d.r].item)==g[d.c].field})?!0:!1},moveColumns:function(b,a){var f=this.grid,g=f.layout,d=g.cells,e,h=0,c=!0;var k={};var l={};
b.sort(function(x,B){return x-B});for(e=0;e<b.length;++e)k[b[e]]=e,b[e]<a&&++h;var n=0,m=0,r=Math.max(b[b.length-1],a);r==d.length&&--r;var y=Math.min(b[0],a);for(e=y;e<=r;++e){var v=k[e];0<=v?l[e]=a-h+v:e<a?(l[e]=y+n,++n):e>=a&&(l[e]=a+b.length-h+m,++m)}h=0;a==d.length&&(--a,c=!1);f._notRefreshSelection=!0;for(e=0;e<b.length;++e)k=b[e],k<a&&(k-=h),++h,k!=a&&(g.moveColumn(d[k].view.idx,d[a].view.idx,k,a,c),d=g.cells),a<=k&&++a;delete f._notRefreshSelection;t.publish("dojox/grid/rearrange/move/"+f.id,
["col",l,b])},moveRows:function(b,a){var f=this.grid,g={},d=[],e=[],h=b.length,c,k;for(c=0;c<h;++c){e=b[c];if(e>=a)break;d.push(e)}e=b.slice(c);c=d;if(h=c.length){var l={};p.forEach(c,function(m){l[m]=!0});g[c[0]]=a-h;d=0;c=c[d]+1;for(k=c-1;c<a;++c)l[c]?(++d,g[c]=a-h+d):(g[c]=k,++k)}c=e;if(h=c.length)for(l={},p.forEach(c,function(m){l[m]=!0}),g[c[h-1]]=a+h-1,d=h-1,c=c[d]-1,k=c+1;c>=a;--c)l[c]?(--d,g[c]=a+d):(g[c]=k,--k);var n=A.clone(g);f.layer("rowmap").setMapping(g);f.forEachLayer(function(m){return"rowmap"!=
m.name()?(m.invalidate(),!0):!1},!1);f.selection.selected=[];f._noInternalMapping=!0;f._refresh();setTimeout(function(){t.publish("dojox/grid/rearrange/move/"+f.id,["row",n,b]);f._noInternalMapping=!1},0)},moveCells:function(b,a){var f=this.grid,g=f.store;if(g.getFeatures()["dojo.data.api.Write"]&&(b.min.row!=a.min.row||b.min.col!=a.min.col)){var d=f.layout.cells,e,h,c=[],k=[];var l=b.min.row;for(e=a.min.row;l<=b.max.row;++l,++e){var n=b.min.col;for(h=a.min.col;n<=b.max.col;++n,++h){for(;d[n]&&d[n].hidden;)++n;
for(;d[h]&&d[h].hidden;)++h;c.push({r:l,c:n});k.push({r:e,c:h,v:d[n].get(l,f._by_idx[l].item)})}}this._hasIdentity(c.concat(k))||(p.forEach(c,function(m){g.setValue(f._by_idx[m.r].item,d[m.c].field,"")}),p.forEach(k,function(m){g.setValue(f._by_idx[m.r].item,d[m.c].field,m.v)}),g.save({onComplete:function(){t.publish("dojox/grid/rearrange/move/"+f.id,["cell",{from:b,to:a}])}}))}},copyCells:function(b,a){var f=this.grid,g=f.store;if(g.getFeatures()["dojo.data.api.Write"]&&(b.min.row!=a.min.row||b.min.col!=
a.min.col)){var d=f.layout.cells,e,h,c=[];var k=b.min.row;for(e=a.min.row;k<=b.max.row;++k,++e){var l=b.min.col;for(h=a.min.col;l<=b.max.col;++l,++h){for(;d[l]&&d[l].hidden;)++l;for(;d[h]&&d[h].hidden;)++h;c.push({r:e,c:h,v:d[l].get(k,f._by_idx[k].item)})}}this._hasIdentity(c)||(p.forEach(c,function(n){g.setValue(f._by_idx[n.r].item,d[n.c].field,n.v)}),g.save({onComplete:function(){setTimeout(function(){t.publish("dojox/grid/rearrange/copy/"+f.id,["cell",{from:b,to:a}])},0)}}))}},changeCells:function(b,
a,f){var g=this.grid,d=g.store;if(d.getFeatures()["dojo.data.api.Write"]){var e=g.layout.cells,h=b.layout.cells,c,k,l=[];var n=a.min.row;for(c=f.min.row;n<=a.max.row;++n,++c){var m=a.min.col;for(k=f.min.col;m<=a.max.col;++m,++k){for(;h[m]&&h[m].hidden;)++m;for(;e[k]&&e[k].hidden;)++k;l.push({r:c,c:k,v:h[m].get(n,b._by_idx[n].item)})}}this._hasIdentity(l)||(p.forEach(l,function(r){d.setValue(g._by_idx[r.r].item,e[r.c].field,r.v)}),d.save({onComplete:function(){t.publish("dojox/grid/rearrange/change/"+
g.id,["cell",f])}}))}},clearCells:function(b){var a=this.grid,f=a.store;if(f.getFeatures()["dojo.data.api.Write"]){var g=a.layout.cells,d,e,h=[];for(d=b.min.row;d<=b.max.row;++d)for(e=b.min.col;e<=b.max.col;++e){for(;g[e]&&g[e].hidden;)++e;h.push({r:d,c:e})}this._hasIdentity(h)||(p.forEach(h,function(c){f.setValue(a._by_idx[c.r].item,g[c.c].field,"")}),f.save({onComplete:function(){t.publish("dojox/grid/rearrange/change/"+a.id,["cell",b])}}))}},insertRows:function(b,a,f){try{var g=this.grid,d=g.store,
e=g.rowCount,h={},c=0,k=[],l,n=0>f,m=this,r=a.length;if(n)f=0;else for(l=f;l<g.rowCount;++l)h[l]=l+r;if(d.getFeatures()["dojo.data.api.Write"]){if(b){var y=b.store;if(n)var v=p.filter(p.map(g.layout.cells,function(q){return q.field}),function(q){return q});else{for(l=0;!x;++l)var x=g._by_idx[l];v=d.getAttributes(x.item)}var B=[];p.forEach(a,function(q,w){var u={},C=b._by_idx[q];if(C){p.forEach(v,function(D){u[D]=y.getValue(C.item,D)});u=m.args.setIdentifierForNewItem(u,d,e+c)||u;try{d.newItem(u),
k.push(f+w),h[e+c]=f+w,++c}catch(D){}}else B.push(q)})}else if(a.length&&A.isObject(a[0]))p.forEach(a,function(q,w){var u=m.args.setIdentifierForNewItem(q,d,e+c)||q;try{d.newItem(u),k.push(f+w),h[e+c]=f+w,++c}catch(C){}});else return;g.layer("rowmap").setMapping(h);d.save({onComplete:function(){g._refresh();setTimeout(function(){t.publish("dojox/grid/rearrange/insert/"+g.id,["row",k])},0)}})}}catch(q){}},removeRows:function(b){var a=this.grid,f=a.store;try{p.forEach(p.map(b,function(g){return a._by_idx[g]}),
function(g){g&&f.deleteItem(g.item)}),f.save({onComplete:function(){t.publish("dojox/grid/rearrange/remove/"+a.id,["row",b])}})}catch(g){}},_getPageInfo:function(){var b=this.grid.scroller,a=b.page,f=b.page,g=b.firstVisibleRow,d=b.lastVisibleRow,e=b.rowsPerPage,h,c,k,l=[];p.forEach(b.pageNodes[0],function(n,m){n&&(k=!1,h=m*e,c=(m+1)*e-1,g>=h&&g<=c&&(a=m,k=!0),d>=h&&d<=c&&(f=m,k=!0),!k&&(h>d||c<g)&&l.push(m))});return{topPage:a,bottomPage:f,invalidPages:l}}});F.registerPlugin(z);return z});