Keyboard.js
6.49 KB
//>>built
define("dojo/_base/declare dojo/aspect dojo/dom-class dojo/on dojo/_base/lang dojo/has ./util/misc dojo/_base/sniff".split(" "),function(q,n,r,p,t,v,E){function w(a){a.preventDefault()}var F={checkbox:1,radio:1,button:1},x=/\bdgrid-cell\b/,y=/\bdgrid-row\b/,d=q(null,{pageSkip:10,tabIndex:0,keyMap:null,headerKeyMap:null,postMixInProperties:function(){this.inherited(arguments);this.keyMap||(this.keyMap=t.mixin({},d.defaultKeyMap));this.headerKeyMap||(this.headerKeyMap=t.mixin({},d.defaultHeaderKeyMap))},
postCreate:function(){function a(a){var b=a.target;return b.type&&(!F[b.type]||32===a.keyCode)}function c(c){function e(){b._focusedHeaderNode&&(b._focusedHeaderNode.tabIndex=-1);if(b.showHeader){if(d)for(var a=b.headerNode.getElementsByTagName("th"),c=0,e;e=a[c];++c){if(k.test(e.className)){b._focusedHeaderNode=l=e;break}}else b._focusedHeaderNode=l=b.headerNode;l&&(l.tabIndex=b.tabIndex)}}function f(){var a=b._focusedNode||l;if(!k.test(a.className)||!c.contains(a)){for(var e=c.getElementsByTagName("*"),
d=0,u;u=e[d];++d)if(k.test(u.className)){a=b._focusedNode=u;break}l.tabIndex=-1;a.tabIndex=b.tabIndex}}var d=b.cellNavigation,k=d?x:y,m=c===b.headerNode,l=c;m?(e(),n.after(b,"renderHeader",e,!0)):(n.after(b,"renderArray",f,!0),n.after(b,"_onNotification",function(a,b){0===b.totalLength?c.tabIndex=0:1===b.totalLength&&"add"===b.type&&f()},!0));b._listeners.push(p(c,"mousedown",function(c){a(c)||b._focusOnNode(c.target,m,c)}));b._listeners.push(p(c,"keydown",function(c){if(!c.metaKey&&!c.altKey){var e=
b[m?"headerKeyMap":"keyMap"][c.keyCode];e&&!a(c)&&e.call(b,c)}}))}this.inherited(arguments);var b=this;this.tabableHeader&&(c(this.headerNode),p(this.headerNode,"dgrid-cellfocusin",function(){b.scrollTo({x:this.scrollLeft})}));c(this.contentNode);this._debouncedEnsureScroll=E.debounce(this._ensureScroll,this)},_pruneRow:function(){var a=this._focusedNode;this._focusedNode=null;this.inherited(arguments);this._focusedNode=a},removeRow:function(a){if(!this._focusedNode)return this.inherited(arguments);
var c=this,b=document.activeElement===this._focusedNode,e=this[this.cellNavigation?"cell":"row"](this._focusedNode),d=e.row||e,f;a=a.element||a;a===d.element&&((f=this.down(d,1,!0))&&f.element!==a||(f=this.up(d,1,!0)),this._removedFocus={active:b,rowId:d.id,columnId:e.column&&e.column.id,siblingId:f&&f.element!==a?f.id:void 0},setTimeout(function(){c._removedFocus&&c._restoreFocus(d.id)},0),this._focusedNode=null);this.inherited(arguments)},insertRow:function(){var a=this.inherited(arguments);this._removedFocus&&
!this._removedFocus.wait&&this._restoreFocus(a);return a},_restoreFocus:function(a){var c=this._removedFocus,b;if((a=(a=a&&this.row(a))&&a.element&&a.id===c.rowId?a:"undefined"!==typeof c.siblingId&&this.row(c.siblingId))&&a.element){if(!a.element.parentNode.parentNode){c.wait=!0;return}"undefined"!==typeof c.columnId&&(b=this.cell(a,c.columnId))&&b.element&&(a=b);c.active&&0!==a.element.offsetHeight?this._focusOnNode(a,!1,null):(r.add(a.element,"dgrid-focus"),a.element.tabIndex=this.tabIndex,this._focusedNode=
a.element)}delete this._removedFocus},addKeyHandler:function(a,c,b){return n.after(this[b?"headerKeyMap":"keyMap"],a,c,!0)},_ensureRowScroll:function(a){var c=this.getScrollPosition().y;c>a.offsetTop?this.scrollTo({y:a.offsetTop}):c+this.contentNode.offsetHeight<a.offsetTop+a.offsetHeight&&this.scrollTo({y:a.offsetTop-this.contentNode.offsetHeight+a.offsetHeight})},_ensureColumnScroll:function(a){var c=this.getScrollPosition().x,b=a.offsetLeft;if(c>b)this.scrollTo({x:b});else{var e=this.bodyNode.clientWidth;
a=a.offsetWidth;var d=b+a;c+e<d&&this.scrollTo({x:e>a?d-e:b})}},_ensureScroll:function(a,c){!a.column&&!a.row&&a.data&&a.element?this._ensureRowScroll(a.element):(this.cellNavigation&&(this.columnSets||1<this.subRows.length)&&!c&&this._ensureRowScroll(a.row.element),this.bodyNode.clientWidth<this.contentNode.offsetWidth&&this._ensureColumnScroll(a.element))},_focusOnNode:function(a,c,b){var e="_focused"+(c?"Header":"")+"Node",d=this[e],f=this.cellNavigation?"cell":"row",g=this[f](a),k,m,l,n,h;if(a=
g&&g.element){if(this.cellNavigation)for(k=a.getElementsByTagName("input"),h=0,l=k.length;h<l;h++)if(m=k[h],(-1!==m.tabIndex||"_dgridLastValue"in m)&&!m.disabled){m.focus();n=!0;break}null!==b&&(b=t.mixin({grid:this},b),b.type&&(b.parentType=b.type),b.bubbles||(b.bubbles=!0));d&&(r.remove(d,"dgrid-focus"),d.removeAttribute("tabindex"),b&&(b[f]=this[f](d),p.emit(d,"dgrid-cellfocusout",b)));d=this[e]=a;b&&(b[f]=g);e=this.cellNavigation?x:y;!n&&e.test(a.className)&&(a.tabIndex=this.tabIndex,a.focus());
r.add(a,"dgrid-focus");b&&p.emit(d,"dgrid-cellfocusin",b);this._debouncedEnsureScroll(g,c)}},focusHeader:function(a){this._focusOnNode(a||this._focusedHeaderNode,!0)},focus:function(a){(a=a||this._focusedNode)?this._focusOnNode(a,!1):(this._removedFocus&&(this._removedFocus.active=!0),this.contentNode.focus())}}),h=d.moveFocusVertical=function(a,c){var b=this.cellNavigation,d=this[b?"cell":"row"](a),d=b&&d.column.id;c=this.down(this._focusedNode,c,!0);b&&(c=this.cell(c,d));this._focusOnNode(c,!1,
a);a.preventDefault()};q=d.moveFocusUp=function(a){h.call(this,a,-1)};v=d.moveFocusDown=function(a){h.call(this,a,1)};var G=d.moveFocusPageUp=function(a){h.call(this,a,-this.pageSkip)},H=d.moveFocusPageDown=function(a){h.call(this,a,this.pageSkip)},z=d.moveFocusHorizontal=function(a,c){if(this.cellNavigation){var b=!this.row(a);this._focusOnNode(this.right(this["_focused"+(b?"Header":"")+"Node"],c),b,a);a.preventDefault()}},A=d.moveFocusLeft=function(a){z.call(this,a,-1)},B=d.moveFocusRight=function(a){z.call(this,
a,1)},C=d.moveHeaderFocusEnd=function(a,c){var b;this.cellNavigation&&(b=this.headerNode.getElementsByTagName("th"),this._focusOnNode(b[c?0:b.length-1],!0,a));a.preventDefault()},I=d.moveHeaderFocusHome=function(a){C.call(this,a,!0)},D=d.moveFocusEnd=function(a,c){var b=this.cellNavigation,d=this.contentNode,h=d.scrollTop+(c?0:d.scrollHeight),d=d[c?"firstChild":"lastChild"],f=-1<d.className.indexOf("dgrid-preload"),g=f?d[(c?"next":"previous")+"Sibling"]:d,k;a.preventDefault();this.scrollTo({y:h});
if(f){for(;g&&0>g.className.indexOf("dgrid-row");)g=g[(c?"next":"previous")+"Sibling"];if(!g)return}!f||1>d.offsetHeight?(b&&(g=this.cell(g,this.cell(a).column.id)),this._focusOnNode(g,!1,a)):k=n.after(this,"renderArray",function(d){var e=d[c?0:d.length-1];b&&(e=this.cell(e,this.cell(a).column.id));this._focusOnNode(e,!1,a);k.remove();return d})},J=d.moveFocusHome=function(a){D.call(this,a,!0)};d.defaultKeyMap={32:w,33:G,34:H,35:D,36:J,37:A,38:q,39:B,40:v};d.defaultHeaderKeyMap={32:w,35:C,36:I,37:A,
39:B};return d});