snappymail/dev/Common/Plugins.js

144 lines
3.1 KiB
JavaScript
Raw Normal View History

2014-09-05 06:49:03 +08:00
(function () {
2014-08-25 23:49:01 +08:00
'use strict';
2014-08-20 23:03:12 +08:00
var
2014-08-25 23:49:01 +08:00
_ = require('_'),
Globals = require('Common/Globals'),
2014-09-05 06:49:03 +08:00
Utils = require('Common/Utils')
2014-08-20 23:03:12 +08:00
;
/**
* @constructor
2014-08-20 23:03:12 +08:00
*/
function Plugins()
{
this.oSettings = require('Storage/Settings');
this.oSimpleHooks = {};
this.aUserViewModelsHooks = [];
this.aAdminViewModelsHooks = [];
}
/**
* @type {Object}
*/
Plugins.prototype.oSettings = {};
2014-08-20 23:03:12 +08:00
/**
* @type {Array}
*/
Plugins.prototype.aUserViewModelsHooks = [];
/**
* @type {Array}
2014-08-20 23:03:12 +08:00
*/
Plugins.prototype.aAdminViewModelsHooks = [];
2014-08-20 23:03:12 +08:00
/**
* @type {Object}
2014-08-20 23:03:12 +08:00
*/
Plugins.prototype.oSimpleHooks = {};
2014-08-20 23:03:12 +08:00
/**
* @param {string} sName
* @param {Function} fCallback
*/
Plugins.prototype.addHook = function (sName, fCallback)
{
2014-08-20 23:03:12 +08:00
if (Utils.isFunc(fCallback))
{
if (!Utils.isArray(this.oSimpleHooks[sName]))
2014-08-20 23:03:12 +08:00
{
this.oSimpleHooks[sName] = [];
2014-08-20 23:03:12 +08:00
}
this.oSimpleHooks[sName].push(fCallback);
}
2014-08-20 23:03:12 +08:00
};
2014-08-20 23:03:12 +08:00
/**
* @param {string} sName
* @param {Array=} aArguments
*/
Plugins.prototype.runHook = function (sName, aArguments)
{
if (Utils.isArray(this.oSimpleHooks[sName]))
2014-08-20 23:03:12 +08:00
{
aArguments = aArguments || [];
_.each(this.oSimpleHooks[sName], function (fCallback) {
2014-08-20 23:03:12 +08:00
fCallback.apply(null, aArguments);
});
}
};
/**
* @param {string} sName
* @return {?}
*/
Plugins.prototype.mainSettingsGet = function (sName)
2014-08-20 23:03:12 +08:00
{
return this.oSettings.settingsGet(sName);
2014-08-20 23:03:12 +08:00
};
2014-08-20 23:03:12 +08:00
/**
* @param {Function} fCallback
* @param {string} sAction
* @param {Object=} oParameters
* @param {?number=} iTimeout
*/
Plugins.prototype.remoteRequest = function (fCallback, sAction, oParameters, iTimeout)
{
if (Globals.__APP__)
2014-08-20 23:03:12 +08:00
{
Globals.__APP__.remote().defaultRequest(fCallback, 'Plugin' + sAction, oParameters, iTimeout);
2014-08-20 23:03:12 +08:00
}
};
/**
* @param {Function} SettingsViewModelClass
* @param {string} sLabelName
* @param {string} sTemplate
* @param {string} sRoute
*/
Plugins.prototype.addSettingsViewModel = function (SettingsViewModelClass, sTemplate, sLabelName, sRoute)
{
2015-02-20 06:13:27 +08:00
this.aUserViewModelsHooks.push([SettingsViewModelClass, sTemplate, sLabelName, sRoute]);
};
/**
* @param {Function} SettingsViewModelClass
* @param {string} sLabelName
* @param {string} sTemplate
* @param {string} sRoute
*/
Plugins.prototype.addSettingsViewModelForAdmin = function (SettingsViewModelClass, sTemplate, sLabelName, sRoute)
{
this.aAdminViewModelsHooks.push([SettingsViewModelClass, sTemplate, sLabelName, sRoute]);
};
Plugins.prototype.runSettingsViewModelHooks = function (bAdmin)
{
2015-02-20 06:13:27 +08:00
_.each(bAdmin ? this.aAdminViewModelsHooks : this.aUserViewModelsHooks, function (aView) {
require('Knoin/Knoin').addSettingsViewModel(aView[0], aView[1], aView[2], aView[3]);
});
};
2014-08-20 23:03:12 +08:00
/**
* @param {string} sPluginSection
* @param {string} sName
* @return {?}
*/
Plugins.prototype.settingsGet = function (sPluginSection, sName)
2014-08-20 23:03:12 +08:00
{
var oPlugin = this.oSettings.settingsGet('Plugins');
2014-08-26 14:31:20 +08:00
oPlugin = oPlugin && !Utils.isUnd(oPlugin[sPluginSection]) ? oPlugin[sPluginSection] : null;
2014-08-20 23:03:12 +08:00
return oPlugin ? (Utils.isUnd(oPlugin[sName]) ? null : oPlugin[sName]) : null;
};
module.exports = new Plugins();
2014-09-05 06:49:03 +08:00
}());