Changeset 10

Show
Ignore:
Timestamp:
08/25/08 03:26:33 (4 years ago)
Author:
tsuckowhomberg
Message:
  • fixed: "selectRow()" would allow selecting indexes greater than the "totalLength"-property of the store; added condition to check whether the index is out of bounds (closes google issue 5)
Location:
trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/CHANGELOG

    r8 r10  
    1 Version 0.1.2 
     1 
     2- fixes: 
     3 - BufferedRowSelectionModel: "selectRow()" would allow selecting indexes greater than 
     4the "totalLength"-property of the store; added condition to check whether the index is 
     5out of bounds (closes google issue 5) 
     6 
     7Version 0.1.2 
    2825-August-2008 
    39 
  • trunk/src/BufferedRowSelectionModel.js

    r4 r10  
    22 * Ext.ux.grid.BufferedRowSelectionModel V0.1 
    33 * Copyright(c) 2007, http://www.siteartwork.de 
    4  *  
     4 * 
    55 * Licensed under the terms of the Open Source LGPL 3.0 
    66 * http://www.gnu.org/licenses/lgpl.html 
     
    1212 
    1313Ext.ux.grid.BufferedRowSelectionModel = function(config) { 
    14      
     14 
    1515 
    1616    this.addEvents({ 
    1717        /** 
    18          * The selection dirty event will be triggered in case records were  
    19          * inserted/ removed at view indexes that may affect the current  
    20          * selection ranges which are only represented by view indexes, but not  
     18         * The selection dirty event will be triggered in case records were 
     19         * inserted/ removed at view indexes that may affect the current 
     20         * selection ranges which are only represented by view indexes, but not 
    2121         * current record-ids 
    2222         */ 
    2323        'selectiondirty' : true 
    24     });         
    25  
    26          
    27      
    28      
     24    }); 
     25 
     26 
     27 
     28 
    2929    Ext.apply(this, config); 
    30      
     30 
    3131    this.bufferedSelections = {}; 
    32      
     32 
    3333    this.pendingSelections = {}; 
    34      
     34 
    3535    Ext.ux.grid.BufferedRowSelectionModel.superclass.constructor.call(this); 
    36      
     36 
    3737}; 
    3838 
    3939Ext.extend(Ext.ux.grid.BufferedRowSelectionModel, Ext.grid.RowSelectionModel, { 
    40      
    41      
     40 
     41 
    4242 // private 
    4343    initEvents : function() 
    4444    { 
    4545        Ext.ux.grid.BufferedRowSelectionModel.superclass.initEvents.call(this); 
    46          
     46 
    4747        this.grid.store.on('add',            this.onAdd,            this); 
    4848        this.grid.store.on('selectionsload', this.onSelectionsLoad, this); 
    49     },     
    50      
    51      
    52      
     49    }, 
     50 
     51 
     52 
    5353    // private 
    5454    onRefresh : function() 
     
    6666            this.fireEvent("selectionchange", this); 
    6767        }*/ 
    68     },     
    69  
    70     
    71      
     68    }, 
     69 
     70 
     71 
    7272    /** 
    7373     * Callback is called when a row gets removed in the view. The process to 
     
    8383     * If r defaults to <tt>null</tt> and index is within the pending selections 
    8484     * range, the selectionchange event will be called, too. 
    85      * Additionally, the method will shift all selections and trigger the  
     85     * Additionally, the method will shift all selections and trigger the 
    8686     * selectiondirty event if any selections are pending. 
    8787     * 
     
    8989    onRemove : function(v, index, r) 
    9090    { 
    91         // if index equals to Number.MIN_VALUE or Number.MAX_VALUE, mark current  
     91        // if index equals to Number.MIN_VALUE or Number.MAX_VALUE, mark current 
    9292        // pending selections as dirty 
    9393        if ((index == Number.MIN_VALUE || index == Number.MAX_VALUE)) { 
     
    9595            this.fireEvent('selectiondirty', this, index, r); 
    9696            return; 
    97         }           
    98          
     97        } 
     98 
    9999        var viewIndex    = index; 
    100100        var fire         = this.bufferedSelections[viewIndex] === true; 
    101101        var ranges       = this.getPendingSelections(); 
    102102        var rangesLength = ranges.length; 
    103          
    104          
     103 
     104 
    105105        delete this.bufferedSelections[viewIndex]; 
    106         delete this.pendingSelections[viewIndex];  
    107            
     106        delete this.pendingSelections[viewIndex]; 
     107 
    108108        if (r) { 
    109109            this.selections.remove(r); 
    110         }  
    111                 
     110        } 
     111 
    112112        if (rangesLength == 0) { 
    113113            this.shiftSelections(viewIndex, -1); 
    114         } else {        
     114        } else { 
    115115            var s = ranges[0]; 
    116116            var e = ranges[rangesLength-1]; 
     
    119119                    this.shiftSelections(viewIndex, -1); 
    120120                } 
    121             }        
    122         } 
    123          
     121            } 
     122        } 
     123 
    124124        if (fire) { 
    125             this.fireEvent('selectionchange', this);  
     125            this.fireEvent('selectionchange', this); 
    126126        } 
    127127    }, 
     
    130130    /** 
    131131     * If records where added to the store, this method will work as a callback. 
    132      * The method gets usually called <b>after</b> the onAdd method of the according  
     132     * The method gets usually called <b>after</b> the onAdd method of the according 
    133133     * view was called. 
    134134     * Selections will be shifted down if, and only if, the listeners for the 
     
    140140        var ranges       = this.getPendingSelections(); 
    141141        var rangesLength = ranges.length; 
    142          
    143         // if index equals to Number.MIN_VALUE or Number.MAX_VALUE, mark current  
     142 
     143        // if index equals to Number.MIN_VALUE or Number.MAX_VALUE, mark current 
    144144        // pending selections as dirty 
    145145        if ((index == Number.MIN_VALUE || index == Number.MAX_VALUE)) { 
    146              
     146 
    147147            // we may shift selections if there are no pendning selections. Everything 
    148148            // in the current buffer range will be shifted up! 
     
    150150                this.shiftSelections(this.grid.getStore().bufferRange[0], records.length); 
    151151            } 
    152          
     152 
    153153            this.fireEvent('selectiondirty', this, index, records.length); 
    154154            return; 
    155         }         
    156          
     155        } 
     156 
    157157        if (rangesLength == 0) { 
    158158            this.shiftSelections(this.grid.getStore().bufferRange[0]+index, records.length); 
     
    160160        } 
    161161 
    162         // it is safe to say that the selection is dirty when the inserted index  
    163         // is less or equal to the first selection range index or less or equal  
     162        // it is safe to say that the selection is dirty when the inserted index 
     163        // is less or equal to the first selection range index or less or equal 
    164164        // to the last selection range index 
    165165        var s         = ranges[0]; 
     
    172172        } 
    173173    }, 
    174      
    175    
    176      
     174 
     175 
     176 
    177177    /** 
    178178     * Shifts current/pending selections. This method can be used when rows where 
     
    186186        var newRequests   = {}; 
    187187        var totalLength = this.grid.store.totalLength; 
    188          
     188 
    189189        this.last = false; 
    190          
     190 
    191191        for (var i in this.bufferedSelections) { 
    192192            index    = parseInt(i); 
     
    195195                break; 
    196196            } 
    197              
     197 
    198198            if (index >= startRow) { 
    199199                newSelections[newIndex] = true; 
    200                  
     200 
    201201                if (this.pendingSelections[i]) { 
    202202                    newRequests[newIndex] = true; 
    203203                } 
    204                  
     204 
    205205            } else { 
    206206                newSelections[i] = true; 
    207                  
     207 
    208208                if (this.pendingSelections[i]) { 
    209209                    newRequests[i] = true; 
    210210                } 
    211211            } 
    212         }  
    213          
     212        } 
     213 
    214214        this.bufferedSelections  = newSelections; 
    215215        this.pendingSelections   = newRequests; 
    216216    }, 
    217      
    218     /** 
    219      * 
    220      * @param {Array} records The records that have been loaded  
     217 
     218    /** 
     219     * 
     220     * @param {Array} records The records that have been loaded 
    221221     * @param {Array} ranges  An array representing the model index ranges the 
    222222     *                        reords have been loaded for. 
     
    225225    { 
    226226        this.pendingSelections = {}; 
    227          
     227 
    228228        this.selections.addAll(records); 
    229         
    230     },     
    231      
     229 
     230    }, 
     231 
    232232    /** 
    233233     * Returns true if there is a next record to select 
     
    238238        return this.last !== false && (this.last+1) < this.grid.store.getTotalCount(); 
    239239    }, 
    240          
     240 
    241241    /** 
    242242     * Gets the number of selected rows. 
     
    246246    { 
    247247        var sels = this.bufferedSelections; 
    248          
     248 
    249249        var c = 0; 
    250250        for (var i in sels) { 
    251             c++;     
    252         } 
    253          
     251            c++; 
     252        } 
     253 
    254254        return c; 
    255     },     
    256      
     255    }, 
     256 
    257257    /** 
    258258     * Returns True if the specified row is selected. 
     
    265265            return this.bufferedSelections[index] === true; 
    266266        } 
    267          
     267 
    268268        var r = index; 
    269269        return (r && this.selections.key(r.id) ? true : false); 
    270     },     
    271      
    272  
    273      
     270    }, 
     271 
     272 
     273 
    274274    /** 
    275275     * Deselects a row. 
     
    282282            this.last = false; 
    283283        } 
    284          
     284 
    285285        if(this.lastActive == index){ 
    286286            this.lastActive = false; 
    287287        } 
    288288        var r = this.grid.store.getAt(index); 
    289          
     289 
    290290        delete this.pendingSelections[index]; 
    291291        delete this.bufferedSelections[index]; 
     
    299299        this.fireEvent("selectionchange", this); 
    300300    }, 
    301      
    302          
     301 
     302 
    303303    /** 
    304304     * Selects a row. 
     
    308308    selectRow : function(index, keepExisting, preventViewNotify) 
    309309    { 
    310         // ignore store count, since this is our buffer and the rowIndex may indeed 
    311         // be greater than the count of records in the store 
    312         if(this.last === index || this.locked || index < 0) return;// || index >= this.grid.store.getCount())) return; 
    313          
    314          
     310        if(this.last === index 
     311           || this.locked 
     312           || index < 0 
     313           || index >= this.grid.store.getTotalCount()) { 
     314            return; 
     315        } 
     316 
     317 
     318 
    315319        var r = this.grid.store.getAt(index); 
    316         
     320 
    317321        if(this.fireEvent("beforerowselect", this, index, keepExisting, r) !== false){ 
    318322            if(!keepExisting || this.singleSelect){ 
    319323                this.clearSelections(); 
    320324            } 
    321              
     325 
    322326            if (r) { 
    323327                this.selections.add(r); 
     
    326330                this.pendingSelections[index] = true; 
    327331            } 
    328              
    329             this.bufferedSelections[index] = true;     
    330              
     332 
     333            this.bufferedSelections[index] = true; 
     334 
    331335            this.last = this.lastActive = index; 
    332              
     336 
    333337            if(!preventViewNotify){ 
    334338                this.grid.getView().onRowSelect(index); 
     
    338342        } 
    339343    }, 
    340      
     344 
    341345    clearPendingSelection : function(index) 
    342346    { 
    343         var r = this.grid.store.getAt(index);    
     347        var r = this.grid.store.getAt(index); 
    344348        if (!r) { 
    345349            return; 
    346350        } 
    347351        this.selections.add(r); 
    348         delete this.pendingSelections[index];  
    349     }, 
    350      
     352        delete this.pendingSelections[index]; 
     353    }, 
     354 
    351355    getPendingSelections : function(asRange) 
    352356    { 
     
    355359        var currentRange  = 0; 
    356360        var tmpArray      = []; 
    357          
     361 
    358362        for (var i in this.pendingSelections) { 
    359363            tmpArray.push(parseInt(i)); 
    360364        } 
    361          
     365 
    362366        tmpArray.sort(function(o1,o2){ 
    363367            if (o1 > o2) { 
     
    369373            } 
    370374        }); 
    371          
     375 
    372376        if (asRange === false) { 
    373377            return tmpArray; 
    374378        } 
    375          
     379 
    376380        var max_i = tmpArray.length; 
    377          
     381 
    378382        if (max_i == 0) { 
    379383            return []; 
    380384        } 
    381          
     385 
    382386        ranges[currentRange] = [tmpArray[0], tmpArray[0]]; 
    383387        for (var i = 0, max_i = max_i-1; i < max_i; i++) { 
     
    389393            } 
    390394        } 
    391          
     395 
    392396        return ranges; 
    393397    }, 
     
    406410            }, this);*/ 
    407411            s.clear(); 
    408              
     412 
    409413            for (var i in this.bufferedSelections) { 
    410                 this.deselectRow(i);         
    411             } 
    412              
     414                this.deselectRow(i); 
     415            } 
     416 
    413417        }else{ 
    414418            this.selections.clear(); 
     
    418422        this.last = false; 
    419423    }, 
    420      
    421      
    422     /** 
    423      * Selects a range of rows. All rows in between startRow and endRow are also  
     424 
     425 
     426    /** 
     427     * Selects a range of rows. All rows in between startRow and endRow are also 
    424428     * selected. 
    425429     * 
     
    433437            return; 
    434438        } 
    435          
     439 
    436440        if(!keepExisting) { 
    437441            this.clearSelections(); 
    438442        } 
    439          
     443 
    440444        if (startRow <= endRow) { 
    441445            for(var i = startRow; i <= endRow; i++) { 
     
    446450                this.selectRow(i, true); 
    447451            } 
    448         }   
     452        } 
    449453    } 
    450    
     454 
    451455}); 
    452456