Permalink
Browse files

better packaking

  • Loading branch information...
1 parent c3aba5d commit 28d3549056058a41d6d6bd746e6ebee691aa459e @tipiirai tipiirai committed Nov 29, 2010
Showing with 499 additions and 9 deletions.
  1. +3 −3 README.md
  2. +15 −0 bin/minify.sh
  3. +305 −2 head.js
  4. +7 −0 head.min.js
  5. +141 −0 src/head.core.js
  6. +19 −1 { → src}/head.detect.js
  7. +6 −1 { → src}/head.loader.js
  8. +3 −2 test/index.html
View
@@ -1,5 +1,5 @@
-## HEADJS
+### HEADJS
-### The only script in your HEAD
+#### The only script in your HEAD
-http://headjs.com
+[headjs.com](http://headjs.com)
View
@@ -0,0 +1,15 @@
+# http://marijnhaverbeke.nl/uglifyjs
+
+cd ../src
+
+echo '/**
+ Head JS: The only script in your <HEAD>
+
+ copyright: "tipiirai" / Tero Piirainen
+ license: MIT
+*/' > ../head.js
+
+grep -A 3000 "(function" head.core.js >> ../head.js
+grep -A 3000 "(function" head.detect.js >> ../head.js
+grep -A 3000 "(function" head.loader.js >> ../head.js
+
View
307 head.js
@@ -1,6 +1,9 @@
+/**
+ Head JS: The only script in your <HEAD>
-// http://marijnhaverbeke.nl/uglifyjs
-// https://github.com/Modernizr/Modernizr/blob/master/modernizr.js
+ copyright: "tipiirai" / Tero Piirainen
+ license: MIT
+*/
(function(doc) {
var html = doc.documentElement,
@@ -136,3 +139,303 @@
})(document);
+(function(doc) {
+
+ /* CSS modernizer */
+ var el = doc.createElement("i"),
+ style = el.style,
+ prefs = ' -o- -moz- -ms- -webkit- -khtml- '.split(' ');
+
+ /*
+ runs a vendor property test (-moz, ...)
+
+ testAll("box-shadow: 0 0 0 red;");
+ */
+ function testAll(definition) {
+ style.cssText = prefs.join(definition + ";");
+ var len = style.cssText ? style.cssText.length : 0;
+ return len > 0 && len < 150;
+ }
+
+ var tests = {
+
+ gradient: function() {
+ var s1 = 'background-image:',
+ s2 = 'gradient(linear,left top,right bottom,from(#9f9),to(#fff));',
+ s3 = 'linear-gradient(left top,#eee,#fff);';
+
+ style.cssText = (s1 + prefs.join(s2 + s1) + prefs.join(s3 + s1)).slice(0,-s1.length);
+ return !!style.backgroundImage;
+ },
+
+ rgba: function() {
+ style.cssText = "background-color:rgba(0,0,0,0.5)";
+ return !!style.backgroundColor;
+ },
+
+ boxshadow: function() {
+ return testAll("box-shadow: 0 0 0 red");
+ },
+
+ textshadow: function() {
+ return style.textShadow === '';
+ },
+
+ multiplebgs: function() {
+ style.cssText = "background:url(//:),url(//:),red url(//:)";
+ return new RegExp("(url\\s*\\(.*?){3}").test(style.background);
+ },
+
+ borderimage: function() {
+ return testAll("border-image: url(m.png) 1 1 stretch");
+ },
+
+ borderradius: function() {
+ return testAll('border-radius:0');
+ },
+
+ opacity: function() {
+ return testAll("opacity:.1");
+ },
+
+ reflections: function() {
+ return testAll("box-reflect:right 0");
+ },
+
+ transforms: function() {
+ return testAll("transform:rotate(1deg)");
+ },
+
+ transitions: function() {
+ return testAll("transition:all .5s linear");
+ }
+
+ };
+
+ var api = window[head_conf.head];
+
+ for (var key in tests) {
+ api.feature(key, tests[key].call());
+ }
+
+})(document);
+(function(doc) {
+
+ var ready = false,
+ queue = [],
+ thelast = [], // functions to be executed last
+ waiters = {}, // functions waiting for scripts
+ scripts = {}; // the scripts in different states
+
+
+ /*** public API ***/
+ var head_var = window.head_conf ? head_conf.head : "head",
+ api = window[head_var] = (window[head_var] || {});
+
+ api.js = function() {
+
+ var args = arguments,
+ rest = [].slice.call(args, 1),
+ next = rest[0];
+
+ if (!ready) {
+ return queue.push(function() {
+ api.js.apply(null, args);
+ });
+ }
+
+ // multiple arguments
+ if (next) {
+
+ // preload all immediately
+ if (!isFunc(next)) preloadAll.apply(null, rest);
+
+ // load all recursively in order
+ load(getScript(args[0]), isFunc(next) ? next : function() {
+ api.js.apply(null, rest);
+ });
+
+ // single script
+ } else {
+ load(getScript(args[0]));
+ }
+
+ return api.js;
+ };
+
+ api.ready = function(key, fn) {
+
+ // single function
+ if (isFunc(key)) { return thelast.push(key); }
+
+ var arr = waiters[key];
+ if (!arr) { arr = waiters[key] = [fn]; }
+ else arr.push[fn];
+ return api.js;
+ };
+
+ /*
+ api.dump = function() {
+ console.info(scripts);
+ };
+ */
+
+ /*** private functions ***/
+ function getScript(url) {
+
+ var script = scripts[url.url || url];
+ if (script) return script;
+
+ if (typeof url == 'object') {
+ for (key in url) {
+ script = { name: key, url: url[key] };
+ }
+ } else {
+ script = { name: url.substring(url.indexOf("/", 10) + 1, url.indexOf("?")), url: url };
+ }
+
+ scripts[script.url] = script;
+ return script;
+ }
+
+ function each(arr, fn) {
+ if (!arr) return;
+
+ // arguments special type
+ if (typeof arr == 'object') { arr = [].slice.call(arr); }
+
+ // do the job
+ for (var i = 0; i < arr.length; i++)
+ fn.call(arr, arr[i], i);
+ }
+
+ function isFunc(el) {
+ return typeof el == 'function';
+ }
+
+ function preloadAll() {
+ each(arguments, function(el) {
+ if (!isFunc(el)) {
+ preload(getScript(el));
+ }
+ });
+ }
+
+ function preload(script, callback) {
+
+ if (!script.state) {
+
+ //* console.info("PRELOAD", script.name)
+
+ script.state = "preloading";
+ script.onpreload = [];
+
+ function onload() {
+ script.state = "preloaded";
+
+ //* console.info(" PRE", script.name);
+ each(script.onpreload, function(el) {
+ el.call();
+ });
+ }
+
+ if (head.browser.mozilla) {
+ var obj = doc.createElement('object');
+ obj.data = script.url;
+ obj.width = 0;
+ obj.height = 0;
+
+ obj.onload = function() {
+ onload();
+
+ // avoid spinning progress indicator with setTimeout
+ setTimeout(function() { doc.body.removeChild(obj); }, 1);
+ };
+
+ doc.body.appendChild(obj);
+
+ } else {
+ scriptTag({ src: script.url, type: 'cache'}, onload);
+ }
+
+ }
+ }
+
+
+ function load(script, callback) {
+
+
+ if (script.state == 'loaded') { return callback(); }
+
+ if (script.state == 'preloading') {
+ return script.onpreload.push(function() {
+ load(script, callback);
+ });
+ }
+
+ //* console.info("LOAD", script.name, ":", script.state)
+
+ script.state = 'loading';
+
+ scriptTag(script.url, function() {
+
+ script.state = 'loaded';
+
+ if (callback) callback.call();
+
+ //* console.info(" LOADED", script.name);
+
+ // waiters for this script
+ each(waiters[script.name], function(fn) {
+ fn.call();
+ });
+
+ // TODO: do not run until DOM is loaded
+ var allLoaded = true;
+
+ for (var key in scripts) {
+ if (scripts[key].state != 'loaded') allLoaded = false;
+ }
+
+ if (allLoaded) {
+ each(thelast, function(fn) {
+ if (!fn.done) fn.call();
+ fn.done = true;
+ });
+ }
+ });
+
+ }
+
+ // if callback == true --> preload
+ function scriptTag(src, callback) {
+
+ var head = doc.getElementsByTagName('head')[0],
+ elem = doc.createElement('script');
+
+ elem.type = 'text/' + (src.type || 'javascript');
+ elem.src = src.src || src;
+
+ elem.onreadystatechange = elem.onload = function() {
+ if (!callback.done) {
+ callback.call();
+ callback.done = true;
+ }
+ if (!api.browser.ie) {
+ head.removeChild(elem);
+ }
+ };
+
+ head.appendChild(elem);
+ }
+
+ // DomContentLoaded no better
+ setTimeout(function() {
+ ready = true;
+ each(queue, function(fn) {
+ fn.call();
+ });
+ }, 50);
+
+
+})(document);
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 28d3549

Please sign in to comment.