Skip to content

Instantly share code, notes, and snippets.

@bobbysmith007
Created November 30, 2016 16:36
Show Gist options
  • Save bobbysmith007/43b03ba715c8eaff9deef4cfded27789 to your computer and use it in GitHub Desktop.
Save bobbysmith007/43b03ba715c8eaff9deef4cfded27789 to your computer and use it in GitHub Desktop.
JSPacakger
JSPackager.load('//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js',"JQUERY",null);
JSPackager.load('//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js',"JQUERY-UI",["JQUERY"]);
JSPackager.load('https://ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js',"ANGULARJS",["JQUERY"]);
JSPackager.load('/script/ucwb.js',"UCWB",null);
JSPackager.load ('/script/22dbf09dcec0ef072432833e660ff6c5-all.js','ALL',["UCWB","ANGULARJS","JQUERY-UI","JQUERY"]);
JSPackager.onload(function (){
//based on http://stackoverflow.com/questions/7054795/adding-a-script-to-the-page-dynamically-with-jquery-never-uses-the-cached-file
// this allows scripts added from content in output-html-snippet to be used
// from cache (as they often are already loaded on the page).
if (jQuery) {
jQuery.ajaxPrefilter(function( options, originalOptions, jqXHR ) {
if(options.dataType == 'script' || originalOptions.dataType == 'script')
options.cache = true;
});
}
jQuery(function(){if(typeof(ADW)=='undefined')ADW={};if(!ADW.Sorters)ADW.Sorters={};ADW.Sorters['ResultsTable']=new TableSorter('ResultsTable', null); ;});
ADW.onload();
angular.bootstrap(document, ['ADW']);
ADW.onloadSections();
ADW.API.onload()
ADW.searchClients.onload();
;});
// Only want one JSPackager in existance, so lets only create
// one if we need to
if (typeof(JSPackager) == "undefined") window.JSPackager = {
// the head tag of the document
_head : document.getElementsByTagName('head')[0],
// a script tag to clone
_script : (function(){
var s = document.createElement('script');
s.setAttribute('type','text/javascript');
// defer doesnt seem to work with this scheme and so we defer manually in removeLoadedDeps
s.setAttribute('async','async');
return s;
})(),
// a list of js libs that load has been called on
loaded:[],
loadedHash:{},
// a list of functions to call when we are finished loading
_onloads:[],
// check if we are finished loading everything and if so run the
// finishedLoading functions
onload: function(){
// add all arguments to the _onloads list
for(var i=0,a; a=arguments[i];i++)JSPackager._onloads.push(a);
var loaded = JSPackager.loaded, fin=true;
for(var o, j=0 ; o=loaded[j] ; j++) if(!o.finished) fin = false;
if(!fin) return;
// run all onLoad functions
var fn;
// console.log('Running JSPackager.onload fns; ', log);
while((fn=JSPackager._onloads.shift()))
if(typeof(jQuery) != 'undefined') jQuery(fn); // not sure about this?
else fn();
},
removeLoadedDeps : function(inp){
var remaining=[], remainingunloaded=[];
for(var j=0, dep ; dep=inp.remaining[j] ; j++){
var depO = JSPackager.loadedHash[dep];
// if we depend on us, lets count us as loaded; this is needlessly defensive
if(depO.key == inp.key) continue;
if( !(depO && depO.loaded) ) remainingunloaded.push(dep);
if( !(depO && depO.loaded && depO.finished) ) remaining.push(dep);
}
inp.remaining = remaining;
inp.remainingunloaded = remainingunloaded;
//console.log('Remaining', inp, inp.remaining);
// dont defer loading till deps are met
// if(inp.remainingunloaded.length == 0 && !inp.loaded) JSPackager.doLoad(inp);
// defer loading till deps are met
if(inp.remaining.length == 0 && !inp.loaded) JSPackager.doLoad(inp);
},
// a function that gets called when the script is finished loading
// removes this script from the remaining dependency lists and when
// looks for scripts whose dependencies are met and doLoads them
// also runs onload
doneLoading : function(inp){
// console.log("Done Loading ", inp.key, inp);
inp.finished = true;
var key = inp.key;
JSPackager.removeAllLoadedDeps();
JSPackager.onload();
},
removeAllLoadedDeps: function(){
var loaded = JSPackager.loaded;
for(var o,i=0 ; o=loaded[i] ; i++){
if(o.loaded) continue;
JSPackager.removeLoadedDeps(o);
}
},
// actually do the load, this is called once all dependencies are loaded
// adds a script tag to the head
doLoad : function(o){
if(o.loaded || o.finished) return;
o.loaded = true;
o.finished = false;
var s = JSPackager._script.cloneNode();
s.setAttribute('src',o.url);
s.onload = s.onreadystatechange = function() {
JSPackager.doneLoading(o);
};
// console.log('Loading',o.key, o.url);
JSPackager._head.appendChild(s);
},
// queue up an object to be loaded. if it has no dependencies
// then doLoad it
load : function(url, key, deps){
if(!key || !JSPackager.loadedHash[key]){
var ku = key||url, o = {
key:ku, url:url, loaded:false, finished:false,
deps:deps||[], remaining:deps||[] };
JSPackager.loadedHash[ku] = o;
JSPackager.loaded.push(o);
JSPackager.removeLoadedDeps(o);
}
else {
// console.log('Skipping already loaded',url, key);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment