Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix(bootstrap): rewritten to $script
Browse files Browse the repository at this point in the history
  • Loading branch information
mhevery committed Apr 21, 2012
1 parent 43d15f8 commit b806b30
Showing 1 changed file with 61 additions and 58 deletions.
119 changes: 61 additions & 58 deletions src/angular-bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
'use strict';

/*!
* $script.js Async loader & dependency manager
* https://github.com/ded/script.js
* (c) Dustin Diaz, Jacob Thornton 2011
* License: MIT
*/
(function(a,b){typeof module!="undefined"?module.exports=b():typeof define=="function"&&define.amd?define(a,b):this[a]=b()})("$script",function(){function q(a,b,c){for(c=0,j=a.length;c<j;++c)if(!b(a[c]))return k;return 1}function r(a,b){q(a,function(a){return!b(a)})}function s(a,b,i){function o(a){return a.call?a():d[a]}function p(){if(!--n){d[m]=1,k&&k();for(var a in f)q(a.split("|"),o)&&!r(f[a],o)&&(f[a]=[])}}a=a[l]?a:[a];var j=b&&b.call,k=j?b:i,m=j?a.join(""):b,n=a.length;return setTimeout(function(){r(a,function(a){if(h[a])return m&&(e[m]=1),h[a]==2&&p();h[a]=1,m&&(e[m]=1),t(!c.test(a)&&g?g+a+".js":a,p)})},0),s}function t(c,d){var e=a.createElement("script"),f=k;e.onload=e.onerror=e[p]=function(){if(e[n]&&!/^c|loade/.test(e[n])||f)return;e.onload=e[p]=null,f=1,h[c]=2,d()},e.async=1,e.src=c,b.insertBefore(e,b.firstChild)}var a=document,b=a.getElementsByTagName("head")[0],c=/^https?:\/\//,d={},e={},f={},g,h={},i="string",k=!1,l="push",m="DOMContentLoaded",n="readyState",o="addEventListener",p="onreadystatechange";return!a[n]&&a[o]&&(a[o](m,function u(){a.removeEventListener(m,u,k),a[n]="complete"},k),a[n]="loading"),s.get=t,s.order=function(a,b,c){(function d(e){e=a.shift(),a.length?s(e,d):s(e,b,c)})()},s.path=function(a){g=a},s.ready=function(a,b,c){a=a[l]?a:[a];var e=[];return!r(a,function(a){d[a]||e[l](a)})&&q(a,function(a){return d[a]})?b():!function(a){f[a]=f[a]||[],f[a][l](b),c&&c(e)}(a.join("|")),s},s});

/**
* @license AngularJS
Expand All @@ -11,7 +18,11 @@
scripts = document.getElementsByTagName("SCRIPT"),
serverPath,
match,
globalVars = {};
globalVars = {},
IGNORE = {
onkeyup: true, onkeydown: true, onresize: true,
event: true, frames: true, external: true,
sessionStorage: true, clipboardData: true, localStorage: true};

for(var j = 0; j < scripts.length; j++) {
match = (scripts[j].src || "").match(filename);
Expand All @@ -20,34 +31,74 @@
}
}

document.write('<link rel="stylesheet" type="text/css" href="' + serverPath + '../css/angular.css"/>');

$script.path(serverPath+'../');
$script('angularFiles', function() {
var index = 0,
scripts = angularFiles.angularSrc;

try { delete window.angularFiles; } catch(e) { window.angularFiles = undefined; }
// initialize the window property cache
for (var prop in window) {
if (IGNORE[prop] || prop.match(/^moz[A-Z]/)) { //skip special variables which keep on changing
continue;
}
try {
globalVars[key(prop)] = window[prop];
} catch(e) {} //ignore properties that throw exception when accessed (common in FF)
}

(function next() {
if (index < scripts.length) {
var file = scripts[index++];

$script(file.replace(/\.js$/, ''), function() {
angularClobberTest(file);
next();
});
} else {
// empty the cache to prevent mem leaks
globalVars = {};

bindJQuery();
publishExternalAPI(window.angular);

angularInit(document, angular.bootstrap);
}
})();
});

function key(prop) {
return "ng-clobber_" + prop;
}

window.angularClobberTest = function(file) {
function angularClobberTest(file) {
var varKey, prop,
clobbered = [];
clobbered = {};

for (prop in window) {
varKey = key(prop);

if (prop === 'event') { //skip special variables which keep on changing
if (IGNORE[prop] || prop.match(/^moz[A-Z]/)) { //skip special variables which keep on changing
continue;
}
else if (!globalVars.hasOwnProperty(varKey)) {
} else if (!globalVars.hasOwnProperty(varKey)) {
//console.log('new global variable found: ', prop);
try {
globalVars[varKey] = window[prop];
} catch(e) {} //ignore properties that throw exception when accessed (common in FF)
} else if (globalVars[varKey] !== window[prop] && !isActuallyNaN(window[prop]) && prop != 'jqLite') {
clobbered.push(prop);
clobbered[prop] = true;
console.error("Global variable clobbered by script " + file + "! Variable name: " + prop);
globalVars[varKey] = window[prop];
}
}
for (varKey in globalVars) {
prop = varKey.substr(11);
if (clobbered.indexOf(prop) == -1 &&
if (prop === 'event' || prop.match(/^moz[A-Z]/)) { //skip special variables which keep on changing
continue;
}
if (!clobbered[prop] &&
prop != 'event' &&
prop != 'jqLite' &&
!isActuallyNaN(globalVars[varKey]) &&
Expand All @@ -60,56 +111,8 @@
}

function isActuallyNaN(val) {
return isNaN(val) && (typeof val === 'number');
}
};

window.addScripts = function(scripts) {
delete window.addScripts;
delete window.angularFiles;

var prop, i;

// initialize the window property cache
for (prop in window) {
try {
globalVars[key(prop)] = window[prop];
} catch(e) {} //ignore properties that throw exception when accessed (common in FF)
}

// load the js scripts
for (i in scripts) {
var file = scripts[i].replace(/src\//, '');
document.write('<script type="text/javascript" src="' + serverPath + file + '" ' +
'onload="angularClobberTest(\'' + file + '\')"></script>');
return (typeof val === 'number') && isNaN(val);
}
};

function addCss(file) {
document.write('<link rel="stylesheet" type="text/css" href="' +
serverPath + '../css/' + file + '"/>');
}

addCss('angular.css');

document.write('<script type="text/javascript" src="' + serverPath + '../angularFiles.js' + '" ' +
'onload="addScripts(angularFiles.angularSrc)"></script>');

function onLoadListener() {
// empty the cache to prevent mem leaks
globalVars = {};

bindJQuery();
publishExternalAPI(window.angular);

angularInit(document, angular.bootstrap);
}

if (window.addEventListener) {
window.addEventListener('load', onLoadListener, false);
} else if (window.attachEvent) {
window.attachEvent('onload', onLoadListener);
}

})(window, document);

0 comments on commit b806b30

Please sign in to comment.