Separate Halcyon (rendering) into standalone target without Flex dependency; JavaScri...
[potlatch2.git] / net / systemeD / halcyon / TagViewer.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:VBox
3         xmlns:mx="http://www.adobe.com/2006/mxml" 
4         xmlns:halcyon="net.systemeD.halcyon.*"
5     backgroundColor="white"
6     creationComplete="loadFeatures()">
7
8   <mx:ViewStack id="stack" width="100%" height="100%">
9
10   <mx:VBox width="100%" height="100%" label="Simple">
11     <mx:HBox borderStyle="inset" verticalAlign="middle" width="100%" paddingLeft="3" id="iconContainer">
12         <mx:Image id="iconImage"/>
13         <mx:Text condenseWhite="true" width="100%" id="iconText"/>
14         <mx:LinkButton label="?" click="openDescription()" id="helpLabel"/>
15     </mx:HBox>
16   </mx:VBox>
17
18   <mx:VBox width="100%" height="100%" label="Advanced" initialize="checkAdvanced()" verticalGap="1">
19     <mx:Label id="advancedID">
20       <mx:htmlText><![CDATA[<i>No Selection</i>]]></mx:htmlText>
21     </mx:Label>
22
23     <mx:DataGrid editable="true" width="100%" height="100%" id="advancedTagGrid">
24             <mx:columns>
25                 <mx:DataGridColumn editable="true" dataField="key" headerText="Key"/>
26                 <mx:DataGridColumn editable="true" dataField="value" headerText="Value"/>
27             </mx:columns>
28     </mx:DataGrid>
29
30     <mx:HBox horizontalAlign="right" width="100%">
31       <mx:LinkButton label="Delete" click="removeTag()"/>
32       <mx:LinkButton label="Add" click="addNewTag()"/>
33     </mx:HBox>
34   </mx:VBox>
35
36   </mx:ViewStack>
37
38   <mx:LinkBar dataProvider="{stack}"/>
39
40   <mx:Script><![CDATA[
41       import net.systemeD.halcyon.connection.*;
42       import net.systemeD.halcyon.mapfeatures.*;
43
44       import mx.collections.*
45
46       private var mapFeatures:MapFeatures;
47       private var selectedEntity:Entity;
48       private var variablesPattern:RegExp = /[$][{]([^}]+)[}]/g;
49       private var collection:ArrayCollection;
50
51       public function setEntity(entity:Entity):void {
52           if ( selectedEntity != entity ) {
53               if ( selectedEntity != null )
54                   selectedEntity.removeEventListener(Connection.TAG_CHANGE, tagChanged);
55               selectedEntity = entity;
56               selectedEntity.addEventListener(Connection.TAG_CHANGE, tagChanged);
57           }
58
59           if ( advancedID != null )
60               setupAdvanced(entity);
61
62           refreshFeatureIcon();
63       }
64
65       private function refreshFeatureIcon():void {
66           var feature:XML = mapFeatures.findMatchingFeature(selectedEntity);
67           if ( feature != null )
68               setFeatureIcon(selectedEntity, feature);
69           else
70               blankFeatureIcon(selectedEntity);
71       }
72
73       private function setFeatureIcon(entity:Entity, feature:XML):void {
74           blankFeatureIcon(entity);
75
76           var icon:XMLList = feature.icon;
77           if ( icon.length == 0 )
78               return;
79
80           if ( icon[0].hasOwnProperty("@image") ) {
81               iconImage.source = icon.@image;
82           }
83
84           var txt:String = icon.children().toXMLString();
85           var replaceTag:Function = function():String {
86               var value:String = entity.getTag(arguments[1]);
87               return value == null ? "" : value;
88           };
89           txt = txt.replace(variablesPattern, replaceTag);
90           iconText.htmlText = "<i>"+feature.@name+"</i><br/>" + txt;
91       }
92
93       private function blankFeatureIcon(entity:Entity):void {
94           iconImage.source = null;
95           iconText.htmlText = entity == null ?
96                "<i>Nothing selected</i>" :
97                "<b>Not recognised</b><br/>Try looking at the tags under the advanced properties";
98       }
99
100       private function checkAdvanced():void {
101           if ( selectedEntity != null )
102              setupAdvanced(selectedEntity);
103       }
104
105       private function setupAdvanced(entity:Entity):void {
106           var entityText:String = "xx";
107           if ( entity is Node ) entityText = "Node";
108           else if ( entity is Way ) entityText = "Way";
109           else if ( entity is Relation ) entityText = "Relation";
110
111           advancedID.htmlText = entityText+": <b>"+entity.id+"</b>";
112
113           if ( collection == null ) {
114               collection = new ArrayCollection();
115               //var sort:Sort = new Sort();
116               //sort.fields = [new SortField("key", true)];
117               //collection.sort = sort;
118               //collection.refresh();
119               advancedTagGrid.dataProvider = collection;
120           }
121           collection.removeAll();
122           var tags:Array = entity.getTagArray();
123           tags.sortOn("key");
124           for each(var tag:Tag in tags)
125               collection.addItem(tag);
126       }
127
128       private function tagChanged(event:TagEvent):void {
129           refreshFeatureIcon();
130           
131           if ( collection != null ) {
132               // check to see if the key is already in our list
133               var exists:Boolean = false;
134               var tag:Tag = null;
135               var i:uint;
136               for ( i = 0; i < collection.length && !exists; i++ ) {
137                   tag = Tag(collection.getItemAt(i));
138                   exists = tag.key == event.key;
139               }
140               if ( !exists ) {
141                   tag = new Tag(selectedEntity, event.key, event.newValue);
142                   collection.addItem(tag);
143                   collection.refresh();
144               } else {
145                   if ( event.newValue == null ) {
146                       collection.removeItemAt(i-1);
147                       collection.refresh();
148                   }
149               }
150           }
151       }
152
153       public function loadFeatures():void {
154           mapFeatures = MapFeatures.getInstance();
155       }
156
157       public function openDescription():void {
158           trace("open description here");
159       }
160
161       public function addNewTag():void {
162           var newKey:String = "(new tag)";
163           var newTag:Tag = new Tag(selectedEntity, newKey, "(new value)");
164           collection.addItem(newTag);
165           advancedTagGrid.editedItemPosition = {rowIndex: collection.getItemIndex(newTag), columnIndex: 0};
166       }
167
168       public function removeTag():void {
169           var k:String = advancedTagGrid.selectedItem.key;
170           selectedEntity.setTag(k, null);
171       }
172   ]]></mx:Script>
173 </mx:VBox>
174