Skip to content

Commit

Permalink
Added sequence method, code cleanup, added some comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdejonge committed Apr 19, 2012
1 parent 4e3be7d commit aaddb3a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 33 deletions.
97 changes: 65 additions & 32 deletions 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 = [];
Expand All @@ -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];
Expand All @@ -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;
Expand All @@ -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")
Expand All @@ -83,7 +94,7 @@
}
}
};

for(var i = 0; i < this.container[_stack].length; i++)
{
var src = this.container[_stack][i];
Expand All @@ -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]))
Expand All @@ -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++)
Expand All @@ -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))
Expand All @@ -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);
2 changes: 1 addition & 1 deletion sjl.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit aaddb3a

Please sign in to comment.