| 1 |
/** |
| 2 |
* |
| 3 |
*/ |
| 4 |
|
| 5 |
var ScriptsLoader; |
| 6 |
/** |
| 7 |
* |
| 8 |
*/ |
| 9 |
(function($) { |
| 10 |
|
| 11 |
/** |
| 12 |
* |
| 13 |
*/ |
| 14 |
ScriptsLoader = function(scripts) { |
| 15 |
// Clone scripts array. |
| 16 |
scripts = $.merge([], scripts); |
| 17 |
|
| 18 |
/** |
| 19 |
* |
| 20 |
*/ |
| 21 |
this.callback = $.Deferred(); |
| 22 |
|
| 23 |
/** |
| 24 |
* |
| 25 |
*/ |
| 26 |
this.loaded = []; |
| 27 |
|
| 28 |
/** |
| 29 |
* |
| 30 |
*/ |
| 31 |
this._loaded = function(script) { |
| 32 |
// Add script into loaded list. |
| 33 |
this.loaded.push(script); |
| 34 |
// Wait until all scripts is loaded. |
| 35 |
if (this.loaded.length == scripts.length) { |
| 36 |
console.log('All is done'); |
| 37 |
// Notify success. |
| 38 |
this.callback.resolveWith(this); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* |
| 44 |
*/ |
| 45 |
this.loadAll = function() { |
| 46 |
this.loadNext(); |
| 47 |
return this.callback; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* |
| 52 |
*/ |
| 53 |
this.load = function(script) { |
| 54 |
// Load script using Ajax! |
| 55 |
return $.getScript(script.src); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* |
| 60 |
*/ |
| 61 |
this.loadNext = function() { |
| 62 |
// If there is more script to load. |
| 63 |
if (scripts.length) { |
| 64 |
// Current Script! |
| 65 |
var script = scripts.shift(); |
| 66 |
// Get load method name to use for loading script file. |
| 67 |
var loadingMethod = 'load'; |
| 68 |
if (script.cjt.loadMethod != undefined) { |
| 69 |
loadingMethod += script.cjt.loadMethod; |
| 70 |
} |
| 71 |
this[loadingMethod](script).done($.proxy( |
| 72 |
function() { |
| 73 |
// Push script object into the queue. |
| 74 |
this.loaded.push(script); |
| 75 |
// Load next script! |
| 76 |
this.loadNext(); |
| 77 |
}, this) |
| 78 |
) |
| 79 |
.fail($.proxy( |
| 80 |
function() { |
| 81 |
// Report failure! |
| 82 |
this.callback.rejectWith(this, [loadingMethod, script]); |
| 83 |
}, this) |
| 84 |
) |
| 85 |
} |
| 86 |
else { |
| 87 |
// All scripts has been loaded!! |
| 88 |
this.callback.resolveWith(this); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* |
| 94 |
*/ |
| 95 |
this.loadTag = function(script) { |
| 96 |
var deferred = $.Deferred(); |
| 97 |
// Create script tag. |
| 98 |
var scriptTag = document.createElement('script'); |
| 99 |
// Set script properties. |
| 100 |
scriptTag.type = 'text/javascript'; |
| 101 |
scriptTag.src = script.src; |
| 102 |
// using jQuery for appending Script tag still load it using $.getScript!! |
| 103 |
// Use raw method to add HTML script tag. |
| 104 |
document.getElementsByTagName('head')[0].appendChild(scriptTag); |
| 105 |
// Check if script loaded every 50 milli seconds! |
| 106 |
var timeout = 4000; // 4 seconds. |
| 107 |
var interval = 50; // Milisecs |
| 108 |
var secondsElapsed = 0; |
| 109 |
var checker = setInterval($.proxy( |
| 110 |
function() { |
| 111 |
if (window[script.cjt.lookFor] != undefined) { |
| 112 |
// No more check for this script. |
| 113 |
clearInterval(checker); |
| 114 |
// Report success! |
| 115 |
deferred.resolve(); |
| 116 |
} |
| 117 |
else if (secondsElapsed == timeout) { |
| 118 |
// No more check for this script. |
| 119 |
clearInterval(checker); |
| 120 |
// Notify that script load is failure. |
| 121 |
deferred.reject(); |
| 122 |
} |
| 123 |
secondsElapsed += interval; |
| 124 |
}, this) |
| 125 |
, interval); |
| 126 |
// Promising! |
| 127 |
return deferred; |
| 128 |
} |
| 129 |
|
| 130 |
} // End class. |
| 131 |
|
| 132 |
})(jQuery); |