PageRenderTime 76ms CodeModel.GetById 61ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/src/org/ishafoundation/archives/transcript/components/generic/UnhandledChangesPopUpManager.as

http://transcriptstudio4isha.googlecode.com/
ActionScript | 82 lines | 60 code | 4 blank | 18 comment | 17 complexity | 6087dcb654a3d4be7fe1d0ef927a717a MD5 | raw file
 1package org.ishafoundation.archives.transcript.components.generic
 2{
 3	import mx.controls.Alert;
 4	import mx.events.CloseEvent;
 5	
 6	/**
 7	 * Convenience class for displaying unsaved changes popup and calling the appropriate functions
 8	 */
 9	public class UnhandledChangesPopUpManager
10	{
11		public static var DISPLAYED_NOW:Boolean = false;
12		
13		public function UnhandledChangesPopUpManager()
14		{
15		}
16		
17		/**
18		 * syncSaveChangesFunction()
19		 * nextFunction()
20		 * 
21		 * if unsavedChanges is false, then simply nextFunction() is called.
22		 */
23		public static function displayIfNecessaryUsingSyncFunc(unsavedChanges:Boolean, syncSaveChangesFunc:Function, nextFunction:Function):Boolean {
24			if (DISPLAYED_NOW) {
25				trace("Not displaying Alert because already displayed");
26				return false;
27			}
28			if (unsavedChanges) {
29				// check whether the users wants to save
30				DISPLAYED_NOW = true;
31				Alert.show("Do you want to commit changes?", "Uncommitted Changes", Alert.YES | Alert.NO | Alert.CANCEL, null, function (event:CloseEvent):void {
32					DISPLAYED_NOW = false;
33					if (event.detail==Alert.YES) {
34						syncSaveChangesFunc();
35						nextFunction();
36					}				
37					else if (event.detail==Alert.NO) {
38						nextFunction();
39					}
40					else if (event.detail==Alert.CANCEL) {
41						// do nothing
42					}
43					else {
44						throw new Error("Unexpected event type: " + event);
45					}
46				});
47			}
48			else {
49				nextFunction();
50			}
51			return true;
52		}
53		
54		/**
55		 * asyncSaveChangesFunction(nextFunction(), saveChangesFailureFunction(String))
56		 * 
57		 * if unsavedChanges is false, then simply nextFunction() is called.
58		 */
59		public static function displayIfNecessaryUsingAsyncFunc(unsavedChanges:Boolean, asyncSaveChangesFunction:Function, saveChangesFailureFunction:Function, nextFunction:Function):void {
60			if (unsavedChanges) {
61				// check whether the users wants to save
62				Alert.show("Do you want to save changes?", "Unsaved Changes", Alert.YES | Alert.NO | Alert.CANCEL, null, function (event:CloseEvent):void {
63					if (event.detail==Alert.YES) {
64						asyncSaveChangesFunction(nextFunction, saveChangesFailureFunction);
65					}				
66					else if (event.detail==Alert.NO) {
67						nextFunction();
68					}
69					else if (event.detail==Alert.CANCEL) {
70						// do nothing
71					}
72					else {
73						throw new Error("Unexpected event type: " + event);
74					}
75				});
76			}
77			else {
78				nextFunction();
79			}				
80		}
81	}
82}