Make advanced tag panel listen to tag changes; fix issues with panel not responding...
[potlatch2.git] / net / systemeD / potlatch2 / TagGrid.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2
3 <!--
4         Advanced tag grid
5 -->
6
7 <mx:DataGrid editable="true" doubleClickEnabled="true" doubleClick="addNewTag()"
8         xmlns:controls="net.systemeD.controls.*"
9         xmlns:mx="http://www.adobe.com/2006/mxml">
10         <mx:columns>
11
12                 <!-- Key -->
13
14                 <mx:DataGridColumn editable="true" dataField="key" headerText="Key">
15                         <mx:itemEditor>
16                                 <mx:Component>
17                                         <controls:AutoComplete
18                                                 dataProvider="{MapFeatures.getInstance().getAutoCompleteKeys(outerDocument.getEntityType())}"
19                                                 labelField="name"
20                                                 rowCount="10"
21                                                 restrict="&#x0020;-&#x10FFFF;"
22                                                 typedText="{outerDocument.selectedItem.key}">
23                                         <mx:Script><![CDATA[ import net.systemeD.potlatch2.mapfeatures.*; ]]></mx:Script>
24                                         </controls:AutoComplete>
25                                 </mx:Component>
26                         </mx:itemEditor>
27                 </mx:DataGridColumn>
28
29                 <!-- Value -->
30
31                 <mx:DataGridColumn editable="true" dataField="value" headerText="Value">
32                         <mx:itemEditor>
33                                 <mx:Component>
34                                         <controls:AutoComplete
35                                                 dataProvider="{MapFeatures.getInstance().getAutoCompleteValues(outerDocument.getEntityType(),outerDocument.selectedItem.key)}"
36                                                 labelField="name"
37                                                 rowCount="10"
38                                                 restrict="&#x0020;-&#x10FFFF;"
39                                                 typedText="{outerDocument.selectedItem.value}">
40                                         <mx:Script><![CDATA[ import net.systemeD.potlatch2.mapfeatures.*; ]]></mx:Script>
41                                         </controls:AutoComplete>
42                                 </mx:Component>
43                         </mx:itemEditor>
44                 </mx:DataGridColumn>
45
46                 <!-- Delete button -->
47
48                 <mx:DataGridColumn width="20" editable="false">
49                         <mx:itemRenderer>
50                                 <mx:Component>
51                     <mx:HBox horizontalAlign="center" verticalAlign="middle">
52                       <mx:Image source="@Embed('../../../embedded/delete_small.svg')"
53                           click='event.stopPropagation();outerDocument.removeTag();'
54                           buttonMode="true" useHandCursor="true" width="12" height="12">
55                       </mx:Image>
56                                         </mx:HBox>
57                                 </mx:Component>
58                         </mx:itemRenderer>
59                 </mx:DataGridColumn>
60         </mx:columns>
61
62         <mx:Script><![CDATA[
63
64                 import net.systemeD.halcyon.connection.*;
65                 import mx.collections.*;
66
67                 private var selectedEntity:Entity;
68                 private var tagDataProvider:ArrayCollection;
69
70                 /** Assign a new selected Entity, and update editing controls appropriately. */
71                 public function init(entity:Entity):void {
72                         if (selectedEntity==entity) return;
73                         if (selectedEntity) selectedEntity.removeEventListener(Connection.TAG_CHANGED, tagChanged);
74
75                         selectedEntity=entity;
76                         if (!selectedEntity) return;
77                         selectedEntity.addEventListener(Connection.TAG_CHANGED, tagChanged, false, 0, true);
78
79                         if ( tagDataProvider == null ) {
80                                 tagDataProvider = new ArrayCollection();
81                                 dataProvider = tagDataProvider;
82                         } else {
83                                 tagDataProvider.removeAll();
84                         }
85
86                         var tags:Array = selectedEntity.getTagArray();
87                         tags.sortOn("key");
88                         for each(var tag:Tag in tags) { tagDataProvider.addItem(tag); }
89                 }
90
91                 /** Update dataProvider in response to a tag being edited or removed */
92                 private function tagChanged(event:TagEvent):void {
93                         var set:Boolean=false;
94                         for (var i:uint=0; i<tagDataProvider.length; i++) {
95                                 if (tagDataProvider[i].key==event.oldKey) {
96                                         if (event.newValue && event.key) {
97                                                 if (!set) {
98                                                         // this entry in the dataProvider has changed
99                                                         tagDataProvider[i].key=event.key;
100                                                         tagDataProvider[i].value=event.newValue;
101                                                         set=true;
102                                                 } else {
103                                                         // trap duplicate tags
104                                                         tagDataProvider.removeItemAt(i);
105                                                 }
106                                         } else {
107                                                 // this entry in the dataProvider has been deleted
108                                                 tagDataProvider.removeItemAt(i);
109                                         }
110                                 }
111                         }
112                         // tell Flex to redraw the grid because we've changed a tag
113                         invalidateList();
114                 }
115
116                 /** Create editing controls for a new key/value pair, with default values. */
117                 public function addNewTag():void {
118                         var newKey:String = "(new tag)";
119                         var newTag:Tag = new Tag(selectedEntity, newKey, "(new value)");
120                         tagDataProvider.addItem(newTag);
121                         editedItemPosition = {rowIndex: tagDataProvider.getItemIndex(newTag), columnIndex: 0};
122                 }
123
124                 /** Remove the selected tag from the selected entity. */
125                 public function removeTag():void {
126                         var k:String = selectedItem.key;
127                         selectedEntity.setTag(k, null, MainUndoStack.getGlobalStack().addAction);
128                 }
129
130                 /** Return the type ("node", "way", "relation") of the selected entity. */
131                 public function getEntityType():String {
132                         return selectedEntity.getType();
133                 }
134
135         ]]></mx:Script>
136 </mx:DataGrid>