snappymail/dev/Common/Plugins.js

110 lines
2.4 KiB
JavaScript
Raw Normal View History

2015-11-15 08:23:16 +08:00
2016-07-02 06:49:59 +08:00
import _ from '_';
2016-06-07 05:57:52 +08:00
import {isFunc, isArray, isUnd} from 'Common/Utils';
import {data as GlobalsData} from 'Common/Globals';
2016-06-16 07:36:44 +08:00
import * as Settings from 'Storage/Settings';
2015-11-15 08:23:16 +08:00
2016-07-02 06:49:59 +08:00
const
SIMPLE_HOOKS = {},
USER_VIEW_MODELS_HOOKS = [],
ADMIN_VIEW_MODELS_HOOKS = [];
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {string} name
* @param {Function} callback
*/
export function addHook(name, callback)
{
if (isFunc(callback))
{
if (!isArray(SIMPLE_HOOKS[name]))
2015-11-15 08:23:16 +08:00
{
2016-06-16 07:36:44 +08:00
SIMPLE_HOOKS[name] = [];
2015-11-15 08:23:16 +08:00
}
2016-06-16 07:36:44 +08:00
SIMPLE_HOOKS[name].push(callback);
2015-11-15 08:23:16 +08:00
}
2016-06-16 07:36:44 +08:00
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {string} name
* @param {Array=} args = []
*/
export function runHook(name, args = [])
{
if (isArray(SIMPLE_HOOKS[name]))
{
_.each(SIMPLE_HOOKS[name], (callback) => {
2016-07-02 06:49:59 +08:00
callback(...args);
2016-06-16 07:36:44 +08:00
});
2015-11-15 08:23:16 +08:00
}
2016-06-16 07:36:44 +08:00
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {string} name
2016-06-30 08:02:45 +08:00
* @returns {?}
2016-06-16 07:36:44 +08:00
*/
export function mainSettingsGet(name)
{
return Settings.settingsGet(name);
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {Function} callback
* @param {string} action
* @param {Object=} parameters
* @param {?number=} timeout
*/
export function remoteRequest(callback, action, parameters, timeout)
{
if (GlobalsData.__APP__)
{
GlobalsData.__APP__.remote().defaultRequest(callback, 'Plugin' + action, parameters, timeout);
2015-11-15 08:23:16 +08:00
}
2016-06-16 07:36:44 +08:00
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {Function} SettingsViewModelClass
* @param {string} labelName
* @param {string} template
* @param {string} route
*/
export function addSettingsViewModel(SettingsViewModelClass, template, labelName, route)
{
USER_VIEW_MODELS_HOOKS.push([SettingsViewModelClass, template, labelName, route]);
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {Function} SettingsViewModelClass
* @param {string} labelName
* @param {string} template
* @param {string} route
*/
export function addSettingsViewModelForAdmin(SettingsViewModelClass, template, labelName, route)
{
ADMIN_VIEW_MODELS_HOOKS.push([SettingsViewModelClass, template, labelName, route]);
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {boolean} admin
*/
export function runSettingsViewModelHooks(admin)
{
const Knoin = require('Knoin/Knoin');
_.each(admin ? ADMIN_VIEW_MODELS_HOOKS : USER_VIEW_MODELS_HOOKS, (view) => {
Knoin.addSettingsViewModel(view[0], view[1], view[2], view[3]);
});
2015-11-15 08:23:16 +08:00
}
2016-06-16 07:36:44 +08:00
/**
* @param {string} pluginSection
* @param {string} name
2016-06-30 08:02:45 +08:00
* @returns {?}
2016-06-16 07:36:44 +08:00
*/
export function settingsGet(pluginSection, name)
{
let plugins = Settings.settingsGet('Plugins');
plugins = plugins && !isUnd(plugins[pluginSection]) ? plugins[pluginSection] : null;
return plugins ? (isUnd(plugins[name]) ? null : plugins[name]) : null;
}