Don't wait for feature images before firing feauresloaded
authorTom Hughes <tom@compton.nu>
Sun, 25 Mar 2012 14:13:18 +0000 (15:13 +0100)
committerTom Hughes <tom@compton.nu>
Sun, 25 Mar 2012 14:13:18 +0000 (15:13 +0100)
net/systemeD/halcyon/FileBank.as
net/systemeD/potlatch2/mapfeatures/Feature.as
net/systemeD/potlatch2/mapfeatures/MapFeatures.as

index f03b760..c79e2ba 100755 (executable)
@@ -30,7 +30,19 @@ package net.systemeD.halcyon {
                        return false;
                }
 
-               /* ==========================================================================================
+        public function fileLoaded(name:String, callback:Function):Boolean {
+            var loaded:Boolean = false;
+            if (files[name]) {
+                if (files[name].info.callbacks) {
+                    files[name].info.callbacks.push(callback);
+                } else {
+                    loaded = true;
+                }
+            }
+            return loaded;
+        }
+
+        /* ==========================================================================================
                   Add an individual file to bank (not from a .zip file)
                   Used when we want to load a file for use later on (e.g. an image referenced in a stylesheet)
                   ========================================================================================== */
@@ -92,17 +104,18 @@ package net.systemeD.halcyon {
         }
 
         private function loadedImage(event:Event):void {
-                       fileLoaded(event.target.loader.info);
+                       fileLoadComplete(event.target.loader.info);
         }
         private function loadedFile(event:Event):void {
-                       fileLoaded(event.target.info);
+                       fileLoadComplete(event.target.info);
         }
-        private function fileLoaded(info:Object):void {
-            for (var c:uint = 0; c < info.callbacks.length; c++) {
-                var callback:Function = info.callbacks[c];
+        private function fileLoadComplete(info:Object):void {
+            var callbacks:Array = info.callbacks;
+            info.callbacks = null;
+            while (callbacks.length > 0) {
+                var callback:Function = callbacks.shift();
                 callback(this, info.filename);
             }
-            info.callbacks = null;
 
             fileReceived();
                }
index 01dd585..8ae89c8 100644 (file)
@@ -150,6 +150,7 @@ package net.systemeD.potlatch2.mapfeatures {
         /** Fetches the feature's image, as defined by the icon element in the feature definition.
         * @param dnd if true, overrides the normal image and returns the one defined by the dnd property instead. */
         private function getImage(dnd:Boolean = false):ByteArray {
+            var fileBank:FileBank = FileBank.getInstance();
             var icon:XMLList = _xml.icon;
             var imageURL:String;
 
@@ -159,8 +160,12 @@ package net.systemeD.potlatch2.mapfeatures {
                 imageURL = icon[0].@image;
             }
 
-            if ( imageURL && FileBank.getInstance().hasFile(imageURL) ) {
-                return FileBank.getInstance().getAsByteArray(imageURL);
+            if ( imageURL && fileBank.hasFile(imageURL) ) {
+                if (fileBank.fileLoaded(imageURL, imageLoaded)) {
+                    return fileBank.getAsByteArray(imageURL);
+                } else {
+                    return null;
+                }
             }
             var bitmap:BitmapAsset = new missingIconCls() as BitmapAsset;
             return new PNGEncoder().encode(bitmap.bitmapData);
@@ -174,6 +179,10 @@ package net.systemeD.potlatch2.mapfeatures {
                return point.length() > 0 && !(XML(point[0]).attribute("draganddrop")[0] == "no");
         }
 
+        private function imageLoaded(fileBank:FileBank, url:String):void {
+            dispatchEvent(new Event("imageChanged"));
+        }
+
         public function htmlDetails(entity:Entity):String {
             var icon:XMLList = _xml.icon;
             return makeHTMLIcon(icon, entity);
index a8048ee..98cd47c 100644 (file)
@@ -72,9 +72,7 @@ package net.systemeD.potlatch2.mapfeatures {
                 if ( catXML.child("category").length() == 0 )
                   _categories.push(new Category(this, catXML.@name, catXML.@id, _categories.length));
             }
-            FileBank.getInstance().onFilesLoaded(function():void {
-                dispatchEvent(new Event("featuresLoaded"));
-            });
+            dispatchEvent(new Event("featuresLoaded"));
         }
 
         private function tagsFromInputSet(inputSet:XMLList, f:Feature):void {