Fix refactoring issues in potlatch2.mxml
authorRichard Fairhurst <richard@systemeD.net>
Wed, 15 Jun 2011 12:43:22 +0000 (13:43 +0100)
committerRichard Fairhurst <richard@systemeD.net>
Wed, 15 Jun 2011 12:43:22 +0000 (13:43 +0100)
net/systemeD/halcyon/Map.as
net/systemeD/halcyon/TileSet.as
net/systemeD/potlatch2/collections/CollectionEvent.as [new file with mode: 0644]
net/systemeD/potlatch2/collections/Imagery.as
net/systemeD/potlatch2/collections/Stylesheets.as
potlatch2.mxml

index 5054ac1609379291326a149bda19a5e403c5061d..b65ac7ac5f3156a1dd17c019617594cd080e7471 100644 (file)
@@ -74,8 +74,6 @@ package net.systemeD.halcyon {
                public var backdrop:Object; 
                /** background tile object */
                public var tileset:TileSet; 
-               /** background tile URL, name and scheme */
-               public var tileparams:Object={ url:'' }; 
                /** show all objects, even if unstyled? */
                public var showall:Boolean=true; 
                
@@ -327,12 +325,6 @@ package net.systemeD.halcyon {
                        download();
                }
 
-               /** Select a new background imagery. */
-               public function setBackground(bg:Object):void {
-                       tileparams=bg;
-                       if (tileset) { tileset.init(bg, bg.url!=''); }
-               }
-
                /** Set background dimming on/off. */
                public function setDimming(dim:Boolean):void {
                        if (tileset) { tileset.setDimming(dim); }
index b5469555c52e82475ea7f9b79286ba70bf25c952..5d2e1eacdcf11ea4222a5bd9ab90ed5a53613156 100644 (file)
@@ -47,9 +47,7 @@ package net.systemeD.halcyon {
                 * @param dim Start with imagery faded?
                 * @param sharpen Start with sharpen filter applied?
                 */
-               public function init(params:Object, update:Boolean=false, dim:Boolean=true, sharpen:Boolean=false):void {
-                       setDimming(dim);
-                       sharpening=sharpen;
+               public function init(params:Object, update:Boolean=false):void {
                        baseurl=params.url;
                        scheme =params.scheme ? params.scheme : '900913';
                        tiles={};
diff --git a/net/systemeD/potlatch2/collections/CollectionEvent.as b/net/systemeD/potlatch2/collections/CollectionEvent.as
new file mode 100644 (file)
index 0000000..bab3ee3
--- /dev/null
@@ -0,0 +1,17 @@
+package net.systemeD.potlatch2.collections {
+
+    import flash.events.Event;
+
+    public class CollectionEvent extends Event {
+
+               public static const SELECT:String = "select";
+
+               public var data:Object;
+
+        public function CollectionEvent(eventname:String, data:Object) {
+            super(eventname);
+                       this.data=data;
+        }
+    }
+
+}
index d0443c55c72853ed3761dc128e97a175d9218572..580017bde74b5eb0a679b38bfe7003a03d7017dd 100644 (file)
@@ -24,7 +24,7 @@ package net.systemeD.potlatch2.collections {
         public static function instance():Imagery { return GLOBAL_INSTANCE; }
 
                public var collection:Array=[];
-               public var selected:Object={};
+               private var _selected:Object={};
 
                private var _map:Map;
                private var _overlay:Sprite;
@@ -157,9 +157,9 @@ package net.systemeD.potlatch2.collections {
 
                public function setBackground(bg:Object):void {
                        // set background
-                       selected=bg;
-                       if (bg.url=='yahoo') { _map.setBackground({url:''}); _yahoo.show(); }
-                                       else { _map.setBackground(bg      ); _yahoo.hide(); }
+                       _selected=bg;
+                       if (bg.url=='yahoo') { dispatchEvent(new CollectionEvent(CollectionEvent.SELECT, {url:''})); _yahoo.show(); }
+                                       else { dispatchEvent(new CollectionEvent(CollectionEvent.SELECT, bg      )); _yahoo.hide(); }
                        // update attribution and logo
                        _overlay.visible=bg.attribution || bg.logo || bg.terms_url;
                        setLogo(); setAttribution(); setTerms();
@@ -170,6 +170,8 @@ package net.systemeD.potlatch2.collections {
                        obj.flush();
                }
                
+               public function get selected():Object { return _selected; }
+               
                private function findBackgroundWithName(name:String):Object {
                        for each (var bg:Object in collection) {
                                if (bg.name==name) { return bg; }
@@ -184,10 +186,10 @@ package net.systemeD.potlatch2.collections {
                private function setAttribution():void {
                        var tf:TextField=TextField(_overlay.getChildAt(0));
                        tf.text='';
-                       if (!selected.attribution) return;
+                       if (!_selected.attribution) return;
                        var attr:Array=[];
-                       for (var provider:String in selected.attribution) {
-                               for each (var bounds:Array in selected.attribution[provider]) {
+                       for (var provider:String in _selected.attribution) {
+                               for each (var bounds:Array in _selected.attribution[provider]) {
                                        if (_map.scale>=bounds[0] && _map.scale<=bounds[1] &&
                                          ((_map.edge_l>bounds[3] && _map.edge_l<bounds[5]) ||
                                           (_map.edge_r>bounds[3] && _map.edge_r<bounds[5]) ||
@@ -212,24 +214,24 @@ package net.systemeD.potlatch2.collections {
 
                private function setLogo():void {
                        while (_overlay.numChildren>2) { _overlay.removeChildAt(2); }
-                       if (!selected.logoData) return;
+                       if (!_selected.logoData) return;
                        var logo:Sprite=new Sprite();
-                       logo.addChild(new Bitmap(selected.logoData));
-                       if (selected.logo_url) { logo.buttonMode=true; logo.addEventListener(MouseEvent.CLICK, launchLogoLink, false, 0, true); }
+                       logo.addChild(new Bitmap(_selected.logoData));
+                       if (_selected.logo_url) { logo.buttonMode=true; logo.addEventListener(MouseEvent.CLICK, launchLogoLink, false, 0, true); }
                        _overlay.addChild(logo);
                        positionLogo();
                }
                private function positionLogo():void {
                        _overlay.getChildAt(2).x=5;
-                       _overlay.getChildAt(2).y=_map.mapheight - 5 - selected.logoHeight - (selected.terms_url ? 10 : 0);
+                       _overlay.getChildAt(2).y=_map.mapheight - 5 - _selected.logoHeight - (_selected.terms_url ? 10 : 0);
                }
                private function launchLogoLink(e:Event):void {
-                       if (!selected.logo_url) return;
-                       navigateToURL(new URLRequest(selected.logo_url), '_blank');
+                       if (!_selected.logo_url) return;
+                       navigateToURL(new URLRequest(_selected.logo_url), '_blank');
                }
                private function setTerms():void {
                        var terms:TextField=TextField(_overlay.getChildAt(1));
-                       if (!selected.terms_url) { terms.text=''; return; }
+                       if (!_selected.terms_url) { terms.text=''; return; }
                        terms.text="Background terms of use";
                        positionTerms();
                        terms.addEventListener(MouseEvent.CLICK, launchTermsLink, false, 0, true);
@@ -239,14 +241,14 @@ package net.systemeD.potlatch2.collections {
                        _overlay.getChildAt(1).y=_map.mapheight - 15;
                }
                private function launchTermsLink(e:Event):void {
-                       if (!selected.terms_url) return;
-                       navigateToURL(new URLRequest(selected.terms_url), '_blank');
+                       if (!_selected.terms_url) return;
+                       navigateToURL(new URLRequest(_selected.terms_url), '_blank');
                }
 
                private function resizeHandler(event:MapEvent):void {
-                       if (selected.logoData) positionLogo();
-                       if (selected.terms_url) positionTerms();
-                       if (selected.attribution) positionAttribution();
+                       if (_selected.logoData) positionLogo();
+                       if (_selected.terms_url) positionTerms();
+                       if (_selected.attribution) positionAttribution();
                }
 
                [Bindable(event="collection_changed")]
index f10d079f9fb0824478585de49a77021ab1fa1b29..1ac3f63dd2c799ac284460df2881240f1a4485b0 100644 (file)
@@ -14,24 +14,31 @@ package net.systemeD.potlatch2.collections {
         private static const GLOBAL_INSTANCE:Stylesheets = new Stylesheets();
         public static function instance():Stylesheets { return GLOBAL_INSTANCE; }
 
+               private static const DEFAULT:String = 'stylesheets/potlatch.css';
+
                public var collection:Array=[];
-               public var selected:Object={};
-               private var _map:Map;
+               private var _selected:String;
 
                /* Load catalogue file */
 
-               public function init(map:Map):void {
-                       _map=map;
+               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;
+                       
+                       // Load the stylesheet catalogue
                        var request:DebugURLRequest = new DebugURLRequest("stylesheets.xml");
                        var loader:URLLoader = new URLLoader();
-               loader.addEventListener(Event.COMPLETE, onStylesheetsLoad);
+               loader.addEventListener(Event.COMPLETE, function(e:Event):void { onStylesheetsLoad(e,request_url); });
                loader.load(request.request);
                }
 
-               private function onStylesheetsLoad(event:Event):void {
+               private function onStylesheetsLoad(event:Event, request_url:String=null):void {
                        var xml:XML = new XML(URLLoader(event.target).data);
                        var saved_url:String = SharedObject.getLocal("user_state").data['stylesheet_url'];
                        var saved_name:String= SharedObject.getLocal("user_state").data['stylesheet_name'];
+                       if (request_url && request_url!=saved_url) { saved_url=request_url; saved_name='Custom'; }
                        var isInMenu:Boolean=false, isSet:Boolean=false;
 
             // first, build the menu from the stylesheet list.
@@ -64,8 +71,8 @@ package net.systemeD.potlatch2.collections {
                 setStylesheet(s.name, s.url);
               } else {
                 //hit and hope. FIXME should this be an error state?
-                collection.push({ name:'Potlatch', url:'stylesheets/potlatch.css'});
-                setStylesheet('Potlatch','stylesheets/potlatch.css');
+                collection.push({ name:'Potlatch', url:DEFAULT});
+                setStylesheet('Potlatch',DEFAULT);
               }
             }
                        FunctionKeyManager.instance().registerListener('Map style',
@@ -74,13 +81,15 @@ package net.systemeD.potlatch2.collections {
                }
 
                public function setStylesheet(name:String,url:String):void {
-                       _map.editableLayer.setStyle(url);
-                       // >>>> REFACTOR: not great to have a reference to editableLayer in here
+                       _selected=url;
+                       dispatchEvent(new CollectionEvent(CollectionEvent.SELECT, url));
                        var obj:SharedObject = SharedObject.getLocal("user_state");
                        obj.setProperty("stylesheet_url",url);
                        obj.setProperty("stylesheet_name",name);
                        obj.flush();
                }
+               
+               public function get selected():String { return _selected; }
 
                private function findStylesheetURLWithName(name:String):String {
                        for each (var ss:Object in collection) {
index 9ad70000da43b539ab1a35d2e781f5001ddf31a7..055f0a6f0665c3c01a213bb6818f403f162db12e 100644 (file)
                        _root.addChild(yahoo);
                        _root.addChild(theMap);
 
-                       // Initialise 900913 background
-                       // >>>> REFACTOR: something odd about accessing map.tileparams here...
-                       theMap.tileset.blocks=[new RegExp("google","i")];       // hard-coded block on Google tiles
-                       theMap.tileset.init(theMap.tileparams, false, 
-                                           params['background_dim']    ==null ? true  : params['background_dim'],
-                                           params['background_sharpen']==null ? false : params['background_sharpen']);
+                       // Initialise stylesheets
+                       Stylesheets.instance().init();
+                       Stylesheets.instance().addEventListener(CollectionEvent.SELECT,
+                               function(e:CollectionEvent):void { theMap.editableLayer.setStyle(String(e.data)); }
+                       );
 
                        // Add core data layer
-                       // >>>> REFACTOR: shouldn't be hardcoded to XMLConnection
                        var conn:Connection = new XMLConnection("Main", params['api'], params['policy'], params);
             conn.addEventListener(Connection.LOAD_STARTED, onDataStart);
             conn.addEventListener(Connection.LOAD_COMPLETED, onDataComplete);
                        conn.addEventListener(MapEvent.ERROR, onMapError);
                        conn.addEventListener(AttentionEvent.ATTENTION, onAttention);
                        conn.addEventListener(AttentionEvent.ALERT, onAlert);
-                       // >>>> REFACTOR: shouldn't be trying to find out stylesheet from here
-                       theMap.addLayer(conn,params['styleurl'] ? params['styleurl'] : 'stylesheets/potlatch.css',false);
+                       theMap.addLayer(conn, Stylesheets.instance().selected, false, true);
 
             // Auto-load vector backgrounds from config
             theMap.addEventListener(MapEvent.INITIALISED, function(e:Event):void { VectorBackgrounds.instance().init(theMap); });
 
                        // Set start position of map
-                       // >>>> REFACTOR: what happens if lat/lon/zoom not supplied? (Hurleston GPX case)
+                       // ** FIXME: if lat/lon/zoom aren't supplied, we need to keep the map in a non-loading state 
+                       //           until the user has decided where to start editing (e.g. when the first GPX loads)
                        theMap.init(params['lat'], params['lon'], params['zoom']);
 
                        // add attribution/logo sprite
                        Globals.vars.map_area.addEventListener(MouseEvent.MOUSE_MOVE, theMap.mouseMoveHandler);
                        Globals.vars.map_area.addEventListener(MouseEvent.MOUSE_DOWN, theMap.mouseDownHandler);
 
-                       // initialise imagery and stylesheets
+                       // initialise imagery
+                       theMap.tileset.blocks=[new RegExp("google","i")];       // hard-coded block on Google tiles
+                       theMap.tileset.setDimming(params['background_dim']    ==null ? true  : params['background_dim']);
+                       theMap.tileset.setSharpen(params['background_sharpen']==null ? false : params['background_sharpen']);
                        Imagery.instance().init(theMap, overlay, yahoo);
-                       Stylesheets.instance().init(theMap);
                        Imagery.instance().addEventListener(MapEvent.BUMP, bumpHandler);
+                       Imagery.instance().addEventListener(CollectionEvent.SELECT,
+                               function(e:CollectionEvent):void { theMap.tileset.init(e.data, e.data!=''); }
+                       );
 
                        // keyboard event attached to stage
                        stage.addEventListener(KeyboardEvent.KEY_UP, theMap.keyUpHandler);
             t.visible = loaderInfo.parameters["show_debug"] == 'true';
                        Globals.vars.root=theMap;       // just for the addDebug function
 
+                       // create controller
             theController = new EditController(theMap, tagViewer, toolbox);
             theController.setActive();
                        theController.addEventListener(AttentionEvent.ATTENTION, onAttention);