1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-01-11 18:00:26 +00:00

Refactor the theme switcher to be a generic plugin switcher

This commit is contained in:
Jermolene 2014-02-08 09:29:37 +00:00
parent b26a32b93a
commit 8ad34e2db9
2 changed files with 42 additions and 32 deletions

View File

@ -1,9 +1,9 @@
/*\ /*\
title: $:/core/modules/themes.js title: $:/core/modules/pluginswitcher.js
type: application/javascript type: application/javascript
module-type: global module-type: global
Manages themes and styling. Manages switching plugins for themes and lingo.
\*/ \*/
(function(){ (function(){
@ -12,42 +12,44 @@ Manages themes and styling.
/*global $tw: false */ /*global $tw: false */
"use strict"; "use strict";
var THEME_PLUGIN_TITLE = "$:/theme", // This tiddler contains the title of the current theme plugin /*
DEFAULT_THEME_PLUGINS = [ options:
"$:/themes/tiddlywiki/snowwhite", wiki: wiki store to be used
"$:/themes/tiddlywiki/vanilla" pluginType: type of plugin to be switched
]; controllerTitle: title of tiddler used to control switching of this resource
defaultPlugins: array of default plugins to be used if nominated plugin isn't found
function ThemeManager(wiki) { */
this.wiki = wiki; function PluginSwitcher(options) {
// There's no theme to start with this.wiki = options.wiki;
this.currentThemeTitle = undefined; this.pluginType = options.pluginType;
// Switch to the current theme this.controllerTitle = options.controllerTitle;
this.switchTheme(); this.defaultPlugins = options.defaultPlugins || [];
// Listen for changes to the theme // Switch to the current plugin
this.switchPlugins();
// Listen for changes to the selected plugin
var self = this; var self = this;
this.wiki.addEventListener("change",function(changes) { this.wiki.addEventListener("change",function(changes) {
if($tw.utils.hop(changes,THEME_PLUGIN_TITLE)) { if($tw.utils.hop(changes,self.controllerTitle)) {
self.switchTheme(); self.switchPlugins();
} }
}); });
} }
ThemeManager.prototype.switchTheme = function() { PluginSwitcher.prototype.switchPlugins = function() {
// Get the name of the current theme // Get the name of the current theme
var themePluginTitle = this.wiki.getTiddlerText(THEME_PLUGIN_TITLE); var selectedPluginTitle = this.wiki.getTiddlerText(this.controllerTitle);
// If it doesn't exist, then fallback to one of the default themes // If it doesn't exist, then fallback to one of the default themes
var index = 0; var index = 0;
while(!this.wiki.getTiddler(themePluginTitle) && index < DEFAULT_THEME_PLUGINS.length) { while(!this.wiki.getTiddler(selectedPluginTitle) && index < this.defaultPlugins.length) {
themePluginTitle = DEFAULT_THEME_PLUGINS[index++]; selectedPluginTitle = this.defaultPlugins[index++];
} }
// Accumulate the titles of the plugins that we need to load // Accumulate the titles of the plugins that we need to load
var themePlugins = [], var plugins = [],
self = this, self = this,
accumulatePlugin = function(title) { accumulatePlugin = function(title) {
var tiddler = self.wiki.getTiddler(title); var tiddler = self.wiki.getTiddler(title);
if(tiddler && tiddler.isPlugin() && themePlugins.indexOf(title) === -1) { if(tiddler && tiddler.isPlugin() && plugins.indexOf(title) === -1) {
themePlugins.push(title); plugins.push(title);
var pluginInfo = JSON.parse(self.wiki.getTiddlerText(title)), var pluginInfo = JSON.parse(self.wiki.getTiddlerText(title)),
dependents = $tw.utils.parseStringArray(tiddler.fields.dependents || ""); dependents = $tw.utils.parseStringArray(tiddler.fields.dependents || "");
$tw.utils.each(dependents,function(title) { $tw.utils.each(dependents,function(title) {
@ -55,23 +57,23 @@ ThemeManager.prototype.switchTheme = function() {
}); });
} }
}; };
accumulatePlugin(themePluginTitle); accumulatePlugin(selectedPluginTitle);
// Unregister any existing theme tiddlers // Unregister any existing theme tiddlers
var unregisteredThemeTiddlers = $tw.wiki.unregisterPluginTiddlers("theme"); var unregisteredTiddlers = $tw.wiki.unregisterPluginTiddlers(this.pluginType);
// Accumulate the titles of shadow tiddlers that have changed as a result of this switch // Accumulate the titles of shadow tiddlers that have changed as a result of this switch
var changedTiddlers = {}; var changedTiddlers = {};
$tw.utils.each(this.wiki.shadowTiddlers,function(shadowInfo,title) { $tw.utils.each(this.wiki.shadowTiddlers,function(shadowInfo,title) {
if(unregisteredThemeTiddlers.indexOf(shadowInfo.source) !== -1) { if(unregisteredTiddlers.indexOf(shadowInfo.source) !== -1) {
changedTiddlers[title] = true; // isDeleted? changedTiddlers[title] = true; // isDeleted?
} }
}); });
// Register any new theme tiddlers // Register any new theme tiddlers
var registeredThemeTiddlers = $tw.wiki.registerPluginTiddlers("theme",themePlugins); var registeredTiddlers = $tw.wiki.registerPluginTiddlers(this.pluginType,plugins);
// Unpack the current theme tiddlers // Unpack the current theme tiddlers
$tw.wiki.unpackPluginTiddlers(); $tw.wiki.unpackPluginTiddlers();
// Accumulate the affected shadow tiddlers // Accumulate the affected shadow tiddlers
$tw.utils.each(this.wiki.shadowTiddlers,function(shadowInfo,title) { $tw.utils.each(this.wiki.shadowTiddlers,function(shadowInfo,title) {
if(registeredThemeTiddlers.indexOf(shadowInfo.source) !== -1) { if(registeredTiddlers.indexOf(shadowInfo.source) !== -1) {
changedTiddlers[title] = false; // isDeleted? changedTiddlers[title] = false; // isDeleted?
} }
}); });
@ -81,6 +83,6 @@ ThemeManager.prototype.switchTheme = function() {
}); });
}; };
exports.ThemeManager = ThemeManager; exports.PluginSwitcher = PluginSwitcher;
})(); })();

