2013-05-29 19:55:23 +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",
|
2013-06-22 23:48:57 +00:00
|
|
|
"classes/Extension",
|
2013-05-27 19:45:33 +00:00
|
|
|
"settings",
|
2013-06-10 21:22:32 +00:00
|
|
|
"text!html/settingsExtensionsAccordion.html",
|
2013-07-23 23:51:21 +00:00
|
|
|
"extensions/partialRendering",
|
2013-07-07 23:49:19 +00:00
|
|
|
"extensions/userCustom",
|
2013-06-10 21:22:32 +00:00
|
|
|
"extensions/googleAnalytics",
|
|
|
|
"extensions/dialogAbout",
|
|
|
|
"extensions/dialogManagePublication",
|
|
|
|
"extensions/dialogManageSynchronization",
|
2013-06-16 10:47:35 +00:00
|
|
|
"extensions/dialogOpenHarddrive",
|
2013-06-10 21:22:32 +00:00
|
|
|
"extensions/documentSelector",
|
|
|
|
"extensions/documentTitle",
|
|
|
|
"extensions/workingIndicator",
|
2013-05-25 00:34:04 +00:00
|
|
|
"extensions/notifications",
|
2013-06-22 23:48:57 +00:00
|
|
|
"extensions/markdownExtra",
|
2013-05-26 01:10:58 +00:00
|
|
|
"extensions/toc",
|
2013-06-10 21:22:32 +00:00
|
|
|
"extensions/mathJax",
|
|
|
|
"extensions/emailConverter",
|
|
|
|
"extensions/scrollLink",
|
2013-06-24 19:47:27 +00:00
|
|
|
"extensions/buttonSync",
|
|
|
|
"extensions/buttonPublish",
|
|
|
|
"extensions/buttonShare",
|
|
|
|
"extensions/buttonStat",
|
|
|
|
"extensions/buttonHtmlCode",
|
|
|
|
"extensions/buttonMarkdownSyntax",
|
|
|
|
"extensions/buttonViewer",
|
2013-06-02 00:38:23 +00:00
|
|
|
"libs/bootstrap",
|
|
|
|
"libs/jquery.waitforimages"
|
2013-06-22 23:48:57 +00:00
|
|
|
], function($, _, utils, Extension, settings, settingsExtensionsAccordionHTML) {
|
2013-05-29 19:55:23 +00:00
|
|
|
|
|
|
|
var extensionMgr = {};
|
|
|
|
|
|
|
|
// Create a list of extensions
|
|
|
|
var extensionList = _.chain(arguments).map(function(argument) {
|
2013-06-22 23:48:57 +00:00
|
|
|
return argument instanceof Extension && argument;
|
2013-05-29 19:55:23 +00:00
|
|
|
}).compact().value();
|
|
|
|
|
2013-07-07 20:07:11 +00:00
|
|
|
// Returns all callbacks with the specified name that are implemented in extensions
|
2013-05-29 19:55:23 +00:00
|
|
|
function getExtensionCallbackList(hookName) {
|
|
|
|
return _.chain(extensionList).map(function(extension) {
|
|
|
|
return extension.config.enabled && extension[hookName];
|
|
|
|
}).compact().value();
|
|
|
|
}
|
|
|
|
|
2013-07-07 20:07:11 +00:00
|
|
|
// Return a function that calls every callbacks with the specified name from all extensions
|
2013-07-18 00:07:22 +00:00
|
|
|
var hookCallbackList = {};
|
2013-06-10 22:32:34 +00:00
|
|
|
function createHook(hookName, noLog) {
|
2013-07-18 00:07:22 +00:00
|
|
|
hookCallbackList[hookName] = getExtensionCallbackList(hookName);
|
2013-05-29 19:55:23 +00:00
|
|
|
return function() {
|
2013-06-10 22:32:34 +00:00
|
|
|
if(!noLog) {
|
|
|
|
logger.log(hookName, arguments);
|
|
|
|
}
|
2013-05-29 19:55:23 +00:00
|
|
|
var callbackArguments = arguments;
|
2013-07-18 00:07:22 +00:00
|
|
|
_.each(hookCallbackList[hookName], function(callback) {
|
2013-07-07 23:49:19 +00:00
|
|
|
// In case user custom callback contains error
|
|
|
|
try {
|
|
|
|
callback.apply(null, callbackArguments);
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
2013-05-29 19:55:23 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2013-07-20 01:08:17 +00:00
|
|
|
|
2013-05-29 19:55:23 +00:00
|
|
|
// Add a Hook to the extensionMgr
|
2013-06-10 22:32:34 +00:00
|
|
|
function addHook(hookName, noLog) {
|
|
|
|
extensionMgr[hookName] = createHook(hookName, noLog);
|
2013-05-29 19:55:23 +00:00
|
|
|
}
|
|
|
|
|
2013-07-20 01:08:17 +00:00
|
|
|
// Used by external modules to listen to extension events
|
|
|
|
extensionMgr.addHookCallback = function(hookName, callback) {
|
|
|
|
hookCallbackList[hookName].push(callback);
|
|
|
|
};
|
|
|
|
|
2013-05-29 19:55:23 +00:00
|
|
|
// Set extension config
|
|
|
|
extensionSettings = settings.extensionSettings || {};
|
|
|
|
_.each(extensionList, function(extension) {
|
|
|
|
extension.config = _.extend({}, extension.defaultConfig, extensionSettings[extension.extensionId]);
|
2013-06-22 23:48:57 +00:00
|
|
|
extension.config.enabled = !extension.isOptional || extension.config.enabled === undefined || extension.config.enabled === true;
|
2013-05-29 19:55:23 +00:00
|
|
|
});
|
2013-07-07 20:07:11 +00:00
|
|
|
|
|
|
|
// Call every onInit callbacks
|
|
|
|
createHook("onInit")();
|
2013-05-29 19:55:23 +00:00
|
|
|
|
|
|
|
// Load/Save extension config from/to settings
|
|
|
|
extensionMgr["onLoadSettings"] = function() {
|
2013-06-09 09:49:19 +00:00
|
|
|
logger.log("onLoadSettings");
|
2013-05-29 19:55:23 +00:00
|
|
|
_.each(extensionList, function(extension) {
|
|
|
|
utils.setInputChecked("#input-enable-extension-" + extension.extensionId, extension.config.enabled);
|
|
|
|
var onLoadSettingsCallback = extension.onLoadSettings;
|
|
|
|
onLoadSettingsCallback && onLoadSettingsCallback();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
extensionMgr["onSaveSettings"] = function(newExtensionSettings, event) {
|
2013-06-09 09:49:19 +00:00
|
|
|
logger.log("onSaveSettings");
|
2013-05-29 19:55:23 +00:00
|
|
|
_.each(extensionList, function(extension) {
|
|
|
|
var newExtensionConfig = _.extend({}, extension.defaultConfig);
|
|
|
|
newExtensionConfig.enabled = utils.getInputChecked("#input-enable-extension-" + extension.extensionId);
|
|
|
|
var onSaveSettingsCallback = extension.onSaveSettings;
|
|
|
|
onSaveSettingsCallback && onSaveSettingsCallback(newExtensionConfig, event);
|
|
|
|
newExtensionSettings[extension.extensionId] = newExtensionConfig;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
addHook("onMessage");
|
|
|
|
addHook("onError");
|
|
|
|
addHook("onOfflineChanged");
|
2013-06-10 22:32:34 +00:00
|
|
|
addHook("onAsyncRunning", true);
|
|
|
|
addHook("onPeriodicRun", true);
|
2013-05-29 19:55:23 +00:00
|
|
|
|
|
|
|
// To access modules that are loaded after extensions
|
|
|
|
addHook("onFileMgrCreated");
|
|
|
|
addHook("onSynchronizerCreated");
|
|
|
|
addHook("onPublisherCreated");
|
2013-05-30 22:16:12 +00:00
|
|
|
addHook("onExtensionMgrCreated");
|
2013-05-29 19:55:23 +00:00
|
|
|
|
|
|
|
// Operations on files
|
|
|
|
addHook("onFileCreated");
|
|
|
|
addHook("onFileDeleted");
|
|
|
|
addHook("onFileSelected");
|
2013-07-18 00:07:22 +00:00
|
|
|
addHook("onFileOpen");
|
|
|
|
addHook("onFileClosed");
|
2013-05-29 19:55:23 +00:00
|
|
|
addHook("onContentChanged");
|
|
|
|
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");
|
2013-07-26 00:44:12 +00:00
|
|
|
addHook("onSectionsCreated");
|
2013-05-29 19:55:23 +00:00
|
|
|
|
|
|
|
var onPreviewFinished = createHook("onPreviewFinished");
|
|
|
|
var onAsyncPreviewCallbackList = getExtensionCallbackList("onAsyncPreview");
|
2013-06-02 00:38:23 +00:00
|
|
|
// The number of times we expect tryFinished to be called
|
|
|
|
var nbAsyncPreviewCallback = onAsyncPreviewCallbackList.length + 1;
|
2013-07-28 17:14:42 +00:00
|
|
|
var previewContentsElt = undefined;
|
|
|
|
var previewContentsJQElt = undefined;
|
2013-05-29 19:55:23 +00:00
|
|
|
extensionMgr["onAsyncPreview"] = function() {
|
2013-06-09 09:49:19 +00:00
|
|
|
logger.log("onAsyncPreview");
|
2013-05-29 19:55:23 +00:00
|
|
|
// Call onPreviewFinished callbacks when all async preview are finished
|
|
|
|
var counter = 0;
|
|
|
|
function tryFinished() {
|
2013-06-02 00:38:23 +00:00
|
|
|
if(++counter === nbAsyncPreviewCallback) {
|
2013-07-28 17:14:42 +00:00
|
|
|
_.defer(function() {
|
|
|
|
var html = "";
|
|
|
|
_.each(previewContentsElt.children, function(elt) {
|
|
|
|
html += elt.innerHTML;
|
|
|
|
});
|
|
|
|
onPreviewFinished(utils.trim(html));
|
|
|
|
logger.log("Preview time: " + (new Date() - extensionMgr.previewStartTime));
|
2013-07-24 23:20:56 +00:00
|
|
|
});
|
2013-05-29 19:55:23 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-02 00:38:23 +00:00
|
|
|
// We assume images are loading in the preview
|
2013-07-28 17:14:42 +00:00
|
|
|
previewContentsJQElt.waitForImages(tryFinished);
|
2013-05-29 19:55:23 +00:00
|
|
|
_.each(onAsyncPreviewCallbackList, function(asyncPreviewCallback) {
|
2013-06-02 00:38:23 +00:00
|
|
|
asyncPreviewCallback(tryFinished);
|
2013-05-29 19:55:23 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
function createSettings(extension) {
|
2013-06-10 21:22:32 +00:00
|
|
|
$("#accordion-extensions").append($(_.template(settingsExtensionsAccordionHTML, {
|
2013-05-29 19:55:23 +00:00
|
|
|
extensionId: extension.extensionId,
|
|
|
|
extensionName: extension.extensionName,
|
2013-06-22 23:48:57 +00:00
|
|
|
isOptional: extension.isOptional,
|
|
|
|
settingsBlock: extension.settingsBlock
|
2013-05-29 19:55:23 +00:00
|
|
|
})));
|
|
|
|
}
|
|
|
|
|
2013-06-19 20:33:46 +00:00
|
|
|
extensionMgr["onReady"] = function() {
|
2013-07-28 17:14:42 +00:00
|
|
|
previewContentsElt = document.getElementById('preview-contents');
|
|
|
|
previewContentsJQElt = $(previewContentsElt);
|
|
|
|
|
2013-05-29 19:55:23 +00:00
|
|
|
// Create accordion in settings dialog
|
|
|
|
_.chain(extensionList).sortBy(function(extension) {
|
|
|
|
return extension.extensionName.toLowerCase();
|
|
|
|
}).each(createSettings);
|
|
|
|
|
|
|
|
// Create extension buttons
|
2013-06-09 09:49:19 +00:00
|
|
|
logger.log("onCreateButton");
|
2013-05-29 19:55:23 +00:00
|
|
|
var onCreateButtonCallbackList = getExtensionCallbackList("onCreateButton");
|
|
|
|
_.each(onCreateButtonCallbackList, function(callback) {
|
|
|
|
$("#extension-buttons").append($('<div class="btn-group">').append(callback()));
|
|
|
|
});
|
2013-06-20 22:28:49 +00:00
|
|
|
|
2013-06-19 20:33:46 +00:00
|
|
|
// Create extension preview buttons
|
|
|
|
logger.log("onCreatePreviewButton");
|
|
|
|
var onCreatePreviewButtonCallbackList = getExtensionCallbackList("onCreatePreviewButton");
|
|
|
|
_.each(onCreatePreviewButtonCallbackList, function(callback) {
|
2013-06-27 23:10:04 +00:00
|
|
|
$("#extension-preview-buttons").append($('<div class="btn-group">').append(callback()));
|
2013-06-19 20:33:46 +00:00
|
|
|
});
|
2013-06-20 22:28:49 +00:00
|
|
|
|
2013-06-19 20:33:46 +00:00
|
|
|
// Call extensions onReady callbacks
|
|
|
|
var onReady = createHook("onReady");
|
|
|
|
onReady();
|
|
|
|
};
|
2013-05-29 19:55:23 +00:00
|
|
|
|
2013-05-30 22:16:12 +00:00
|
|
|
// For extensions that need to call other extensions
|
|
|
|
extensionMgr.onExtensionMgrCreated(extensionMgr);
|
2013-05-29 19:55:23 +00:00
|
|
|
return extensionMgr;
|
2013-05-25 00:34:04 +00:00
|
|
|
});
|