[Commits] r1045 - in core/trunk/geoext: lib/GeoExt/widgets lib/GeoExt/widgets/tree tests/lib/GeoExt/widgets/tree

commits at geoext.org commits at geoext.org
Thu Jun 11 20:10:24 CEST 2009


Author: tschaub
Date: 2009-06-11 20:10:24 +0200 (Thu, 11 Jun 2009)
New Revision: 1045

Modified:
   core/trunk/geoext/lib/GeoExt/widgets/LegendPanel.js
   core/trunk/geoext/lib/GeoExt/widgets/tree/LayerContainer.js
   core/trunk/geoext/tests/lib/GeoExt/widgets/tree/LayerContainer.html
Log:
Correcting the store add listeners so they properly order things when multiple records are added at once.  r=tcoulter (closes #86)

Modified: core/trunk/geoext/lib/GeoExt/widgets/LegendPanel.js
===================================================================
--- core/trunk/geoext/lib/GeoExt/widgets/LegendPanel.js	2009-06-11 18:06:33 UTC (rev 1044)
+++ core/trunk/geoext/lib/GeoExt/widgets/LegendPanel.js	2009-06-11 18:10:24 UTC (rev 1045)
@@ -98,6 +98,7 @@
         var store = this.layerStore;
         var count = store.getCount();
         var panelIndex = -1;
+        var legendCount = this.items ? this.items.length : 0;
         for(var i=count-1; i>=0; --i) {
             var layer = store.getAt(i).get("layer");
             var legendGenerator = GeoExt[
@@ -106,7 +107,7 @@
             if(layer.displayInLayerSwitcher && legendGenerator &&
                 (store.getAt(i).get("hideInLegend") !== true)) {
                     ++panelIndex;
-                    if(index === i) {
+                    if(index === i || panelIndex > legendCount-1) {
                         break;
                     }
             }
@@ -150,7 +151,7 @@
      *  :param index: ``Integer`` The index of the inserted record.
      */
     onStoreAdd: function(store, records, index) {
-        var panelIndex = this.recordIndexToPanelIndex(index);
+        var panelIndex = this.recordIndexToPanelIndex(index+records.length-1);
         for (var i=0, len=records.length; i<len; i++) {
             this.addLegend(records[i], panelIndex);
         }

Modified: core/trunk/geoext/lib/GeoExt/widgets/tree/LayerContainer.js
===================================================================
--- core/trunk/geoext/lib/GeoExt/widgets/tree/LayerContainer.js	2009-06-11 18:06:33 UTC (rev 1044)
+++ core/trunk/geoext/lib/GeoExt/widgets/tree/LayerContainer.js	2009-06-11 18:10:24 UTC (rev 1045)
@@ -80,7 +80,7 @@
      */
     onStoreAdd: function(store, records, index) {
         if(!this._reordering) {
-            var nodeIndex = this.recordIndexToNodeIndex(index);
+            var nodeIndex = this.recordIndexToNodeIndex(index+records.length-1);
             for(var i=0; i<records.length; ++i) {
                 this.addLayerNode(records[i], nodeIndex);
             }
@@ -117,11 +117,12 @@
     recordIndexToNodeIndex: function(index) {
         var store = this.layerStore;
         var count = store.getCount();
+        var nodeCount = this.childNodes.length;
         var nodeIndex = -1;
         for(var i=count-1; i>=0; --i) {
             if(store.getAt(i).get("layer").displayInLayerSwitcher) {
                 ++nodeIndex;
-                if(index === i) {
+                if(index === i || nodeIndex > nodeCount-1) {
                     break;
                 }
             }

Modified: core/trunk/geoext/tests/lib/GeoExt/widgets/tree/LayerContainer.html
===================================================================
--- core/trunk/geoext/tests/lib/GeoExt/widgets/tree/LayerContainer.html	2009-06-11 18:06:33 UTC (rev 1044)
+++ core/trunk/geoext/tests/lib/GeoExt/widgets/tree/LayerContainer.html	2009-06-11 18:10:24 UTC (rev 1045)
@@ -54,9 +54,73 @@
             t.ok(node.firstChild.layer === layer, "child layer is correct");
 
             node.destroy();
+            //map.destroy();
             
         }
         
+        function test_order(t) {
+            
+            t.plan(16);
+            
+            var map = new OpenLayers.Map({
+                div: "map",
+                allOverlays: true
+            });
+            
+            var store = new GeoExt.data.LayerStore({
+                map: map
+            });
+            
+            var root = new GeoExt.tree.LayerContainer({
+                layerStore: store
+            });
+            
+            var panel = new Ext.tree.TreePanel({
+                renderTo: document.body,
+                root: root
+            });
+            
+            var a = new OpenLayers.Layer("a");
+            var b = new OpenLayers.Layer("b");
+            var c = new OpenLayers.Layer("c");
+            var d = new OpenLayers.Layer("d");
+            
+            var reader = new GeoExt.data.LayerReader();
+            
+            // add two records to empty root
+            store.add(reader.readRecords([a, b]).records);
+            t.eq(root.childNodes.length, 2, "[a, b] two records added");
+            t.eq(root.childNodes[0].layer.name, "b", "[a, b] last layer drawn at top of root");
+            t.eq(root.childNodes[1].layer.name, "a", "[a, b] first layer drawn at bottom of root");
+            
+            // add two records to root with two existing child nodes
+            store.add(reader.readRecords([c, d]).records);
+            t.eq(root.childNodes.length, 4, "[a, b, c, d] four records total");
+            t.eq(root.childNodes[0].layer.name, "d", "[a, b, c, d] last layer drawn at top of root");
+            t.eq(root.childNodes[1].layer.name, "c", "[a, b, c, d] third layer drawn at correct index");
+            t.eq(root.childNodes[2].layer.name, "b", "[a, b, c, d] second layer drawn at correct index");
+            t.eq(root.childNodes[3].layer.name, "a", "[a, b, c, d] first layer drawn at bottom of root");
+            
+            // remove the first two layers in draw order
+            store.remove(store.getAt(0));
+            store.remove(store.getAt(0));
+            t.eq(root.childNodes.length, 2, "[c, d] two records total");
+            t.eq(root.childNodes[0].layer.name, "d", "[c, d] last layer drawn at top of root");
+            t.eq(root.childNodes[1].layer.name, "c", "[c, d] first layer drawn at bottom of root");
+            
+            // insert two records in the middle
+            store.insert(1, reader.readRecords([a, b]).records);
+            t.eq(root.childNodes.length, 4, "[c, a, b, d] four records total");
+            t.eq(root.childNodes[0].layer.name, "d", "[c, a, b, d] last layer drawn at top of root");
+            t.eq(root.childNodes[1].layer.name, "b", "[c, a, b, d] third layer drawn at correct index");
+            t.eq(root.childNodes[2].layer.name, "a", "[c, a, b, d] second layer drawn at correct index");
+            t.eq(root.childNodes[3].layer.name, "c", "[c, a, b, d] first layer drawn at bottom of root");
+            
+            root.destroy();
+            //map.destroy();
+            
+        }
+        
         </script>
     </head>
     <body>



More information about the Commits mailing list