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

commits at geoext.org commits at geoext.org
Fri Jun 12 21:57:16 CEST 2009


Author: tschaub
Date: 2009-06-12 21:57:16 +0200 (Fri, 12 Jun 2009)
New Revision: 1067

Added:
   core/trunk/geoext/tests/lib/GeoExt/widgets/LegendImage.html
Modified:
   core/trunk/geoext/lib/GeoExt/widgets/LegendImage.js
   core/trunk/geoext/tests/list-tests.html
Log:
Avoid endlessly calling onImageLoadError by registering it as a single listener each time the image source is set.  r=ahocever (closes #90)

Modified: core/trunk/geoext/lib/GeoExt/widgets/LegendImage.js
===================================================================
--- core/trunk/geoext/lib/GeoExt/widgets/LegendImage.js	2009-06-12 19:44:23 UTC (rev 1066)
+++ core/trunk/geoext/lib/GeoExt/widgets/LegendImage.js	2009-06-12 19:57:16 UTC (rev 1067)
@@ -26,19 +26,31 @@
      *  ``String``  The url of the image to load
      */
     url: null,
+    
+    /** api: config[defaultImgSrc]
+     *  ``String`` Path to image that will be used if the legend image fails
+     *  to load.  Default is Ext.BLANK_IMAGE_URL.
+     */
+    defaultImgSrc: null,
 
     /** api: config[imgCls]
      *  ``String``  Optional css class to apply to img tag
      */
     imgCls: null,
-
+    
     /** private: method[initComponent]
      *  Initializes the legend image component. 
      */
     initComponent: function() {
         GeoExt.LegendImage.superclass.initComponent.call(this);
-        this.autoEl = {tag: 'img',
-            'class': (this.imgCls ? this.imgCls : ''), src: this.url};
+        if(this.defaultImgSrc === null) {
+            this.defaultImgSrc = Ext.BLANK_IMAGE_URL;
+        }
+        this.autoEl = {
+            tag: "img",
+            "class": (this.imgCls ? this.imgCls : ""),
+            src: this.defaultImgSrc
+        };
     },
 
     /** api: method[setUrl]
@@ -47,8 +59,11 @@
      *  Sets the url of the image.
      */
     setUrl: function(url) {
+        this.url = url;
         var el = this.getEl();
         if (el) {
+            el.un("error", this.onImageLoadError, this);
+            el.on("error", this.onImageLoadError, this, {single: true});
             el.dom.src = url;
         }
     },
@@ -59,14 +74,19 @@
      */
     onRender: function(ct, position) {
         GeoExt.LegendImage.superclass.onRender.call(this, ct, position);
-        this.getEl().on('error', this.onImageLoadError, this);
+        if(this.url) {
+            this.setUrl(this.url);
+        }
     },
 
     /** private: method[onDestroy]
      *  Private method called during the destroy sequence.
      */
     onDestroy: function() {
-        this.getEl().un('error', this.onImageLoadError, this);
+        var el = this.getEl();
+        if(el) {
+            el.un("error", this.onImageLoadError, this);
+        }
         GeoExt.LegendImage.superclass.onDestroy.apply(this, arguments);
     },
     
@@ -74,9 +94,10 @@
      *  Private method called if the legend image fails loading.
      */
     onImageLoadError: function() {
-        this.getEl().dom.src = Ext.BLANK_IMAGE_URL;
+        this.getEl().dom.src = this.defaultImgSrc;
     }
 
 });
 
+
 Ext.reg('gx_legendimage', GeoExt.LegendImage);

Added: core/trunk/geoext/tests/lib/GeoExt/widgets/LegendImage.html
===================================================================
--- core/trunk/geoext/tests/lib/GeoExt/widgets/LegendImage.html	                        (rev 0)
+++ core/trunk/geoext/tests/lib/GeoExt/widgets/LegendImage.html	2009-06-12 19:57:16 UTC (rev 1067)
@@ -0,0 +1,57 @@
+<!DOCTYPE html>
+<html>
+  <head>
+    <script type="text/javascript" src="../../../../../openlayers/lib/OpenLayers.js"></script>
+    <script type="text/javascript" src="../../../../../ext/adapter/ext/ext-base.js"></script>
+    <script type="text/javascript" src="../../../../../ext/ext-all-debug.js"></script>
+    <script type="text/javascript" src="../../../../lib/GeoExt.js"></script>
+
+    <script type="text/javascript">
+
+        function test_constructor(t) {
+            t.plan(2);
+            
+            var legend;
+            
+            // create a legend with the default config
+            legend = new GeoExt.LegendImage();
+            t.eq(legend.defaultImgSrc, Ext.BLANK_IMAGE_URL, "defaultImgSrc defaults to Ext.BLANK_IMAGE_URL");
+            legend.destroy();
+            
+            // create a legend with a custom defaultImgSrc
+            legend = new GeoExt.LegendImage({defaultImgSrc: "foo"});
+            t.eq(legend.defaultImgSrc, "foo", "defaultImgSrc can be set in config");
+            legend.destroy();
+            
+        }
+        
+        function test_onImageLoadError(t) {
+            t.plan(2);
+            
+            var legend, calls = 0;
+
+            // create a legend with a bogus image url (one call to error handler)
+            legend = new GeoExt.LegendImage({
+                url: "bogus",
+                defaultImgSrc: "also-bogus",
+                renderTo: "legend",
+                onImageLoadError: function() {
+                    ++calls;
+                    GeoExt.LegendImage.prototype.onImageLoadError.apply(this, arguments);
+                }
+            });
+            t.delay_call(0.5, function() {
+                t.eq(calls, 1, "onImageLoadError called once for bogus image src");
+                var el = legend.getEl();
+                t.eq(el && el.dom.src.split("/").pop(), "also-bogus", "defaultImgSrc set as image src");
+                legend.destroy();
+            });            
+            
+        }
+
+
+    </script>
+  <body>
+    <div id="legend"></div>
+  </body>
+</html>

Modified: core/trunk/geoext/tests/list-tests.html
===================================================================
--- core/trunk/geoext/tests/list-tests.html	2009-06-12 19:44:23 UTC (rev 1066)
+++ core/trunk/geoext/tests/list-tests.html	2009-06-12 19:57:16 UTC (rev 1067)
@@ -17,6 +17,7 @@
   <li>lib/GeoExt/widgets/form/FormPanel.html</li>
   <li>lib/GeoExt/widgets/tree/LayerNode.html</li>
   <li>lib/GeoExt/widgets/tree/LayerContainer.html</li>
+  <li>lib/GeoExt/widgets/LegendImage.html</li>
   <li>lib/GeoExt/widgets/LegendPanel.html</li>
   <li>lib/GeoExt/widgets/ZoomSlider.html</li>
 </ul>



More information about the Commits mailing list