1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 10:07:19 +00:00

Add support for data modules

Modules can now be JSON or tiddler dictionaries, as well as executable
code
This commit is contained in:
Jeremy Ruston 2012-11-17 13:08:25 +00:00
parent fc61b9aca7
commit 3f58ead593
2 changed files with 22 additions and 6 deletions

View File

@ -196,6 +196,7 @@ $tw.utils.parseStringArray = function(value) {
// Parse a block of name:value fields. The `fields` object is used as the basis for the return value // Parse a block of name:value fields. The `fields` object is used as the basis for the return value
$tw.utils.parseFields = function(text,fields) { $tw.utils.parseFields = function(text,fields) {
fields = fields || {};
text.split(/\r?\n/mg).forEach(function(line) { text.split(/\r?\n/mg).forEach(function(line) {
var p = line.indexOf(":"); var p = line.indexOf(":");
if(p !== -1) { if(p !== -1) {
@ -540,9 +541,21 @@ Define all modules stored in ordinary tiddlers
*/ */
$tw.Wiki.prototype.defineTiddlerModules = function() { $tw.Wiki.prototype.defineTiddlerModules = function() {
$tw.utils.each(this.tiddlers,function(tiddler,title,object) { $tw.utils.each(this.tiddlers,function(tiddler,title,object) {
if(tiddler.fields.type === "application/javascript" && tiddler.hasField("module-type")) { if(tiddler.hasField("module-type")) {
// Define the module switch (tiddler.fields.type) {
$tw.modules.define(tiddler.fields.title,tiddler.fields["module-type"],tiddler.fields.text); case "application/javascript":
// We don't need to register JavaScript tiddlers in the browser
if(!$tw.browser) {
$tw.modules.define(tiddler.fields.title,tiddler.fields["module-type"],tiddler.fields.text);
}
break;
case "application/json":
$tw.modules.define(tiddler.fields.title,tiddler.fields["module-type"],JSON.parse(tiddler.fields.text));
break;
case "application/x-tiddler-dictionary":
$tw.modules.define(tiddler.fields.title,tiddler.fields["module-type"],$tw.utils.parseFields(tiddler.fields.text));
break;
}
} }
}); });
}; };
@ -1034,9 +1047,8 @@ $tw.boot.startup = function() {
// Unpack bundle tiddlers // Unpack bundle tiddlers
$tw.wiki.unpackBundleTiddlers(); $tw.wiki.unpackBundleTiddlers();
// Register typed modules from the tiddlers we've just loaded // Register typed modules from the tiddlers we've just loaded
if(!$tw.browser) { $tw.wiki.defineTiddlerModules();
$tw.wiki.defineTiddlerModules(); // And any modules within bundles
}
$tw.wiki.defineBundledModules(); $tw.wiki.defineBundledModules();
// Run any startup modules // Run any startup modules
$tw.modules.forEachModuleOfType("startup",function(title,module) { $tw.modules.forEachModuleOfType("startup",function(title,module) {

View File

@ -51,6 +51,10 @@ $tw.modules.define = function(moduleName,moduleType,definition) {
definition: definition, definition: definition,
exports: undefined exports: undefined
}; };
// If the definition is already an object we can use it as the exports
if(typeof moduleInfo.definition === "object") {
moduleInfo.exports = definition;
}
// Store the module in the titles hashmap // Store the module in the titles hashmap
if(Object.prototype.hasOwnProperty.call($tw.modules.titles,moduleName)) { if(Object.prototype.hasOwnProperty.call($tw.modules.titles,moduleName)) {
console.log("Warning: Redefined module - " + moduleName); console.log("Warning: Redefined module - " + moduleName);