Ticket #37: geoext-37.patch

File geoext-37.patch, 7.5 kB (added by ahocevar, 1 year ago)

fixed test html (needs width and height css

  • tests/lib/GeoExt/data/LayerStore.html

    old new  
    9494             
    9595        } 
    9696         
     97        function test_reorder(t) { 
     98             
     99            t.plan(24); 
     100             
     101            var map = new OpenLayers.Map("mappanel"); 
     102            var a = new OpenLayers.Layer.Vector("a"); 
     103            var b = new OpenLayers.Layer.Vector("b"); 
     104            var c = new OpenLayers.Layer.Vector("c"); 
     105 
     106            var store = new GeoExt.data.LayerStore({ 
     107                map: map 
     108            }); 
     109             
     110            store.add(store.reader.readRecords([a, b, c]).records); 
     111 
     112            t.eq(store.getCount(), 3, "[a, b, c] three layers in store"); 
     113            t.eq(store.getAt(0).get("layer").name, "a", "[a, b, c] first layer correct in store"); 
     114            t.eq(store.getAt(1).get("layer").name, "b", "[a, b, c] second layer correct in store"); 
     115            t.eq(store.getAt(2).get("layer").name, "c", "[a, b, c] third layer correct in store"); 
     116            t.eq(map.layers.length, 3, "[a, b, c] three layers on map"); 
     117            t.eq(map.layers[0].name, "a", "[a, b, c] first layer correct on map"); 
     118            t.eq(map.layers[1].name, "b", "[a, b, c] second layer correct on map"); 
     119            t.eq(map.layers[2].name, "c", "[a, b, c] third layer correct on map"); 
     120             
     121            // move "a" to second position 
     122            map.setLayerIndex(a, 1); 
     123 
     124            t.eq(store.getCount(), 3, "[b, a, c] three layers in store"); 
     125            t.eq(store.getAt(0).get("layer").name, "b", "[b, c, a] first layer correct in store"); 
     126            t.eq(store.getAt(1).get("layer").name, "a", "[b, c, a] second layer correct in store"); 
     127            t.eq(store.getAt(2).get("layer").name, "c", "[b, c, a] third layer correct in store"); 
     128            t.eq(map.layers.length, 3, "[a, b, c] three layers on map"); 
     129            t.eq(map.layers[0].name, "b", "[b, c, a] first layer correct on map"); 
     130            t.eq(map.layers[1].name, "a", "[b, c, a] second layer correct on map"); 
     131            t.eq(map.layers[2].name, "c", "[b, c, a] third layer correct on map"); 
     132             
     133            // move "c" to first position 
     134            map.setLayerIndex(c, 0); 
     135 
     136            t.eq(store.getCount(), 3, "[c, b, a] three layers in store"); 
     137            t.eq(store.getAt(0).get("layer").name, "c", "[c, b, a] first layer correct in store"); 
     138            t.eq(store.getAt(1).get("layer").name, "b", "[c, b, a] second layer correct in store"); 
     139            t.eq(store.getAt(2).get("layer").name, "a", "[c, b, a] third layer correct in store"); 
     140            t.eq(map.layers.length, 3, "[c, b, a] three layers on map"); 
     141            t.eq(map.layers[0].name, "c", "[c, b, a] first layer correct on map"); 
     142            t.eq(map.layers[1].name, "b", "[c, b, a] second layer correct on map"); 
     143            t.eq(map.layers[2].name, "a", "[c, b, a] third layer correct on map"); 
     144             
     145            map.destroy(); 
     146             
     147        } 
     148         
     149        function test_insert(t) { 
     150             
     151            t.plan(10); 
     152             
     153            var map = new OpenLayers.Map("mappanel"); 
     154            var a = new OpenLayers.Layer.Vector("a"); 
     155            var b = new OpenLayers.Layer.Vector("b"); 
     156            var c = new OpenLayers.Layer.Vector("c"); 
     157            var d = new OpenLayers.Layer.Vector("d"); 
     158 
     159            var store = new GeoExt.data.LayerStore({ 
     160                map: map 
     161            }); 
     162             
     163            store.add(store.reader.readRecords([a, b, c]).records); 
     164             
     165            // insert d into second position 
     166            store.insert(1, store.reader.readRecords([d]).records); 
     167             
     168            t.eq(store.getCount(), 4, "[a, d, b, c] four layers in store"); 
     169            t.eq(store.getAt(0).get("layer").name, "a", "[a, d, b, c] first layer correct in store"); 
     170            t.eq(store.getAt(1).get("layer").name, "d", "[a, d, b, c] second layer correct in store"); 
     171            t.eq(store.getAt(2).get("layer").name, "b", "[a, d, b, c] third layer correct in store"); 
     172            t.eq(store.getAt(3).get("layer").name, "c", "[a, d, b, c] fourth layer correct in store"); 
     173            t.eq(map.layers.length, 4, "[a, d, b, c] four layers on map"); 
     174            t.eq(map.layers[0].name, "a", "[a, d, b, c] first layer correct on map"); 
     175            t.eq(map.layers[1].name, "d", "[a, d, b, c] second layer correct on map"); 
     176            t.eq(map.layers[2].name, "b", "[a, d, b, c] third layer correct on map"); 
     177            t.eq(map.layers[3].name, "c", "[a, d, b, c] fourth layer correct on map"); 
     178             
     179            map.destroy(); 
     180 
     181        } 
     182         
     183 
    97184    </script> 
     185  </head>   
    98186  <body> 
    99     <div id="mappanel"></div> 
     187    <div id="mappanel" style="width:400px; height:300px"></div> 
    100188  </body> 
    101189</html> 
  • lib/GeoExt/data/LayerStore.js

    old new  
    100100        if(!this.map) { 
    101101            this.map = map; 
    102102            map.events.on({ 
     103                "changelayer": this.onChangeLayer, 
    103104                "addlayer": this.onAddLayer, 
    104105                "removelayer": this.onRemoveLayer, 
    105106                scope: this 
     
    119120    unbind: function() { 
    120121        if(this.map) { 
    121122            this.map.events.un({ 
     123                "changelayer": this.onChangeLayer, 
    122124                "addlayer": this.onAddLayer, 
    123125                "removelayer": this.onRemoveLayer, 
    124126                scope: this 
     
    131133            this.map = null; 
    132134        } 
    133135    }, 
     136     
     137    /** 
     138     * Method: onChangeLayer 
     139     * Handler for layer changes.  When layer order changes, this moves the 
     140     *     appropriate record within the store. 
     141     * 
     142     * Parameters: 
     143     * evt - {Object} 
     144     */ 
     145    onChangeLayer: function(evt) { 
     146        var layer = evt.layer; 
     147        if(evt.property === "order") { 
     148            if(!this._adding && !this._removing) { 
     149                var layerIndex = this.map.getLayerIndex(layer); 
     150                var recordIndex = this.findBy(function(rec, id) { 
     151                    return rec.get("layer") === layer; 
     152                }); 
     153                if(recordIndex > -1) { 
     154                    if(layerIndex !== recordIndex) { 
     155                        var record = this.getAt(recordIndex); 
     156                        this._removing = true; 
     157                        this.remove(record); 
     158                        delete this._removing; 
     159                        this._adding = true; 
     160                        this.insert(layerIndex, [record]); 
     161                        delete this._adding; 
     162                    } 
     163                } 
     164            } 
     165        } 
     166    }, 
    134167    
    135168    /** 
    136169     * Method: onAddLayer 
     
    176209    onAdd: function(store, records, index) { 
    177210        if(!this._adding) { 
    178211            this._adding = true; 
    179             for(var i=0; i<records.length; ++i) { 
    180                 this.map.addLayer(records[i].get("layer")); 
     212            var layer; 
     213            for(var i=records.length-1; i>=0; --i) { 
     214                layer = records[i].get("layer"); 
     215                this.map.addLayer(layer); 
     216                if(index !== this.map.layers.length-1) { 
     217                    this.map.setLayerIndex(layer, index); 
     218                } 
    181219            } 
    182220            delete this._adding; 
    183221        }