Permalink
Browse files

Normalize formatting

  • Loading branch information...
1 parent f13f093 commit 1b55ac857c57611b245ecbd3db48f7c0fa63a16b @joelfillmore joelfillmore committed Jul 15, 2012
Showing with 105 additions and 104 deletions.
  1. +70 −75 PxLoader.js
  2. +10 −11 PxLoaderImage.js
  3. +18 −11 PxLoaderSound.js
  4. +7 −7 PxLoaderVideo.js
View
145 PxLoader.js
@@ -2,6 +2,7 @@
* PixelLab Resource Loader
* Loads resources while providing progress updates.
*/
+
function PxLoader(settings) {
// merge settings with defaults
@@ -22,10 +23,10 @@ function PxLoader(settings) {
settings.noProgressTimeout = Infinity; // do not stop waiting by default
}
- var entries = [], // holds resources to be loaded with their status
+ var entries = [],
+ // holds resources to be loaded with their status
progressListeners = [],
- timeStarted,
- progressChanged = +new Date;
+ timeStarted, progressChanged = +new Date;
/**
* The status of a resource
@@ -49,7 +50,7 @@ function PxLoader(settings) {
return val;
}
- return [ val ];
+ return [val];
};
// add an entry to the list of resources to be loaded
@@ -97,16 +98,15 @@ function PxLoader(settings) {
bestIndex = Infinity;
for (var i = 0; i < resource.tags.length; i++) {
for (var j = 0; j < Math.min(orderedTags.length, bestIndex); j++) {
- if(resource.tags[i] == orderedTags[j] && j < bestIndex) {
- bestIndex = j;
- if (bestIndex === 0) break;
- }
- if (bestIndex === 0) break;
- }
+ if (resource.tags[i] == orderedTags[j] && j < bestIndex) {
+ bestIndex = j;
+ if (bestIndex === 0) break;
+ }
+ if (bestIndex === 0) break;
+ }
}
return bestIndex;
};
-
return function(a, b) {
// check tag order first
var aOrder = getTagOrder(a),
@@ -152,14 +152,15 @@ function PxLoader(settings) {
}
// see if the resource has loaded
- entry.resource.checkStatus();
+ if (entry.resource.checkStatus) {
+ entry.resource.checkStatus();
+ }
// if still waiting, mark as timed out or make sure we check again
if (entry.status === ResourceState.WAITING) {
if (timedOut) {
entry.resource.onTimeout();
- }
- else {
+ } else {
checkAgain = true;
}
}
@@ -177,8 +178,7 @@ function PxLoader(settings) {
this.isBusy = function() {
for (var i = 0, len = entries.length; i < len; i++) {
- if (entries[i].status === ResourceState.QUEUED ||
- entries[i].status === ResourceState.WAITING) {
+ if (entries[i].status === ResourceState.QUEUED || entries[i].status === ResourceState.WAITING) {
return true;
}
}
@@ -188,7 +188,7 @@ function PxLoader(settings) {
var onProgress = function(resource, statusType) {
// find the entry for the resource
var entry = null;
- for(var i=0, len = entries.length; i < len; i++) {
+ for (var i = 0, len = entries.length; i < len; i++) {
if (entries[i].resource === resource) {
entry = entries[i];
break;
@@ -212,8 +212,7 @@ function PxLoader(settings) {
if (listener.tags.length === 0) {
// no tags specified so always tell the listener
shouldCall = true;
- }
- else {
+ } else {
// listener only wants to hear about certain tags
shouldCall = resource.tags.contains(listener.tags);
}
@@ -246,16 +245,13 @@ function PxLoader(settings) {
if (listener.tags.length === 0) {
// no tags specified so always tell the listener
includeResource = true;
- }
- else {
+ } else {
includeResource = entry.resource.tags.contains(listener.tags);
}
if (includeResource) {
total++;
- if (entry.status === ResourceState.LOADED ||
- entry.status === ResourceState.ERROR ||
- entry.status === ResourceState.TIMEOUT) {
+ if (entry.status === ResourceState.LOADED || entry.status === ResourceState.ERROR || entry.status === ResourceState.TIMEOUT) {
completed++;
}
}
@@ -319,64 +315,63 @@ function PxLoader(settings) {
};
}
-
// Tag object to handle tag intersection; once created not meant to be changed
// Performance rationale: http://jsperf.com/lists-indexof-vs-in-operator/3
+
function PxLoaderTags(values) {
- this.array = [];
- this.object = {};
- this.value = null; // single value
- this.length = 0;
-
- if (values !== null && values !== undefined) {
- if(Array.isArray(values)) {
- this.array = values;
- } else if(typeof values === 'object') {
- for(var key in values) {
- this.array.push(key);
- }
- } else {
- this.array.push(values);
- this.value = values;
- }
-
- this.length = this.array.length;
-
- // convert array values to object with truthy values, used by contains function below
- for(var i = 0; i < this.length; i++) {
- this.object[this.array[i]] = true;
- }
- }
-
- // compare this object with another; return true if they share at least one value
- this.contains = function(other) {
- if(this.length === 0 || other.length === 0) {
- return false;
- } else if(this.length === 1) {
- if (other.length === 1) {
- return this.value === other.value;
- } else {
- return other.object.hasOwnProperty(this.value);
- }
- } else if(other.length < this.length) {
- return other.contains(this); // better to loop through the smaller object
- } else {
- for(var key in this.object) {
- if(other.object[key]) {
- return true;
- }
- }
- return false;
- }
- }
+ this.array = [];
+ this.object = {};
+ this.value = null; // single value
+ this.length = 0;
+
+ if (values !== null && values !== undefined) {
+ if (Array.isArray(values)) {
+ this.array = values;
+ } else if (typeof values === 'object') {
+ for (var key in values) {
+ this.array.push(key);
+ }
+ } else {
+ this.array.push(values);
+ this.value = values;
+ }
+
+ this.length = this.array.length;
+
+ // convert array values to object with truthy values, used by contains function below
+ for (var i = 0; i < this.length; i++) {
+ this.object[this.array[i]] = true;
+ }
+ }
+
+ // compare this object with another; return true if they share at least one value
+ this.contains = function(other) {
+ if (this.length === 0 || other.length === 0) {
+ return false;
+ } else if (this.length === 1) {
+ if (other.length === 1) {
+ return this.value === other.value;
+ } else {
+ return other.object.hasOwnProperty(this.value);
+ }
+ } else if (other.length < this.length) {
+ return other.contains(this); // better to loop through the smaller object
+ } else {
+ for (var key in this.object) {
+ if (other.object[key]) {
+ return true;
+ }
+ }
+ return false;
+ }
+ }
}
// shims to ensure we have newer Array utility methods
-
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArray
if (!Array.isArray) {
- Array.isArray = function(arg) {
- return Object.prototype.toString.call(arg) == '[object Array]';
- };
+ Array.isArray = function(arg) {
+ return Object.prototype.toString.call(arg) == '[object Array]';
+ };
}
View
21 PxLoaderImage.js
@@ -1,8 +1,8 @@
// @depends PxLoader.js
-
/**
* PxLoader plugin to load images
*/
+
function PxLoaderImage(url, tags, priority) {
var self = this,
loader = null;
@@ -11,7 +11,7 @@ function PxLoaderImage(url, tags, priority) {
this.tags = tags;
this.priority = priority;
- var onReadyStateChange = function () {
+ var onReadyStateChange = function() {
if (self.img.readyState == 'complete') {
removeEventHandlers();
loader.onLoad(self);
@@ -62,8 +62,7 @@ function PxLoaderImage(url, tags, priority) {
removeEventHandlers();
if (self.img.complete) {
loader.onLoad(self);
- }
- else {
+ } else {
loader.onTimeout(self);
}
};
@@ -72,22 +71,22 @@ function PxLoaderImage(url, tags, priority) {
this.getName = function() {
return url;
};
-
- // cross-browser event binding
+
+ // cross-browser event binding
this.bind = function(eventName, eventHandler) {
if (self.img.addEventListener) {
- self.img.addEventListener(eventName, eventHandler, false);
+ self.img.addEventListener(eventName, eventHandler, false);
} else if (self.img.attachEvent) {
- self.img.attachEvent('on'+eventName, eventHandler);
+ self.img.attachEvent('on' + eventName, eventHandler);
}
};
- // cross-browser event un-binding
+ // cross-browser event un-binding
this.unbind = function(eventName, eventHandler) {
if (self.img.removeEventListener) {
self.img.removeEventListener(eventName, eventHandler, false);
} else if (self.img.detachEvent) {
- self.img.detachEvent('on'+eventName, eventHandler);
+ self.img.detachEvent('on' + eventName, eventHandler);
}
};
@@ -100,4 +99,4 @@ PxLoader.prototype.addImage = function(url, tags, priority) {
// return the img element to the caller
return imageLoader.img;
-};
+};
View
29 PxLoaderSound.js
@@ -1,8 +1,8 @@
// @depends PxLoader.js
-
/**
* PxLoader plugin to load sound using SoundManager2
*/
+
function PxLoaderSound(id, url, tags, priority) {
var self = this,
loader = null;
@@ -13,12 +13,16 @@ function PxLoaderSound(id, url, tags, priority) {
'id': id,
'url': url,
'autoLoad': false,
- 'onload': function() { loader.onLoad(self); },
+ 'onload': function() {
+ loader.onLoad(self);
+ },
// HTML5-only event: Fires when a browser has chosen to stop downloading.
// "The user agent is intentionally not currently fetching media data,
// but does not have the entire media resource downloaded."
- 'onsuspend': function() { loader.onTimeout(self); },
+ 'onsuspend': function() {
+ loader.onTimeout(self);
+ },
// Fires at a regular interval when a sound is loading and new data
// has been received.
@@ -27,7 +31,6 @@ function PxLoaderSound(id, url, tags, priority) {
bytesTotal = this['bytesTotal'];
// TODO: provide percentage complete updates to loader?
-
// see if we have loaded the file
if (bytesLoaded > 0 && (bytesLoaded === bytesTotal)) {
loader.onLoad(self);
@@ -47,21 +50,25 @@ function PxLoaderSound(id, url, tags, priority) {
var iDevice = navigator.userAgent.match(/(ipad|iphone|ipod)/i);
if (iDevice) {
loader.onTimeout(self);
- }
- else {
+ } else {
this.sound['load']();
}
};
this.checkStatus = function() {
switch(self.sound['readyState']) {
- case 0: // uninitialised
- case 1: // loading
+ case 0:
+ // uninitialised
+ break;
+ case 1:
+ // loading
break;
- case 2: // failed/error
+ case 2:
+ // failed/error
loader.onError(self);
break;
- case 3: // loaded/success
+ case 3:
+ // loaded/success
loader.onLoad(self);
break;
}
@@ -81,4 +88,4 @@ PxLoader.prototype.addSound = function(id, url, tags, priority) {
var soundLoader = new PxLoaderSound(id, url, tags, priority);
this.add(soundLoader);
return soundLoader.sound;
-};
+};
View
14 PxLoaderVideo.js
@@ -1,8 +1,8 @@
// @depends PxLoader.js
-
/**
* PxLoader plugin to load video elements
*/
+
function PxLoaderVideo(url, tags, priority) {
var self = this;
var loader = null;
@@ -16,7 +16,7 @@ function PxLoaderVideo(url, tags, priority) {
this.tags = tags;
this.priority = priority;
- var onReadyStateChange = function () {
+ var onReadyStateChange = function() {
if (self.vid.readyState != 4) {
return;
}
@@ -81,21 +81,21 @@ function PxLoaderVideo(url, tags, priority) {
return url;
};
- // cross-browser event binding
+ // cross-browser event binding
this.bind = function(eventName, eventHandler) {
if (self.vid.addEventListener) {
self.vid.addEventListener(eventName, eventHandler, false);
} else if (self.vid.attachEvent) {
- self.vid.attachEvent('on'+eventName, eventHandler);
+ self.vid.attachEvent('on' + eventName, eventHandler);
}
};
- // cross-browser event un-binding
+ // cross-browser event un-binding
this.unbind = function(eventName, eventHandler) {
if (self.vid.removeEventListener) {
self.vid.removeEventListener(eventName, eventHandler, false);
} else if (self.vid.detachEvent) {
- self.vid.detachEvent('on'+eventName, eventHandler);
+ self.vid.detachEvent('on' + eventName, eventHandler);
}
};
@@ -108,4 +108,4 @@ PxLoader.prototype.addVideo = function(url, tags, priority) {
// return the vid element to the caller
return videoLoader.vid;
-};
+};

0 comments on commit 1b55ac8

Please sign in to comment.