Convert the stylesheet collection to be a vector of 'Stylesheet' typed objects. Enabl...
authorAndy Allan <andy@gravitystorm.co.uk>
Sat, 24 Mar 2012 19:27:49 +0000 (19:27 +0000)
committerAndy Allan <andy@gravitystorm.co.uk>
Sat, 24 Mar 2012 19:27:49 +0000 (19:27 +0000)
net/systemeD/halcyon/Stylesheet.as [new file with mode: 0644]
net/systemeD/potlatch2/StyleSelector.mxml
net/systemeD/potlatch2/VectorSourceDialog.mxml
net/systemeD/potlatch2/collections/Stylesheets.as
potlatch2.mxml

diff --git a/net/systemeD/halcyon/Stylesheet.as b/net/systemeD/halcyon/Stylesheet.as
new file mode 100644 (file)
index 0000000..5a86c3f
--- /dev/null
@@ -0,0 +1,23 @@
+package net.systemeD.halcyon {
+
+    /**
+    * A Stylesheet is a simple object refering to the name and url of the stylesheet.
+    */
+    public class Stylesheet {
+
+        /** The user-visible name of the stylesheet */
+        public var name:String;
+
+        /** The (relative to potlatch2.swf) url of the stylesheet */
+        public var url:String;
+
+        /** Should this stylesheet be considered a "core style"? */
+        public var coreStyle:Boolean;
+
+        public function Stylesheet(name:String, url:String, coreStyle:Boolean = false) {
+            this.name = name;
+            this.url = url;
+            this.coreStyle = coreStyle;
+        }
+    }
+}
\ No newline at end of file
index 5714f99..cb9b57f 100644 (file)
@@ -11,7 +11,7 @@
 
        <s:List width="100%" height="100%" id="mapStyle"
                labelField="name"
-               change="FlexGlobals.topLevelApplication.styleButton.close(); Stylesheets.instance().setStylesheet(mapStyle.selectedItem.name, mapStyle.selectedItem.url);"
+               change="FlexGlobals.topLevelApplication.styleButton.close(); Stylesheets.instance().setStylesheet(mapStyle.selectedItem);"
             dataProvider="{Stylesheets.instance().getCollection()}"
             selectedItem="{Stylesheets.instance().selected}">
       <s:layout>
index 5f49e10..5913680 100644 (file)
 
        [Bindable(event="bogus")]
        public function get styles():ArrayCollection {
-               return new ArrayCollection(Stylesheets.instance().collection);
+               return Stylesheets.instance().getFullCollection();
        }
 
        [Bindable(event="layers_changed")]
