Created
May 9, 2014 16:13
-
-
Save Dakta/baac0094836c555c24b9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function tbobject() { | |
Toolbox = { | |
utils: TBUtils, | |
version: '2.0.1', | |
modules: {}, | |
register_module: function(module) { | |
this.modules[module.shortname] = module; | |
}, | |
init: function () { | |
// call every module's init() method on page load | |
for (m in this.modules) { | |
var module = this.modules[m]; | |
if (module.setting('enabled')) { | |
module.init(); | |
} | |
} | |
}, | |
build_settings: function () { | |
var tabs = {}; | |
for (m in this.modules) { | |
var module = this.modules[m]; | |
tabs[module.shortname] = module.config; | |
} | |
return tabs; | |
} | |
}; | |
// Prototype for all Toolbox modules | |
function TBModule (name, version) { | |
// PUBLIC: Module Metadata | |
this.name = name; | |
this.version = version; | |
this.__defineGetter__('shortname', function () { | |
return name.trim().toLowerCase().replace(' ', '_'); | |
}); | |
// Settings and Defaults | |
// PRIVATE: default values for module settings | |
settings_defaults = []; | |
settings_defaults['enabled'] = false; | |
// PUBLIC settings interface | |
this.setting = function (name, value) { | |
value = (value !== undefined) ? value : null; | |
// are we setting or getting? | |
if (value) { | |
// setting | |
return Toolbox.utils.setSetting(this.shortname, name, value); | |
} else { | |
// getting | |
// do we have a default? | |
if (settings_defaults.indexOf(name) != -1) { | |
// we know what the default should be | |
return Toolbox.utils.getSetting(this.shortname, name, settings_defaults[name]); | |
} else { | |
// getSetting defaults to null for default value | |
return Toolbox.utils.getSetting(this.shortname, name); | |
} | |
} | |
}; | |
this.init = function () { | |
// pass | |
} | |
// Configuration | |
this.config = { | |
title: this.name, | |
body: '', | |
help: '' | |
} | |
} | |
// init() gets called on page load | |
TBModule.prototype.init = function() { | |
// body... | |
}; | |
// We create the module | |
test_module = new TBModule('Test Module', '0.0'); | |
// Enable it | |
test_module.setting('enabled', true); | |
// Write our init(), which is called when the page is ready | |
test_module.init = function () { | |
alert("Test Module"); | |
} | |
// register our module | |
Toolbox.register_module(test_module); | |
foo_module = new TBModule('Foo Module', '0.0'); | |
foo_module.setting('enabled', true); | |
foo_module.init = function () { | |
alert("Huh"); | |
} | |
Toolbox.register_module(foo_module); | |
// This needs to be called last. There's probably some clever way to do it, but I haven't figured it out. | |
Toolbox.init(); | |
} | |
// Add script to page | |
(function () { | |
var s = document.createElement('script'); | |
s.textContent = "(" + tbobject.toString() + ')();'; | |
document.head.appendChild(s); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment