1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-26 15:23:15 +00:00

Made the module and plugin globals a bit less global

This commit is contained in:
Jeremy Ruston 2012-04-30 19:14:39 +01:00
parent d93bbbbe7b
commit 3d0d8556f2
5 changed files with 50 additions and 48 deletions

View File

@ -430,7 +430,7 @@ Recipe.tiddlerOutputter = {
var title = tiddlers[t], var title = tiddlers[t],
tid = this.store.getTiddler(title); tid = this.store.getTiddler(title);
out.push("<" + "script type=\"text/javascript\" data-tiddler-title=\"" + title + "\">"); out.push("<" + "script type=\"text/javascript\" data-tiddler-title=\"" + title + "\">");
out.push("$tw.defineModule(\"" + title + "\",\"" + tid.module + "\",function(module,exports,require) {"); out.push("$tw.modules.define(\"" + title + "\",\"" + tid.module + "\",function(module,exports,require) {");
out.push(tid.text); out.push(tid.text);
out.push("});"); out.push("});");
out.push("</" + "script>"); out.push("</" + "script>");

View File

@ -36,10 +36,12 @@ if(typeof(window) === "undefined" && !global.$tw) {
} }
// Modules store registers all the modules the system has seen // Modules store registers all the modules the system has seen
$tw.modules = $tw.modules || {}; // hashmap by module name of {fn:, exports:, moduleType:} $tw.modules = $tw.modules || {};
$tw.modules.titles = $tw.modules.titles || {} // hashmap by module title of {fn:, exports:, moduleType:}
// Plugins store organises module exports by module type // Plugins store organises module exports by module type
$tw.plugins = $tw.plugins || {}; // hashmap by module type of array of exports $tw.plugins = $tw.plugins || {};
$tw.plugins.moduleTypes = $tw.plugins.moduleTypes || {}; // hashmap by module type of array of exports
// Config object // Config object
$tw.config = $tw.config || {}; $tw.config = $tw.config || {};
@ -177,23 +179,23 @@ $tw.utils.resolvePath = function(sourcepath,rootpath) {
/////////////////////////// Plugin mechanism /////////////////////////// Plugin mechanism
/* /*
Register a single plugin module in the $tw.plugins hashmap Register a single plugin module in the $tw.plugins.moduleTypes hashmap
*/ */
$tw.registerPlugin = function(name,moduleType,moduleExports) { $tw.plugins.registerPlugin = function(name,moduleType,moduleExports) {
if(!(moduleType in $tw.plugins)) { if(!(moduleType in $tw.plugins.moduleTypes)) {
$tw.plugins[moduleType] = []; $tw.plugins.moduleTypes[moduleType] = [];
} }
$tw.plugins[moduleType].push(moduleExports); $tw.plugins.moduleTypes[moduleType].push(moduleExports);
}; };
/* /*
Register all plugin module tiddlers Register all plugin module tiddlers
*/ */
$tw.registerPlugins = function() { $tw.plugins.registerPlugins = function() {
for(var title in $tw.wiki.shadows.tiddlers) { for(var title in $tw.wiki.shadows.tiddlers) {
var tiddler = $tw.wiki.shadows.getTiddler(title); var tiddler = $tw.wiki.shadows.getTiddler(title);
if(tiddler.fields.type === "application/javascript" && tiddler.fields["module-type"] !== undefined) { if(tiddler.fields.type === "application/javascript" && tiddler.fields["module-type"] !== undefined) {
$tw.registerPlugin(title,tiddler.fields["module-type"],$tw.executeModule(title)); $tw.plugins.registerPlugin(title,tiddler.fields["module-type"],$tw.modules.execute(title));
} }
} }
}; };
@ -201,9 +203,9 @@ $tw.registerPlugins = function() {
/* /*
Get all the plugins of a particular type in a hashmap by their `name` field Get all the plugins of a particular type in a hashmap by their `name` field
*/ */
$tw.getPluginsByTypeAsHashmap = function(moduleType,nameField) { $tw.plugins.getPluginsByTypeAsHashmap = function(moduleType,nameField) {
nameField = nameField || "name"; nameField = nameField || "name";
var plugins = $tw.plugins[moduleType], var plugins = $tw.plugins.moduleTypes[moduleType],
results = {}; results = {};
if(plugins) { if(plugins) {
for(var t=0; t<plugins.length; t++) { for(var t=0; t<plugins.length; t++) {
@ -216,8 +218,8 @@ $tw.getPluginsByTypeAsHashmap = function(moduleType,nameField) {
/* /*
Apply the exports of the plugin modules of a particular type to a target object Apply the exports of the plugin modules of a particular type to a target object
*/ */
$tw.applyPluginMethods = function(moduleType,object) { $tw.plugins.applyMethods = function(moduleType,object) {
var modules = $tw.plugins[moduleType], var modules = $tw.plugins.moduleTypes[moduleType],
n,m,f; n,m,f;
if(modules) { if(modules) {
for(n=0; n<modules.length; n++) { for(n=0; n<modules.length; n++) {
@ -271,23 +273,23 @@ $tw.Tiddler.fieldPlugins = {};
Install any tiddler field plugin modules Install any tiddler field plugin modules
*/ */
$tw.Tiddler.installPlugins = function() { $tw.Tiddler.installPlugins = function() {
$tw.Tiddler.fieldPlugins = $tw.getPluginsByTypeAsHashmap("tiddlerfield"); $tw.Tiddler.fieldPlugins = $tw.plugins.getPluginsByTypeAsHashmap("tiddlerfield");
}; };
/* /*
Register and install the built in tiddler field plugins Register and install the built in tiddler field plugins
*/ */
$tw.registerPlugin($tw.config.root + "/kernel/tiddlerfields/modified","tiddlerfield",{ $tw.plugins.registerPlugin($tw.config.root + "/kernel/tiddlerfields/modified","tiddlerfield",{
name: "modified", name: "modified",
parse: $tw.utils.parseDate, parse: $tw.utils.parseDate,
stringify: $tw.utils.stringifyDate stringify: $tw.utils.stringifyDate
}); });
$tw.registerPlugin($tw.config.root + "/kernel/tiddlerfields/created","tiddlerfield",{ $tw.plugins.registerPlugin($tw.config.root + "/kernel/tiddlerfields/created","tiddlerfield",{
name: "created", name: "created",
parse: $tw.utils.parseDate, parse: $tw.utils.parseDate,
stringify: $tw.utils.stringifyDate stringify: $tw.utils.stringifyDate
}); });
$tw.registerPlugin($tw.config.root + "/kernel/tiddlerfields/tags","tiddlerfield",{ $tw.plugins.registerPlugin($tw.config.root + "/kernel/tiddlerfields/tags","tiddlerfield",{
name: "tags", name: "tags",
parse: $tw.utils.parseStringArray, parse: $tw.utils.parseStringArray,
stringify: function(value) { stringify: function(value) {
@ -350,7 +352,7 @@ $tw.Wiki.tiddlerDeserializerPlugins = {};
Install any tiddler deserializer plugin modules Install any tiddler deserializer plugin modules
*/ */
$tw.Wiki.installPlugins = function() { $tw.Wiki.installPlugins = function() {
$tw.Wiki.tiddlerDeserializerPlugins = $tw.getPluginsByTypeAsHashmap("tiddlerdeserializer"); $tw.Wiki.tiddlerDeserializerPlugins = $tw.plugins.getPluginsByTypeAsHashmap("tiddlerdeserializer");
}; };
/* /*
@ -380,7 +382,7 @@ $tw.Wiki.prototype.deserializeTiddlers = function(type,text,srcFields) {
/* /*
Register the built in tiddler deserializer plugins Register the built in tiddler deserializer plugins
*/ */
$tw.registerPlugin($tw.config.root + "/kernel/tiddlerdeserializer/js","tiddlerdeserializer",{ $tw.plugins.registerPlugin($tw.config.root + "/kernel/tiddlerdeserializer/js","tiddlerdeserializer",{
name: "application/javascript", name: "application/javascript",
deserialize: function(text,fields) { deserialize: function(text,fields) {
var headerCommentRegExp = /^\/\*\\\n((?:^[^\n]*\n)+?)(^\\\*\/$\n?)/mg, var headerCommentRegExp = /^\/\*\\\n((?:^[^\n]*\n)+?)(^\\\*\/$\n?)/mg,
@ -392,7 +394,7 @@ $tw.registerPlugin($tw.config.root + "/kernel/tiddlerdeserializer/js","tiddlerde
return [fields]; return [fields];
} }
}); });
$tw.registerPlugin($tw.config.root + "/kernel/tiddlerdeserializer/tid","tiddlerdeserializer",{ $tw.plugins.registerPlugin($tw.config.root + "/kernel/tiddlerdeserializer/tid","tiddlerdeserializer",{
name: "application/x-tiddler", name: "application/x-tiddler",
deserialize: function(text,fields) { deserialize: function(text,fields) {
var split = text.indexOf("\n\n"); var split = text.indexOf("\n\n");
@ -422,13 +424,13 @@ if($tw.isBrowser) {
/* /*
Execute the module named 'moduleName'. The name can optionally be relative to the module named 'moduleRoot' Execute the module named 'moduleName'. The name can optionally be relative to the module named 'moduleRoot'
*/ */
$tw.executeModule = function(moduleName,moduleRoot) { $tw.modules.execute = function(moduleName,moduleRoot) {
var name = moduleRoot ? $tw.utils.resolvePath(moduleName,moduleRoot) : moduleName, var name = moduleRoot ? $tw.utils.resolvePath(moduleName,moduleRoot) : moduleName,
require = function(modRequire) { require = function(modRequire) {
return $tw.executeModule(modRequire,name); return $tw.modules.execute(modRequire,name);
}, },
exports = {}, exports = {},
module = $tw.modules[name]; module = $tw.modules.titles[name];
if(!module) { if(!module) {
throw new Error("Cannot find module named '" + moduleName + "' required by module '" + moduleRoot + "', resolved to " + name); throw new Error("Cannot find module named '" + moduleName + "' required by module '" + moduleRoot + "', resolved to " + name);
} }
@ -444,7 +446,7 @@ $tw.executeModule = function(moduleName,moduleRoot) {
/* /*
Register a deserializer that can extract tiddlers from the DOM Register a deserializer that can extract tiddlers from the DOM
*/ */
$tw.registerPlugin($tw.config.root + "/kernel/tiddlerdeserializer/dom","tiddlerdeserializer",{ $tw.plugins.registerPlugin($tw.config.root + "/kernel/tiddlerdeserializer/dom","tiddlerdeserializer",{
name: "(DOM)", name: "(DOM)",
deserialize: function(node) { deserialize: function(node) {
var extractTextTiddler = function(node) { var extractTextTiddler = function(node) {
@ -545,7 +547,7 @@ $tw.loadTiddlersFromFile = function(file,basetitle) {
/* /*
Load all the plugins from the plugins directory Load all the plugins from the plugins directory
*/ */
$tw.loadPlugins = function(filepath,basetitle) { $tw.plugins.loadPlugins = function(filepath,basetitle) {
basetitle = basetitle || "$:/plugins"; basetitle = basetitle || "$:/plugins";
var stat = fs.statSync(filepath); var stat = fs.statSync(filepath);
if(stat.isDirectory()) { if(stat.isDirectory()) {
@ -553,7 +555,7 @@ $tw.loadPlugins = function(filepath,basetitle) {
for(var f=0; f<files.length; f++) { for(var f=0; f<files.length; f++) {
var file = files[f]; var file = files[f];
if(file !== ".DS_Store" && path.extname(file) !== ".meta") { if(file !== ".DS_Store" && path.extname(file) !== ".meta") {
$tw.loadPlugins(filepath + "/" + file,basetitle + "/" + file); $tw.plugins.loadPlugins(filepath + "/" + file,basetitle + "/" + file);
} }
} }
} else if(stat.isFile()) { } else if(stat.isFile()) {
@ -564,9 +566,9 @@ $tw.loadPlugins = function(filepath,basetitle) {
/* /*
Execute the module named 'moduleName'. The name can optionally be relative to the module named 'moduleRoot' Execute the module named 'moduleName'. The name can optionally be relative to the module named 'moduleRoot'
*/ */
$tw.executeModule = function(moduleName,moduleRoot) { $tw.modules.execute = function(moduleName,moduleRoot) {
var name = moduleRoot ? $tw.utils.resolvePath(moduleName,moduleRoot) : moduleName, var name = moduleRoot ? $tw.utils.resolvePath(moduleName,moduleRoot) : moduleName,
module = $tw.modules[name], module = $tw.modules.titles[name],
tiddler = $tw.wiki.getTiddler(name), tiddler = $tw.wiki.getTiddler(name),
sandbox = { sandbox = {
module: module, module: module,
@ -574,7 +576,7 @@ $tw.executeModule = function(moduleName,moduleRoot) {
console: console, console: console,
$tw: $tw, $tw: $tw,
require: function(title) { require: function(title) {
return $tw.executeModule(title,name); return $tw.modules.execute(title,name);
} }
}; };
if(!tiddler || tiddler.fields.type !== "application/javascript") { if(!tiddler || tiddler.fields.type !== "application/javascript") {
@ -585,7 +587,7 @@ $tw.executeModule = function(moduleName,moduleRoot) {
module = module || { module = module || {
moduleType: tiddler.fields["module-type"] moduleType: tiddler.fields["module-type"]
}; };
$tw.modules[name] = module; $tw.modules.titles[name] = module;
// Execute it to get its exports if we haven't already done so // Execute it to get its exports if we haven't already done so
if(!module.exports) { if(!module.exports) {
vm.runInNewContext(tiddler.fields.text,sandbox,tiddler.fields.title); vm.runInNewContext(tiddler.fields.text,sandbox,tiddler.fields.title);
@ -596,7 +598,7 @@ $tw.executeModule = function(moduleName,moduleRoot) {
} }
// Load plugins from the plugins directory // Load plugins from the plugins directory
$tw.loadPlugins(path.resolve(path.dirname(module.filename),$tw.config.pluginSubDir)); $tw.plugins.loadPlugins(path.resolve(path.dirname(module.filename),$tw.config.pluginSubDir));
// End of if(!$tw.isBrowser) // End of if(!$tw.isBrowser)
} }
@ -604,14 +606,14 @@ $tw.loadPlugins(path.resolve(path.dirname(module.filename),$tw.config.pluginSubD
/////////////////////////// Final initialisation /////////////////////////// Final initialisation
// Register plugins from the tiddlers we've just loaded // Register plugins from the tiddlers we've just loaded
$tw.registerPlugins(); $tw.plugins.registerPlugins();
// Now we can properly install all of our extension plugins // Now we can properly install all of our extension plugins
$tw.Tiddler.installPlugins(); $tw.Tiddler.installPlugins();
$tw.Wiki.installPlugins(); $tw.Wiki.installPlugins();
// Run any startup plugin modules // Run any startup plugin modules
var mainModules = $tw.plugins["startup"]; var mainModules = $tw.plugins.moduleTypes["startup"];
for(var m=0; m<mainModules.length; m++) { for(var m=0; m<mainModules.length; m++) {
mainModules[m].startup.call($tw); mainModules[m].startup.call($tw);
} }

View File

@ -17,7 +17,7 @@ if(window && !window.$tw) {
window.$tw = {isBrowser: true}; window.$tw = {isBrowser: true};
} }
$tw.modules = {}; // hashmap by module name of {fn:, exports:, moduleType:} $tw.modules = {titles: {}}; // hashmap by module name of {fn:, exports:, moduleType:}
/* /*
Define a JavaScript tiddler module for later execution Define a JavaScript tiddler module for later execution
@ -25,8 +25,8 @@ Define a JavaScript tiddler module for later execution
moduleType: type of module moduleType: type of module
fn: function defining the module, called with the arguments (module,require,exports) fn: function defining the module, called with the arguments (module,require,exports)
*/ */
$tw.defineModule = function(moduleName,moduleType,fn) { $tw.modules.define = function(moduleName,moduleType,fn) {
$tw.modules[moduleName] = { $tw.modules.titles[moduleName] = {
moduleType: moduleType, moduleType: moduleType,
fn: fn fn: fn
}; };

View File

@ -14,14 +14,14 @@ This is the main application logic for both the client and server
exports.startup = function() { exports.startup = function() {
var modules,n,m,f; var modules,n,m,f;
// Set up additional global objects // Set up additional global objects
$tw.applyPluginMethods("global",$tw); $tw.plugins.applyMethods("global",$tw);
// Wire up plugin modules // Wire up plugin modules
$tw.applyPluginMethods("config",$tw.config); $tw.plugins.applyMethods("config",$tw.config);
$tw.applyPluginMethods("utils",$tw.utils); $tw.plugins.applyMethods("utils",$tw.utils);
$tw.applyPluginMethods("tiddlermethod",$tw.Tiddler.prototype); $tw.plugins.applyMethods("tiddlermethod",$tw.Tiddler.prototype);
$tw.applyPluginMethods("wikimethod",$tw.Wiki.prototype); $tw.plugins.applyMethods("wikimethod",$tw.Wiki.prototype);
$tw.applyPluginMethods("treeutils",$tw.Tree); $tw.plugins.applyMethods("treeutils",$tw.Tree);
$tw.applyPluginMethods("treenode",$tw.Tree); $tw.plugins.applyMethods("treenode",$tw.Tree);
// Set up the wiki store // Set up the wiki store
$tw.wiki.initMacros(); $tw.wiki.initMacros();
$tw.wiki.initEditors(); $tw.wiki.initEditors();

View File

@ -202,7 +202,7 @@ exports.initParsers = function(moduleType) {
// Install the parser modules // Install the parser modules
moduleType = moduleType || "parser"; moduleType = moduleType || "parser";
$tw.wiki.parsers = {}; $tw.wiki.parsers = {};
var modules = $tw.plugins[moduleType], var modules = $tw.plugins.moduleTypes[moduleType],
n,m,f; n,m,f;
if(modules) { if(modules) {
for(n=0; n<modules.length; n++) { for(n=0; n<modules.length; n++) {
@ -214,7 +214,7 @@ exports.initParsers = function(moduleType) {
} }
} }
// Install the wikitext rules // Install the wikitext rules
modules = $tw.plugins["wikitextrule"]; modules = $tw.plugins.moduleTypes["wikitextrule"];
var wikitextparser = this.parsers["text/x-tiddlywiki"]; var wikitextparser = this.parsers["text/x-tiddlywiki"];
if(modules && wikitextparser) { if(modules && wikitextparser) {
for(n=0; n<modules.length; n++) { for(n=0; n<modules.length; n++) {
@ -268,7 +268,7 @@ exports.initMacros = function(moduleType) {
moduleType = moduleType || "macro"; moduleType = moduleType || "macro";
$tw.wiki.macros = {}; $tw.wiki.macros = {};
var MacroClass = require("./treenodes/macro.js").Macro, var MacroClass = require("./treenodes/macro.js").Macro,
modules = $tw.plugins[moduleType], modules = $tw.plugins.moduleTypes[moduleType],
n,m,f, n,m,f,
subclassMacro = function(module) { subclassMacro = function(module) {
// Make a copy of the Macro() constructor function // Make a copy of the Macro() constructor function
@ -300,7 +300,7 @@ exports.initEditors = function(moduleType) {
var editMacro = this.macros.edit; var editMacro = this.macros.edit;
if(editMacro) { if(editMacro) {
editMacro.editors = {}; editMacro.editors = {};
$tw.applyPluginMethods(moduleType,editMacro.editors); $tw.plugins.applyMethods(moduleType,editMacro.editors);
} }
}; };