index 1ac3f63..d84b6f2 100644 (file)
@@ -5,10 +5,14 @@ package net.systemeD.potlatch2.collections {
        import flash.net.*;
        import net.systemeD.halcyon.DebugURLRequest;
        import net.systemeD.halcyon.Map;
+       import net.systemeD.halcyon.Stylesheet;
        import net.systemeD.potlatch2.FunctionKeyManager;
        import net.systemeD.potlatch2.Yahoo;
        import mx.collections.ArrayCollection;
 
+    /**
+    *  A collection of available stylesheets
+    */
        public class Stylesheets extends EventDispatcher {
 
         private static const GLOBAL_INSTANCE:Stylesheets = new Stylesheets();
@@ -16,16 +20,18 @@ package net.systemeD.potlatch2.collections {
 
                private static const DEFAULT:String = 'stylesheets/potlatch.css';
 
-               public var collection:Array=[];
-               private var _selected:String;
+               private var collection:Vector.<Stylesheet> = new Vector.<Stylesheet>;
+               private var _selected:Stylesheet;
 
                /* Load catalogue file */
 
                public function init(request_url:String=null):void {
                        // First, we set _selected in case it's needed before the stylesheet catalogue loads
-                       _selected=request_url;
-                       _selected=_selected ? _selected : SharedObject.getLocal("user_state").data['stylesheet_url'];
-                       _selected=_selected ? _selected : DEFAULT;
+                       var url:String = request_url;
+                       url = url ? url : SharedObject.getLocal("user_state").data['stylesheet_url'];
+                       url = url ? url : DEFAULT;
+
+                       _selected = new Stylesheet("Default", url);
                        
                        // Load the stylesheet catalogue
                        var request:DebugURLRequest = new DebugURLRequest("stylesheets.xml");
@@ -43,68 +49,83 @@ package net.systemeD.potlatch2.collections {
 
             // first, build the menu from the stylesheet list.
             // Also ensure the saved_url is in the menu (might be either saved from before, or supplied via loaderInfo)
-            collection=new Array();
+            collection = new Vector.<Stylesheet>;
                        for each(var set:XML in xml.stylesheet) {
-                               var obj:Object={};
-                               for (var a:String in set.children()) {
-                                       obj[set.child(a).name()]=set.child(a);
-                               }
-                collection.push(obj);
-                               if (obj.url==saved_url) { isInMenu=true; }
-                               else if (obj.name==saved_name && obj.name!='Custom') { isInMenu=true; saved_url=obj.url; }
+                var s:Stylesheet = new Stylesheet(set.name, set.url, set.corestyle);
+                collection.push(s);
+                               if (s.url==saved_url) { isInMenu=true; }
+                               else if (s.name==saved_name && s.name!='Custom') { isInMenu=true; saved_url=s.url; }
                        }
-                       if (saved_url && !isInMenu) { collection.push({ name:saved_name, url:saved_url }); }
+                       if (saved_url && !isInMenu) { collection.push(new Stylesheet(saved_name, saved_url)); }
 
             // pick a stylesheet to be set. It should be the saved one, if it is in the menu
             // or alternatively the first one on the menu,
             // or finally try 'stylesheets/potlatch.css'
-                       for each (var ss:Object in collection) {
+                       for each (var ss:Stylesheet in collection) {
                                if (ss.name==saved_nameĀ || ss.url==saved_url) {
-                                       setStylesheet(ss.name, ss.url);
+                                       setStylesheet(ss);
                     isSet = true;
                     break;
                                }
                        }
             if (!isSet) {
               if(collection.length > 0) {
-                var s:Object = collection[0];
-                setStylesheet(s.name, s.url);
+                setStylesheet(collection[0]);
               } else {
                 //hit and hope. FIXME should this be an error state?
-                collection.push({ name:'Potlatch', url:DEFAULT});
-                setStylesheet('Potlatch',DEFAULT);
+                var d:Stylesheet = new Stylesheet('Potlatch', DEFAULT);
+                collection.push(d);
+                setStylesheet(d);
               }
             }
                        FunctionKeyManager.instance().registerListener('Map style',
-                               function(o:String):void { setStylesheet(o,findStylesheetURLWithName(o)); });
+                               function(o:String):void { setStylesheet(findStylesheetWithName(o)); });
                        dispatchEvent(new Event("collection_changed"));
                }
 
-               public function setStylesheet(name:String,url:String):void {
-                       _selected=url;
-                       dispatchEvent(new CollectionEvent(CollectionEvent.SELECT, url));
+               public function setStylesheet(ss:Stylesheet):void {
+                       _selected=ss;
+                       dispatchEvent(new CollectionEvent(CollectionEvent.SELECT, ss.url));
                        var obj:SharedObject = SharedObject.getLocal("user_state");
-                       obj.setProperty("stylesheet_url",url);
-                       obj.setProperty("stylesheet_name",name);
+                       obj.setProperty("stylesheet_url",ss.url);
+                       obj.setProperty("stylesheet_name",ss.name);
                        obj.flush();
                }
-               
-               public function get selected():String { return _selected; }
 
-               private function findStylesheetURLWithName(name:String):String {
-                       for each (var ss:Object in collection) {
-                               if (ss.name==name) { return ss.url; }
+               /** The currently selected stylesheet */
+               public function get selected():Stylesheet { return _selected; }
+
+               private function findStylesheetWithName(name:String):Stylesheet {
+                       for each (var ss:Stylesheet in collection) {
+                               if (ss.name==name) { return ss; }
                        }
-                       return '';
+                       return null;
                }
-               
+
+               /**
+               *  Get the list of core stylesheets
+               */
                [Bindable(event="collection_changed")]
                public function getCollection():ArrayCollection {
                        var available:Array=[];
-                       for each (var ss:Object in collection) {
-                               if (!ss.corestyle || ss.corestyle!='no') available.push(ss);
+                       for each (var ss:Stylesheet in collection) {
+                if (ss.coreStyle == true) {
+                  available.push(ss);
+                }
                        }
                        return new ArrayCollection(available);
                }
+
+               /**
+               *  Get the list of all stylesheets
+               */
+               [Bindable(event="collection_changed")]
+               public function getFullCollection():ArrayCollection {
+            var all:Array=[];
+            for each (var ss:Stylesheet in collection) {
+                all.push(ss);
+            }
+            return new ArrayCollection(all);
+        }
        }
 }
index 87c1e5c..dda7c4b 100644 (file)
                        conn.addEventListener(AttentionEvent.ATTENTION, onAttention);
                        conn.addEventListener(AttentionEvent.ALERT, onAlert);
                        if (params['licence_status']) conn.statusFetcher=new StatusFetcher("http://wtfe.gryph.de/api/0.6/problems",conn);
-                       theMap.addLayer(conn, Stylesheets.instance().selected, false, true);
+                       theMap.addLayer(conn, Stylesheets.instance().selected.url, false, true);
 
             // Auto-load vector backgrounds from config
             theMap.addEventListener(MapEvent.INITIALISED, function(e:Event):void { VectorBackgrounds.instance().init(theMap); });