2013-05-25 00:34:04 +00:00
|
|
|
define( [
|
2013-05-25 18:13:59 +00:00
|
|
|
"jquery",
|
2013-05-25 00:34:04 +00:00
|
|
|
"underscore",
|
2013-05-27 19:45:33 +00:00
|
|
|
"utils",
|
|
|
|
"settings",
|
2013-05-28 23:41:09 +00:00
|
|
|
"extensions/button-sync",
|
2013-05-27 19:45:33 +00:00
|
|
|
"extensions/button-publish",
|
|
|
|
"extensions/button-share",
|
|
|
|
"extensions/document-selector",
|
|
|
|
"extensions/document-title",
|
|
|
|
"extensions/manage-publication",
|
|
|
|
"extensions/manage-synchronization",
|
|
|
|
"extensions/working-indicator",
|
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",
|
2013-05-27 23:27:38 +00:00
|
|
|
"extensions/email-converter",
|
2013-05-27 19:45:33 +00:00
|
|
|
"extensions/scroll-link",
|
|
|
|
"lib/bootstrap"
|
|
|
|
], function($, _, utils, settings) {
|
2013-05-25 00:34:04 +00:00
|
|
|
|
2013-05-27 19:45:33 +00:00
|
|
|
var extensionMgr = {};
|
2013-05-25 00:34:04 +00:00
|
|
|
|
2013-05-26 22:59:03 +00:00
|
|
|
// Create a list of extensions
|
2013-05-27 19:45:33 +00:00
|
|
|
var extensionList = _.chain(
|
|
|
|
arguments
|
|
|
|
).map(function(argument) {
|
|
|
|
return _.isObject(argument) && argument.extensionId && argument;
|
|
|
|
}).compact().value();
|
2013-05-25 00:34:04 +00:00
|
|
|
|
|
|
|
// Return every named callbacks implemented in extensions
|
2013-05-26 22:59:03 +00:00
|
|
|
function getExtensionCallbackList(hookName) {
|
2013-05-27 19:45:33 +00:00
|
|
|
return _.chain(
|
|
|
|
extensionList
|
|
|
|
).map(function(extension) {
|
|
|
|
return extension.config.enabled && extension[hookName];
|
|
|
|
}).compact().value();
|
2013-05-25 00:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-28 23:41:09 +00:00
|
|
|
logger.debug(hookName, arguments);
|
2013-05-25 00:34:04 +00:00
|
|
|
var callbackArguments = arguments;
|
|
|
|
_.each(callbackList, function(callback) {
|
|
|
|
callback.apply(null, callbackArguments);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-05-27 19:45:33 +00:00
|
|
|
// Add a Hook to the extensionMgr
|
2013-05-26 22:59:03 +00:00
|
|
|
function addHook(hookName) {
|
2013-05-27 19:45:33 +00:00
|
|
|
extensionMgr[hookName] = createHook(hookName);
|
2013-05-25 00:34:04 +00:00
|
|
|
}
|
|
|
|
|
2013-05-27 19:45:33 +00:00
|
|
|
// Set extension config
|
|
|
|
extensionSettings = settings.extensionSettings || {};
|
|
|
|
_.each(extensionList, function(extension) {
|
|
|
|
extension.config = _.extend({}, extension.defaultConfig, extensionSettings[extension.extensionId]);
|
|
|
|
extension.config.enabled = !extension.optional || extension.config.enabled === undefined || extension.config.enabled === true;
|
|
|
|
});
|
2013-05-25 18:13:59 +00:00
|
|
|
|
2013-05-27 19:45:33 +00:00
|
|
|
// Load/Save extension config from/to settings
|
|
|
|
extensionMgr["onLoadSettings"] = function() {
|
2013-05-28 23:41:09 +00:00
|
|
|
logger.debug("onLoadSettings");
|
2013-05-25 00:34:04 +00:00
|
|
|
_.each(extensionList, function(extension) {
|
2013-05-27 19:45:33 +00:00
|
|
|
utils.setInputChecked("#input-enable-extension-" + extension.extensionId, extension.config.enabled);
|
|
|
|
var onLoadSettingsCallback = extension.onLoadSettings;
|
|
|
|
onLoadSettingsCallback && onLoadSettingsCallback();
|
2013-05-25 00:34:04 +00:00
|
|
|
});
|
2013-05-27 19:45:33 +00:00
|
|
|
};
|
|
|
|
extensionMgr["onSaveSettings"] = function(newExtensionSettings, event) {
|
2013-05-28 23:41:09 +00:00
|
|
|
logger.debug("onSaveSettings");
|
2013-05-27 19:45:33 +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;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
addHook("onReady");
|
|
|
|
addHook("onMessage");
|
|
|
|
addHook("onError");
|
|
|
|
addHook("onOfflineChanged");
|
|
|
|
addHook("onAsyncRunning");
|
|
|
|
|
2013-05-28 23:41:09 +00:00
|
|
|
// To access modules that are loaded after extensions
|
2013-05-27 19:45:33 +00:00
|
|
|
addHook("onFileMgrCreated");
|
|
|
|
addHook("onSynchronizerCreated");
|
|
|
|
addHook("onPublisherCreated");
|
|
|
|
|
|
|
|
// Operations on files
|
|
|
|
addHook("onFileCreated");
|
|
|
|
addHook("onFileDeleted");
|
|
|
|
addHook("onFileSelected");
|
2013-05-28 23:41:09 +00:00
|
|
|
addHook("onContentChanged");
|
2013-05-27 19:45:33 +00:00
|
|
|
addHook("onTitleChanged");
|
|
|
|
|
|
|
|
// Sync events
|
|
|
|
addHook("onSyncRunning");
|
|
|
|
addHook("onSyncSuccess");
|
|
|
|
addHook("onSyncImportSuccess");
|
|
|
|
addHook("onSyncExportSuccess");
|
|
|
|
addHook("onSyncRemoved");
|
|
|
|
|
|
|
|
// Publish events
|
|
|
|
addHook("onPublishRunning");
|
|
|
|
addHook("onPublishSuccess");
|
|
|
|
addHook("onNewPublishSuccess");
|
|
|
|
addHook("onPublishRemoved");
|
|
|
|
|
|
|
|
// Operations on Layout
|
|
|
|
addHook("onLayoutConfigure");
|
|
|
|
addHook("onLayoutCreated");
|
|
|
|
|
|
|
|
// Operations on PageDown
|
|
|
|
addHook("onEditorConfigure");
|
|
|
|
|
|
|
|
var onPreviewFinished = createHook("onPreviewFinished");
|
|
|
|
var onAsyncPreviewCallbackList = getExtensionCallbackList("onAsyncPreview");
|
|
|
|
extensionMgr["onAsyncPreview"] = function() {
|
2013-05-28 23:41:09 +00:00
|
|
|
logger.debug("onAsyncPreview");
|
2013-05-27 19:45:33 +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-27 19:45:33 +00:00
|
|
|
}
|
|
|
|
_.each(onAsyncPreviewCallbackList, function(asyncPreviewCallback) {
|
|
|
|
asyncPreviewCallback(function() {
|
|
|
|
counter++;
|
|
|
|
tryFinished();
|
2013-05-25 00:34:04 +00:00
|
|
|
});
|
2013-05-27 19:45:33 +00:00
|
|
|
});
|
|
|
|
tryFinished();
|
2013-05-25 00:34:04 +00:00
|
|
|
};
|
|
|
|
|
2013-05-27 23:27:38 +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
|
|
|
|
})));
|
|
|
|
}
|
|
|
|
|
2013-05-27 19:45:33 +00:00
|
|
|
$(function() {
|
|
|
|
// Create accordion in settings dialog
|
2013-05-27 23:27:38 +00:00
|
|
|
_.chain(
|
|
|
|
extensionList
|
|
|
|
).sortBy(function(extension) {
|
|
|
|
return extension.extensionName.toLowerCase();
|
|
|
|
}).each(createSettings);
|
2013-05-28 23:41:09 +00:00
|
|
|
|
|
|
|
// Create extension buttons
|
|
|
|
logger.debug("onCreateButton");
|
|
|
|
var onCreateButtonCallbackList = getExtensionCallbackList("onCreateButton");
|
|
|
|
_.each(onCreateButtonCallbackList, function(callback) {
|
|
|
|
$("#extension-buttons").append($('<div class="btn-group">').append(callback()));
|
|
|
|
});
|
|
|
|
|
2013-05-27 19:45:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return extensionMgr;
|
2013-05-25 00:34:04 +00:00
|
|
|
});
|