Merge branch 'master' into history
[potlatch2.git] / net / systemeD / potlatch2 / TagViewer.mxml
index 3e5c691..dd1fefb 100644 (file)
@@ -1,4 +1,10 @@
 <?xml version="1.0" encoding="utf-8"?>
+<!---
+  The TagViewer is the container for the entire sidebar. It has a number of components, and subpanels, in a ViewStack that change
+  depending on what is selected, i.e. the ControllerState.
+
+  @see net.systemeD.potlatch2.controller.ControllerState
+    -->
 <mx:VBox
     xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:mx="library://ns.adobe.com/flex/mx"
@@ -32,6 +38,8 @@
                                                        import mx.core.DragSource;
 
                                                        private function dragPOI(event:MouseEvent, tags:Array):void {
+                                                               if (outerDocument.controller.map.dragstate==outerDocument.controller.map.DRAGGING) return;
+
                                                                // Get the drag initiator component from the event object.
                                                                var dragInitiator:Image = event.currentTarget as Image;
                                                                var dragSource:DragSource = new DragSource();
@@ -55,8 +63,8 @@
 
   <!-- Standard tagging panel -->
 
-  <mx:VBox id="tagsPanel" width="100%" height="100%" creationPolicy="auto">
-    <mx:ViewStack id="stack" width="100%" height="100%">
+  <mx:VBox id="tagsPanel" width="100%" height="100%" creationPolicy="auto" tabChildren="true">
+    <mx:ViewStack id="stack" width="100%" height="100%" change="tagsPanel.tabChildren=(event.newIndex==0)">
       <mx:VBox width="100%" height="100%" label="Simple" id="editorContainer" creationComplete="initEditorStackUIs()" styleName="dndPanelVbox">
         <mx:VBox width="100%" verticalGap="1" styleName="dndTagHeader">
           <mx:HBox width="100%" id="iconContainer" styleName="featureSelector">
@@ -70,7 +78,7 @@
         </mx:VBox>
       </mx:VBox>
       <mx:VBox width="100%" height="100%" label="Advanced" id="advancedContainer" initialize="checkAdvanced()" verticalGap="1">
-        <mx:Label id="advancedID" click="openEntityPage()">
+        <mx:Label id="advancedID" click="new HistoryDialog().init(selectedEntity);">
           <mx:htmlText><![CDATA[<i>No Selection</i>]]></mx:htmlText>
         </mx:Label>
 
                        disabledIcon="@Embed('../../../embedded/view_accordion_disabled.png')"
                        click="setEditorStackUI(false)" id="accordionLabel" paddingTop="6"
                        toolTip="Show in sliding windows"
-                       enabled="{editorStack is SuperTabNavigator &amp;&amp; stack.selectedIndex==0}" />
+                       enabled="{editorStack is TabNavigator &amp;&amp; stack.selectedIndex==0}" />
          </mx:HBox>
   </mx:VBox>
 
     <sidepanel:BackgroundPanel id="backgroundPanelContents" width="100%"/>
   </mx:VBox>
 
+  <!-- merge tags from background layer -->
+
+  <mx:VBox id="backgroundMergePanel" width="100%" height="100%" horizontalScrollPolicy="off" styleName="dndPanelVbox">
+    <sidepanel:BackgroundMergePanel id="backgroundMergePanelContents" width="100%" />
+  </mx:VBox>
+
 </mx:ViewStack>
 
   <fx:Script><![CDATA[
       import net.systemeD.halcyon.connection.*;
       import net.systemeD.halcyon.MapPaint;
+      import net.systemeD.potlatch2.EditController;
       import net.systemeD.potlatch2.mapfeatures.*;
+      import net.systemeD.potlatch2.history.HistoryDialog;
+      import net.systemeD.potlatch2.mapfeatures.editors.*;
       import net.systemeD.potlatch2.utils.*;
+      import net.systemeD.controls.CollapsiblePanel;
 
       import mx.collections.*;
-      import mx.containers.*;
+      import mx.containers.VBox;
+      import mx.containers.HBox;
+      import mx.containers.TabNavigator;
+      import mx.containers.Accordion;
       import mx.events.*;
       import mx.core.*;
       import mx.managers.PopUpManager;
       import mx.managers.DragManager;
       import mx.core.DragSource;
       import mx.controls.TabBar;
-      import flexlib.containers.SuperTabNavigator;
+      import spark.components.Form;
+      import spark.layouts.FormLayout;
+//    import flexlib.containers.SuperTabNavigator;
+
+      [Bindable] [Embed(source="../../../embedded/tab_basic.png"       )] private var tabIconBasic:Class;
+      [Bindable] [Embed(source="../../../embedded/tab_address.png"     )] private var tabIconAddress:Class;
+      [Bindable] [Embed(source="../../../embedded/tab_cycle.png"       )] private var tabIconCycle:Class;
+      [Bindable] [Embed(source="../../../embedded/tab_details.png"     )] private var tabIconDetails:Class;
+      [Bindable] [Embed(source="../../../embedded/tab_restrictions.png")] private var tabIconRestrictions:Class;
+      [Bindable] [Embed(source="../../../embedded/tab_transport.png"   )] private var tabIconTransport:Class;
+      [Bindable] [Embed(source="../../../embedded/tab_walk.png"        )] private var tabIconWalk:Class;
+      private var tabIcons:Object= { Basic:tabIconBasic, Details:tabIconDetails, Address:tabIconAddress, Walk:tabIconWalk, Cycle:tabIconCycle, 
+                                        Transport:tabIconTransport, Restrictions:tabIconRestrictions};
 
       [Bindable]
       public var dndPrompt:String="Add new points by dragging them onto the map";
 
-      private var editorStackTabNavigator:SuperTabNavigator;
+      private var editorStackTabNavigator:TabNavigator;
       private var editorStackAccordion:Accordion;
       [Bindable] private var editorStack:Container;
 
       public var mapFeatures:MapFeatures;
+      public var controller:EditController;
       private var selectedEntity:Entity;
       private var connection:Connection;
       private var currentCategorySelector:CategorySelector;
                        selectedEntity.removeEventListener(Connection.TAG_CHANGED, tagChanged);
                        selectedEntity.removeEventListener(Connection.ADDED_TO_RELATION, addedToRelation);
                        selectedEntity.removeEventListener(Connection.REMOVED_FROM_RELATION, removedFromRelation);
+                       if (selectedEntity is EntityCollection) EntityCollection(selectedEntity).releaseListeners();
                }
 
                if (entities.length==0) {
               markerPanelContents.init(selectedEntity, layer);
               sidebar.selectedChild = markerPanel;
             } else if (connection is SnapshotConnection) {
-              backgroundPanelContents.init(selectedEntity, layer);
+              backgroundPanelContents.init(selectedEntity);
               sidebar.selectedChild = backgroundPanel;
             } else {
               refreshFeatureIcon();
               sidebar.selectedChild = tagsPanel;
             }
 
+        } else if (entities.length==2
+                      && xor(entities[0].connection is SnapshotConnection, entities[1].connection is SnapshotConnection)
+                      && xor(!controller.map.getLayerForEntity(entities[0]).isBackground, !controller.map.getLayerForEntity(entities[1]).isBackground) ) {
+            backgroundMergePanelContents.init(entities);
+            sidebar.selectedChild = backgroundMergePanel;
+
                } else if(isMultipleEditable(entities)) {
                        selectedEntity = new EntityCollection(entities);
                        selectedEntity.addEventListener(Connection.TAG_CHANGED, tagChanged);
                UIComponent.resumeBackgroundProcessing();
       }
 
+      private function xor(a:Boolean, b:Boolean):Boolean {
+          return ( a || b) && !(a && b);
+      }
+
       private function refreshFeatureIcon():void {
           var oldFeature:Feature = feature;
           feature = selectedEntity == null ? null : mapFeatures.findMatchingFeature(selectedEntity);
       }
 
       private var tabComponents:Object = {};
+      private var subpanelComponents:Object = {};
 
       private function initialiseEditors():void {
-          editorStack.removeAllChildren();
-          if ( selectedEntity == null || feature == null )
-              return;
+               editorStack.removeAllChildren();
+               if ( selectedEntity == null || feature == null )
+                       return;
+
+               var basicEditorBox:VBox = createEditorBox();
+               basicEditorBox.label = "Basic";
+               basicEditorBox.icon=tabIconBasic;
+               editorStack.addChild(basicEditorBox);
+
+               var tabs:Object = {};
+               var tabList:Array = [];
+               tabComponents = {};
+               var subpanels:Object = {};
+               subpanelComponents = {};
+
+               // First create the tabs
+               for each (var factory:EditorFactory in feature.editors) {
+                       var category:String = factory.category;
+                       if (category!='') {
+                               var tab:VBox = tabs[category];
+                               if ( tab == null) {
+                                       tab = createEditorBox();
+                                       tab.label = category;
+                                       if (tabIcons[category]) tab.icon=tabIcons[category];
+                                       tabs[category] = tab;
+                                       tabList.push(tab);      
+                               }
+                       }
+               }
 
-          var editorBox:VBox = createEditorBox();
-          editorBox.label = "Basic";
-          editorStack.addChild(editorBox);
+               // Put the tabs on-screen in order
+               tabList.sort(sortCategories,16);
+               for each (tab in tabList) {
+                       editorStack.addChild(tab);
+                       tabComponents[tab] = [];
+               }
 
-          var tabs:Object = {};
-          tabComponents = {};
+               // Then add the individual editors to them
+               for each (factory in feature.editors) {
 
-          for each (var factory:EditorFactory in feature.editors) {
-              if ( factory.presence.isEditorPresent(factory, selectedEntity, null) ) {
-                  var editor:DisplayObject = factory.createEditorInstance(selectedEntity);
-                  if ( editor != null )
-                      editorBox.addChild(editor);
-              }
-              var category:String = factory.category;
-              if (category!='') {
-                     var tab:VBox = tabs[category];
-                     if ( tab == null) {
-                         tab = createEditorBox();
-                         tab.label = category;
-                         editorStack.addChild(tab);
-                         tabs[category] = tab;
-                         tabComponents[tab] = [];
-                     }
-                     var catEditor:DisplayObject = factory.createEditorInstance(selectedEntity);
-                     if ( catEditor != null )
-                         tabComponents[tab].push(catEditor);
-                     //    tab.addChild(catEditor);
-              }
-          }
-      }
+                       // Add to basic editor box first
+                       if ( factory.presence.isEditorPresent(factory, selectedEntity, null) ) {
+                               var editor:DisplayObject = factory.createEditorInstance(selectedEntity);
+                               if (editor) Form(basicEditorBox.getChildByName("form")).addElement(UIComponent(editor));
+                       }
 
-      private function createEditorBox():VBox {
-          var box:VBox = new VBox();
-          box.percentWidth = 100;
-          box.percentHeight = 100;
-          box.styleName = "dndEditorContainer";
-          return box;
+                       // Then prepare to add to category panel
+                       category=factory.category;
+                       if (factory.category=='') continue;
+                       var catEditor:DisplayObject = factory.createEditorInstance(selectedEntity);
+                       if (!catEditor) continue;
+                       tab=tabs[category];
+                       
+                       // Create subcategory panel if needed
+                       if (factory.subcategory) {
+                               var subcategory:String = factory.subcategory;
+                               if (!subpanels[category]) { subpanels[category]={}; }
+                               var subpanel:CollapsiblePanel = subpanels[category][subcategory];
+                               if (!subpanel) {
+                                       subpanel=new CollapsiblePanel(false);
+                                       subpanel.percentWidth=100;
+                                       subpanel.styleName="subcategoryPanel";
+                                       subpanel.title=subcategory;
+                                       subpanels[category][subcategory]=subpanel;
+                                       addConstrainedForm(subpanel);
+                                       tabComponents[tab].push(subpanel);
+                               }
+                               Form(subpanel.getChildByName("form")).addElement(UIComponent(catEditor));
+                       } else {
+                               tabComponents[tab].push(catEditor);
+                       }
+               }
       }
 
-      private function ensureEditorsPopulated(tab:VBox):void {
-          var components:Array = tabComponents[tab];
-          if ( components == null || tab == null || tab.numChildren >= components.length )
-              return;
-          for each (var component:DisplayObject in components ) {
-              tab.addChild(component);
-          }
-      }
+       // ** FIXME: Order probably shouldn't be hard-coded, but configurable
+       private static var categoryOrder:Array=["Restrictions","Transport","Cycle","Walk","Address","Details"]; // reverse order
+       private function sortCategories(a:VBox,b:VBox):int {
+               var a1:int=categoryOrder.indexOf(a.label);
+               var a2:int=categoryOrder.indexOf(b.label);
+               if (a1<a2) { return 1; }
+               else if (a1>a2) { return -1; }
+               return 0;
+       }
+       
+       private function createEditorBox():VBox {
+               var box:VBox = new VBox();
+               box.percentWidth = 100;
+               box.percentHeight = 100;
+               box.styleName = "dndEditorContainer";
+               addConstrainedForm(box);
+               return box;
+       }
+       
+       private function addConstrainedForm(parentObject:DisplayObjectContainer):void {
+               var form:Form = new Form();
+               form.name = "form";
+               form.percentWidth=100;
+               form.addEventListener(mx.events.ResizeEvent.RESIZE, formResizeHandler, false, 0, true);
+               parentObject.addChild(form);
+               if (parentObject.width>0) form.maxWidth=parentObject.width;
+       }
+       
+       private function formResizeHandler(e:Event):void {
+               var form:Form=Form(e.target);
+               if (form.parent.width>0) form.maxWidth=form.parent.width;
+       }
+
+       private function ensureEditorsPopulated(tab:VBox):void {
+               var components:Array = tabComponents[tab];
+               var form:Form=Form(tab.getChildByName("form"));
+               if ( components == null || tab == null || form.numElements >= components.length ) return;
+               for each (var component:DisplayObject in components ) {
+                       form.addElement(UIComponent(component));
+               }
+       }
 
        private function initEditorStackUIs():void {
-               editorStackTabNavigator = new SuperTabNavigator();
-               editorStackTabNavigator.allowTabSqueezing=false;
-               editorStackTabNavigator.minTabWidth=10;
-               editorStackTabNavigator.closePolicy="close_never";
-               editorStackTabNavigator.scrollSpeed=20;
+               editorStackTabNavigator = new TabNavigator();
+//             editorStackTabNavigator.allowTabSqueezing=false;
+//             editorStackTabNavigator.minTabWidth=10;
+//             editorStackTabNavigator.closePolicy="close_never";
+//             editorStackTabNavigator.scrollSpeed=20;
                editorStackTabNavigator.creationPolicy="auto";
                editorStackTabNavigator.percentWidth=100;
                editorStackTabNavigator.percentHeight=100;
                editorStackTabNavigator.styleName="dndStackTab";
-               editorStackTabNavigator.popUpButtonPolicy="off"
+//             editorStackTabNavigator.popUpButtonPolicy="off"
 
 
                editorStackAccordion = new Accordion();
                editorStackAccordion.percentHeight=100;
                editorStackAccordion.creationPolicy="auto";
                editorStackAccordion.styleName="dndStackAccordion";
+               /* FIXME: the accordion icons should be right-aligned. See:
+               http://www.kristoferjoseph.com/blog/2008/11/06/positioning-the-flex-accordion-header-icon
+               http://blog.flexexamples.com/2007/09/13/changing-text-alignment-in-an-flex-accordion-header/
+               */
 
                setEditorStackUI(true);
        }
        }
 
        private function editorStackUIUpdate(event:Event):void {
-               if (editorStack is SuperTabNavigator) {
-                       var e:SuperTabNavigator = editorStack as SuperTabNavigator;
+               if (editorStack is TabNavigator) {
+                       var e:TabNavigator = editorStack as TabNavigator;
                        if (e.selectedIndex<0) { return; }
                        for (var i:uint=0; i<e.numChildren; i++) {
                                e.getTabAt(i).selected=(i==e.selectedIndex);
                        if      ( entity is Node     ) entityText = "Node";
                        else if ( entity is Way      ) entityText = "Way";
                        else if ( entity is Relation ) entityText = "Relation";
-                       advancedID.htmlText = entityText+": <b>"+entity.id+"</b>";
+                       advancedID.htmlText = entityText+": <b>"+entity.id+"</b> "+(entity.status ? entity.status : '');
                }
 
                removeRelationListeners();
       }
 
          public function addNewTag():void {
-               if (stack.selectedChild!=advancedContainer) { return; }
-               advancedTagGrid.addNewTag();
+               if (sidebar.selectedChild==multiplePanel) { multiAdvancedTagGrid.addNewTag(); }
+               else if (stack.selectedChild==advancedContainer) { advancedTagGrid.addNewTag(); }
          }
 
       private function addedToRelation(event:RelationMemberEvent):void {
                                break;
                        
                        case 3: // Delete relation
-                               Alert.show("Do you really want to delete the relation?","Are you sure?",Alert.YES | Alert.CANCEL,null,
+                               var warning:String="This relation has # members. Deleting it will affect all of them and erase this $. Are you really sure?";
+                               warning=warning.replace("#",rel.length).replace("$",rel.getRelationType());
+                               Alert.show(warning,"Are you sure?",Alert.YES | Alert.CANCEL,null,
                                        function(event:CloseEvent):void { 
                                                if (event.detail==Alert.CANCEL) return;
                                                rel.remove(MainUndoStack.getGlobalStack().addAction);
-                                       } );
+                                       } , null, Alert.CANCEL);
                                break;
                }
        }
               navigateToURL(new URLRequest(feature.helpURL), "potlatch_help");
       }
 
