|
| 1 | +/* |
| 2 | + * DefaultLoader |
| 3 | + * Visit http://createjs.com/ for documentation, updates and examples. |
| 4 | + * |
| 5 | + * |
| 6 | + * Copyright (c) 2012 gskinner.com, inc. |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person |
| 9 | + * obtaining a copy of this software and associated documentation |
| 10 | + * files (the "Software"), to deal in the Software without |
| 11 | + * restriction, including without limitation the rights to use, |
| 12 | + * copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + * copies of the Software, and to permit persons to whom the |
| 14 | + * Software is furnished to do so, subject to the following |
| 15 | + * conditions: |
| 16 | + * |
| 17 | + * The above copyright notice and this permission notice shall be |
| 18 | + * included in all copies or substantial portions of the Software. |
| 19 | + * |
| 20 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 21 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 22 | + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 23 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 24 | + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 25 | + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 26 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 27 | + * OTHER DEALINGS IN THE SOFTWARE. |
| 28 | + */ |
| 29 | + |
| 30 | +// namespace: |
| 31 | +this.createjs = this.createjs || {}; |
| 32 | + |
| 33 | +(function () { |
| 34 | + "use strict"; |
| 35 | + |
| 36 | + /** |
| 37 | + * An internal helper class that preloads audio. Note that this class and its methods are not documented |
| 38 | + * properly to avoid generating HTML documentation. |
| 39 | + * #class DefaultLoader |
| 40 | + * @param {String} src The source of the sound to load. |
| 41 | + * @param {Object} owner A reference to the class that created this instance. |
| 42 | + * @constructor |
| 43 | + */ |
| 44 | + function DefaultLoader(src) { |
| 45 | + this.EventDispatcher_constructor(); |
| 46 | + this._init(src); |
| 47 | + } |
| 48 | + var p = createjs.extend(DefaultLoader, createjs.EventDispatcher); |
| 49 | + |
| 50 | + /** |
| 51 | + * If load results in an object, it is stored as result |
| 52 | + * #property result |
| 53 | + * @type {Object} |
| 54 | + */ |
| 55 | + p.result = null; |
| 56 | + |
| 57 | + /** |
| 58 | + * Indicates the percentage of loading progress, between 0 and 1. |
| 59 | + * #property progress |
| 60 | + * @type {number} |
| 61 | + */ |
| 62 | + p.progress = -1; |
| 63 | + |
| 64 | + /** |
| 65 | + * The source of the sound to load. Used by callback functions when we return this class. |
| 66 | + * #property src |
| 67 | + * @type {String} |
| 68 | + */ |
| 69 | + p.src = null; |
| 70 | + |
| 71 | + /** |
| 72 | + * The result of the loading operation |
| 73 | + * #property _result |
| 74 | + * @type {Object} |
| 75 | + * @protected |
| 76 | + */ |
| 77 | + p._result = null; |
| 78 | + |
| 79 | + // Calbacks |
| 80 | + /** |
| 81 | + * The callback that fires when the load completes. |
| 82 | + * #property onload |
| 83 | + * #event load |
| 84 | + * @type {Method} |
| 85 | + */ |
| 86 | + p.onload = null; |
| 87 | + |
| 88 | + /** |
| 89 | + * The callback that fires when data is being fetched at a rate that would allow playback without interruption. This follows HTML tag naming. |
| 90 | + * #property oncanplaythrough |
| 91 | + * #event canplaythrough |
| 92 | + * @type {Method} |
| 93 | + */ |
| 94 | + p.oncanplaythrough = null; |
| 95 | + |
| 96 | + /** |
| 97 | + * The callback that fires as the load progresses. This follows HTML tag naming. |
| 98 | + * #property onprogress |
| 99 | + * #event progress |
| 100 | + * @type {Method} |
| 101 | + */ |
| 102 | + p.onprogress = null; |
| 103 | + |
| 104 | + /** |
| 105 | + * The callback that fires if the load hits an error. This follows HTML tag naming. |
| 106 | + * #property onerror |
| 107 | + * #event error |
| 108 | + * @type {Method} |
| 109 | + * @protected |
| 110 | + */ |
| 111 | + p.onerror = null; |
| 112 | + |
| 113 | + // constructor |
| 114 | + p._init = function (src) { |
| 115 | + this.src = src; |
| 116 | + }; |
| 117 | + |
| 118 | + /** |
| 119 | + * Begin loading the content. |
| 120 | + * #method load |
| 121 | + * @param {String} src The path to the sound. |
| 122 | + */ |
| 123 | + p.load = function (src) { |
| 124 | + if (src != null) {this.src = src;} |
| 125 | + // plugin specific code |
| 126 | + }; |
| 127 | + |
| 128 | + /** |
| 129 | + * The DefaultLoader has reported progress. |
| 130 | + * |
| 131 | + * <strong>Note</strong>: this is not a public API, but is used to allow preloaders to subscribe to load |
| 132 | + * progress as if this is an HTML audio tag. This reason is why this still uses a callback instead of an event. |
| 133 | + * #method handleProgress |
| 134 | + * @param {event} event Progress event that gives event.loaded and event.total if server is configured correctly |
| 135 | + * @protected |
| 136 | + */ |
| 137 | + p.handleProgress = function (event) { |
| 138 | + this.progress = event.loaded / event.total; |
| 139 | + var e = new createjs.Event("progress"); |
| 140 | + e.loaded = event.loaded; |
| 141 | + e.total = event.total; |
| 142 | + e.progress = this.progress; |
| 143 | + |
| 144 | + this.dispatchEvent(e); |
| 145 | + this.onprogress && this.onprogress(e); |
| 146 | + }; |
| 147 | + |
| 148 | + /** |
| 149 | + * The sound has completed loading. |
| 150 | + * #method handleLoad |
| 151 | + * @protected |
| 152 | + */ |
| 153 | + p.handleLoad = function (event) { |
| 154 | + // TODO consider params of event |
| 155 | + this.progress = 1; |
| 156 | + var e = new createjs.Event("load"); |
| 157 | + e.target = this; |
| 158 | + this.dispatchEvent(e) |
| 159 | + this.onload && this.onload(e); |
| 160 | + }; |
| 161 | + |
| 162 | + /** |
| 163 | + * The sound has loading enough to play through without interruption at the current download rate. |
| 164 | + * #method handleLoad |
| 165 | + * @protected |
| 166 | + */ |
| 167 | + p.handleCanPlayThrough = function (event) { |
| 168 | + // TODO double check params of canplaythrough event |
| 169 | + var e = new createjs.Event("canplaythrough"); |
| 170 | + e.target = this; |
| 171 | + this.dispatchEvent(e) |
| 172 | + this.oncanplaythrough && this.oncanplaythrough(e) |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Errors have been caused by the DefaultLoader. |
| 177 | + * #method handleError |
| 178 | + * @protected |
| 179 | + */ |
| 180 | + p.handleError = function (event) { |
| 181 | + this.dispatchEvent (event); |
| 182 | + this.onerror && this.onerror(event); |
| 183 | + }; |
| 184 | + |
| 185 | + /** |
| 186 | + * Remove all external references from loader |
| 187 | + * #method cleanUp |
| 188 | + */ |
| 189 | + p.cleanUp = function () { |
| 190 | + this.src = null; |
| 191 | + this.onload = null; |
| 192 | + this.onprogress = null; |
| 193 | + this.onerror = null; |
| 194 | + this.removeAllEventListeners(); |
| 195 | + }; |
| 196 | + |
| 197 | + p.toString = function () { |
| 198 | + return "[DefaultLoader]"; |
| 199 | + }; |
| 200 | + |
| 201 | + createjs.DefaultLoader = createjs.promote(DefaultLoader, "EventDispatcher"); |
| 202 | +}()); |
0 commit comments