source: Dev/branches/rest-dojo-ui/client/dojox/grid/TreeSelection.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 5.0 KB
Line 
1define([
2        "../main",
3        "dojo/_base/declare",
4        "dojo/_base/array",
5        "dojo/_base/lang",
6        "dojo/dom-attr",
7        "dojo/query",
8        "./DataSelection"
9], function(dojox, declare, array, lang, domAttr, query, DataSelection){
10
11return declare("dojox.grid.TreeSelection", DataSelection, {
12        setMode: function(mode){
13                this.selected = {};
14                this.sorted_sel = [];
15                this.sorted_ltos = {};
16                this.sorted_stol = {};
17                DataSelection.prototype.setMode.call(this, mode);
18        },
19        addToSelection: function(inItemOrIndex){
20                if(this.mode == 'none'){ return; }
21                var idx = null;
22                if(typeof inItemOrIndex == "number" || typeof inItemOrIndex == "string"){
23                        idx = inItemOrIndex;
24                }else{
25                        idx = this.grid.getItemIndex(inItemOrIndex);
26                }
27                if(this.selected[idx]){
28                        this.selectedIndex = idx;
29                }else{
30                        if(this.onCanSelect(idx) !== false){
31                                this.selectedIndex = idx;
32                                var rowNodes = query("tr[dojoxTreeGridPath='" + idx + "']", this.grid.domNode);
33                                if(rowNodes.length){
34                                        domAttr.set(rowNodes[0], "aria-selected", "true");
35                                }
36                                this._beginUpdate();
37                                this.selected[idx] = true;
38                                this._insertSortedSelection(idx);
39                                //this.grid.onSelected(idx);
40                                this.onSelected(idx);
41                                //this.onSetSelected(idx, true);
42                                this._endUpdate();
43                        }
44                }
45        },
46        deselect: function(inItemOrIndex){
47                if(this.mode == 'none'){ return; }
48                var idx = null;
49                if(typeof inItemOrIndex == "number" || typeof inItemOrIndex == "string"){
50                        idx = inItemOrIndex;
51                }else{
52                        idx = this.grid.getItemIndex(inItemOrIndex);
53                }
54                if(this.selectedIndex == idx){
55                        this.selectedIndex = -1;
56                }
57                if(this.selected[idx]){
58                        if(this.onCanDeselect(idx) === false){
59                                return;
60                        }
61                        var rowNodes = query("tr[dojoxTreeGridPath='" + idx + "']", this.grid.domNode);
62                        if(rowNodes.length){
63                                domAttr.set(rowNodes[0], "aria-selected", "false");
64                        }
65                        this._beginUpdate();
66                        delete this.selected[idx];
67                        this._removeSortedSelection(idx);
68                        //this.grid.onDeselected(idx);
69                        this.onDeselected(idx);
70                        //this.onSetSelected(idx, false);
71                        this._endUpdate();
72                }
73        },
74        getSelected: function(){
75                var result = [];
76                for(var i in this.selected){
77                        if(this.selected[i]){
78                                result.push(this.grid.getItem(i));
79                        }
80                }
81                return result;
82        },
83        getSelectedCount: function(){
84                var c = 0;
85                for(var i in this.selected){
86                        if(this.selected[i]){
87                                c++;
88                        }
89                }
90                return c;
91        },
92        _bsearch: function(v){
93                var o = this.sorted_sel;
94                var h = o.length - 1, l = 0, m;
95                while(l<=h){
96                        var cmp = this._comparePaths(o[m = (l + h) >> 1], v);
97                        if(cmp < 0){ l = m + 1; continue; }
98                        if(cmp > 0){ h = m - 1; continue; }
99                        return m;
100                }
101                return cmp < 0 ? m - cmp : m;
102        },
103        _comparePaths: function(a, b){
104                for(var i=0, l=(a.length < b.length ? a.length : b.length); i<l; i++){
105                        if(a[i]<b[i]){ return -1; }
106                        if(a[i]>b[i]){ return 1; }
107                }
108                if(a.length<b.length){ return -1; }
109                if(a.length>b.length){ return 1; }
110                return 0;
111        },
112        _insertSortedSelection: function(index){
113                index = String(index);
114                var s = this.sorted_sel;
115                var sl = this.sorted_ltos;
116                var ss = this.sorted_stol;
117
118                var lpath = index.split('/');
119                lpath = array.map(lpath, function(item){ return parseInt(item, 10); });
120                sl[lpath] = index;
121                ss[index] = lpath;
122
123                if(s.length === 0){
124                        s.push(lpath);
125                        return;
126                }
127                if(s.length==1){
128                        var cmp = this._comparePaths(s[0], lpath);
129                        if(cmp==1){ s.unshift(lpath); }
130                        else{ s.push(lpath); }
131                        return;
132                }
133
134                var idx = this._bsearch(lpath);
135                this.sorted_sel.splice(idx, 0, lpath);
136        },
137        _removeSortedSelection: function(index){
138                index = String(index);
139                var s = this.sorted_sel;
140                var sl = this.sorted_ltos;
141                var ss = this.sorted_stol;
142
143                if(s.length === 0){
144                        return;
145                }
146
147                var lpath = ss[index];
148                if(!lpath){ return; }
149
150                var idx = this._bsearch(lpath);
151                if(idx > -1){
152                        delete sl[lpath];
153                        delete ss[index];
154                        s.splice(idx, 1);
155                }
156        },
157        getFirstSelected: function(){
158                if(!this.sorted_sel.length||this.mode == 'none'){ return -1; }
159                var fpath = this.sorted_sel[0];
160                if(!fpath){
161                        return -1;
162                }
163                fpath = this.sorted_ltos[fpath];
164                if(!fpath){
165                        return -1;
166                }
167                return fpath;
168        },
169        getNextSelected: function(inPrev){
170                if(!this.sorted_sel.length||this.mode == 'none'){ return -1; }
171                inPrev = String(inPrev);
172                var prevPath = this.sorted_stol[inPrev];
173                if(!prevPath){ return -1; }
174
175                var idx = this._bsearch(prevPath);
176                var lpath = this.sorted_sel[idx+1];
177                if(!lpath){
178                        return -1;
179                }
180                return this.sorted_ltos[lpath];
181        },
182        _range: function(inFrom, inTo, func){
183                if(!lang.isString(inFrom) && inFrom < 0){
184                        inFrom = inTo;
185                }
186                var cells = this.grid.layout.cells,
187                        store = this.grid.store,
188                        grid = this.grid;
189                inFrom = new dojox.grid.TreePath(String(inFrom), grid);
190                inTo = new dojox.grid.TreePath(String(inTo), grid);
191
192                if(inFrom.compare(inTo) > 0){
193                        var tmp = inFrom;
194                        inFrom = inTo;
195                        inTo = tmp;
196                }
197
198                var inFromStr = inFrom._str, inToStr = inTo._str;
199
200                // select/deselect the first
201                func(inFromStr);
202
203                var p = inFrom;
204                while((p = p.next())){
205                        if(p._str == inToStr){
206                                break;
207                        }
208                        func(p._str);
209                }
210
211                // select/deselect the last
212                func(inToStr);
213        }
214});
215});
Note: See TracBrowser for help on using the repository browser.