UNPKG

roc

Version:

Build modern web applications easily

136 lines (118 loc) 4.49 kB
'use strict'; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.registerActions = registerActions; exports.registerAction = registerAction; exports.removeActions = removeActions; exports.getActions = getActions; exports.setActions = setActions; var _isFunction = require('lodash/isFunction'); var _isFunction2 = _interopRequireDefault(_isFunction); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } // This needs to be global, same case as with configuration global.roc = global.roc || {}; global.roc.actions = global.roc.actions || []; /** * Register actions with Roc. * * @param {Object<rocAction>} actions - Object with actions. * @param {string} extensionName - Name of the extension to register the actions on. */ function registerActions(actions, extensionName) { // Look for the extensionName and only add if not already there const index = global.roc.actions.findIndex(_ref => { let name = _ref.name; return extensionName === name; }); if (index === -1) { let extensionActions = {}; Object.keys(actions).forEach(key => { const action = (0, _isFunction2.default)(actions[key]) ? actions[key]() : actions[key].action(); extensionActions = _extends({}, extensionActions, { [key]: _extends({}, createActionHelper(action, actions[key].extension, actions[key].hook, actions[key].description)) }); }); global.roc.actions = [].concat(global.roc.actions, { name: extensionName, actions: extensionActions }); } } /** * Register single action with Roc. * * @param {function} action - The action function. * @param {string} actionName - The name of the action. * @param {string} extensionName - Name of the extension to register the actions on. * @param {boolean} [project=false] - If the action belongs to the project. */ function registerAction(action, actionName, extensionName) { let project = arguments.length <= 3 || arguments[3] === undefined ? false : arguments[3]; // Look for the extensionName and update if it exists const index = global.roc.actions.findIndex(_ref2 => { let name = _ref2.name; return extensionName === name; }); if (index !== -1) { global.roc.actions[index].actions = _extends({}, global.roc.actions[index].actions, { [actionName]: _extends({}, createActionHelper(action())) }); } else { global.roc.actions.push({ project, name: extensionName, actions: { [actionName]: _extends({}, createActionHelper(action())) } }); } } function createActionHelper(action, extension, hook, description) { return { action, extension, hook, description }; } /** * Removes actions from Roc. * * @param {string} extensionToRemove - Name of the extension to remove registered actions for. * @param {string} actionToRemove - Name of the action to remove, if left undefined all actions for the extension will * be removed. */ function removeActions(extensionToRemove, actionToRemove) { if (!extensionToRemove) { throw new Error('You need to at least specify the extension to remove actions for.'); } global.roc.actions.map(extension => { if (!actionToRemove && extension.name !== extensionToRemove) { return extension; } else if (extension.name === extensionToRemove) { delete extension.actions[actionToRemove]; return extension; } }).filter(element => !!element); } /** * Gets the registered actions. * * @returns {Object[]} - The registered actions as an array where the order will be based on the order they registered * themselves with Roc. */ function getActions() { return global.roc.actions; } /** * Sets the registered actions. * * @param {Object[]} actions - The actions as an array where the order should be based on the order they registered * themselves with Roc. */ function setActions(actions) { global.roc.actions = actions; } //# sourceMappingURL=actions.js.map