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