-      /** Open up a new browser page showing OSM's view of the current entity. */
-      public function openEntityPage():void {
-          if (selectedEntity != null && selectedEntity.id >= 0) {
-              // This is slightly hard-coded, but not drastically. The ../s could be changed for string manipulation of the apiBase
-              var urlBase:String = connection.apiBase + '../../browse/'
-              navigateToURL(new URLRequest(urlBase+selectedEntity.getType()+'/'+selectedEntity.id), "potlatch_browse");
-          }
-      }
-
       public function addToRelation():void {
           new RelationSelectPanel().init(selectedEntity,new Object());
       }
           var action:CompositeUndoableAction = new CompositeUndoableAction(
                   "Set "+selectedEntity.getType()+" "+selectedEntity.id+" to "+newFeature.name);
                  selectedEntity.suspend();
+          
+          // build a list of tags that are editable in new feature
+          var editableTags:Array = new Array();
+          for each( var editor:EditorFactory in newFeature.editors ) {
+              if ( editor is SingleTagEditorFactory ) {
+                  var singleTagEditor:SingleTagEditorFactory = editor as SingleTagEditorFactory;
+                  editableTags.push(singleTagEditor.key);
+              }
+          }
 
           // remove tags from the current feature
           if ( feature != null ) {
               for each( var oldtag:Object in feature.tags ) {
-                  selectedEntity.setTag(oldtag["k"], null, action.push);
+                  if ( editableTags.indexOf(oldtag["k"]) < 0 ) {
+                      selectedEntity.setTag(oldtag["k"], null, action.push);
+                  }
               }
           }
 
                if (rows!=Math.floor(rows)) { rows=Math.floor(rows+1); }
                event.target.height=rows*(event.target.rowHeight+1);
        }
+
   ]]></fx:Script>
 </mx:VBox>