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