YUI.add("aui-datatable-selection",function(e,t){var n=e.Lang,r=n.isArray,i=n.isString,s=n.isObject,o="CLASS_NAMES_SELECTION",u="activeCell",a="activeCoord",f="activeRow",l="activeCoordChange",c="boundingBox",h="cell",p="cellIndex",d="children",v="focused",m="key",g="mousedown",y="mouseenter",b="mouseup",w="selection",E="tabindex",S=".",x=function(e,t,n){return Math.min(Math.max(e,t),n)},T=function(){};T.ATTRS={activeCell:{getter:"_getActiveCell"},activeCoord:{value:[-1,-1]},activeRow:{getter:"_getActiveRow"},selection:{setter:"_setSelection"},tabIndex:{value:0}},e.mix(T.prototype,{_capturing:!1,_selectionEnd:null,_selectionSeed:null,_selectionStart:null,initializer:function(){var e=this,t=e.get(c);e[o]={cell:e.getClassName(h),selection:e.getClassName(w)},e._bindSelectionUI(),t.addClass(e[o].selection)},destroy:function(){var e=this;e._selectionKeyHandler.detach()},captureSelection:function(t){var n=this,r=[],i=[],s=[],o=[],u;for(u=0;u<t.length;u++){var a=t[u],f=n.getCell(a);o.push(a[0]),r.push(f),i.push(n.getColumn(f))}o=e.Array.unique(o),i=e.Array.unique(i);for(u=0;u<o.length;u++)o[u]=n.getRow(o[u]),s[u]=n.getRecord(o[u]);return{cells:r,cols:i,coords:t,records:s,rows:o}},getActiveColumn:function(){var e=this;return e.getColumn(e.get(u))},getActiveRecord:function(){var e=this;return e.getRecord(e.get(f))},getCoord:function(e){var t=this,n=t.getCell(e),r=t.body.tbodyNode,i=r.get("firstChild.rowIndex");return[n.get("parentNode.rowIndex")-i,n.get(p)]},_afterActiveCoordChange:function(e){var t=this,n=t.getCell(e.newVal);n&&n.setAttribute(E,0).focus()},_bindSelectionUI:function(){var t=this,n=t[o];t._selectionKeyHandler=e.getDoc().on(m,e.bind(t._onSelectionKey,t),"down:enter,37,38,39,40"),t.after(l,t._afterActiveCellIndexChange),t.delegate(b,e.bind(t._onSelectionMouseUp,t),S+n.cell),t.delegate(g,e.bind(t._onSelectionMouseDown,t),S+n.cell),t.delegate(y,e.bind(t._onSelectionMouseEnter,t),S+n.cell)},_getActiveCell:function(){var e=this,t=e.get(a),n=t[0],r=t[1];return n>-1&&r>-1?e.getCell([n,r]):null},_getActiveRow:function(){var e=this,t=e.get(a),n=t[0];return n>-1?e.getRow(n):null},_onSelectionMouseDown:function(e){var t=this,n=e.currentTarget,r=t.get(c),i=t.getCoord(n);r.unselectable(),t._capturing=!0,t._selectionSeed=n,t._selectionStart=t._selectionEnd=t.getCoord(n),t.set(a,i)},_onSelectionMouseEnter:function(e){var t=this,n=e.currentTarget;if(!t._capturing)return;t._selectionSeed=n,t._selectionEnd=t.getCoord(n),t.set(w,{start:t._selectionStart,end:t._selectionEnd})},_onSelectionMouseUp:function(e){var t=this,n=t.get(c);t.get(v)&&(t._selectionEnd=t.getCoord(t._selectionSeed),t.set(w,{start:t._selectionStart,end:t._selectionEnd})),n.selectable(),t._capturing=!1},_onSelectionKey:function(e){var t=this,n=t.body,r=n.tbodyNode,i=e.keyCode,s=t.get(u),o,f=r.get(d).size(),l=n.get("columns").length,c,h;s&&t.get(v)&&(o=t.getCoord(s),c=o[0],h=o[1],i===37?h--:i===38?c--:i===39?h++:i===40&&c++,c=x(c,0,f-1),h=x(h,0,l-1),t.set(a,[c,h]),t.set(w,[c,h]),e.preventDefault())},_parseRange:function(e){var t=e[0],n=e[1],r=[],i,s;for(i=Math.min(t[0],n[0]);i<=Math.max(t[0],n[0]);i++)for(s=Math.min(t[1],n[1]);s<=Math.max(t[1],n[1]);s++)r.push([i,s]);return r},_setSelection:function(t){var n=this;return r(t)?r(t[0])||(t=[t]):s(t)?t=n._parseRange([t.start,t.end]):e.instanceOf(t,e.Node)&&(t=[n.getCoord(t)]),n.captureSelection(t)}}),e.DataTable.Selection=T,e.Base.mix(e.DataTable,[T]),e.DataTable.prototype.getColumn=function(t){return function(n){var r;return e.instanceOf(n,e.Node)&&(r=this.getCell(n),n=r&&(r.get("className").match(new RegExp(this.getClassName("col","(\\w+)")))||[])[1]),t.call(this,n)}}(e.DataTable.prototype.getColumn),e.DataTable.prototype.getRow=function(t){return function(n){var r=this,i=r.body.tbodyNode,s;return e.instanceOf(n,e.Node)?(s=n.ancestor(function(e){return e.get("parentNode").compareTo(i)},!0),s):t.call(this,n)}}(e.DataTable.prototype.getRow),e.DataTable.prototype._setColumns=function(t){function f(e){var t={},n,i,l;r.push(e),o.push(t);for(n in e)e.hasOwnProperty(n)&&(i=e[n],a(i)?t[n]=i.slice():s(i,!0)&&i.constructor===t.constructor?(l=u(i,r),t[n]=l===-1?f(i):o[l]):t[n]=e[n]);return t}function l(e){return e=e.replace(/\s+/,"-"),n[e]?e+=n[e]++:n[e]=1,e}function c(t,n){var r=[],s,o,u,h;for(s=0,o=t.length;s<o;++s)r[s]=u=i(t[s])?{key:t[s]}:f(t[s]),h=e.stamp(u),u.id||(u.id=h),u.field&&(u.name=u.field),n?u._parent=n:delete u._parent,u._id=l(u.name||u.key||u.id),a(u.children)&&(u.children=c(u.children,u));return r}var n={},r=[],o=[],u=e.Array.indexOf,a=e.Lang.isArray;return t&&c(t)}},"2.0.0",{requires:["aui-datatable-core"],skinnable:!0});
