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