Don't let empty comments through
[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:ViewStack>
68
69   <mx:ControlBar>
70     <mx:Spacer width="100%"/>
71     <mx:Button id="cancelButton" label="Cancel" click="close();" styleName="titleWindowButton" />
72     <mx:Button id="saveButton" label="Save >" click="startSave();" styleName="titleWindowButton" />
73   </mx:ControlBar>
74   
75   <mx:Script><![CDATA[
76   
77     import mx.controls.*;
78     import mx.managers.PopUpManager;
79         import mx.core.Application;
80     import mx.events.DataGridEvent;
81     import mx.events.DataGridEventReason;
82     
83     import net.systemeD.halcyon.connection.*;
84     import net.systemeD.halcyon.AttentionEvent;
85     
86     private var conn:Connection = Connection.getConnectionInstance();
87         private var doSkip:Boolean = false;
88     
89     [Bindable]
90     private var failureText:String = "";
91
92         public function dontPrompt():void {
93                 if (processSequence.initialized) { skipInput(); } else { doSkip=true; }
94         }
95
96         private function skipInput():void {
97         processSequence.selectedChild = createChangesetTab;
98                 saveButton.enabled = false;
99                 changesetCreated();
100         }
101
102         private function commentChanged(event:Event):void {
103                 for (var i:int=changesetTags.length-1; i>0; i--) {
104                         if (changesetTags[i]['k']=='comment') { changesetTags.removeItemAt(i); }
105                 }
106                 if (event.target.text!='') changesetTags.addItem( { k:'comment', v: event.target.text } );
107     }
108
109         private function monitorEnter(event:TextEvent):void {
110                 if (event.text=="\n") { event.preventDefault(); startSave(); }
111         }
112
113         private function addNewTag():void {
114                 changesetTags.addItem( { k:'(new key)', v:'(new value)' } );
115                 advancedTagGrid.editedItemPosition = { rowIndex: changesetTags.length-1, columnIndex: 0 };
116         }
117
118         private function removeTag():void {
119                 changesetTags.removeItemAt(advancedTagGrid.selectedIndex);
120         }
121     
122     private function startSave():void {
123     
124         // move to next sequence
125         processSequence.selectedChild = createChangesetTab;
126         saveButton.enabled = false;
127         
128         var tags:Object = new Object();
129         for each (var tag:Object in changesetTags) {
130            tags[tag['k']] = tag['v'];
131         }
132         
133         // add the listeners
134         conn.addEventListener(Connection.NEW_CHANGESET, changesetCreated);
135         conn.addEventListener(Connection.NEW_CHANGESET_ERROR, changesetError);
136         conn.createChangeset(tags);
137     }
138
139     private function allowForEdit(event:DataGridEvent):void {
140         /* check before editing the tag grid that it's neither created_by nor version tags */
141         var item:Object = ((event.currentTarget as DataGrid).dataProvider as ArrayCollection)[event.rowIndex];
142         if(item.k == 'created_by' || item.k == 'version' || item.k == 'build') {
143           event.preventDefault();
144         }
145     }
146
147     private function verifyInput(event:DataGridEvent):void {
148         /* check before saving any change that the new key isn't created_by nor version */
149         // it is OK if the user cancels the edit
150         if( event.reason == DataGridEventReason.CANCELLED ) return;
151
152         var editor:TextInput = (event.currentTarget as DataGrid).itemEditorInstance as TextInput;
153
154         if( event.dataField == "k" ) {
155           if( editor.text == 'created_by' || editor.text == 'version' || editor.text == 'build') {
156             event.preventDefault();
157           }
158         }
159     }
160
161     private function changesetCreated(event:EntityEvent=null):void {
162         var changeset:Changeset = conn.getActiveChangeset();
163         addStatus("Saving to changeset "+changeset.id);
164         
165         saveProgress.label = "Uploading changes";
166         conn.addEventListener(Connection.SAVE_COMPLETED, saveCompleted);
167         conn.uploadChanges();
168     }
169     
170     private function changesetError(event:Event):void {
171         fail("Error creating changeset");
172     }
173     
174     private function saveCompleted(event:SaveCompleteEvent):void {
175         if ( event.saveOK ) {
176                         conn.dispatchEvent(new AttentionEvent(AttentionEvent.ALERT, null, "Changes successfully saved"));
177             close();
178         } else {
179             fail("Failure when uploading data");
180                 }
181         if (saveButton && saveButton.parent) saveButton.parent.removeChild(saveButton);
182         cancelButton.label = "Close";
183     }
184     
185     private function addStatus(text:String):void {
186         var label:Text = new Text();
187         label.text = text;
188         
189         infoBox.addChild(label);
190     }
191     
192     private function fail(text:String):void {
193         processSequence.selectedChild = failureTab;
194         failureText = text;
195     }
196     
197     private function close():void {
198         conn.removeEventListener(Connection.NEW_CHANGESET, changesetCreated);
199         conn.removeEventListener(Connection.NEW_CHANGESET_ERROR, changesetError);
200         conn.removeEventListener(Connection.SAVE_COMPLETED, saveCompleted);
201         PopUpManager.removePopUp(this);
202     }
203   ]]></mx:Script>
204 </mx:TitleWindow>
205