Fix #6874 - Grid column resize
[roojs1] / Roo / bootstrap / Table / RowSelectionModel.js
1
2 /**
3  *  @deprecated
4  *
5  *  
6  */
7
8 Roo.bootstrap.Table.RowSelectionModel = function(config){
9     Roo.apply(this, config);
10     this.selections = new Roo.util.MixedCollection(false, function(o){
11         return o.id;
12     });
13
14     this.last = false;
15     this.lastActive = false;
16
17     this.addEvents({
18         /**
19              * @event selectionchange
20              * Fires when the selection changes
21              * @param {SelectionModel} this
22              */
23             "selectionchange" : true,
24         /**
25              * @event afterselectionchange
26              * Fires after the selection changes (eg. by key press or clicking)
27              * @param {SelectionModel} this
28              */
29             "afterselectionchange" : true,
30         /**
31              * @event beforerowselect
32              * Fires when a row is selected being selected, return false to cancel.
33              * @param {SelectionModel} this
34              * @param {Number} rowIndex The selected index
35              * @param {Boolean} keepExisting False if other selections will be cleared
36              */
37             "beforerowselect" : true,
38         /**
39              * @event rowselect
40              * Fires when a row is selected.
41              * @param {SelectionModel} this
42              * @param {Number} rowIndex The selected index
43              * @param {Roo.data.Record} r The record
44              */
45             "rowselect" : true,
46         /**
47              * @event rowdeselect
48              * Fires when a row is deselected.
49              * @param {SelectionModel} this
50              * @param {Number} rowIndex The selected index
51              */
52         "rowdeselect" : true
53     });
54     Roo.bootstrap.Table.RowSelectionModel.superclass.constructor.call(this);
55     this.locked = false;
56  };
57
58 Roo.extend(Roo.bootstrap.Table.RowSelectionModel, Roo.bootstrap.Table.AbstractSelectionModel,  {
59     /**
60      * @cfg {Boolean} singleSelect
61      * True to allow selection of only one row at a time (defaults to false)
62      */
63     singleSelect : false,
64
65     // private
66     initEvents : function()
67     {
68
69         //if(!this.grid.enableDragDrop && !this.grid.enableDrag){
70         //    this.growclickrid.on("mousedown", this.handleMouseDown, this);
71         //}else{ // allow click to work like normal
72          //   this.grid.on("rowclick", this.handleDragableRowClick, this);
73         //}
74         //this.grid.on("rowdblclick", this.handleMouseDBClick, this);
75         this.grid.on("rowclick", this.handleMouseDown, this);
76         
77         this.rowNav = new Roo.KeyNav(this.grid.getGridEl(), {
78             "up" : function(e){
79                 if(!e.shiftKey){
80                     this.selectPrevious(e.shiftKey);
81                 }else if(this.last !== false && this.lastActive !== false){
82                     var last = this.last;
83                     this.selectRange(this.last,  this.lastActive-1);
84                     this.grid.getView().focusRow(this.lastActive);
85                     if(last !== false){
86                         this.last = last;
87                     }
88                 }else{
89                     this.selectFirstRow();
90                 }
91                 this.fireEvent("afterselectionchange", this);
92             },
93             "down" : function(e){
94                 if(!e.shiftKey){
95                     this.selectNext(e.shiftKey);
96                 }else if(this.last !== false && this.lastActive !== false){
97                     var last = this.last;
98                     this.selectRange(this.last,  this.lastActive+1);
99                     this.grid.getView().focusRow(this.lastActive);
100                     if(last !== false){
101                         this.last = last;
102                     }
103                 }else{
104                     this.selectFirstRow();
105                 }
106                 this.fireEvent("afterselectionchange", this);
107             },
108             scope: this
109         });
110         this.grid.store.on('load', function(){
111             this.selections.clear();
112         },this);
113         /*
114         var view = this.grid.view;
115         view.on("refresh", this.onRefresh, this);
116         view.on("rowupdated", this.onRowUpdated, this);
117         view.on("rowremoved", this.onRemove, this);
118         */
119     },
120
121     // private
122     onRefresh : function()
123     {
124         var ds = this.grid.store, i, v = this.grid.view;
125         var s = this.selections;
126         s.each(function(r){
127             if((i = ds.indexOfId(r.id)) != -1){
128                 v.onRowSelect(i);
129             }else{
130                 s.remove(r);
131             }
132         });
133     },
134
135     // private
136     onRemove : function(v, index, r){
137         this.selections.remove(r);
138     },
139
140     // private
141     onRowUpdated : function(v, index, r){
142         if(this.isSelected(r)){
143             v.onRowSelect(index);
144         }
145     },
146
147     /**
148      * Select records.
149      * @param {Array} records The records to select
150      * @param {Boolean} keepExisting (optional) True to keep existing selections
151      */
152     selectRecords : function(records, keepExisting)
153     {
154         if(!keepExisting){
155             this.clearSelections();
156         }
157             var ds = this.grid.store;
158         for(var i = 0, len = records.length; i < len; i++){
159             this.selectRow(ds.indexOf(records[i]), true);
160         }
161     },
162
163     /**
164      * Gets the number of selected rows.
165      * @return {Number}
166      */
167     getCount : function(){
168         return this.selections.length;
169     },
170
171     /**
172      * Selects the first row in the grid.
173      */
174     selectFirstRow : function(){
175         this.selectRow(0);
176     },
177
178     /**
179      * Select the last row.
180      * @param {Boolean} keepExisting (optional) True to keep existing selections
181      */
182     selectLastRow : function(keepExisting){
183         //this.selectRow(this.grid.dataSource.getCount() - 1, keepExisting);
184         this.selectRow(this.grid.store.getCount() - 1, keepExisting);
185     },
186
187     /**
188      * Selects the row immediately following the last selected row.
189      * @param {Boolean} keepExisting (optional) True to keep existing selections
190      */
191     selectNext : function(keepExisting)
192     {
193             if(this.last !== false && (this.last+1) < this.grid.store.getCount()){
194             this.selectRow(this.last+1, keepExisting);
195             this.grid.getView().focusRow(this.last);
196         }
197     },
198
199     /**
200      * Selects the row that precedes the last selected row.
201      * @param {Boolean} keepExisting (optional) True to keep existing selections
202      */
203     selectPrevious : function(keepExisting){
204         if(this.last){
205             this.selectRow(this.last-1, keepExisting);
206             this.grid.getView().focusRow(this.last);
207         }
208     },
209
210     /**
211      * Returns the selected records
212      * @return {Array} Array of selected records
213      */
214     getSelections : function(){
215         return [].concat(this.selections.items);
216     },
217
218     /**
219      * Returns the first selected record.
220      * @return {Record}
221      */
222     getSelected : function(){
223         return this.selections.itemAt(0);
224     },
225
226
227     /**
228      * Clears all selections.
229      */
230     clearSelections : function(fast)
231     {
232         if(this.locked) {
233             return;
234         }
235         if(fast !== true){
236                 var ds = this.grid.store;
237             var s = this.selections;
238             s.each(function(r){
239                 this.deselectRow(ds.indexOfId(r.id));
240             }, this);
241             s.clear();
242         }else{
243             this.selections.clear();
244         }
245         this.last = false;
246     },
247
248
249     /**
250      * Selects all rows.
251      */
252     selectAll : function(){
253         if(this.locked) {
254             return;
255         }
256         this.selections.clear();
257         for(var i = 0, len = this.grid.store.getCount(); i < len; i++){
258             this.selectRow(i, true);
259         }
260     },
261
262     /**
263      * Returns True if there is a selection.
264      * @return {Boolean}
265      */
266     hasSelection : function(){
267         return this.selections.length > 0;
268     },
269
270     /**
271      * Returns True if the specified row is selected.
272      * @param {Number/Record} record The record or index of the record to check
273      * @return {Boolean}
274      */
275     isSelected : function(index){
276             var r = typeof index == "number" ? this.grid.store.getAt(index) : index;
277         return (r && this.selections.key(r.id) ? true : false);
278     },
279
280     /**
281      * Returns True if the specified record id is selected.
282      * @param {String} id The id of record to check
283      * @return {Boolean}
284      */
285     isIdSelected : function(id){
286         return (this.selections.key(id) ? true : false);
287     },
288
289
290     // private
291     handleMouseDBClick : function(e, t){
292         
293     },
294     // private
295     handleMouseDown : function(e, t)
296     {
297             var rowIndex = this.grid.headerShow  ? t.dom.rowIndex - 1 : t.dom.rowIndex ; // first row is header???
298         if(this.isLocked() || rowIndex < 0 ){
299             return;
300         };
301         if(e.shiftKey && this.last !== false){
302             var last = this.last;
303             this.selectRange(last, rowIndex, e.ctrlKey);
304             this.last = last; // reset the last
305             t.focus();
306     
307         }else{
308             var isSelected = this.isSelected(rowIndex);
309             //Roo.log("select row:" + rowIndex);
310             if(isSelected){
311                 this.deselectRow(rowIndex);
312             } else {
313                         this.selectRow(rowIndex, true);
314             }
315     
316             /*
317                 if(e.button !== 0 && isSelected){
318                 alert('rowIndex 2: ' + rowIndex);
319                     view.focusRow(rowIndex);
320                 }else if(e.ctrlKey && isSelected){
321                     this.deselectRow(rowIndex);
322                 }else if(!isSelected){
323                     this.selectRow(rowIndex, e.button === 0 && (e.ctrlKey || e.shiftKey));
324                     view.focusRow(rowIndex);
325                 }
326             */
327         }
328         this.fireEvent("afterselectionchange", this);
329     },
330     // private
331     handleDragableRowClick :  function(grid, rowIndex, e) 
332     {
333         if(e.button === 0 && !e.shiftKey && !e.ctrlKey) {
334             this.selectRow(rowIndex, false);
335             grid.view.focusRow(rowIndex);
336              this.fireEvent("afterselectionchange", this);
337         }
338     },
339     
340     /**
341      * Selects multiple rows.
342      * @param {Array} rows Array of the indexes of the row to select
343      * @param {Boolean} keepExisting (optional) True to keep existing selections
344      */
345     selectRows : function(rows, keepExisting){
346         if(!keepExisting){
347             this.clearSelections();
348         }
349         for(var i = 0, len = rows.length; i < len; i++){
350             this.selectRow(rows[i], true);
351         }
352     },
353
354     /**
355      * Selects a range of rows. All rows in between startRow and endRow are also selected.
356      * @param {Number} startRow The index of the first row in the range
357      * @param {Number} endRow The index of the last row in the range
358      * @param {Boolean} keepExisting (optional) True to retain existing selections
359      */
360     selectRange : function(startRow, endRow, keepExisting){
361         if(this.locked) {
362             return;
363         }
364         if(!keepExisting){
365             this.clearSelections();
366         }
367         if(startRow <= endRow){
368             for(var i = startRow; i <= endRow; i++){
369                 this.selectRow(i, true);
370             }
371         }else{
372             for(var i = startRow; i >= endRow; i--){
373                 this.selectRow(i, true);
374             }
375         }
376     },
377
378     /**
379      * Deselects a range of rows. All rows in between startRow and endRow are also deselected.
380      * @param {Number} startRow The index of the first row in the range
381      * @param {Number} endRow The index of the last row in the range
382      */
383     deselectRange : function(startRow, endRow, preventViewNotify){
384         if(this.locked) {
385             return;
386         }
387         for(var i = startRow; i <= endRow; i++){
388             this.deselectRow(i, preventViewNotify);
389         }
390     },
391
392     /**
393      * Selects a row.
394      * @param {Number} row The index of the row to select
395      * @param {Boolean} keepExisting (optional) True to keep existing selections
396      */
397     selectRow : function(index, keepExisting, preventViewNotify)
398     {
399             if(this.locked || (index < 0 || index > this.grid.store.getCount())) {
400             return;
401         }
402         if(this.fireEvent("beforerowselect", this, index, keepExisting) !== false){
403             if(!keepExisting || this.singleSelect){
404                 this.clearSelections();
405             }
406             
407             var r = this.grid.store.getAt(index);
408             //console.log('selectRow - record id :' + r.id);
409             
410             this.selections.add(r);
411             this.last = this.lastActive = index;
412             if(!preventViewNotify){
413                 var proxy = new Roo.Element(
414                                 this.grid.getRowDom(index)
415                 );
416                 proxy.addClass('bg-info info');
417             }
418             this.fireEvent("rowselect", this, index, r);
419             this.fireEvent("selectionchange", this);
420         }
421     },
422
423     /**
424      * Deselects a row.
425      * @param {Number} row The index of the row to deselect
426      */
427     deselectRow : function(index, preventViewNotify)
428     {
429         if(this.locked) {
430             return;
431         }
432         if(this.last == index){
433             this.last = false;
434         }
435         if(this.lastActive == index){
436             this.lastActive = false;
437         }
438         
439         var r = this.grid.store.getAt(index);
440         if (!r) {
441             return;
442         }
443         
444         this.selections.remove(r);
445         //.console.log('deselectRow - record id :' + r.id);
446         if(!preventViewNotify){
447         
448             var proxy = new Roo.Element(
449                 this.grid.getRowDom(index)
450             );
451             proxy.removeClass('bg-info info');
452         }
453         this.fireEvent("rowdeselect", this, index);
454         this.fireEvent("selectionchange", this);
455     },
456
457     // private
458     restoreLast : function(){
459         if(this._last){
460             this.last = this._last;
461         }
462     },
463
464     // private
465     acceptsNav : function(row, col, cm){
466         return !cm.isHidden(col) && cm.isCellEditable(col, row);
467     },
468
469     // private
470     onEditorKey : function(field, e){
471         var k = e.getKey(), newCell, g = this.grid, ed = g.activeEditor;
472         if(k == e.TAB){
473             e.stopEvent();
474             ed.completeEdit();
475             if(e.shiftKey){
476                 newCell = g.walkCells(ed.row, ed.col-1, -1, this.acceptsNav, this);
477             }else{
478                 newCell = g.walkCells(ed.row, ed.col+1, 1, this.acceptsNav, this);
479             }
480         }else if(k == e.ENTER && !e.ctrlKey){
481             e.stopEvent();
482             ed.completeEdit();
483             if(e.shiftKey){
484                 newCell = g.walkCells(ed.row-1, ed.col, -1, this.acceptsNav, this);
485             }else{
486                 newCell = g.walkCells(ed.row+1, ed.col, 1, this.acceptsNav, this);
487             }
488         }else if(k == e.ESC){
489             ed.cancelEdit();
490         }
491         if(newCell){
492             g.startEditing(newCell[0], newCell[1]);
493         }
494     }
495 });