automatically select comment TextField on clicking 'save'
[potlatch2.git] / net / systemeD / potlatch2 / save / SaveManager.as
1 package net.systemeD.potlatch2.save {
2
3     import flash.events.*;
4     import flash.net.*;
5     import mx.managers.PopUpManager;
6     import mx.core.Application;
7     import net.systemeD.halcyon.connection.*;
8     import org.iotashan.oauth.*;
9
10     public class SaveManager {
11     
12         private static var instance:SaveManager = new SaveManager();
13
14         public static function saveChanges():void {
15             instance.save(instance.saveData);
16         }
17
18         public static function ensureAccess(callback:Function):void {
19             instance.save(callback);
20         }
21
22         private function save(callback:Function):void {
23             var conn:Connection = Connection.getConnectionInstance();
24             if (conn.hasAccessToken()) {
25                 callback();
26             } else {
27                 getNewToken(callback);
28             }
29         }
30
31         private function getNewToken(onCompletion:Function):void {
32             var oauthPanel:OAuthPanel = OAuthPanel(
33                 PopUpManager.createPopUp(Application(Application.application), OAuthPanel, true));
34             PopUpManager.centerPopUp(oauthPanel);
35             
36             var listener:Function = function(event:Event):void {
37                 var accessToken:OAuthToken = oauthPanel.accessToken;
38                 if ( oauthPanel.shouldRemember ) {
39                     var obj:SharedObject = SharedObject.getLocal("access_token");
40                     obj.setProperty("oauth_token", accessToken.key);
41                     obj.setProperty("oauth_token_secret", accessToken.secret);
42                     obj.flush();
43                 }
44                 onCompletion();
45             }
46             oauthPanel.addEventListener(OAuthPanel.ACCESS_TOKEN_EVENT, listener);
47         }
48         
49         private function saveData():void {
50             var saveDialog:SaveDialog = SaveDialog(
51                 PopUpManager.createPopUp(Application(Application.application), SaveDialog, true));
52             PopUpManager.centerPopUp(saveDialog);
53
54                         if (Connection.getConnectionInstance().getActiveChangeset()) {
55                                 saveDialog.dontPrompt();
56                         }
57         }
58     }
59     
60 }
61