Stackedit/js/extension-manager.js

161 lines
5.3 KiB
JavaScript
Raw Normal View History

2013-05-25 00:34:04 +00:00
define( [
2013-05-25 18:13:59 +00:00
"jquery",
"utils",
2013-05-25 00:34:04 +00:00
"underscore",
2013-05-25 18:13:59 +00:00
"bootstrap",
2013-05-25 00:34:04 +00:00
"extensions/notifications",
"extensions/markdown-extra",
2013-05-26 01:10:58 +00:00
"extensions/toc",
2013-05-25 00:34:04 +00:00
"extensions/math-jax",
"extensions/scroll-link"
2013-05-25 18:13:59 +00:00
], function($, utils) {
2013-05-25 00:34:04 +00:00
var extensionManager = {};
2013-05-26 22:59:03 +00:00
// Create a list of extensions
2013-05-25 00:34:04 +00:00
var extensionList = _.chain(arguments)
.map(function(argument) {
2013-05-25 18:13:59 +00:00
return _.isObject(argument) && argument.extensionId && argument;
2013-05-25 00:34:04 +00:00
}).compact().value();
// Return every named callbacks implemented in extensions
2013-05-26 22:59:03 +00:00
function getExtensionCallbackList(hookName) {
2013-05-25 00:34:04 +00:00
return _.chain(extensionList)
.map(function(extension) {
2013-05-26 22:59:03 +00:00
return extension.config.enabled && extension[hookName];
2013-05-25 00:34:04 +00:00
}).compact().value();
}
// Return a function that calls every callbacks from extensions
2013-05-26 22:59:03 +00:00
function createHook(hookName) {
var callbackList = getExtensionCallbackList(hookName);
2013-05-25 00:34:04 +00:00
return function() {
2013-05-26 22:59:03 +00:00
console.debug(hookName);
2013-05-25 00:34:04 +00:00
var callbackArguments = arguments;
_.each(callbackList, function(callback) {
callback.apply(null, callbackArguments);
});
};
}
2013-05-26 22:59:03 +00:00
// Add a Hook to the extensionManager
function addHook(hookName) {
extensionManager[hookName] = createHook(hookName);
2013-05-25 00:34:04 +00:00
}
2013-05-25 18:13:59 +00:00
var accordionTmpl = [
'<div class="accordion-group">',
'<div class="accordion-heading">',
'<label class="checkbox pull-right">',
'<input id="input-enable-extension-<%= extensionId %>" type="checkbox" <% if(!optional) { %> disabled <% } %>> enabled',
'</label>',
'<a id="accordion-toggle-test" data-toggle="collapse" data-parent="#accordion-extensions" class="accordion-toggle" href="#collapse-<%= extensionId %>">',
'<%= extensionName %>',
'</a>',
'</div>',
'<div id="collapse-<%= extensionId %>" class="accordion-body collapse">',
'<div class="accordion-inner"><%= settingsBloc %></div>',
'</div>',
'</div>'].join("");
function createSettings(extension) {
$("#accordion-extensions").append($(_.template(accordionTmpl, {
extensionId: extension.extensionId,
extensionName: extension.extensionName,
optional: extension.optional,
settingsBloc: extension.settingsBloc
})));
}
extensionManager.init = function(extensionSettings) {
// Set extension config
extensionSettings = extensionSettings || {};
2013-05-25 00:34:04 +00:00
_.each(extensionList, function(extension) {
2013-05-25 18:13:59 +00:00
extension.config = _.extend({}, extension.defaultConfig, extensionSettings[extension.extensionId]);
2013-05-26 01:10:58 +00:00
extension.config.enabled = !extension.optional || extension.config.enabled === undefined || extension.config.enabled === true;
2013-05-25 00:34:04 +00:00
});
2013-05-25 18:13:59 +00:00
// Load/Save extension config from/to settings
extensionManager["onLoadSettings"] = function() {
2013-05-26 22:59:03 +00:00
console.debug("onLoadSettings");
2013-05-25 18:13:59 +00:00
_.each(extensionList, function(extension) {
utils.setInputChecked("#input-enable-extension-" + extension.extensionId, extension.config.enabled);
var onLoadSettingsCallback = extension.onLoadSettings;
onLoadSettingsCallback && onLoadSettingsCallback();
});
};
extensionManager["onSaveSettings"] = function(newExtensionSettings, event) {
2013-05-26 22:59:03 +00:00
console.debug("onSaveSettings");
2013-05-25 18:13:59 +00:00
_.each(extensionList, function(extension) {
var newExtensionConfig = extension.defaultConfig || {};
newExtensionConfig.enabled = utils.getInputChecked("#input-enable-extension-" + extension.extensionId);
var onSaveSettingsCallback = extension.onSaveSettings;
onSaveSettingsCallback && onSaveSettingsCallback(newExtensionConfig, event);
newExtensionSettings[extension.extensionId] = newExtensionConfig;
});
};
2013-05-26 22:59:03 +00:00
addHook("onMessage");
addHook("onError");
addHook("onOfflineChanged");
// To store reference to modules that are accessible from extensions
addHook("onFileManagerCreated");
addHook("onSynchronizerCreated");
addHook("onPublisherCreated");
// Operations on files
addHook("onFileSystemLoaded");
addHook("onFileCreated");
addHook("onFileDeleted");
addHook("onFileChanged");
addHook("onFileSelected");
addHook("onTitleChanged");
addHook("onSyncImportSuccess");
addHook("onSyncExportSuccess");
addHook("onSyncRemoved");
addHook("onPublishSuccess");
addHook("onNewPublishSuccess");
addHook("onPublishRemoved");
// Operations on Layout
addHook("onLayoutConfigure");
addHook("onLayoutCreated");
2013-05-25 18:13:59 +00:00
2013-05-26 22:59:03 +00:00
// Operations on PageDown
addHook("onEditorConfigure");
var onPreviewFinished = createHook("onPreviewFinished");
2013-05-25 18:13:59 +00:00
var onAsyncPreviewCallbackList = getExtensionCallbackList("onAsyncPreview");
extensionManager["onAsyncPreview"] = function() {
2013-05-26 22:59:03 +00:00
console.debug("onAsyncPreview");
2013-05-25 18:13:59 +00:00
// Call onPreviewFinished callbacks when all async preview are finished
var counter = 0;
function tryFinished() {
if(counter === onAsyncPreviewCallbackList.length) {
onPreviewFinished();
}
2013-05-25 00:34:04 +00:00
}
2013-05-25 18:13:59 +00:00
_.each(onAsyncPreviewCallbackList, function(asyncPreviewCallback) {
asyncPreviewCallback(function() {
counter++;
tryFinished();
});
2013-05-25 00:34:04 +00:00
});
2013-05-25 18:13:59 +00:00
tryFinished();
};
// Call onReady callbacks
2013-05-26 22:59:03 +00:00
var onReady = createHook("onReady");
extensionManager["onReady"] = function() {
// Create accordion in settings dialog
_.each(extensionList, createSettings);
onReady();
};
2013-05-25 00:34:04 +00:00
};
return extensionManager;
});