View File

@ -39,7 +39,15 @@ exports.startup = function() {
// Set up the command modules // Set up the command modules
$tw.Commander.initCommands(); $tw.Commander.initCommands();
// Kick off the theme manager // Kick off the theme manager
$tw.themeManager = new $tw.ThemeManager($tw.wiki); $tw.themeManager = new $tw.PluginSwitcher({
wiki: $tw.wiki,
pluginType: "theme",
controllerTitle: "$:/theme",
defaultPlugins: [
"$:/themes/tiddlywiki/snowwhite",
"$:/themes/tiddlywiki/vanilla"
]
});
// Get the default tiddlers // Get the default tiddlers
var defaultTiddlersTitle = "$:/DefaultTiddlers", var defaultTiddlersTitle = "$:/DefaultTiddlers",
defaultTiddlersTiddler = $tw.wiki.getTiddler(defaultTiddlersTitle), defaultTiddlersTiddler = $tw.wiki.getTiddler(defaultTiddlersTitle),
@ -63,7 +71,7 @@ exports.startup = function() {
confirmationMessage = "You have unsaved changes in TiddlyWiki"; confirmationMessage = "You have unsaved changes in TiddlyWiki";
event.returnValue = confirmationMessage; // Gecko event.returnValue = confirmationMessage; // Gecko
} }
return confirmationMessage; // Webkit, Safari, Chrome etc. return confirmationMessage;
}); });
// Install the popup manager // Install the popup manager
$tw.popup = new $tw.utils.Popup({ $tw.popup = new $tw.utils.Popup({