Skip to content
This repository
Browse code

Normalize formatting

  • Loading branch information...
commit 1b55ac857c57611b245ecbd3db48f7c0fa63a16b 1 parent f13f093
Joel Fillmore joelfillmore authored
145 PxLoader.js
@@ -2,6 +2,7 @@
2 2 * PixelLab Resource Loader
3 3 * Loads resources while providing progress updates.
4 4 */
  5 +
5 6 function PxLoader(settings) {
6 7
7 8 // merge settings with defaults
@@ -22,10 +23,10 @@ function PxLoader(settings) {
22 23 settings.noProgressTimeout = Infinity; // do not stop waiting by default
23 24 }
24 25
25   - var entries = [], // holds resources to be loaded with their status
  26 + var entries = [],
  27 + // holds resources to be loaded with their status
26 28 progressListeners = [],
27   - timeStarted,
28   - progressChanged = +new Date;
  29 + timeStarted, progressChanged = +new Date;
29 30
30 31 /**
31 32 * The status of a resource
@@ -49,7 +50,7 @@ function PxLoader(settings) {
49 50 return val;
50 51 }
51 52
52   - return [ val ];
  53 + return [val];
53 54 };
54 55
55 56 // add an entry to the list of resources to be loaded
@@ -97,16 +98,15 @@ function PxLoader(settings) {
97 98 bestIndex = Infinity;
98 99 for (var i = 0; i < resource.tags.length; i++) {
99 100 for (var j = 0; j < Math.min(orderedTags.length, bestIndex); j++) {
100   - if(resource.tags[i] == orderedTags[j] && j < bestIndex) {
101   - bestIndex = j;
102   - if (bestIndex === 0) break;
103   - }
104   - if (bestIndex === 0) break;
105   - }
  101 + if (resource.tags[i] == orderedTags[j] && j < bestIndex) {
  102 + bestIndex = j;
  103 + if (bestIndex === 0) break;
  104 + }
  105 + if (bestIndex === 0) break;
  106 + }
106 107 }
107 108 return bestIndex;
108 109 };
109   -
110 110 return function(a, b) {
111 111 // check tag order first
112 112 var aOrder = getTagOrder(a),
@@ -152,14 +152,15 @@ function PxLoader(settings) {
152 152 }
153 153
154 154 // see if the resource has loaded
155   - entry.resource.checkStatus();
  155 + if (entry.resource.checkStatus) {
  156 + entry.resource.checkStatus();
  157 + }
156 158
157 159 // if still waiting, mark as timed out or make sure we check again
158 160 if (entry.status === ResourceState.WAITING) {
159 161 if (timedOut) {
160 162 entry.resource.onTimeout();
161   - }
162   - else {
  163 + } else {
163 164 checkAgain = true;
164 165 }
165 166 }
@@ -177,8 +178,7 @@ function PxLoader(settings) {
177 178
178 179 this.isBusy = function() {
179 180 for (var i = 0, len = entries.length; i < len; i++) {
180   - if (entries[i].status === ResourceState.QUEUED ||
181   - entries[i].status === ResourceState.WAITING) {
  181 + if (entries[i].status === ResourceState.QUEUED || entries[i].status === ResourceState.WAITING) {
182 182 return true;
183 183 }
184 184 }
@@ -188,7 +188,7 @@ function PxLoader(settings) {
188 188 var onProgress = function(resource, statusType) {
189 189 // find the entry for the resource
190 190 var entry = null;
191   - for(var i=0, len = entries.length; i < len; i++) {
  191 + for (var i = 0, len = entries.length; i < len; i++) {
192 192 if (entries[i].resource === resource) {
193 193 entry = entries[i];
194 194 break;
@@ -212,8 +212,7 @@ function PxLoader(settings) {
212 212 if (listener.tags.length === 0) {
213 213 // no tags specified so always tell the listener
214 214 shouldCall = true;
215   - }
216   - else {
  215 + } else {
217 216 // listener only wants to hear about certain tags
218 217 shouldCall = resource.tags.contains(listener.tags);
219 218 }
@@ -246,16 +245,13 @@ function PxLoader(settings) {
246 245 if (listener.tags.length === 0) {
247 246 // no tags specified so always tell the listener
248 247 includeResource = true;
249   - }
250   - else {
  248 + } else {
251 249 includeResource = entry.resource.tags.contains(listener.tags);
252 250 }
253 251
254 252 if (includeResource) {
255 253 total++;
256   - if (entry.status === ResourceState.LOADED ||
257   - entry.status === ResourceState.ERROR ||
258   - entry.status === ResourceState.TIMEOUT) {
  254 + if (entry.status === ResourceState.LOADED || entry.status === ResourceState.ERROR || entry.status === ResourceState.TIMEOUT) {
259 255 completed++;
260 256 }
261 257 }
@@ -319,64 +315,63 @@ function PxLoader(settings) {
319 315 };
320 316 }
321 317
322   -
323 318 // Tag object to handle tag intersection; once created not meant to be changed
324 319 // Performance rationale: http://jsperf.com/lists-indexof-vs-in-operator/3
  320 +
325 321 function PxLoaderTags(values) {
326 322
327   - this.array = [];
328   - this.object = {};
329   - this.value = null; // single value
330   - this.length = 0;
331   -
332   - if (values !== null && values !== undefined) {
333   - if(Array.isArray(values)) {
334   - this.array = values;
335   - } else if(typeof values === 'object') {
336   - for(var key in values) {
337   - this.array.push(key);
338   - }
339   - } else {
340   - this.array.push(values);
341   - this.value = values;
342   - }
343   -
344   - this.length = this.array.length;
345   -
346   - // convert array values to object with truthy values, used by contains function below
347   - for(var i = 0; i < this.length; i++) {
348   - this.object[this.array[i]] = true;
349   - }
350   - }
351   -
352   - // compare this object with another; return true if they share at least one value
353   - this.contains = function(other) {
354   - if(this.length === 0 || other.length === 0) {
355   - return false;
356   - } else if(this.length === 1) {
357   - if (other.length === 1) {
358   - return this.value === other.value;
359   - } else {
360   - return other.object.hasOwnProperty(this.value);
361   - }
362   - } else if(other.length < this.length) {
363   - return other.contains(this); // better to loop through the smaller object
364   - } else {
365   - for(var key in this.object) {
366   - if(other.object[key]) {
367   - return true;
368   - }
369   - }
370   - return false;
371   - }
372   - }
  323 + this.array = [];
  324 + this.object = {};
  325 + this.value = null; // single value
  326 + this.length = 0;
  327 +
  328 + if (values !== null && values !== undefined) {
  329 + if (Array.isArray(values)) {
  330 + this.array = values;
  331 + } else if (typeof values === 'object') {
  332 + for (var key in values) {
  333 + this.array.push(key);
  334 + }
  335 + } else {
  336 + this.array.push(values);
  337 + this.value = values;
  338 + }
  339 +
  340 + this.length = this.array.length;
  341 +
  342 + // convert array values to object with truthy values, used by contains function below
  343 + for (var i = 0; i < this.length; i++) {
  344 + this.object[this.array[i]] = true;
  345 + }
  346 + }
  347 +
  348 + // compare this object with another; return true if they share at least one value
  349 + this.contains = function(other) {
  350 + if (this.length === 0 || other.length === 0) {
  351 + return false;
  352 + } else if (this.length === 1) {
  353 + if (other.length === 1) {
  354 + return this.value === other.value;
  355 + } else {
  356 + return other.object.hasOwnProperty(this.value);
  357 + }
  358 + } else if (other.length < this.length) {
  359 + return other.contains(this); // better to loop through the smaller object
  360 + } else {
  361 + for (var key in this.object) {
  362 + if (other.object[key]) {
  363 + return true;
  364 + }
  365 + }
  366 + return false;
  367 + }
  368 + }
373 369 }
374 370
375 371 // shims to ensure we have newer Array utility methods
376   -
377 372 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArray
378 373 if (!Array.isArray) {
379   - Array.isArray = function(arg) {
380   - return Object.prototype.toString.call(arg) == '[object Array]';
381   - };
  374 + Array.isArray = function(arg) {
  375 + return Object.prototype.toString.call(arg) == '[object Array]';
  376 + };
382 377 }
21 PxLoaderImage.js
... ... @@ -1,8 +1,8 @@
1 1 // @depends PxLoader.js
2   -
3 2 /**
4 3 * PxLoader plugin to load images
5 4 */
  5 +
6 6 function PxLoaderImage(url, tags, priority) {
7 7 var self = this,
8 8 loader = null;
@@ -11,7 +11,7 @@ function PxLoaderImage(url, tags, priority) {
11 11 this.tags = tags;
12 12 this.priority = priority;
13 13
14   - var onReadyStateChange = function () {
  14 + var onReadyStateChange = function() {
15 15 if (self.img.readyState == 'complete') {
16 16 removeEventHandlers();
17 17 loader.onLoad(self);
@@ -62,8 +62,7 @@ function PxLoaderImage(url, tags, priority) {
62 62 removeEventHandlers();
63 63 if (self.img.complete) {
64 64 loader.onLoad(self);
65   - }
66   - else {
  65 + } else {
67 66 loader.onTimeout(self);
68 67 }
69 68 };
@@ -72,22 +71,22 @@ function PxLoaderImage(url, tags, priority) {
72 71 this.getName = function() {
73 72 return url;
74 73 };
75   -
76   - // cross-browser event binding
  74 +
  75 + // cross-browser event binding
77 76 this.bind = function(eventName, eventHandler) {
78 77 if (self.img.addEventListener) {
79   - self.img.addEventListener(eventName, eventHandler, false);
  78 + self.img.addEventListener(eventName, eventHandler, false);
80 79 } else if (self.img.attachEvent) {
81   - self.img.attachEvent('on'+eventName, eventHandler);
  80 + self.img.attachEvent('on' + eventName, eventHandler);
82 81 }
83 82 };
84 83
85   - // cross-browser event un-binding
  84 + // cross-browser event un-binding
86 85 this.unbind = function(eventName, eventHandler) {
87 86 if (self.img.removeEventListener) {
88 87 self.img.removeEventListener(eventName, eventHandler, false);
89 88 } else if (self.img.detachEvent) {
90   - self.img.detachEvent('on'+eventName, eventHandler);
  89 + self.img.detachEvent('on' + eventName, eventHandler);
91 90 }
92 91 };
93 92
@@ -100,4 +99,4 @@ PxLoader.prototype.addImage = function(url, tags, priority) {
100 99
101 100 // return the img element to the caller
102 101 return imageLoader.img;
103   -};
  102 +};
29 PxLoaderSound.js
... ... @@ -1,8 +1,8 @@
1 1 // @depends PxLoader.js
2   -
3 2 /**
4 3 * PxLoader plugin to load sound using SoundManager2
5 4 */
  5 +
6 6 function PxLoaderSound(id, url, tags, priority) {
7 7 var self = this,
8 8 loader = null;
@@ -13,12 +13,16 @@ function PxLoaderSound(id, url, tags, priority) {
13 13 'id': id,
14 14 'url': url,
15 15 'autoLoad': false,
16   - 'onload': function() { loader.onLoad(self); },
  16 + 'onload': function() {
  17 + loader.onLoad(self);
  18 + },
17 19
18 20 // HTML5-only event: Fires when a browser has chosen to stop downloading.
19 21 // "The user agent is intentionally not currently fetching media data,
20 22 // but does not have the entire media resource downloaded."
21   - 'onsuspend': function() { loader.onTimeout(self); },
  23 + 'onsuspend': function() {
  24 + loader.onTimeout(self);
  25 + },
22 26
23 27 // Fires at a regular interval when a sound is loading and new data
24 28 // has been received.
@@ -27,7 +31,6 @@ function PxLoaderSound(id, url, tags, priority) {
27 31 bytesTotal = this['bytesTotal'];
28 32
29 33 // TODO: provide percentage complete updates to loader?
30   -
31 34 // see if we have loaded the file
32 35 if (bytesLoaded > 0 && (bytesLoaded === bytesTotal)) {
33 36 loader.onLoad(self);
@@ -47,21 +50,25 @@ function PxLoaderSound(id, url, tags, priority) {
47 50 var iDevice = navigator.userAgent.match(/(ipad|iphone|ipod)/i);
48 51 if (iDevice) {
49 52 loader.onTimeout(self);
50   - }
51   - else {
  53 + } else {
52 54 this.sound['load']();
53 55 }
54 56 };
55 57
56 58 this.checkStatus = function() {
57 59 switch(self.sound['readyState']) {
58   - case 0: // uninitialised
59   - case 1: // loading
  60 + case 0:
  61 + // uninitialised
  62 + break;
  63 + case 1:
  64 + // loading
60 65 break;
61   - case 2: // failed/error
  66 + case 2:
  67 + // failed/error
62 68 loader.onError(self);
63 69 break;
64   - case 3: // loaded/success
  70 + case 3:
  71 + // loaded/success
65 72 loader.onLoad(self);
66 73 break;
67 74 }
@@ -81,4 +88,4 @@ PxLoader.prototype.addSound = function(id, url, tags, priority) {
81 88 var soundLoader = new PxLoaderSound(id, url, tags, priority);
82 89 this.add(soundLoader);
83 90 return soundLoader.sound;
84   -};
  91 +};
14 PxLoaderVideo.js
... ... @@ -1,8 +1,8 @@
1 1 // @depends PxLoader.js
2   -
3 2 /**
4 3 * PxLoader plugin to load video elements
5 4 */
  5 +
6 6 function PxLoaderVideo(url, tags, priority) {
7 7 var self = this;
8 8 var loader = null;
@@ -16,7 +16,7 @@ function PxLoaderVideo(url, tags, priority) {
16 16 this.tags = tags;
17 17 this.priority = priority;
18 18
19   - var onReadyStateChange = function () {
  19 + var onReadyStateChange = function() {
20 20 if (self.vid.readyState != 4) {
21 21 return;
22 22 }
@@ -81,21 +81,21 @@ function PxLoaderVideo(url, tags, priority) {
81 81 return url;
82 82 };
83 83
84   - // cross-browser event binding
  84 + // cross-browser event binding
85 85 this.bind = function(eventName, eventHandler) {
86 86 if (self.vid.addEventListener) {
87 87 self.vid.addEventListener(eventName, eventHandler, false);
88 88 } else if (self.vid.attachEvent) {
89   - self.vid.attachEvent('on'+eventName, eventHandler);
  89 + self.vid.attachEvent('on' + eventName, eventHandler);
90 90 }
91 91 };
92 92
93   - // cross-browser event un-binding
  93 + // cross-browser event un-binding
94 94 this.unbind = function(eventName, eventHandler) {
95 95 if (self.vid.removeEventListener) {
96 96 self.vid.removeEventListener(eventName, eventHandler, false);
97 97 } else if (self.vid.detachEvent) {
98   - self.vid.detachEvent('on'+eventName, eventHandler);
  98 + self.vid.detachEvent('on' + eventName, eventHandler);
99 99 }
100 100 };
101 101
@@ -108,4 +108,4 @@ PxLoader.prototype.addVideo = function(url, tags, priority) {
108 108
109 109 // return the vid element to the caller
110 110 return videoLoader.vid;
111   -};
  111 +};

0 comments on commit 1b55ac8

Please sign in to comment.
Something went wrong with that request. Please try again.