/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

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