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