[Commits] r356 - in core/trunk/geoext: lib/GeoExt/data tests/lib/GeoExt/data
commits at geoext.org
commits at geoext.org
Tue Apr 7 21:11:26 CEST 2009
Author: tschaub
Date: 2009-04-07 21:11:26 +0200 (Tue, 07 Apr 2009)
New Revision: 356
Modified:
core/trunk/geoext/lib/GeoExt/data/LayerStore.js
core/trunk/geoext/tests/lib/GeoExt/data/LayerStore.html
Log:
If bound to a map, the layer store will respect the order of layers on the map. Reordering map layers reorders records in the store. Inserting a record in the store places the layer in the appropriate order on the map. r=ahocevar (closes #37)
Modified: core/trunk/geoext/lib/GeoExt/data/LayerStore.js
===================================================================
--- core/trunk/geoext/lib/GeoExt/data/LayerStore.js 2009-04-07 09:04:15 UTC (rev 355)
+++ core/trunk/geoext/lib/GeoExt/data/LayerStore.js 2009-04-07 19:11:26 UTC (rev 356)
@@ -100,6 +100,7 @@
if(!this.map) {
this.map = map;
map.events.on({
+ "changelayer": this.onChangeLayer,
"addlayer": this.onAddLayer,
"removelayer": this.onRemoveLayer,
scope: this
@@ -119,6 +120,7 @@
unbind: function() {
if(this.map) {
this.map.events.un({
+ "changelayer": this.onChangeLayer,
"addlayer": this.onAddLayer,
"removelayer": this.onRemoveLayer,
scope: this
@@ -131,6 +133,37 @@
this.map = null;
}
},
+
+ /**
+ * Method: onChangeLayer
+ * Handler for layer changes. When layer order changes, this moves the
+ * appropriate record within the store.
+ *
+ * Parameters:
+ * evt - {Object}
+ */
+ onChangeLayer: function(evt) {
+ var layer = evt.layer;
+ if(evt.property === "order") {
+ if(!this._adding && !this._removing) {
+ 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
@@ -176,8 +209,13 @@
onAdd: function(store, records, index) {
if(!this._adding) {
this._adding = true;
- for(var i=0; i<records.length; ++i) {
- this.map.addLayer(records[i].get("layer"));
+ var layer;
+ for(var i=records.length-1; i>=0; --i) {
+ layer = records[i].get("layer");
+ this.map.addLayer(layer);
+ if(index !== this.map.layers.length-1) {
+ this.map.setLayerIndex(layer, index);
+ }
}
delete this._adding;
}
Modified: core/trunk/geoext/tests/lib/GeoExt/data/LayerStore.html
===================================================================
--- core/trunk/geoext/tests/lib/GeoExt/data/LayerStore.html 2009-04-07 09:04:15 UTC (rev 355)
+++ core/trunk/geoext/tests/lib/GeoExt/data/LayerStore.html 2009-04-07 19:11:26 UTC (rev 356)
@@ -94,8 +94,96 @@
}
+ 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");
+
+ map.destroy();
+
+ }
+
+ function test_insert(t) {
+
+ t.plan(10);
+
+ 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 d = new OpenLayers.Layer.Vector("d");
+
+ var store = new GeoExt.data.LayerStore({
+ map: map
+ });
+
+ store.add(store.reader.readRecords([a, b, c]).records);
+
+ // insert d into second position
+ store.insert(1, store.reader.readRecords([d]).records);
+
+ t.eq(store.getCount(), 4, "[a, d, b, c] four layers in store");
+ t.eq(store.getAt(0).get("layer").name, "a", "[a, d, b, c] first layer correct in store");
+ t.eq(store.getAt(1).get("layer").name, "d", "[a, d, b, c] second layer correct in store");
+ t.eq(store.getAt(2).get("layer").name, "b", "[a, d, b, c] third layer correct in store");
+ t.eq(store.getAt(3).get("layer").name, "c", "[a, d, b, c] fourth layer correct in store");
+ t.eq(map.layers.length, 4, "[a, d, b, c] four layers on map");
+ t.eq(map.layers[0].name, "a", "[a, d, b, c] first layer correct on map");
+ t.eq(map.layers[1].name, "d", "[a, d, b, c] second layer correct on map");
+ t.eq(map.layers[2].name, "b", "[a, d, b, c] third layer correct on map");
+ t.eq(map.layers[3].name, "c", "[a, d, b, c] fourth layer correct on map");
+
+ map.destroy();
+
+ }
+
+
</script>
+ </head>
<body>
- <div id="mappanel"></div>
+ <div id="mappanel" style="width:400px; height:300px"></div>
</body>
</html>
More information about the Commits
mailing list