PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/DiegoRay/actionscripts/org/casaframework/load/base/Load.as

https://github.com/joemaffia/flash-junk
ActionScript | 180 lines | 89 code | 30 blank | 61 comment | 9 complexity | bf6daf2efa3c0cda9089d1e5e7d3becb MD5 | raw file
  1. /*
  2. CASA Framework for ActionScript 2.0
  3. Copyright (C) 2007 CASA Framework
  4. http://casaframework.org
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. import org.casaframework.event.EventDispatcher;
  18. import org.casaframework.load.base.LoadInterface;
  19. import org.casaframework.time.FrameDelay;
  20. /**
  21. Base load class. Class needs to be extended further to function properly.
  22. @author Aaron Clinger
  23. @version 05/06/07
  24. @since Flash Player 7
  25. */
  26. class org.casaframework.load.base.Load extends EventDispatcher implements LoadInterface {
  27. public static var EVENT_LOAD_START:String = 'onLoadStart';
  28. public static var EVENT_LOAD_ERROR:String = 'onLoadError';
  29. public static var EVENT_LOAD_COMPLETE:String = 'onLoadComplete';
  30. public static var EVENT_LOAD_INIT:String = 'onLoadInit';
  31. private var $filePath:String;
  32. private var $target:Object;
  33. private var $hasLoaded:Boolean;
  34. private var $isLoading:Boolean;
  35. private var $frameDelay:FrameDelay;
  36. /**
  37. @param target: A path to a container where the file specified by <code>filePath</code> should be loaded into.
  38. @param filePath: The absolute or relative URL of the file to be loaded.
  39. */
  40. private function Load(target:Object, filePath:String) {
  41. super();
  42. this.$target = target;
  43. this.$filePath = filePath;
  44. this.$hasLoaded = this.$isLoading = false;
  45. this.$setClassDescription('org.casaframework.load.base.Load');
  46. }
  47. /**
  48. Begins the loading process and broadcasts events to observers.
  49. @usageNote Use {@link EventDispatcher#addEventObserver} to listen for broadcasted events.
  50. @sends onLoadStart = function(sender:Load) {}
  51. */
  52. public function start():Void {
  53. if (this.isLoading() || this.hasLoaded())
  54. return;
  55. if (this.$filePath == undefined || this.$target == undefined) {
  56. this.dispatchEvent(Load.EVENT_LOAD_ERROR, this);
  57. return;
  58. }
  59. this.$startLoad();
  60. this.dispatchEvent(Load.EVENT_LOAD_START, this);
  61. }
  62. /**
  63. Unloads a file that has previously loaded, or cancels a currently loading file from completing.
  64. @usageNote If you issue this command while a file is loading, event <code>onLoadError</code> is also invoked.
  65. */
  66. public function stop():Void {
  67. if (this.isLoading()) {
  68. this.$stopLoad();
  69. this.dispatchEvent(Load.EVENT_LOAD_ERROR, this);
  70. return;
  71. }
  72. this.$stopLoad();
  73. }
  74. public function getFilePath():String {
  75. return this.$filePath;
  76. }
  77. public function hasLoaded():Boolean {
  78. return this.$hasLoaded;
  79. }
  80. public function isLoading():Boolean {
  81. return this.$isLoading;
  82. }
  83. public function destroy():Void {
  84. this.$clean();
  85. delete this.$hasLoaded;
  86. delete this.$filePath;
  87. delete this.$target;
  88. super.destroy();
  89. }
  90. /**
  91. <strong>This function needs to be extended by a subclass.</strong>
  92. */
  93. private function $startLoad():Void {
  94. this.$isLoading = true;
  95. this.$hasLoaded = false;
  96. }
  97. /**
  98. <strong>This function needs to be extended by a subclass.</strong>
  99. */
  100. private function $stopLoad():Void {
  101. this.$clean();
  102. this.$hasLoaded = false;
  103. }
  104. /**
  105. <strong>This function needs to be called by a subclass.</strong>
  106. If target in subclass has an reliable <code>"onLoad"</code> handler call this method after target is defined in the constructor.
  107. @param loadContainer: <strong>[optional]</strong> Defines object file is loading into and has the event handler <code>"onLoad"</code>; defaults to <code>$target</code>.
  108. */
  109. private function $remapOnLoadHandler(loadContainer:Object):Void {
  110. var _this:Load = this;
  111. var targ:Object = (loadContainer == undefined) ? this.$target : loadContainer;
  112. targ.onLoad = function(success:Boolean):Void {
  113. _this.$onLoad(success);
  114. };
  115. }
  116. private function $onLoad(success:Boolean):Void {
  117. if (success)
  118. this.$onComplete();
  119. }
  120. /**
  121. @sends onLoadComplete = function(sender:Load) {}
  122. @sends onLoadError = function(sender:Load) {}
  123. */
  124. private function $onComplete():Void {
  125. this.$hasLoaded = true;
  126. this.dispatchEvent(Load.EVENT_LOAD_COMPLETE, this);
  127. this.$frameDelay = new FrameDelay(this, '$onInitialized');
  128. this.$frameDelay.start();
  129. }
  130. /**
  131. @sends onLoadInit = function(sender:Load) {}
  132. */
  133. private function $onInitialized():Void {
  134. this.dispatchEvent(Load.EVENT_LOAD_INIT, this);
  135. this.$clean();
  136. }
  137. private function $clean():Void {
  138. this.$frameDelay.destroy();
  139. delete this.$frameDelay;
  140. delete this.$isLoading;
  141. }
  142. }