fix minor breakage
[potlatch2.git] / potlatch2.mxml
index 4d4b842..660b8d9 100755 (executable)
@@ -4,6 +4,8 @@
        xmlns:halcyon="net.systemeD.halcyon.*"
        xmlns:potlatch2="net.systemeD.potlatch2.*"
        layout="vertical"
+       horizontalScrollPolicy="off"
+       verticalScrollPolicy="off"
        horizontalAlign="center" 
        addedToStage="initApp()">
        
@@ -27,6 +29,7 @@
             creationComplete="bgButton.popUp = new BackgroundSelector();"/>
         <mx:PopUpButton id="styleButton" label="Map Style" openAlways="true"
             creationComplete="styleButton.popUp = new StyleSelector();"/>
+        <mx:Button label="GPS" click="trackLoader.load();"/>
         <mx:Spacer width="100%"/>
         <mx:Button label="Undo" click="MainUndoStack.getGlobalStack().undo();"
             enabled="{MainUndoStack.getGlobalStack().canUndo()}"/>
 
     </mx:Canvas>
 
-               <potlatch2:Toolbox id="toolbox" 
-                       height="46" width="253" 
-                       headerHeight="6" headerColors="[black, gray]" 
-                       borderThicknessRight="0" borderThicknessLeft="0" borderThicknessBottom="0" 
-                       paddingLeft="4" paddingTop="4" layout="horizontal"
-                       includeInLayout="false" 
-                       creationComplete="toolboxClose.y=-6; toolboxClose.x=239;">
-
-                       <mx:Image data="@Embed('embedded/close_small.png')"
-                           includeInLayout="false" id="toolboxClose" click="toolbox.toggle();" />
-
-               <mx:Button icon="@Embed('embedded/delete.svg')"
-                               click='toolbox.doDelete();' 
-                               enabled="{toolbox.canDo('delete')}" 
-                               alpha="{toolbox.getAlpha('delete')}" 
-                               toolTip="Delete item" 
-                               width="28" height="28" textAlign="left" paddingLeft="6" paddingRight="0" />
-               <mx:Button icon="@Embed('embedded/direction.svg')" 
-                click='toolbox.doReverseDirection();'
-                               enabled="{toolbox.canDo('reverseDirection')}" 
-                               alpha="{toolbox.getAlpha('reverseDirection')}" 
-                               toolTip="Reverse direction" 
-                               width="28" height="28" textAlign="left" paddingLeft="8" paddingRight="0" />
-               <mx:Button icon="@Embed('embedded/cut.svg')" 
-                click='toolbox.doSplit();'
-                               enabled="{toolbox.canDo('split')}" 
-                               alpha="{toolbox.getAlpha('split')}" 
-                               toolTip="Split way" 
-                               width="28" height="28" textAlign="left" paddingLeft="8" paddingRight="0" />
-               <mx:Button icon="@Embed('embedded/straighten.svg')" 
-                               click='toolbox.doStraighten();' 
-                               enabled="{toolbox.canDo('straighten')}" 
-                               alpha="{toolbox.getAlpha('straighten')}" 
-                               toolTip="Straighten way" 
-                               width="28" height="28" textAlign="left" paddingLeft="5" paddingRight="0" />
-               <mx:Button icon="@Embed('embedded/circle.svg')" 
-                               click='toolbox.doCircularise();' 
-                               enabled="{toolbox.canDo('circularise')}" 
-                               alpha="{toolbox.getAlpha('circularise')}" 
-                               toolTip="Make circular" 
-                               width="28" height="28" textAlign="left" paddingLeft="4" paddingRight="0" />
-               <mx:Button icon="@Embed('embedded/quadrilateralise.svg')" 
-                               click='toolbox.doQuadrilateralise();' 
-                               enabled="{toolbox.canDo('quadrilateralise')}" 
-                               alpha="{toolbox.getAlpha('quadrilateralise')}" 
-                               toolTip="Make right-angled" 
-                               width="28" height="28" textAlign="left" paddingLeft="6" paddingRight="0" />
-               <mx:Button icon="@Embed('embedded/parallel.svg')" 
-                               click='toolbox.doParallelise();' 
-                               enabled="{toolbox.canDo('parallelise')}" 
-                               alpha="{toolbox.getAlpha('parallelise')}" 
-                               toolTip="Create parallel way" 
-                               width="28" height="28" textAlign="left" paddingLeft="8" paddingRight="0" />
-
-               </potlatch2:Toolbox>
-
     </mx:HDividedBox>
 
        <mx:Script><![CDATA[
         import mx.events.DragEvent;
         import mx.managers.DragManager;
         import mx.core.DragSource;
+        import mx.controls.Alert;
 
         public var theMap:Map;
         public var theController:EditController;
                public var yahoo:Yahoo;
+               public var trackLoader:TrackLoader;
+               public var toolbox:Toolbox;
 
                private var savecount:uint=0;
                private var loadcount:uint=0;
                        Globals.vars.map_area = map_area;
                        Globals.vars.root = map_area.rawChildren;                       // set up global reference to root level
                        var _root:IChildList = map_area.rawChildren;            // convenient local shorthand
+                       Globals.vars.nocache = loaderInfo.parameters['nocache'] == 'true';
+
+                       // populate sharedObject with loaderInfo parameters if supplied
+                       var obj:SharedObject = SharedObject.getLocal("user_state");
+                       var objChanged:Boolean = false;
+                       if (loaderInfo.parameters['tileurl']) {
+                               obj.setProperty('background_url',loaderInfo.parameters['tileurl']); 
+                               obj.setProperty('background_name','Custom'); 
+                               objChanged=true;
+                       }
+                       if (loaderInfo.parameters['style']) { 
+                               obj.setProperty('stylesheet_url',loaderInfo.parameters['style']);
+                               obj.setProperty('stylesheet_name','Custom');
+                               objChanged=true; 
+                       }
+                       if (objChanged) { obj.flush(); }
+
+                       // load imagery and style XML
+               var request:DebugURLRequest = new DebugURLRequest("imagery.xml");
+               var loader:URLLoader = new URLLoader();
+               loader.addEventListener(Event.COMPLETE, onImageryLoad);
+               loader.load(request.request);
+
+                       var request2:DebugURLRequest = new DebugURLRequest("stylesheets.xml");
+                       var loader2:URLLoader = new URLLoader();
+               loader2.addEventListener(Event.COMPLETE, onStylesheetsLoad);
+               loader2.load(request2.request);
 
                        // map backdrop object
             var w:uint = map_area.width;
                        _root.addChild(yahoo);
                        _root.addChild(theMap);
             theMap.updateSize(w,h);
-                       if (loaderInfo.parameters['tileurl']) {
-                               setBackground('custom',loaderInfo.parameters['tileurl'],2);
-                       }
 
                        // add mask for map
                        var s:Sprite=new Sprite();
                        // keyboard event attached to stage
                        stage.addEventListener(KeyboardEvent.KEY_UP, theMap.keyUpHandler);
 
-                       // load imagery XML
-               var request:URLRequest = new URLRequest("imagery.xml?"+Math.random());
-               var loader:URLLoader = new URLLoader();
-               loader.addEventListener(Event.COMPLETE, onImageryLoad);
-               loader.load(request);
-
                        // position toolbox
-                       toolbox.x=stage.stageWidth-275;
-                       toolbox.y=stage.stageHeight-100;
+                       toolbox=Toolbox(PopUpManager.createPopUp(this,Toolbox,false));
+                       toolbox.init(theController);
+                       toolbox.x=stage.stageWidth-toolbox.width-15;
+                       toolbox.y=stage.stageHeight-toolbox.height-15;
 
                        // add debug field
                        var t:TextField=new TextField();
             conn.addEventListener(Connection.SAVE_COMPLETED, onDataComplete);
             conn.addEventListener(Connection.DATA_DIRTY, onDataDirty);
             conn.addEventListener(Connection.DATA_CLEAN, onDataClean);
+                       conn.addEventListener(MapEvent.ERROR, onMapError);
+
+                       // create GPS trackloader
+                       trackLoader=new TrackLoader(theMap,conn.apiBase);
+
+            // show help dialog on startup, if required
+            // show_help == always => on every startup
+            // show_help == once => show on first startup only
+            var show_help:String = loaderInfo.parameters["show_help"];
+
+            if (show_help == 'always' || (show_help == 'once' && obj.data["help_shown"] != "true")) {
+              new HelpDialog().init();
+            }
                }
                
+               public function onMapError(event:MapEvent):void {
+                       Alert.show(event.params.message, 'Error', mx.controls.Alert.OK);
+               }
+
         public function onResizeMap():void {
             if ( theMap != null )
                 theMap.updateSize(map_area.width, map_area.height);
+
+                       if (toolbox) {
+                               toolbox.x=Math.min(toolbox.x,stage.stageWidth-toolbox.width-15);
+                               toolbox.y=Math.min(toolbox.y,stage.stageHeight-toolbox.height-15);
+                       }
         }
 
         private function onDataStart(event:Event):void {
                }
         private function onDataDirty(event:Event):void {
                        saveButton.enabled=true;
+            if (ExternalInterface.available) {
+              ExternalInterface.call("markChanged", false);
+            }
                }
                private function onDataClean(event:Event):void {
                        saveButton.enabled=false;
+            if (ExternalInterface.available) {
+              ExternalInterface.call("markChanged", true);
+            }
                }
                
         private function onImageryLoad(event:Event):void {
                        var xml:XML = new XML(URLLoader(event.target).data);
-
-                       // create menu
+                       var saved_url:String = SharedObject.getLocal("user_state").data['background_url'];
+                       var saved_name:String= SharedObject.getLocal("user_state").data['background_name'];
+                       var xml_url:String, xml_name:String, isSet:Boolean=false;
             theController.imagery=new Array(
                                { label: "None", data: "" },
                                { label: "Yahoo", data: "yahoo" } );
                        for each(var set:XML in xml.set) {
-                theController.imagery.push({ label:set.child("name"), data:set.child("url") });
+                               xml_url= set.child("url");
+                               xml_name=set.child("name");
+                theController.imagery.push({ label:xml_name, data:xml_url });
+                               if (xml_url==saved_url || (xml_name==saved_name && xml_name!='Custom')) { isSet=true; }
                        }
+                       if (!isSet) { theController.imagery.push({ label:saved_name, data:saved_url }); }
 
-                       // set imagery from shared object
-                       var previous:String = SharedObject.getLocal("user_state").data['background'];
                        for each (var bg:Object in theController.imagery) {
-                               if (bg['label']==previous) { 
-                                       setBackground(bg['label'], bg['data'], 1);
+                               if (bg['label']==saved_nameĀ || bg['data']==saved_url) {
+                                       setBackground(bg['label'], bg['data']);
                                }
                        }
                }
-
-               public function setBackground(name:String,url:String,priority:uint=2):void {
+               
+               public function setBackground(name:String,url:String):void {
                        // ** this should take an object with all parameters (source tag, etc.)
-                       var set:Boolean;
-                       if (url=='yahoo') {
-                               set=theMap.setBackground('',2);
-                               yahoo.show();
-                       } else {
-                               set=theMap.setBackground(url,priority)
-                               yahoo.hide();
+                       if (url=='yahoo') { theMap.setBackground('') ; yahoo.show(); }
+                                    else { theMap.setBackground(url); yahoo.hide(); }
+                       var obj:SharedObject = SharedObject.getLocal("user_state");
+                       obj.setProperty("background_url",url );
+                       obj.setProperty("background_name",name);
+                       obj.flush();
+               }
+
+               private function onStylesheetsLoad(event:Event):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'];
+                       var xml_url:String, xml_name:String, isInMenu:Boolean=false, isSet:Boolean=false;
+
+            // 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)
+            theController.stylesheets=new Array();
+                       for each(var set:XML in xml.stylesheet) {
+                               xml_url= set.child("url");
+                               xml_name=set.child("name");
+                theController.stylesheets.push({ label:xml_name, data:xml_url });
+                               if (xml_url==saved_url || (xml_name==saved_name && xml_name!='Custom')) { isInMenu=true; }
                        }
-                       if (set) {
-                               var obj:SharedObject = SharedObject.getLocal("user_state");
-                               obj.setProperty("background", name);
-                               obj.flush();
+                       if (saved_url && !isInMenu) { theController.stylesheets.push({ label:saved_name, data: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 'potlatch.css'
+                       for each (var ss:Object in theController.stylesheets) {
+                               if (ss['label']==saved_nameĀ || ss['data']==saved_url) {
+                                       setStylesheet(ss['label'], ss['data']);
+                    isSet = true;
+                    break;
+                               }
                        }
+            if (!isSet) {
+              if(theController.stylesheets.length > 0) {
+                var s:Object = theController.stylesheets[0];
+                setStylesheet(s['label'], s['data']);
+              } else {
+                //hit and hope. FIXME should this be an error state?
+                theController.stylesheets.push({ label:'Potlatch', data:'potlatch.css'});
+                setStylesheet('Potlatch','potlatch.css');
+              }
+            }
                }
 
+               public function setStylesheet(name:String,url:String):void {
+                       theMap.setStyle(url);
+                       var obj:SharedObject = SharedObject.getLocal("user_state");
+                       obj.setProperty("stylesheet_url",url);
+                       obj.setProperty("stylesheet_name",name);
+                       obj.flush();
+               }
+
+
         private function dragEnterHandler(event:DragEvent):void {
             // Get the drop target component from the event object.
             var dropTarget:Canvas=event.currentTarget as Canvas;
         }
         
         private function dragDropHandler(event:DragEvent):void {
-            // Get the data identified by the color format from the drag source.
-            // Blame http://www.adobe.com/devnet/flex/quickstart/adding_drag_and_drop/#manual
-            // for whatever horrid abuse of "color format" this is doing
+                       // Deselect the dragged icon
+                       if (event.dragSource.dataForFormat('container')) {
+                               event.dragSource.dataForFormat('container').selectedItem=-1;
+                       }
+
+                       // Get the tags
             var tags:Array = event.dragSource.dataForFormat('tags') as Array;
             var mapLoc:Point = Globals.vars.root.globalToLocal(new Point(event.stageX, event.stageY));
             var lat:Number = Globals.vars.root.coord2lat(mapLoc.y);