Merge branch 'master' of https://github.com/systemed/potlatch2
[potlatch2.git] / net / systemeD / potlatch2 / find / FindDialog.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:TitleWindow
3         xmlns:mx="http://www.adobe.com/2006/mxml" 
4         layout="vertical" showCloseButton="true"
5         horizontalAlign="center" title="Search"
6         width="458" height="350" verticalGap="0" creationComplete="txtFind.setFocus()">
7         <mx:Script><![CDATA[
8         import flash.events.Event;
9                 import mx.core.Application;
10                 import mx.events.CloseEvent;
11                 import mx.events.FlexEvent;
12                 import mx.managers.PopUpManager;
13                 import mx.rpc.events.ResultEvent;
14                 import mx.rpc.http.HTTPService;
15                 import mx.controls.Alert;
16         import mx.events.ListEvent;
17                         
18                 import net.systemeD.halcyon.Globals;
19                 
20                 private var nominatim:HTTPService = new HTTPService;
21                 private var nominatimURL:String = "http://open.mapquestapi.com/nominatim/v1/search?";   // ** FIXME - this should be user-configurable
22                 private var nomOptions:Object = { addressdetails:"1", limit:"10", format:"xml" };
23         private var selectedResult:Object = {lat: "0", lon: "0"};
24
25                 [Bindable]
26         private var searchResults:XMLList;
27                                 
28                 
29                 public function init():void {
30                         PopUpManager.addPopUp(this, Application(Application.application), true);
31                         PopUpManager.centerPopUp(this);
32                         this.addEventListener(CloseEvent.CLOSE, findDialog_close);
33                         
34                         var obj:SharedObject = SharedObject.getLocal("user_state");
35         
36                 }
37                 
38                 private function findDialog_close(evt:CloseEvent):void {
39                         PopUpManager.removePopUp(this);
40                 }
41
42                 private function goFetch(q:String):void {
43
44                         var searchCall:String = nominatimURL;
45                         for (var item:String in nomOptions) {
46                                 searchCall += item + "="; 
47                                 searchCall += nomOptions[item] + "&";
48                         }
49                         searchCall += "q=" + q;
50                         this.nominatim.url = searchCall
51                         this.nominatim.addEventListener(ResultEvent.RESULT, goneFetched);
52                         this.nominatim.resultFormat = 'e4x';
53
54                         this.nominatim.send();
55                         
56                 }
57                 
58                 private function goneFetched(e:ResultEvent):void {
59                         searchResults = e.result.* as XMLList;
60                 }
61
62                 protected function btnFind_clickHandler(event:MouseEvent):void {
63                         goFetch(txtFind.text);
64                 }
65
66
67                 protected function txtFind_enterHandler(event:FlexEvent):void {
68                         goFetch(txtFind.text);
69                 }
70
71         private function setSelected(evt:ListEvent):void {
72             //Alert.show(evt.itemRenderer.data.@lat);
73             selectedResult = { lat: evt.itemRenderer.data.@lat, lon: evt.itemRenderer.data.@lon };
74         }
75         
76         private function goToSelected():void {
77             Application.application.theMap.moveMapFromLatLon(selectedResult.lat, selectedResult.lon);
78             //Application.application.theMap.redraw();            
79             PopUpManager.removePopUp(this);
80         }
81         
82         
83         ]]>
84         </mx:Script>
85
86         <mx:HBox horizontalAlign="left" width="100%">
87                 <mx:Label text="Enter place name: " />
88                 <mx:TextInput id="txtFind" width="100%" enter="txtFind_enterHandler(event)"/>
89                 <mx:Button label="Search" id="btnFind" click="btnFind_clickHandler(event)"/>            
90         </mx:HBox>
91         
92         <mx:Spacer height="10"/>
93         
94         <mx:DataGrid id="dgResults" dataProvider="{searchResults}" change="setSelected(event)" width="100%" height="100%">
95                 <mx:columns>
96                         <mx:DataGridColumn headerText="Name" dataField="@display_name" minWidth="200" />
97                         <mx:DataGridColumn headerText="Type" dataField="@type"/>
98                         <mx:DataGridColumn headerText="Class" dataField="@class"/>            
99                         <mx:DataGridColumn headerText="ID" dataField="@osm_id"/>
100         </mx:columns>
101         </mx:DataGrid>
102
103     <mx:ControlBar>
104         <mx:Label text="Results from Nominatim" />
105         <mx:Spacer width="100%"/>
106         <mx:Button label="Cancel" click="PopUpManager.removePopUp(this);" styleName="titleWindowButton" />
107         <mx:Button label="Jump to" click="goToSelected();" styleName="titleWindowButton" enabled="{dgResults.selectedItem != null? true : false}" />        
108     </mx:ControlBar>
109     
110 </mx:TitleWindow>