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