[Commits] r340 - in sandbox/opengeo/geoexplorer: . lib/GeoExt/data tests/lib/GeoExt/data

commits at geoext.org commits at geoext.org
Fri Apr 3 00:57:37 CEST 2009


Author: tschaub
Date: 2009-04-03 00:57:37 +0200 (Fri, 03 Apr 2009)
New Revision: 340

Modified:
   sandbox/opengeo/geoexplorer/lib/GeoExt/data/LayerStore.js
   sandbox/opengeo/geoexplorer/modifications.txt
   sandbox/opengeo/geoexplorer/tests/lib/GeoExt/data/LayerStore.html
Log:
Listen for layer reordering on the map and update store.  (see #37)

Modified: sandbox/opengeo/geoexplorer/lib/GeoExt/data/LayerStore.js
===================================================================
--- sandbox/opengeo/geoexplorer/lib/GeoExt/data/LayerStore.js	2009-04-02 22:29:20 UTC (rev 339)
+++ sandbox/opengeo/geoexplorer/lib/GeoExt/data/LayerStore.js	2009-04-02 22:57:37 UTC (rev 340)
@@ -96,6 +96,7 @@
         if(!this.map) {
             this.map = map;
             map.events.on({
+                "changelayer": this.onLayerChange,
                 "addlayer": this.onAddLayer,
                 "removelayer": this.onRemoveLayer,
                 scope: this
@@ -115,6 +116,7 @@
     unbind: function() {
         if(this.map) {
             this.map.events.un({
+                "changelayer": this.onLayerChange,
                 "addlayer": this.onAddLayer,
                 "removelayer": this.onRemoveLayer,
                 scope: this
@@ -127,6 +129,35 @@
             this.map = null;
         }
     },
+    
+    /**
+     * Method: onLayerChange
+     * Handler for layer changes.  When layer order changes, this moves the
+     *     appropriate record within the store.
+     *
+     * Parameters:
+     * evt - {Object}
+     */
+    onLayerChange: function(evt) {
+        var layer = evt.layer;
+        if(evt.property === "order") {
+            var layerIndex = this.map.getLayerIndex(layer);
+            var recordIndex = this.findBy(function(rec, id) {
+                return rec.get("layer") === layer;
+            });
+            if(recordIndex > -1) {
+                if(layerIndex !== recordIndex) {
+                    var record = this.getAt(recordIndex);
+                    this._removing = true;
+                    this.remove(record);
+                    delete this._removing;
+                    this._adding = true;
+                    this.insert(layerIndex, [record]);
+                    delete this._adding;
+                }
+            }
+        }
+    },
    
     /**
      * Method: onAddLayer

Modified: sandbox/opengeo/geoexplorer/modifications.txt
===================================================================
--- sandbox/opengeo/geoexplorer/modifications.txt	2009-04-02 22:29:20 UTC (rev 339)
+++ sandbox/opengeo/geoexplorer/modifications.txt	2009-04-02 22:57:37 UTC (rev 340)
@@ -8,5 +8,6 @@
  * Clone feature and layer in record.clone (see #34)
  * Set allOverlays in MapPanel (see #35)
  * Take care not to add records twice in LayerStore (see #36)
+ * Respect layer order in map and store (see #37)
  * Merge changes from trunk up to r319
 

Modified: sandbox/opengeo/geoexplorer/tests/lib/GeoExt/data/LayerStore.html
===================================================================
--- sandbox/opengeo/geoexplorer/tests/lib/GeoExt/data/LayerStore.html	2009-04-02 22:29:20 UTC (rev 339)
+++ sandbox/opengeo/geoexplorer/tests/lib/GeoExt/data/LayerStore.html	2009-04-02 22:57:37 UTC (rev 340)
@@ -94,6 +94,57 @@
             
         }
         
+        function test_reorder(t) {
+            
+            t.plan(24);
+            
+            var map = new OpenLayers.Map("mappanel");
+            var a = new OpenLayers.Layer.Vector("a");
+            var b = new OpenLayers.Layer.Vector("b");
+            var c = new OpenLayers.Layer.Vector("c");
+
+            var store = new GeoExt.data.LayerStore({
+                map: map
+            });
+            
+            store.add(store.reader.readRecords([a, b, c]).records);
+
+            t.eq(store.getCount(), 3, "[a, b, c] three layers in store");
+            t.eq(store.getAt(0).get("layer").name, "a", "[a, b, c] first layer correct in store");
+            t.eq(store.getAt(1).get("layer").name, "b", "[a, b, c] second layer correct in store");
+            t.eq(store.getAt(2).get("layer").name, "c", "[a, b, c] third layer correct in store");
+            t.eq(map.layers.length, 3, "[a, b, c] three layers on map");
+            t.eq(map.layers[0].name, "a", "[a, b, c] first layer correct on map");
+            t.eq(map.layers[1].name, "b", "[a, b, c] second layer correct on map");
+            t.eq(map.layers[2].name, "c", "[a, b, c] third layer correct on map");
+            
+            // move "a" to second position
+            map.setLayerIndex(a, 1);
+
+            t.eq(store.getCount(), 3, "[b, a, c] three layers in store");
+            t.eq(store.getAt(0).get("layer").name, "b", "[b, c, a] first layer correct in store");
+            t.eq(store.getAt(1).get("layer").name, "a", "[b, c, a] second layer correct in store");
+            t.eq(store.getAt(2).get("layer").name, "c", "[b, c, a] third layer correct in store");
+            t.eq(map.layers.length, 3, "[a, b, c] three layers on map");
+            t.eq(map.layers[0].name, "b", "[b, c, a] first layer correct on map");
+            t.eq(map.layers[1].name, "a", "[b, c, a] second layer correct on map");
+            t.eq(map.layers[2].name, "c", "[b, c, a] third layer correct on map");
+            
+            // move "c" to first position
+            map.setLayerIndex(c, 0);
+
+            t.eq(store.getCount(), 3, "[c, b, a] three layers in store");
+            t.eq(store.getAt(0).get("layer").name, "c", "[c, b, a] first layer correct in store");
+            t.eq(store.getAt(1).get("layer").name, "b", "[c, b, a] second layer correct in store");
+            t.eq(store.getAt(2).get("layer").name, "a", "[c, b, a] third layer correct in store");
+            t.eq(map.layers.length, 3, "[c, b, a] three layers on map");
+            t.eq(map.layers[0].name, "c", "[c, b, a] first layer correct on map");
+            t.eq(map.layers[1].name, "b", "[c, b, a] second layer correct on map");
+            t.eq(map.layers[2].name, "a", "[c, b, a] third layer correct on map");
+            
+            
+        }
+
     </script>
   <body>
     <div id="mappanel"></div>



More information about the Commits mailing list