Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added sequence method, code cleanup, added some comments.

  • Loading branch information...
commit aaddb3a67c349edb20683abff08315a75e1a6cd6 1 parent 4e3be7d
@frankdejonge authored
Showing with 66 additions and 33 deletions.
  1. +65 −32 sjl.js
  2. +1 −1  sjl.min.js
View
97 sjl.js
@@ -1,10 +1,19 @@
;(function(){
"use strict";
+ // store the window object
var root = this;
- var prev_Sjl = root.Sjl;
+
+ /// store previous sjl
+ var prev_Sjl = root.sjl;
+
+ // toString shortcut
var toString = Object.prototype.toString;
-
+
+ // store the headtag
+ var head = root.document.getElementsByTagName('head')[0];
+
+ // Define the Sjl object
var Sjl = function(){
this.stack = 1;
this.container = [];
@@ -14,14 +23,15 @@
this.instack = [];
this.fired = [];
};
-
+
+ // Add, adds scripts to a load stack
Sjl.prototype.add = function(scripts)
{
if( ! is_array(this.container[this.stack]))
{
this.container[this.stack] = [];
}
-
+
if( ! is_array(scripts))
{
scripts = [scripts];
@@ -40,15 +50,16 @@
this.instack[hash].push(this.stack);
}
}
-
+
return this;
};
-
+
+ // Load, loads the current stack and increments the stack pointer
Sjl.prototype.load = function(scripts, callback)
{
var _stack = this.stack;
this.loaded[_stack] = 0;
-
+
if(is_function(scripts))
{
this.callbacks[_stack] = scripts;
@@ -58,15 +69,15 @@
this.add(scripts);
this.callbacks[_stack] = callback;
}
-
+
if( ! is_array(this.container[_stack]) || this.container[_stack].length < 1)
{
this.fireCallback(_stack);
return;
}
-
+
var _Sjl = this;
-
+
var scriptload = function ()
{
if ( ! this.readyState || this.readyState == "loaded" || this.readyState == "complete")
@@ -83,7 +94,7 @@
}
}
};
-
+
for(var i = 0; i < this.container[_stack].length; i++)
{
var src = this.container[_stack][i];
@@ -109,13 +120,36 @@
}
this.stack++;
};
-
+
+ // Sequence, loads and fires callbacks in sequence.
+ Sjl.prototype.seq = Sjl.prototype.sequence = function()
+ {
+ var args = Array.prototype.slice.call(arguments);
+ if(args.length > 0){
+ var arg = Array.prototype.shift.call(args);
+ if(is_function(arg))
+ {
+ arg();
+ this.seq.apply(this, args);
+ }
+ else
+ {
+ var _sjl = this;
+ this.load(arg, function(){
+ _sjl.seq.apply(_sjl, args);
+ });
+ }
+ }
+ };
+
+ // noConflict method
Sjl.prototype.noConflict = function()
{
root.Sjl = prev_Sjl;
return this;
};
-
+
+ // fireCallback, checks if there is a callback and fires it.
Sjl.prototype.fireCallback = function(_stack)
{
if( ! this.fired[_stack] && is_function(this.callbacks[_stack]))
@@ -124,7 +158,8 @@
this.callbacks[_stack].call();
}
};
-
+
+ // stackLoaded, checks wether a stack is loaded.
Sjl.prototype.stackLoaded = function(_stack)
{
for(var i = 0; i < this.container[_stack].length; i++)
@@ -134,17 +169,17 @@
return false;
}
}
-
+
return true;
- }
-
- var head = root.document.getElementsByTagName('head')[0];
-
+ };
+
+ // check wether the input is an array
function is_array(arr)
{
return toString.call(arr) === '[object Array]';
- }
-
+ };
+
+ // check wether the an items is found in an array
function in_array(arr, item)
{
if( ! is_array(arr))
@@ -159,25 +194,23 @@
}
}
return false;
- }
-
+ };
+
+ // check wether the input is an array
function is_function(func)
{
return toString.call(func) === '[object Function]';
- }
-
- function is_string(str)
- {
- return toString.call(str) === '[object String]';
- }
-
+ };
+
+ // expose the Sjl object
root.sjl = new Sjl();
-
+
+ // check for main script loading
var s = root.document.getElementsByTagName('script');
var c = s[s.length-1], dataMain;
if((dataMain = c.dataMain) || (dataMain = c.getAttribute('data-main')))
{
root.sjl.load(dataMain);
}
-
+
}).call(this);
View
2  sjl.min.js
@@ -1 +1 @@
-(function(){function i(a){return c.call(a)==="[object String]"}function h(a){return c.call(a)==="[object Function]"}function g(a,b){if(!f(a)){return false}for(var c=0;c<a.length;c++){if(a[c]===b){return true}}return false}function f(a){return c.call(a)==="[object Array]"}"use strict";var a=this;var b=a.Sjl;var c=Object.prototype.toString;var d=function(){this.stack=1;this.container=[];this.callbacks=[];this.injected=[];this.loaded=[];this.instack=[];this.fired=[]};d.prototype.add=function(a){if(!f(this.container[this.stack])){this.container[this.stack]=[]}if(!f(a)){a=[a]}for(var b=0;b<a.length;b++){if(!g(this.loaded,a[b])&&!g(this.container[this.stack],a[b])){this.container[this.stack].push(a[b]);var c="Sjl-loaded-"+a[b].replace(/[^a-zA-Z0-9]+/g,"-");if(!f(this.instack[c])){this.instack[c]=[]}this.instack[c].push(this.stack)}}return this};d.prototype.load=function(b,c){var d=this.stack;this.loaded[d]=0;if(h(b)){this.callbacks[d]=b}else{this.add(b);this.callbacks[d]=c}if(!f(this.container[d])||this.container[d].length<1){this.fireCallback(d);return}var i=this;var j=function(){if(!this.readyState||this.readyState=="loaded"||this.readyState=="complete"){this.onload=this.onreadystatechange=null;i.loaded[this.className]=true;for(var a=0;a<i.instack[this.className].length;a++){var b=i.instack[this.className][a];if(i.stackLoaded(b)){i.fireCallback(b)}}}};for(var k=0;k<this.container[d].length;k++){var l=this.container[d][k];var m="Sjl-loaded-"+l.replace(/[^a-zA-Z0-9]+/g,"-");if(!g(this.injected,this.container[d][k])){this.injected.push(this.container[d][k]);var n=a.document.createElement("script");n.type="text/javascript";n.className=m;n.src=l;n.onload=n.onreadystatechange=j;e.appendChild(n)}else if(i.loaded[m]==true){if(i.stackLoaded(d)){i.fireCallback(d);return true}}}this.stack++};d.prototype.noConflict=function(){a.Sjl=b;return this};d.prototype.fireCallback=function(a){if(!this.fired[a]&&h(this.callbacks[a])){this.fired[a]=true;this.callbacks[a].call()}};d.prototype.stackLoaded=function(a){for(var b=0;b<this.container[a].length;b++){if(this.loaded["Sjl-loaded-"+this.container[a][b].replace(/[^a-zA-Z0-9]+/g,"-")]!=true){return false}}return true};var e=a.document.getElementsByTagName("head")[0];a.sjl=new d;var j=a.document.getElementsByTagName("script");var k=j[j.length-1],l;if((l=k.dataMain)||(l=k.getAttribute("data-main"))){a.sjl.load(l)}}).call(this)
+(function(){function h(a){return c.call(a)==="[object Function]"}function g(a,b){if(!f(a)){return false}for(var c=0;c<a.length;c++){if(a[c]===b){return true}}return false}function f(a){return c.call(a)==="[object Array]"}"use strict";var a=this;var b=a.sjl;var c=Object.prototype.toString;var d=a.document.getElementsByTagName("head")[0];var e=function(){this.stack=1;this.container=[];this.callbacks=[];this.injected=[];this.loaded=[];this.instack=[];this.fired=[]};e.prototype.add=function(a){if(!f(this.container[this.stack])){this.container[this.stack]=[]}if(!f(a)){a=[a]}for(var b=0;b<a.length;b++){if(!g(this.loaded,a[b])&&!g(this.container[this.stack],a[b])){this.container[this.stack].push(a[b]);var c="Sjl-loaded-"+a[b].replace(/[^a-zA-Z0-9]+/g,"-");if(!f(this.instack[c])){this.instack[c]=[]}this.instack[c].push(this.stack)}}return this};e.prototype.load=function(b,c){var e=this.stack;this.loaded[e]=0;if(h(b)){this.callbacks[e]=b}else{this.add(b);this.callbacks[e]=c}if(!f(this.container[e])||this.container[e].length<1){this.fireCallback(e);return}var i=this;var j=function(){if(!this.readyState||this.readyState=="loaded"||this.readyState=="complete"){this.onload=this.onreadystatechange=null;i.loaded[this.className]=true;for(var a=0;a<i.instack[this.className].length;a++){var b=i.instack[this.className][a];if(i.stackLoaded(b)){i.fireCallback(b)}}}};for(var k=0;k<this.container[e].length;k++){var l=this.container[e][k];var m="Sjl-loaded-"+l.replace(/[^a-zA-Z0-9]+/g,"-");if(!g(this.injected,this.container[e][k])){this.injected.push(this.container[e][k]);var n=a.document.createElement("script");n.type="text/javascript";n.className=m;n.src=l;n.onload=n.onreadystatechange=j;d.appendChild(n)}else if(i.loaded[m]==true){if(i.stackLoaded(e)){i.fireCallback(e);return true}}}this.stack++};e.prototype.seq=e.prototype.sequence=function(){var a=Array.prototype.slice.call(arguments);if(a.length>0){var b=Array.prototype.shift.call(a);if(h(b)){b();this.seq.apply(this,a)}else{var c=this;this.load(b,function(){c.seq.apply(c,a)})}}};e.prototype.noConflict=function(){a.Sjl=b;return this};e.prototype.fireCallback=function(a){if(!this.fired[a]&&h(this.callbacks[a])){this.fired[a]=true;this.callbacks[a].call()}};e.prototype.stackLoaded=function(a){for(var b=0;b<this.container[a].length;b++){if(this.loaded["Sjl-loaded-"+this.container[a][b].replace(/[^a-zA-Z0-9]+/g,"-")]!=true){return false}}return true};a.sjl=new e;var i=a.document.getElementsByTagName("script");var j=i[i.length-1],k;if((k=j.dataMain)||(k=j.getAttribute("data-main"))){a.sjl.load(k)}}).call(this)
Please sign in to comment.
Something went wrong with that request. Please try again.