1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 18:17:20 +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],
tid = this.store.getTiddler(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("});");
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
$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
$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
$tw.config = $tw.config || {};
@ -177,23 +179,23 @@ $tw.utils.resolvePath = function(sourcepath,rootpath) {
/////////////////////////// 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) {
if(!(moduleType in $tw.plugins)) {
$tw.plugins[moduleType] = [];
$tw.plugins.registerPlugin = function(name,moduleType,moduleExports) {
if(!(moduleType in $tw.plugins.moduleTypes)) {
$tw.plugins.moduleTypes[moduleType] = [];
}
$tw.plugins[moduleType].push(moduleExports);
$tw.plugins.moduleTypes[moduleType].push(moduleExports);
};
/*
Register all plugin module tiddlers
*/
$tw.registerPlugins = function() {
$tw.plugins.registerPlugins = function() {
for(var title in $tw.wiki.shadows.tiddlers) {
var tiddler = $tw.wiki.shadows.getTiddler(title);
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
*/
$tw.getPluginsByTypeAsHashmap = function(moduleType,nameField) {
$tw.plugins.getPluginsByTypeAsHashmap = function(moduleType,nameField) {
nameField = nameField || "name";
var plugins = $tw.plugins[moduleType],
var plugins = $tw.plugins.moduleTypes[moduleType],
results = {};
if(plugins) {
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
*/
$tw.applyPluginMethods = function(moduleType,object) {
var modules = $tw.plugins[moduleType],
$tw.plugins.applyMethods = function(moduleType,object) {
var modules = $tw.plugins.moduleTypes[moduleType],
n,m,f;
if(modules) {
for(n=0; n<modules.length; n++) {
@ -271,23 +273,23 @@ $tw.Tiddler.fieldPlugins = {};
Install any tiddler field plugin modules
*/
$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
*/
$tw.registerPlugin($tw.config.root + "/kernel/tiddlerfields/modified","tiddlerfield",{
$tw.plugins.registerPlugin($tw.config.root + "/kernel/tiddlerfields/modified","tiddlerfield",{
name: "modified",
parse: $tw.utils.parseDate,
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",
parse: $tw.utils.parseDate,
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",
parse: $tw.utils.parseStringArray,
stringify: function(value) {
@ -350,7 +352,7 @@ $tw.Wiki.tiddlerDeserializerPlugins = {};
Install any tiddler deserializer plugin modules
*/
$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
*/
$tw.registerPlugin($tw.config.root + "/kernel/tiddlerdeserializer/js","tiddlerdeserializer",{
$tw.plugins.registerPlugin($tw.config.root + "/kernel/tiddlerdeserializer/js","tiddlerdeserializer",{
name: "application/javascript",
deserialize: function(text,fields) {
var headerCommentRegExp = /^\/\*\\\n((?:^[^\n]*\n)+?)(^\\\*\/$\n?)/mg,
@ -392,7 +394,7 @@ $tw.registerPlugin($tw.config.root + "/kernel/tiddlerdeserializer/js","tiddlerde
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",
deserialize: function(text,fields) {
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'
*/
$tw.executeModule = function(moduleName,moduleRoot) {
$tw.modules.execute = function(moduleName,moduleRoot) {
var name = moduleRoot ? $tw.utils.resolvePath(moduleName,moduleRoot) : moduleName,
require = function(modRequire) {
return $tw.executeModule(modRequire,name);
return $tw.modules.execute(modRequire,name);
},
exports = {},
module = $tw.modules[name];
module = $tw.modules.titles[name];
if(!module) {
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
*/
$tw.registerPlugin($tw.config.root + "/kernel/tiddlerdeserializer/dom","tiddlerdeserializer",{
$tw.plugins.registerPlugin($tw.config.root + "/kernel/tiddlerdeserializer/dom","tiddlerdeserializer",{
name: "(DOM)",
deserialize: function(node) {
var extractTextTiddler = function(node) {
@ -545,7 +547,7 @@ $tw.loadTiddlersFromFile = function(file,basetitle) {
/*
Load all the plugins from the plugins directory
*/
$tw.loadPlugins = function(filepath,basetitle) {
$tw.plugins.loadPlugins = function(filepath,basetitle) {
basetitle = basetitle || "$:/plugins";
var stat = fs.statSync(filepath);
if(stat.isDirectory()) {
@ -553,7 +555,7 @@ $tw.loadPlugins = function(filepath,basetitle) {
for(var f=0; f<files.length; f++) {
var file = files[f];
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()) {
@ -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'
*/
$tw.executeModule = function(moduleName,moduleRoot) {
$tw.modules.execute = function(moduleName,moduleRoot) {
var name = moduleRoot ? $tw.utils.resolvePath(moduleName,moduleRoot) : moduleName,
module = $tw.modules[name],
module = $tw.modules.titles[name],
tiddler = $tw.wiki.getTiddler(name),
sandbox = {
module: module,
@ -574,7 +576,7 @@ $tw.executeModule = function(moduleName,moduleRoot) {
console: console,
$tw: $tw,
require: function(title) {
return $tw.executeModule(title,name);
return $tw.modules.execute(title,name);
}
};
if(!tiddler || tiddler.fields.type !== "application/javascript") {
@ -585,7 +587,7 @@ $tw.executeModule = function(moduleName,moduleRoot) {
module = module || {
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
if(!module.exports) {
vm.runInNewContext(tiddler.fields.text,sandbox,tiddler.fields.title);
@ -596,7 +598,7 @@ $tw.executeModule = function(moduleName,moduleRoot) {
}
// 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)
}
@ -604,14 +606,14 @@ $tw.loadPlugins(path.resolve(path.dirname(module.filename),$tw.config.pluginSubD
/////////////////////////// Final initialisation
// Register plugins from the tiddlers we've just loaded
$tw.registerPlugins();
$tw.plugins.registerPlugins();
// Now we can properly install all of our extension plugins
$tw.Tiddler.installPlugins();
$tw.Wiki.installPlugins();
// Run any startup plugin modules
var mainModules = $tw.plugins["startup"];
var mainModules = $tw.plugins.moduleTypes["startup"];
for(var m=0; m<mainModules.length; m++) {
mainModules[m].startup.call($tw);
}

View File

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

View File

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

View File

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