1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-26 19:47:20 +00:00

Update plugin loading mechanism to allow plugins to be unregistered

We still have to explicitly unpack them after registering or
unregistering
This commit is contained in:
Jeremy Ruston 2013-04-30 22:56:17 +01:00
parent e875de63a9
commit 6d455d77cd

View File

@ -619,7 +619,7 @@ Construct a wiki store object
*/ */
$tw.Wiki = function() { $tw.Wiki = function() {
this.tiddlers = {}; this.tiddlers = {};
this.plugins = {}; // Hashmap of plugin information by title this.plugins = []; // Array of registered plugins, ordered by priority
this.shadowTiddlers = {}; // Hashmap by title of {source:, tiddler:} this.shadowTiddlers = {}; // Hashmap by title of {source:, tiddler:}
}; };
@ -637,19 +637,54 @@ $tw.Wiki.prototype.addTiddlers = function(tiddlers) {
}; };
/* /*
Extract constituent tiddlers from plugin tiddlers so that we can easily access them in getTiddler() Register the plugin tiddlers of a particular type, optionally restricting registration to an array of tiddler titles. Return the array of titles affected
*/ */
$tw.Wiki.prototype.unpackPluginTiddlers = function(pluginType) { $tw.Wiki.prototype.registerPluginTiddlers = function(pluginType,titles) {
// Collect up the titles of all the plugin tiddlers
var self = this, var self = this,
pluginInfoList = []; registeredTitles = [];
$tw.utils.each(this.tiddlers,function(tiddler,title,object) { // Go through the provided titles, or the entire tiddler list, looking for plugins of this type
if(tiddler.fields.type === "application/json" && tiddler.hasField("plugin") && tiddler.fields["plugin-type"] === pluginType) { var checkTiddler = function(tiddler) {
pluginInfoList.push(tiddler); if(tiddler && tiddler.fields.type === "application/json" && tiddler.hasField("plugin") && tiddler.fields["plugin-type"] === pluginType) {
self.plugins.push(tiddler);
registeredTitles.push(tiddler.fields.title);
} }
}); };
// Sort the titles by the `plugin-priority` field if(titles) {
pluginInfoList.sort(function(a,b) { $tw.utils.each(titles,function(title) {
checkTiddler(self.getTiddler(title));
});
} else {
$tw.utils.each(this.tiddlers,function(tiddler,title) {
checkTiddler(tiddler);
});
}
return registeredTitles;
};
/*
Unregister the plugin tiddlers of a particular type, returning an array of the titles affected
*/
$tw.Wiki.prototype.unregisterPluginTiddlers = function(pluginType) {
var self = this,
titles = [];
// Remove any previous registered plugins of this type
for(var t=this.plugins.length-1; t>=0; t--) {
var tiddler = this.plugins[t];
if(tiddler.fields["plugin-type"] === pluginType) {
titles.push(tiddler.fields.title);
this.plugins.splice(t,1);
}
}
return titles;
};
/*
Unpack the currently registered plugins, creating shadow tiddlers for their constituent tiddlers
*/
$tw.Wiki.prototype.unpackPluginTiddlers = function() {
var self = this;
// Sort the plugin titles by the `plugin-priority` field
this.plugins.sort(function(a,b) {
if("plugin-priority" in a.fields && "plugin-priority" in b.fields) { if("plugin-priority" in a.fields && "plugin-priority" in b.fields) {
return a.fields["plugin-priority"] - b.fields["plugin-priority"]; return a.fields["plugin-priority"] - b.fields["plugin-priority"];
} else if("plugin-priority" in a.fields) { } else if("plugin-priority" in a.fields) {
@ -664,10 +699,11 @@ $tw.Wiki.prototype.unpackPluginTiddlers = function(pluginType) {
return +1; return +1;
} }
}); });
// Now go through the plugins in ascending order // Now go through the plugins in ascending order and assign the shadows
$tw.utils.each(pluginInfoList,function(tiddler) { this.shadowTiddlers = {};
// Save the plugin information $tw.utils.each(this.plugins,function(tiddler) {
var pluginInfo = self.plugins[tiddler.fields.title] = JSON.parse(tiddler.fields.text); // Get the plugin information
var pluginInfo = JSON.parse(tiddler.fields.text);
// Extract the constituent tiddlers // Extract the constituent tiddlers
$tw.utils.each(pluginInfo.tiddlers,function(constituentTiddler,constituentTitle) { $tw.utils.each(pluginInfo.tiddlers,function(constituentTiddler,constituentTitle) {
// Save the tiddler object // Save the tiddler object
@ -706,7 +742,7 @@ $tw.Wiki.prototype.defineTiddlerModules = function() {
/* /*
Register all the module tiddlers that have a module type Register all the module tiddlers that have a module type
*/ */
$tw.Wiki.prototype.definePluginModules = function() { $tw.Wiki.prototype.defineShadowModules = function() {
var self = this; var self = this;
$tw.utils.each(this.shadowTiddlers,function(element,title) { $tw.utils.each(this.shadowTiddlers,function(element,title) {
var tiddler = self.getTiddler(title); var tiddler = self.getTiddler(title);
@ -1206,11 +1242,12 @@ $tw.boot.startup = function() {
// Load tiddlers // Load tiddlers
$tw.loadTiddlers(); $tw.loadTiddlers();
// Unpack plugin tiddlers // Unpack plugin tiddlers
$tw.wiki.unpackPluginTiddlers("plugin"); $tw.wiki.registerPluginTiddlers("plugin");
$tw.wiki.unpackPluginTiddlers();
// Register typed modules from the tiddlers we've just loaded // Register typed modules from the tiddlers we've just loaded
$tw.wiki.defineTiddlerModules(); $tw.wiki.defineTiddlerModules();
// And any modules within plugins // And any modules within plugins
$tw.wiki.definePluginModules(); $tw.wiki.defineShadowModules();
// Make sure the crypto state tiddler is up to date // Make sure the crypto state tiddler is up to date
$tw.crypto.updateCryptoStateTiddler(); $tw.crypto.updateCryptoStateTiddler();
// Run any startup modules // Run any startup modules