Skip to content

Instantly share code, notes, and snippets.

@backbone87
Created September 13, 2013 10:52
Show Gist options
  • Save backbone87/6549204 to your computer and use it in GitHub Desktop.
Save backbone87/6549204 to your computer and use it in GitHub Desktop.
/**
* namespacing and prototyping utility
*
* <T> bbit.local([String nspart, ..., ]Function T closure)
* <T> bbit.mt([String nspart, ..., ]Function T closure)
* <T> bbit.jq([String nspart, ..., ]Function T closure)
* Function bbit.thrower(String msg)
* <lambda> bbit.inherit(Function constructor, mixed parent)
*
* @copyright backboneIT | Oliver Hoff 2012
* @author Oliver Hoff <oliver@hofff.com>
* @license http://opensource.org/licenses/lgpl-3.0.html
*/
(function(global, undef) {
if(global.bbit) return;
var self = global.bbit = {},
local = function() {
var args = Array.prototype.slice.apply(arguments), base = args.shift(), rt;
rt = function(ns, fn) {
if(fn) {
var a = ns.split("."), s, i = 0;
ns = base;
do ns = ns[s = a[i]] ? ns[s] : ns[s] = {}; while(++i < a.length);
} else {
fn = ns;
ns = base;
}
return fn.apply(ns, args);
};
if(!base) base = rt;
return rt;
},
thrower = self.thrower = function(msg) {
return function() { throw new Exception(msg); };
};
self.local = local(self);
self.mt = global.MooTools ? local(undef, global.document.id, global.$$) : thrower("MooTools missing");
self.jq = global.jQuery ? local(undef, global.jQuery) : thrower("jQuery missing");
self.inherit = function(fn, parent) {
var proto = new Function();
proto.prototype = parent;
fn.prototype = new proto();
return fn;
};
self.css = function(css, d) {
if(!d) d = document;
var s = d.createElement("style");
s.type = "text/css";
if(s.styleSheet) {
s.styleSheet.cssText = css;
d.documentElement.firstChild.insertBefore(s);
} else {
s.insertBefore(d.createTextNode(css), undef);
d.documentElement.firstChild.insertBefore(s, undef);
}
};
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment