diff options
| author | Jules Laplace <jules@okfoc.us> | 2015-09-17 21:26:52 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2015-09-17 21:26:52 -0400 |
| commit | 5d68db92b37545fb03332a46b01d0f1aad8ce64b (patch) | |
| tree | 28aea3c304ab5adc01236f60b719dbaff834c375 /StoneIsland/platforms/ios/www/js/vendor/loader.js | |
| parent | 58bf27362c0b3d1d41e135aa8bdbd38297a4d0f9 (diff) | |
populate selector
Diffstat (limited to 'StoneIsland/platforms/ios/www/js/vendor/loader.js')
| -rw-r--r-- | StoneIsland/platforms/ios/www/js/vendor/loader.js | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/StoneIsland/platforms/ios/www/js/vendor/loader.js b/StoneIsland/platforms/ios/www/js/vendor/loader.js new file mode 100644 index 00000000..cc9644f8 --- /dev/null +++ b/StoneIsland/platforms/ios/www/js/vendor/loader.js @@ -0,0 +1,97 @@ +var Loader = Loader || (function(){ + function Loader (readyCallback, view){ + this.assets = {}; + this.images = []; + this.readyCallback = readyCallback; + this.count = 0 + this.view = view + this.loaded = false + } + + // Register an asset as loading + Loader.prototype.register = function(s){ + this.assets[s] = false; + this.count += 1 + } + + // Signal that an asset has loaded + Loader.prototype.ready = function(s){ + window.debug && console.log("ready >> " + s); + + this.assets[s] = true; + if (this.loaded) return; + + this.view && this.view.update( this.percentRemaining() ) + + if (! this.isReady()) return; + + this.loaded = true; + if (this.view) { + this.view && this.view.finish(this.readyCallback) + } + else { + this.readyCallback && this.readyCallback(); + } + } + + // (boolean) Is the loader ready? + Loader.prototype.isReady = function(){ + for (var s in this.assets) { + if (this.assets.hasOwnProperty(s) && this.assets[s] != true) { + return false; + } + } + return true; + } + + // (float) Percentage of assets remaining + Loader.prototype.percentRemaining = function(){ + return this.remainingAssets() / this.count + } + + // (int) Number of assets remaining + Loader.prototype.remainingAssets = function(){ + var n = 0; + for (var s in this.assets) { + if (this.assets.hasOwnProperty(s) && this.assets[s] != true) { + n++; + // console.log('remaining: ' + s); + } + } + return n; + } + + // Preload the images in config.images + Loader.prototype.preloadImages = function(images){ + this.register("preload"); + for (var i = 0; i < images.length; i++) { + this.preloadImage(images[i]); + } + this.ready("preload"); + } + Loader.prototype.preloadImage = function(src, register, cb){ + if (! src || src == "none") return; + var _this = this; + if (! cb && typeof register !== "string") { + cb = register + register = null + } + if (register) { + this.register(src); + } + var img = new Image(); + img.onload = function(){ + if (cb) { + cb(img); + } + if (register) { + _this.ready(src); + } + } + img.src = src; + if (img.complete) img.onload(); + _this.images.push(img); + } + + return Loader; +})(); |
