2013-04-29 11:07:39 +00:00
|
|
|
/*\
|
2014-02-08 09:29:37 +00:00
|
|
|
title: $:/core/modules/pluginswitcher.js
|
2013-04-29 11:07:39 +00:00
|
|
|
type: application/javascript
|
|
|
|
module-type: global
|
|
|
|
|
2014-02-15 11:32:11 +00:00
|
|
|
Manages switching plugins for themes and languages.
|
2013-04-29 11:07:39 +00:00
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2014-02-08 09:29:37 +00:00
|
|
|
/*
|
|
|
|
options:
|
|
|
|
wiki: wiki store to be used
|
|
|
|
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
|
2016-08-15 18:47:26 +00:00
|
|
|
onSwitch: callback when plugin is switched (single parameter is array of plugin titles)
|
2014-02-08 09:29:37 +00:00
|
|
|
*/
|
|
|
|
function PluginSwitcher(options) {
|
|
|
|
this.wiki = options.wiki;
|
|
|
|
this.pluginType = options.pluginType;
|
|
|
|
this.controllerTitle = options.controllerTitle;
|
|
|
|
this.defaultPlugins = options.defaultPlugins || [];
|
2016-08-15 18:47:26 +00:00
|
|
|
this.onSwitch = options.onSwitch;
|
2014-02-08 09:29:37 +00:00
|
|
|
// Switch to the current plugin
|
|
|
|
this.switchPlugins();
|
|
|
|
// Listen for changes to the selected plugin
|
2013-04-30 21:57:10 +00:00
|
|
|
var self = this;
|
|
|
|
this.wiki.addEventListener("change",function(changes) {
|
2014-02-08 09:29:37 +00:00
|
|
|
if($tw.utils.hop(changes,self.controllerTitle)) {
|
|
|
|
self.switchPlugins();
|
2013-04-30 21:57:10 +00:00
|
|
|
}
|
|
|
|
});
|
2013-04-29 11:07:39 +00:00
|
|
|
}
|
|
|
|
|
2014-02-08 09:29:37 +00:00
|
|
|
PluginSwitcher.prototype.switchPlugins = function() {
|
2013-04-30 21:57:10 +00:00
|
|
|
// Get the name of the current theme
|
2014-02-08 09:29:37 +00:00
|
|
|
var selectedPluginTitle = this.wiki.getTiddlerText(this.controllerTitle);
|
2013-12-01 20:28:56 +00:00
|
|
|
// If it doesn't exist, then fallback to one of the default themes
|
|
|
|
var index = 0;
|
2014-02-08 09:29:37 +00:00
|
|
|
while(!this.wiki.getTiddler(selectedPluginTitle) && index < this.defaultPlugins.length) {
|
|
|
|
selectedPluginTitle = this.defaultPlugins[index++];
|
2013-12-01 20:28:56 +00:00
|
|
|
}
|
2013-04-30 21:57:10 +00:00
|
|
|
// Accumulate the titles of the plugins that we need to load
|
2014-02-08 09:29:37 +00:00
|
|
|
var plugins = [],
|
2013-04-30 21:57:10 +00:00
|
|
|
self = this,
|
|
|
|
accumulatePlugin = function(title) {
|
|
|
|
var tiddler = self.wiki.getTiddler(title);
|
2014-02-08 09:29:37 +00:00
|
|
|
if(tiddler && tiddler.isPlugin() && plugins.indexOf(title) === -1) {
|
|
|
|
plugins.push(title);
|
2013-12-14 09:46:02 +00:00
|
|
|
var pluginInfo = JSON.parse(self.wiki.getTiddlerText(title)),
|
|
|
|
dependents = $tw.utils.parseStringArray(tiddler.fields.dependents || "");
|
|
|
|
$tw.utils.each(dependents,function(title) {
|
2013-04-30 21:57:10 +00:00
|
|
|
accumulatePlugin(title);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2014-02-08 09:29:37 +00:00
|
|
|
accumulatePlugin(selectedPluginTitle);
|
2013-04-30 21:57:10 +00:00
|
|
|
// Unregister any existing theme tiddlers
|
2014-02-08 09:29:37 +00:00
|
|
|
var unregisteredTiddlers = $tw.wiki.unregisterPluginTiddlers(this.pluginType);
|
2013-04-30 21:57:10 +00:00
|
|
|
// Register any new theme tiddlers
|
2014-02-08 09:29:37 +00:00
|
|
|
var registeredTiddlers = $tw.wiki.registerPluginTiddlers(this.pluginType,plugins);
|
2013-04-30 21:57:10 +00:00
|
|
|
// Unpack the current theme tiddlers
|
|
|
|
$tw.wiki.unpackPluginTiddlers();
|
2016-08-15 18:47:26 +00:00
|
|
|
// Call the switch handler
|
|
|
|
if(this.onSwitch) {
|
|
|
|
this.onSwitch(plugins);
|
|
|
|
}
|
2013-04-30 21:57:10 +00:00
|
|
|
};
|
|
|
|
|
2014-02-08 09:29:37 +00:00
|
|
|
exports.PluginSwitcher = PluginSwitcher;
|
2013-04-29 11:07:39 +00:00
|
|
|
|
|
|
|
})();
|