Further clean-up Bootstrap code.

This commit is contained in:
Jürg Lehni 2011-03-04 22:22:22 +00:00
parent c25db2377f
commit 7b237ceedc

72
lib/bootstrap.js vendored
View file

@ -1,9 +1,9 @@
/**
* Bootstrap JavaScript Library
* (c) 2006 - 2010 Juerg Lehni, http://scratchdisk.com/
* (c) 2006 - 2011 Juerg Lehni, http://lehni.org/
*
* Bootstrap is released under the MIT license
* http://bootstrap-js.net/
* http://bootstrapjs.org/
*
* Inspirations:
* http://dean.edwards.name/weblog/2006/03/base/
@ -35,11 +35,15 @@ var Base = this.Base = new function() {
_describe = Object.getOwnPropertyDescriptor;
function define(obj, name, desc) {
if (_define)
try { delete obj[name]; return _define(obj, name, desc); } catch (e) {}
if (_define) {
try {
delete obj[name];
return _define(obj, name, desc);
} catch (e) {}
}
if ((desc.get || desc.set) && obj.__defineGetter__) {
if (desc.get) obj.__defineGetter__(name, desc.get);
if (desc.set) obj.__defineSetter__(name, desc.set);
desc.get && obj.__defineGetter__(name, desc.get);
desc.set && obj.__defineSetter__(name, desc.set);
} else {
obj[name] = desc.value;
}
@ -47,13 +51,18 @@ var Base = this.Base = new function() {
}
function describe(obj, name) {
if (_describe)
try { return _describe(obj, name); } catch (e) {}
if (_describe) {
try {
return _describe(obj, name);
} catch (e) {}
}
var get = obj.__lookupGetter__ && obj.__lookupGetter__(name);
return get
? { enumerable: true, configurable: true, get: get, set: obj.__lookupSetter__(name) }
? { get: get, set: obj.__lookupSetter__(name), enumerable: true,
configurable: true }
: has.call(obj, name)
? { enumerable: true, configurable: true, writable: true, value: obj[name] }
? { value: obj[name], enumerable: true, configurable: true,
writable: true }
: null;
}
@ -61,21 +70,32 @@ var Base = this.Base = new function() {
var beans, bean;
function field(name, val, dontCheck, generics) {
var val = val || (val = describe(src, name)) && (val.get ? val : val.value),
var val = val || (val = describe(src, name))
&& (val.get ? val : val.value),
func = typeof val == 'function', res = val,
prev = preserve || func ? (val && val.get ? name in dest : dest[name]) : null;
if (generics && func && (!preserve || !generics[name])) generics[name] = function(bind) {
return bind && dest[name].apply(bind, slice.call(arguments, 1));
prev = preserve || func
? (val && val.get ? name in dest : dest[name]) : null;
if (generics && func && (!preserve || !generics[name])) {
generics[name] = function(bind) {
return bind && dest[name].apply(bind,
slice.call(arguments, 1));
}
}
if ((dontCheck || val !== undefined && has.call(src, name)) && (!preserve || !prev)) {
if ((dontCheck || val !== undefined && has.call(src, name))
&& (!preserve || !prev)) {
if (func) {
if (prev && /\bthis\.base\b/.test(val)) {
var fromBase = base && base[name] == prev;
res = function() {
var tmp = describe(this, 'base');
define(this, 'base', { value: fromBase ? base[name] : prev, configurable: true });
try { return val.apply(this, arguments); }
finally { tmp ? define(this, 'base', tmp) : delete this.base; }
define(this, 'base', { value: fromBase
? base[name] : prev, configurable: true });
try {
return val.apply(this, arguments);
} finally {
tmp ? define(this, 'base', tmp)
: delete this.base;
}
};
res.toString = function() {
return val.toString();
@ -89,7 +109,8 @@ var Base = this.Base = new function() {
}
if (!res || func || !res.get && !res.set)
res = { value: res, writable: true };
if ((describe(dest, name) || { configurable: true }).configurable) {
if ((describe(dest, name)
|| { configurable: true }).configurable) {
res.configurable = true;
res.enumerable = enumerable;
}
@ -99,7 +120,8 @@ var Base = this.Base = new function() {
if (src) {
beans = src.beans && [];
for (var name in src)
if (has.call(src, name) && !/^(statics|generics|preserve|beans|prototype|__proto__|toString|valueOf)$/.test(name))
if (has.call(src, name)
&& !/^(statics|generics|preserve|beans|prototype|__proto__|toString|valueOf)$/.test(name))
field(name, null, true, generics);
field('toString');
field('valueOf');
@ -161,8 +183,10 @@ var Base = this.Base = new function() {
Base.inject = function(src) {
if (src) {
var proto = this.prototype, base = proto.__proto__ && proto.__proto__.constructor;
inject(proto, src, false, base && base.prototype, src.preserve, src.generics && this);
var proto = this.prototype,
base = proto.__proto__ && proto.__proto__.constructor;
inject(proto, src, false, base && base.prototype, src.preserve,
src.generics && this);
inject(this, src.statics, true, base, src.preserve);
}
for (var i = 1, l = arguments.length; i < l; i++)
@ -172,7 +196,8 @@ var Base = this.Base = new function() {
Base.extend = function(src) {
var proto = new this(this.dont), ctor = extend(proto);
define(proto, 'constructor', { value: ctor, writable: true, configurable: true });
define(proto, 'constructor',
{ value: ctor, writable: true, configurable: true });
ctor.dont = {};
inject(ctor, this, true);
return arguments.length ? this.inject.apply(ctor, arguments) : ctor;
@ -253,4 +278,3 @@ var Base = this.Base = new function() {
}
});
}