826749b3ce75eae1bf020228e52f063c3766eb27
[potlatch2.git] / net / systemeD / potlatch2 / save / SaveDialog.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:TitleWindow
3     xmlns:fx="http://ns.adobe.com/mxml/2009"
4         xmlns:mx="library://ns.adobe.com/flex/mx"
5         layout="vertical"
6         horizontalAlign="center" title="Save Changes"
7         width="350" height="330" verticalGap="0">
8
9   <fx:Declarations>
10   <mx:ArrayCollection id="changesetTags">
11     <fx:Object k="created_by" v="Potlatch 2"/>
12     <fx:Object k="version" v="{application.version}"/>
13     <fx:Object k="build" v="{application.build_number}"/>
14   </mx:ArrayCollection>
15   </fx:Declarations>
16   
17   <mx:ViewStack id="processSequence" width="100%" height="100%" 
18       creationPolicy="all" creationComplete="if (doSkip) skipInput() else comment.setFocus()">
19   
20     <!-- section for entering tags -->
21     <mx:VBox width="100%" height="100%" verticalGap="0">
22       <mx:ViewStack id="tagStack" width="100%" height="100%">
23         <mx:VBox width="100%" height="100%" label="Simple">
24           <mx:Text id="editDescription" width="100%">
25             <mx:text>
26                Please enter a description of your edits. This will be used to give other
27                mappers an idea of what changes you are making.
28             </mx:text>
29           </mx:Text>
30           <mx:HBox width="100%">
31             <mx:Image data="@Embed('../../../../embedded/read_this.png')" />
32             <mx:Text id="databaseWarning" width="100%">
33               <mx:text>
34                  Your changes are saved to the main OpenStreetMap database for everyone to 
35                  see. This is not your own private map. Please don&apos;t save test changes 
36                  or edits for personal use only.
37               </mx:text>
38             </mx:Text>
39           </mx:HBox>
40           <mx:Label id="commentLabel" text="Comment:"/>
41           <mx:TextArea id="comment" maxChars="255" width="100%" height="100%" change="commentChanged(event);" textInput="monitorEnter(event);" />
42         </mx:VBox>
43         
44         <mx:VBox width="100%" height="100%" label="Advanced">
45           <mx:Label text="Changeset tags:"/>
46           <mx:DataGrid editable="true" width="100%" id="advancedTagGrid"
47               dataProvider="{changesetTags}"
48               itemEditBeginning="allowForEdit(event)"
49               itemEditEnd="verifyInput(event)">
50             <mx:columns>
51                 <mx:DataGridColumn editable="true" dataField="k" headerText="Key">
52                     <mx:itemEditor><fx:Component><mx:TextInput restrict="&#x0020;-&#x10FFFF;" /></fx:Component></mx:itemEditor>
53                 </mx:DataGridColumn>
54                 <mx:DataGridColumn editable="true" dataField="v" headerText="Value">
55                     <mx:itemEditor><fx:Component><mx:TextInput restrict="&#x0020;-&#x10FFFF;" /></fx:Component></mx:itemEditor>
56                 </mx:DataGridColumn>
57             </mx:columns>
58           </mx:DataGrid>        
59                   <mx:HBox horizontalAlign="right" width="100%">
60                     <mx:LinkButton label="Delete" click="removeTag()" enabled="{advancedTagGrid.selectedItem != null? true : false}"/>
61                     <mx:LinkButton label="Add" click="addNewTag()"/>
62                   </mx:HBox>
63                   <mx:HRule width="100%" />
64         </mx:VBox>
65       </mx:ViewStack>
66       <mx:LinkBar dataProvider="{tagStack}"/>
67     </mx:VBox>
68     
69     <mx:VBox width="100%" height="100%" id="createChangesetTab">
70       <mx:VBox width="100%" height="100%" id="infoBox"/>
71       <mx:Spacer height="100%"/>
72       <mx:ProgressBar label="Creating changeset" labelPlacement="bottom" width="100%"
73           indeterminate="true" id="saveProgress"/>
74     </mx:VBox>
75     
76     <mx:VBox width="100%" height="100%" id="failureTab">
77       <mx:Text width="100%" styleName="failText" text="{failureText}"/>
78     </mx:VBox>
79
80     <mx:VBox width="100%" height="100%" id="dataTab">
81       <mx:TextArea width="100%" height="100%" id="dataText"/>
82     </mx:VBox>
83
84   </mx:ViewStack>
85
86   <mx:ControlBar>
87     <mx:Button id="dataButton" label="View data" visible="false" click="processSequence.selectedChild=dataTab" styleName="titleWindowButton" />
88     <mx:Spacer width="100%"/>
89     <mx:Button id="cancelButton" label="Cancel" click="close();" styleName="titleWindowButton" />
90     <mx:Button id="saveButton" label="Save >" click="startSave();" styleName="titleWindowButton" />
91   </mx:ControlBar>
92   
93   <fx:Script><![CDATA[
94   
95     import mx.controls.*;
96     import mx.managers.PopUpManager;
97         import mx.core.Application;
98         import mx.core.FlexGlobals;
99     import mx.events.DataGridEvent;
100     import mx.events.DataGridEventReason;
101     
102     import net.systemeD.halcyon.connection.*;
103     import net.systemeD.halcyon.AttentionEvent;
104     
105     private var _connection:Connection;
106         private var doSkip:Boolean = false;
107     
108     [Bindable] private var failureText:String = "";
109         [Bindable] private var application:Object = FlexGlobals.topLevelApplication;
110
111         public function setConnection(connection:Connection):void {
112                 _connection=connection;
113         }
114
115         public function dontPrompt():void {
116                 if (processSequence.initialized) { skipInput(); } else { doSkip=true; }
117         }
118
119         private function skipInput():void {
120         processSequence.selectedChild = createChangesetTab;
121                 saveButton.enabled = false;
122                 changesetCreated();
123         }
124
125         private function commentChanged(event:Event):void {
126                 for (var i:int=changesetTags.length-1; i>0; i--) {
127                         if (changesetTags[i]['k']=='comment') { changesetTags.removeItemAt(i); }
128                 }
129                 if (event.target.text!='') changesetTags.addItem( { k:'comment', v: event.target.text } );
130     }
131
132         private function monitorEnter(event:TextEvent):void {
133                 if (event.text=="\n") { event.preventDefault(); startSave(); }
134         }
135
136         private function addNewTag():void {
137                 changesetTags.addItem( { k:'(new key)', v:'(new value)' } );
138                 advancedTagGrid.editedItemPosition = { rowIndex: changesetTags.length-1, columnIndex: 0 };
139         }
140
141         private function removeTag():void {
142                 changesetTags.removeItemAt(advancedTagGrid.selectedIndex);
143         }
144     
145     private function startSave():void {
146     
147         // move to next sequence
148         processSequence.selectedChild = createChangesetTab;
149         saveButton.enabled = false;
150         
151         var tags:Object = new Object();
152         for each (var tag:Object in changesetTags) {
153            tags[tag['k']] = tag['v'];
154         }
155         
156         // add the listeners
157         _connection.addEventListener(Connection.NEW_CHANGESET, changesetCreated);
158         _connection.addEventListener(Connection.NEW_CHANGESET_ERROR, changesetError);
159         _connection.createChangeset(tags);
160     }
161
162     private function allowForEdit(event:DataGridEvent):void {
163         /* check before editing the tag grid that it's neither created_by nor version tags */
164         var item:Object = ((event.currentTarget as DataGrid).dataProvider as ArrayCollection)[event.rowIndex];
165         if(item.k == 'created_by' || item.k == 'version' || item.k == 'build') {
166           event.preventDefault();
167         }
168     }
169
170     private function verifyInput(event:DataGridEvent):void {
171         /* check before saving any change that the new key isn't created_by nor version */
172         // it is OK if the user cancels the edit
173         if( event.reason == DataGridEventReason.CANCELLED ) return;
174
175         var editor:TextInput = (event.currentTarget as DataGrid).itemEditorInstance as TextInput;
176
177         if( event.dataField == "k" ) {
178           if( editor.text == 'created_by' || editor.text == 'version' || editor.text == 'build') {
179             event.preventDefault();
180           }
181         }
182     }
183
184     private function changesetCreated(event:EntityEvent=null):void {
185         var changeset:Changeset = _connection.getActiveChangeset();
186         addStatus("Saving to changeset "+changeset.id);
187         
188         saveProgress.label = "Uploading changes";
189         _connection.addEventListener(Connection.SAVE_COMPLETED, saveCompleted);
190         var rawData:*=_connection.uploadChanges();
191         if (rawData) {
192             dataText.text=rawData.toString();
193             dataButton.visible=true;
194         }
195     }
196     
197     private function changesetError(event:Event):void {
198         fail("Error creating changeset");
199     }
200     
201     private function saveCompleted(event:SaveCompleteEvent):void {
202         if ( event.saveOK ) {
203                         _connection.dispatchEvent(new AttentionEvent(AttentionEvent.ALERT, null, "Changes successfully saved"));
204             if (processSequence.selectedChild!=dataTab) close();
205         } else {
206             fail("Failure when uploading data");
207                 }
208         if (saveButton && saveButton.parent) saveButton.parent.removeChild(saveButton);
209         cancelButton.label = "Close";
210     }
211     
212     private function addStatus(text:String):void {
213         var label:Text = new Text();
214         label.text = text;
215         
216         infoBox.addChild(label);
217     }
218     
219     private function fail(text:String):void {
220         processSequence.selectedChild = failureTab;
221         failureText = text;
222     }
223
224     private function close():void {
225         _connection.removeEventListener(Connection.NEW_CHANGESET, changesetCreated);
226         _connection.removeEventListener(Connection.NEW_CHANGESET_ERROR, changesetError);
227         _connection.removeEventListener(Connection.SAVE_COMPLETED, saveCompleted);
228         PopUpManager.removePopUp(this);
229     }
230   ]]></fx:Script>
231 </mx:TitleWindow>
232