Debian Codegraph @

dojox/grid/enhanced/plugins/NestedSorting.js

raw symbols SCIP
1 define([
2 "dojo/_base/declare",
3 "dojo/_base/array",
4 "dojo/_base/connect",
5 "dojo/_base/lang",
6 "dojo/_base/html",
7 "dojo/_base/event",
8 "dojo/_base/window",
9 "dojo/keys",
10 "dojo/query",
11 "dojo/string",
12 "../_Plugin",
13 "../../EnhancedGrid"
14 ], function(declare, array, connect, lang, html, evt, win, keys, query, string, _Plugin, EnhancedGrid){
15
16 var NestedSorting = declare("dojox.grid.enhanced.plugins.NestedSorting", _Plugin, {
17 // summary:
18 // Provides nested sorting feature
19 //
20 // description:
21 // A flexible way to control multiple column sorting, including
22 //
23 // 1. Set default sorting order
24 // 2. Disable sorting for certain columns
25 // 3. Set sorting order dynamically with JS API
26 //
27 // example:
28 // | <script type="text/javascript">
29 // | var grid = new dojox.grid.EnhancedGrid({plugins : {nestedSorting: true}},
30 // | sortFields: [{attribute: 'col4', descending: false},...],//set default sorting order
31 // | canSort: function(index, field){ return true},//disable sorting for a column
32 // | ... }, dojo.byId('gridDiv'));
33 // | grid.startup();
34 // | //set new sorting order
35 // | grid.setSortIndex([{attribute: 'col3', descending: true},...])
36 // | </script>
37
38 // name: String
39 // Plugin name
40 name: "nestedSorting",
41
42 _currMainSort: 'none',//'none'|'asc'|'desc'
43
44 _currRegionIdx: -1,
45
46 _a11yText: {
47 'dojoxGridDescending' : '&#9662;',
48 'dojoxGridAscending' : '&#9652;',
49 'dojoxGridAscendingTip' : '&#1784;',
50 'dojoxGridDescendingTip': '&#1783;',
51 'dojoxGridUnsortedTip' : 'x' //'&#10006;'
52 },
53
54 constructor: function(){
55 this._sortDef = [];
56 this._sortData = {};
57 this._headerNodes = {};
58 //column index that are hidden, un-sortable or indirect selection etc.
59 this._excludedColIdx = [];
60 this.nls = this.grid._nls;
61 this.grid.setSortInfo = function(){};
62 this.grid.setSortIndex = lang.hitch(this, '_setGridSortIndex');
63 this.grid.getSortIndex = function(){};
64 this.grid.getSortProps = lang.hitch(this, 'getSortProps');
65 if(this.grid.sortFields){
66 this._setGridSortIndex(this.grid.sortFields, null, true);
67 }
68 this.connect(this.grid.views, 'render', '_initSort');//including column resize
69 this.initCookieHandler();
70 if(this.grid.plugin('rearrange')){
71 this.subscribe("dojox/grid/rearrange/move/" + this.grid.id, lang.hitch(this, '_onColumnDnD'));
72 }else{
73 this.connect(this.grid.layout, 'moveColumn', '_onMoveColumn');
74 }
75 },
76 onStartUp: function(){
77 //overwrite base Grid functions
78 this.inherited(arguments);
79 this.connect(this.grid, 'onHeaderCellClick', '_onHeaderCellClick');
80 this.connect(this.grid, 'onHeaderCellMouseOver', '_onHeaderCellMouseOver');
81 this.connect(this.grid, 'onHeaderCellMouseOut', '_onHeaderCellMouseOut');
82 },
83 _onMoveColumn: function(sourceViewIndex, destViewIndex, cellIndex, targetIndex, before){
84 var cr = this._getCurrentRegion(),
85 idx = cr && this._getRegionHeader(cr).getAttribute('idx'),
86 c = this._headerNodes[idx],
87 sortData = this._sortData,
88 newSortData = {},
89 sortIndex, data;
90 if(cr){
91 this._blurRegion(cr);
92 this._currRegionIdx = array.indexOf(this._getRegions(), c.firstChild);
93 }
94 if(targetIndex < cellIndex){
95 for(sortIndex in sortData){
96 sortIndex = parseInt(sortIndex, 10);
97 data = sortData[sortIndex];
98 if(data){
99 if(sortIndex >= targetIndex && sortIndex < cellIndex){
100 newSortData[sortIndex + 1] = data;
101 }else if(sortIndex == cellIndex){
102 newSortData[targetIndex] = data;
103 }else{
104 newSortData[sortIndex] = data;
105 }
106 }
107 }
108 }else if(targetIndex > cellIndex + 1){
109 if(!before){
110 targetIndex++;
111 }
112 for(sortIndex in sortData){
113 sortIndex = parseInt(sortIndex, 10);
114 data = sortData[sortIndex];
115 if(data){
116 if(sortIndex > cellIndex && sortIndex < targetIndex){
117 newSortData[sortIndex - 1] = data;
118 }else if(sortIndex == cellIndex){
119 newSortData[targetIndex - 1] = data;
120 }else{
121 newSortData[sortIndex] = data;
122 }
123 }
124 }
125 }
126 this._sortData = newSortData;
127 this._initSort(false);
128 },
129 _onColumnDnD: function(type, mapping){
130 // summary:
131 // Update nested sorting after column moved
132 if(type !== 'col'){return;}
133 var m = mapping, obj = {}, d = this._sortData, p;
134 var cr = this._getCurrentRegion();
135 this._blurRegion(cr);
136 var idx = this._getRegionHeader(cr).getAttribute('idx');
137 for(p in m){
138 if(d[p]){
139 obj[m[p]] = d[p];
140 delete d[p];
141 }
142 if(p === idx){
143 idx = m[p];
144 }
145 }
146 for(p in obj){
147 d[p] = obj[p];
148 }
149 var c = this._headerNodes[idx];
150 this._currRegionIdx = array.indexOf(this._getRegions(), c.firstChild);
151 this._initSort(false);
152 },
153 _setGridSortIndex: function(inIndex, inAsc, noRefresh){
154 if(lang.isArray(inIndex)){
155 var i, d, cell;
156 for(i = 0; i < inIndex.length; i++){
157 d = inIndex[i];
158 cell = this.grid.getCellByField(d.attribute);
159 if(!cell){
160 console.warn('Invalid sorting option, column ', d.attribute, ' not found.');
161 return;
162 }
163 if(cell['nosort'] || !this.grid.canSort(cell.index, cell.field)){
164 console.warn('Invalid sorting option, column ', d.attribute, ' is unsortable.');
165 return;
166 }
167 }
168 this.clearSort();
169 array.forEach(inIndex, function(d, i){
170 cell = this.grid.getCellByField(d.attribute);
171 this.setSortData(cell.index, 'index', i);
172 this.setSortData(cell.index, 'order', d.descending ? 'desc': 'asc');
173 }, this);
174 }else if(!isNaN(inIndex)){
175 if(inAsc === undefined){ return; }//header click from base DataGrid
176 this.setSortData(inIndex, 'order', inAsc ? 'asc' : 'desc');
177 }else{
178 return;
179 }
180 this._updateSortDef();
181 if(!noRefresh){
182 this.grid.sort();
183 }
184 },
185 getSortProps: function(){
186 // summary:
187 // Overwritten, see DataGrid.getSortProps()
188 return this._sortDef.length ? this._sortDef : null;
189 },
190 _initSort: function(postSort){
191 // summary:
192 // Initiate sorting
193 var g = this.grid, n = g.domNode, len = this._sortDef.length;
194 html.toggleClass(n, 'dojoxGridSorted', !!len);
195 html.toggleClass(n, 'dojoxGridSingleSorted', len === 1);
196 html.toggleClass(n, 'dojoxGridNestSorted', len > 1);
197 if(len > 0){
198 this._currMainSort = this._sortDef[0].descending ? 'desc' : 'asc';
199 }
200 var idx, excluded = this._excludedCoIdx = [];//reset it
201 //cache column index of hidden, un-sortable or indirect selection
202 this._headerNodes = query("th", g.viewsHeaderNode).forEach(function(n){
203 idx = parseInt(n.getAttribute('idx'), 10);
204 if(html.style(n, 'display') === 'none' || g.layout.cells[idx]['nosort'] || (g.canSort && !g.canSort(idx, g.layout.cells[idx]['field']))){
205 excluded.push(idx);
206 }
207 });
208 this._headerNodes.forEach(this._initHeaderNode, this);
209 this._initFocus();
210 if(postSort){
211 this._focusHeader();
212 }
213 },
214 _initHeaderNode: function(node){
215 // summary:
216 // Initiate sort for each header cell node
217 html.toggleClass(node, 'dojoxGridSortNoWrap', true);
218 var sortNode = query('.dojoxGridSortNode', node)[0];
219 if(sortNode){
220 html.toggleClass(sortNode, 'dojoxGridSortNoWrap', true);
221 }
222 if(array.indexOf(this._excludedCoIdx, node.getAttribute('idx')) >= 0){
223 html.addClass(node, 'dojoxGridNoSort');
224 return;
225 }
226 if(!query('.dojoxGridSortBtn', node).length){
227 //clear any previous connects
228 this._connects = array.filter(this._connects, function(conn){
229 if(conn._sort){
230 connect.disconnect(conn);
231 return false;
232 }
233 return true;
234 });
235 var n = html.create('a', {
236 className: 'dojoxGridSortBtn dojoxGridSortBtnNested',
237 title: string.substitute(this.nls.sortingState, [this.nls.nestedSort, this.nls.ascending]),
238 innerHTML: '1'
239 }, node.firstChild, 'last');
240 n.onmousedown = evt.stop;
241 n = html.create('a', {
242 className: 'dojoxGridSortBtn dojoxGridSortBtnSingle',
243 title: string.substitute(this.nls.sortingState, [this.nls.singleSort, this.nls.ascending])
244 }, node.firstChild, 'last');
245 n.onmousedown = evt.stop;
246 }else{
247 //deal with small height grid which doesn't re-render the grid after refresh
248 var a1 = query('.dojoxGridSortBtnSingle', node)[0];
249 var a2 = query('.dojoxGridSortBtnNested', node)[0];
250 a1.className = 'dojoxGridSortBtn dojoxGridSortBtnSingle';
251 a2.className = 'dojoxGridSortBtn dojoxGridSortBtnNested';
252 a2.innerHTML = '1';
253 html.removeClass(node, 'dojoxGridCellShowIndex');
254 html.removeClass(node.firstChild, 'dojoxGridSortNodeSorted');
255 html.removeClass(node.firstChild, 'dojoxGridSortNodeAsc');
256 html.removeClass(node.firstChild, 'dojoxGridSortNodeDesc');
257 html.removeClass(node.firstChild, 'dojoxGridSortNodeMain');
258 html.removeClass(node.firstChild, 'dojoxGridSortNodeSub');
259 }
260 this._updateHeaderNodeUI(node);
261 },
262 _onHeaderCellClick: function(e){
263 // summary:
264 // See dojox.grid.enhanced._Events._onHeaderCellClick()
265 this._focusRegion(e.target);
266 if(html.hasClass(e.target, 'dojoxGridSortBtn')){
267 this._onSortBtnClick(e);
268 evt.stop(e);
269 this._focusRegion(this._getCurrentRegion());
270 }
271 },
272 _onHeaderCellMouseOver: function(e){
273 // summary:
274 // See dojox.grid._Events._onHeaderCellMouseOver()
275 // When user mouseover other columns than sorted column in a single sorted grid,
276 // We need to show 1 in the sorted column
277 if(!e.cell){return; }
278 if(this._sortDef.length > 1){ return; }
279 if(this._sortData[e.cellIndex] && this._sortData[e.cellIndex].index === 0){ return; }
280 var p;
281 for(p in this._sortData){
282 if(this._sortData[p] && this._sortData[p].index === 0){
283 html.addClass(this._headerNodes[p], 'dojoxGridCellShowIndex');
284 break;
285 }
286 }
287 if(!html.hasClass(win.body(), 'dijit_a11y')){ return; }
288 //a11y support
289 var i = e.cell.index, node = e.cellNode;
290 var singleSortBtn = query('.dojoxGridSortBtnSingle', node)[0];
291 var nestedSortBtn = query('.dojoxGridSortBtnNested', node)[0];
292
293 var sortMode = 'none';
294 if(html.hasClass(this.grid.domNode, 'dojoxGridSingleSorted')){
295 sortMode = 'single';
296 }else if(html.hasClass(this.grid.domNode, 'dojoxGridNestSorted')){
297 sortMode = 'nested';
298 }
299 var nestedIndex = nestedSortBtn.getAttribute('orderIndex');
300 if(nestedIndex === null || nestedIndex === undefined){
301 nestedSortBtn.setAttribute('orderIndex', nestedSortBtn.innerHTML);
302 nestedIndex = nestedSortBtn.innerHTML;
303 }
304 if(this.isAsc(i)){
305 nestedSortBtn.innerHTML = nestedIndex + this._a11yText.dojoxGridDescending;
306 }else if(this.isDesc(i)){
307 nestedSortBtn.innerHTML = nestedIndex + this._a11yText.dojoxGridUnsortedTip;
308 }else{
309 nestedSortBtn.innerHTML = nestedIndex + this._a11yText.dojoxGridAscending;
310 }
311 if(this._currMainSort === 'none'){
312 singleSortBtn.innerHTML = this._a11yText.dojoxGridAscending;
313 }else if(this._currMainSort === 'asc'){
314 singleSortBtn.innerHTML = this._a11yText.dojoxGridDescending;
315 }else if(this._currMainSort === 'desc'){
316 singleSortBtn.innerHTML = this._a11yText.dojoxGridUnsortedTip;
317 }
318 },
319 _onHeaderCellMouseOut: function(e){
320 // summary:
321 // See dojox.grid.enhanced._Events._onHeaderCellMouseOut()
322 var p;
323 for(p in this._sortData){
324 if(this._sortData[p] && this._sortData[p].index === 0){
325 html.removeClass(this._headerNodes[p], 'dojoxGridCellShowIndex');
326 break;
327 }
328 }
329 },
330 _onSortBtnClick: function(e){
331 // summary:
332 // If the click target is single sort button, do single sort.
333 // Else if the click target is nested sort button, do nest sort.
334 // Otherwise return.
335 var cellIdx = e.cell.index;
336 if(html.hasClass(e.target, 'dojoxGridSortBtnSingle')){
337 this._prepareSingleSort(cellIdx);
338 }else if(html.hasClass(e.target, 'dojoxGridSortBtnNested')){
339 this._prepareNestedSort(cellIdx);
340 }else{
341 return;
342 }
343 evt.stop(e);
344 this._doSort(cellIdx);
345 },
346 _doSort: function(cellIdx){
347 if(!this._sortData[cellIdx] || !this._sortData[cellIdx].order){
348 this.setSortData(cellIdx, 'order', 'asc'); //no sorting data
349 }else if(this.isAsc(cellIdx)){
350 this.setSortData(cellIdx, 'order', 'desc'); //change to 'desc'
351 }else if(this.isDesc(cellIdx)){
352 this.removeSortData(cellIdx); //remove from sorting sequence
353 }
354 this._updateSortDef();
355 this.grid.sort();
356 this._initSort(true);
357 },
358 setSortData: function(cellIdx, attr, value){
359 // summary:
360 // Set sorting data for a column.
361 var sd = this._sortData[cellIdx];
362 if(!sd){
363 sd = this._sortData[cellIdx] = {};
364 }
365 sd[attr] = value;
366 },
367 removeSortData: function(cellIdx){
368 var d = this._sortData, i = d[cellIdx].index, p;
369 delete d[cellIdx];
370 for(p in d){
371 if(d[p].index > i){
372 d[p].index--;
373 }
374 }
375 },
376 _prepareSingleSort: function(cellIdx){
377 // summary:
378 // Prepare the single sort, also called main sort, this will clear any existing sorting and just sort the grid by current column.
379 var d = this._sortData, p;
380 for(p in d){
381 delete d[p];
382 }
383 this.setSortData(cellIdx, 'index', 0);
384 this.setSortData(cellIdx, 'order', this._currMainSort === 'none' ? null : this._currMainSort);
385 if(!this._sortData[cellIdx] || !this._sortData[cellIdx].order){
386 this._currMainSort = 'asc';
387 }else if(this.isAsc(cellIdx)){
388 this._currMainSort = 'desc';
389 }else if(this.isDesc(cellIdx)){
390 this._currMainSort = 'none';
391 }
392 },
393 _prepareNestedSort: function(cellIdx){
394 // summary:
395 // Prepare the nested sorting, this will order the column on existing sorting result.
396 var i = this._sortData[cellIdx] ? this._sortData[cellIdx].index : null;
397 if(i === 0 || !!i){ return; }
398 this.setSortData(cellIdx, 'index', this._sortDef.length);
399 },
400 _updateSortDef: function(){
401 this._sortDef.length = 0;
402 var d = this._sortData, p;
403 for(p in d){
404 this._sortDef[d[p].index] = {
405 attribute: this.grid.layout.cells[p].field,
406 descending: d[p].order === 'desc'
407 };
408 }
409 },
410 _updateHeaderNodeUI: function(node){
411 // summary:
412 // Update the column header UI based on current sorting state.
413 // Show indicator of the sorting order of the column, no order no indicator
414 var cell = this._getCellByNode(node);
415 var cellIdx = cell.index;
416 var data = this._sortData[cellIdx];
417 var sortNode = query('.dojoxGridSortNode', node)[0];
418 var singleSortBtn = query('.dojoxGridSortBtnSingle', node)[0];
419 var nestedSortBtn = query('.dojoxGridSortBtnNested', node)[0];
420
421 html.toggleClass(singleSortBtn, 'dojoxGridSortBtnAsc', this._currMainSort === 'asc');
422 html.toggleClass(singleSortBtn, 'dojoxGridSortBtnDesc', this._currMainSort === 'desc');
423 if(this._currMainSort === 'asc'){
424 singleSortBtn.title = string.substitute(this.nls.sortingState, [this.nls.singleSort, this.nls.descending]);
425 }else if(this._currMainSort === 'desc'){
426 singleSortBtn.title = string.substitute(this.nls.sortingState, [this.nls.singleSort, this.nls.unsorted]);
427 }else{
428 singleSortBtn.title = string.substitute(this.nls.sortingState, [this.nls.singleSort, this.nls.ascending]);
429 }
430
431 var _this = this;
432 function setWaiState(){
433 var columnInfo = 'Column ' + (cell.index + 1) + ' ' + cell.field;
434 var orderState = 'none';
435 var orderAction = 'ascending';
436 if(data){
437 orderState = data.order === 'asc' ? 'ascending' : 'descending';
438 orderAction = data.order === 'asc' ? 'descending' : 'none';
439 }
440 var a11ySingleLabel = columnInfo + ' - is sorted by ' + orderState;
441 var a11yNestedLabel = columnInfo + ' - is nested sorted by ' + orderState;
442 var a11ySingleLabelHover = columnInfo + ' - choose to sort by ' + orderAction;
443 var a11yNestedLabelHover = columnInfo + ' - choose to nested sort by ' + orderAction;
444
445 singleSortBtn.setAttribute("aria-label", a11ySingleLabel);
446 nestedSortBtn.setAttribute("aria-label", a11yNestedLabel);
447
448 var handles = [
449 _this.connect(singleSortBtn, "onmouseover", function(){
450 singleSortBtn.setAttribute("aria-label", a11ySingleLabelHover);
451 }),
452 _this.connect(singleSortBtn, "onmouseout", function(){
453 singleSortBtn.setAttribute("aria-label", a11ySingleLabel);
454 }),
455 _this.connect(nestedSortBtn, "onmouseover", function(){
456 nestedSortBtn.setAttribute("aria-label", a11yNestedLabelHover);
457 }),
458 _this.connect(nestedSortBtn, "onmouseout", function(){
459 nestedSortBtn.setAttribute("aria-label", a11yNestedLabel);
460 })
461 ];
462 array.forEach(handles, function(handle){ handle._sort = true; });
463 }
464 setWaiState();
465
466 var a11y = html.hasClass(win.body(), "dijit_a11y");
467 if(!data){
468 nestedSortBtn.innerHTML = this._sortDef.length + 1;
469 nestedSortBtn.title = string.substitute(this.nls.sortingState, [this.nls.nestedSort, this.nls.ascending]);
470 if(a11y){sortNode.innerHTML = this._a11yText.dojoxGridUnsortedTip;}
471 return;
472 }
473 if(data.index || (data.index === 0 && this._sortDef.length > 1)){
474 nestedSortBtn.innerHTML = data.index + 1;
475 }
476 html.addClass(sortNode, 'dojoxGridSortNodeSorted');
477 if(this.isAsc(cellIdx)){
478 html.addClass(sortNode, 'dojoxGridSortNodeAsc');
479 nestedSortBtn.title = string.substitute(this.nls.sortingState, [this.nls.nestedSort, this.nls.descending]);
480 if(a11y){sortNode.innerHTML = this._a11yText.dojoxGridAscendingTip;}
481 }else if(this.isDesc(cellIdx)){
482 html.addClass(sortNode, 'dojoxGridSortNodeDesc');
483 nestedSortBtn.title = string.substitute(this.nls.sortingState, [this.nls.nestedSort, this.nls.unsorted]);
484 if(a11y){sortNode.innerHTML = this._a11yText.dojoxGridDescendingTip;}
485 }
486 html.addClass(sortNode, (data.index === 0 ? 'dojoxGridSortNodeMain' : 'dojoxGridSortNodeSub'));
487 },
488 isAsc: function(cellIndex){
489 return this._sortData[cellIndex].order === 'asc';
490 },
491 isDesc: function(cellIndex){
492 return this._sortData[cellIndex].order === 'desc';
493 },
494 _getCellByNode: function(node){
495 var i;
496 for(i = 0; i < this._headerNodes.length; i++){
497 if(this._headerNodes[i] === node){
498 return this.grid.layout.cells[i];
499 }
500 }
501 return null;
502 },
503 clearSort: function(){
504 this._sortData = {};
505 this._sortDef.length = 0;
506 },
507
508 //persistence
509 initCookieHandler: function(){
510 if(this.grid.addCookieHandler){
511 this.grid.addCookieHandler({
512 name: "sortOrder",
513 onLoad: lang.hitch(this, '_loadNestedSortingProps'),
514 onSave: lang.hitch(this, '_saveNestedSortingProps')
515 });
516 }
517 },
518 _loadNestedSortingProps: function(sortInfo, grid){
519 this._setGridSortIndex(sortInfo);
520 },
521 _saveNestedSortingProps: function(grid){
522 return this.getSortProps();
523 },
524
525 //focus & keyboard
526 _initFocus: function(){
527 var f = this.focus = this.grid.focus;
528 this._focusRegions = this._getRegions();
529 if(!this._headerArea){
530 var area = this._headerArea = f.getArea('header');
531 area.onFocus = f.focusHeader = lang.hitch(this, '_focusHeader');
532 area.onBlur = f.blurHeader = f._blurHeader = lang.hitch(this, '_blurHeader');
533 area.onMove = lang.hitch(this, '_onMove');
534 area.onKeyDown = lang.hitch(this, '_onKeyDown');
535 area._regions = [];
536 area.getRegions = null;
537 this.connect(this.grid, 'onBlur', '_blurHeader');
538 }
539 },
540 _focusHeader: function(e){
541 // summary:
542 // Overwritten, see _FocusManager.focusHeader()
543 // delayed: Boolean
544 // If called from "this.focus._delayedHeaderFocus()"
545 if(this._currRegionIdx === -1){
546 this._onMove(0, 1, null);
547 }else{
548 var region = this._getCurrentRegion();
549 this._focusRegion(region);
550 //keep grid body scrolled by header
551 var view = this._getRegionView(region);
552 view.scrollboxNode.scrollLeft = view.headerNode.scrollLeft;
553 }
554 try{
555 if(e){
556 evt.stop(e);
557 }
558 }catch(e){}
559 return true;
560 },
561 _blurHeader: function(e){
562 this._blurRegion(this._getCurrentRegion());
563 return true;
564 },
565 _onMove: function(rowStep, colStep, e){
566 var curr = this._currRegionIdx || 0, regions = this._focusRegions;
567 var region = regions[curr + colStep];
568 if(!region){
569 return;
570 }else if(html.style(region, 'display') === 'none' || html.style(region, 'visibility') === 'hidden'){
571 //if the region is invisible, keep finding next
572 this._onMove(rowStep, colStep + (colStep > 0 ? 1 : -1), e);
573 return;
574 }
575 this._focusRegion(region);
576 //keep grid body scrolled by header
577 var view = this._getRegionView(region);
578 view.scrollboxNode.scrollLeft = view.headerNode.scrollLeft;
579 },
580 _onKeyDown: function(e, isBubble){
581 if(isBubble){
582 switch(e.keyCode){
583 case keys.ENTER:
584 case keys.SPACE:
585 if(html.hasClass(e.target, 'dojoxGridSortBtnSingle') ||
586 html.hasClass(e.target, 'dojoxGridSortBtnNested')){
587 this._onSortBtnClick(e);
588 }
589 }
590 }
591 },
592 _getRegionView: function(region){
593 var header = region;
594 while(header && !html.hasClass(header, 'dojoxGridHeader')){ header = header.parentNode; }
595 if(header){
596 return array.filter(this.grid.views.views, function(view){
597 return view.headerNode === header;
598 })[0] || null;
599 }
600 return null;
601 },
602 _getRegions: function(){
603 var regions = [], cells = this.grid.layout.cells;
604 this._headerNodes.forEach(function(n, i){
605 if(html.style(n, 'display') === 'none'){return;}
606 if(cells[i]['isRowSelector']){
607 regions.push(n);
608 return;
609 }
610 query('.dojoxGridSortNode,.dojoxGridSortBtnNested,.dojoxGridSortBtnSingle', n).forEach(function(node){
611 node.setAttribute('tabindex', 0);
612 regions.push(node);
613 });
614 },this);
615 return regions;
616 },
617 _focusRegion: function(region){
618 // summary:
619 // Focus the given region
620 if(!region){return;}
621 var currRegion = this._getCurrentRegion();
622 if(currRegion && region !== currRegion){
623 this._blurRegion(currRegion);
624 }
625 var header = this._getRegionHeader(region);
626 html.addClass(header, 'dojoxGridCellSortFocus');
627 if(html.hasClass(region, 'dojoxGridSortNode')){
628 html.addClass(region, 'dojoxGridSortNodeFocus');
629 }else if(html.hasClass(region, 'dojoxGridSortBtn')){
630 html.addClass(region, 'dojoxGridSortBtnFocus');
631 }
632 //For invisible nodes, IE will throw error when calling focus().
633 try{
634 region.focus();
635 }catch(e){}
636 this.focus.currentArea('header');
637 this._currRegionIdx = array.indexOf(this._focusRegions, region);
638 },
639 _blurRegion: function(region){
640 if(!region){return;}
641 var header = this._getRegionHeader(region);
642 html.removeClass(header, 'dojoxGridCellSortFocus');
643 if(html.hasClass(region, 'dojoxGridSortNode')){
644 html.removeClass(region, 'dojoxGridSortNodeFocus');
645 }else if(html.hasClass(region, 'dojoxGridSortBtn')){
646 html.removeClass(region, 'dojoxGridSortBtnFocus');
647 }
648 region.blur();
649 },
650 _getCurrentRegion: function(){
651 return this._focusRegions ? this._focusRegions[this._currRegionIdx] : null;
652 },
653 _getRegionHeader: function(region){
654 while(region && !html.hasClass(region, 'dojoxGridCell')){
655 region = region.parentNode;
656 }
657 return region;
658 },
659 destroy: function(){
660 this._sortDef = this._sortData = null;
661 this._headerNodes = this._focusRegions = null;
662 this.inherited(arguments);
663 }
664 });
665
666 EnhancedGrid.registerPlugin(NestedSorting);
667
668 return NestedSorting;
669 });