Skip to content

Commit

Permalink
Remove the whole plugin in disable function. Add docs (#1810)
Browse files Browse the repository at this point in the history
  • Loading branch information
offtherailz authored and mbarto committed May 11, 2017
1 parent b917598 commit c34cd52
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions web/client/components/plugins/PluginsContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ const PluginsContainer = React.createClass({
(this.props.pluginsConfig && this.props.pluginsConfig[this.props.mode] || [])
.map((plugin) => PluginsUtils.getPluginDescriptor(this.getState, this.props.plugins,
this.props.pluginsConfig[this.props.mode], plugin, this.state.loadedPlugins))
.filter(plugin => PluginsUtils.filterDisabledPlugins({plugin: plugin && plugin.impl || plugin}, this.getState))
.filter((plugin) => plugin && plugin.impl.loadPlugin).forEach((plugin) => {
if (!this.state.loadedPlugins[plugin.name]) {
if (!plugin.impl.enabler || plugin.impl.enabler(state)) {
Expand Down
25 changes: 22 additions & 3 deletions web/client/utils/PluginsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@ const defaultMonitoredState = [{name: "mapType", path: 'maptype.mapType'}, {name
const {combineEpics} = require('redux-observable');

const {memoize, get} = require('lodash');

/**
* Gives a reduced version of the status to check.
* It cached the last state to prevent re-evaluations if the input didn't change.
* @memberof utils.PluginsUtils
* @function
* @param {Object} state the state
* @param {Object[]} monitor an array of objects in the form `{name: "a", path: "b"}` used to produce the monitoredState
* @return {Object} the state filtered using the monitor rules
* @example
* const monitor =[{name: "a", path: "b"}`];
* const state = {b: "test"}
* filterState(state, monitor); // returns {a: "test"}
*/
const filterState = memoize((state, monitor) => {
return monitor.reduce((previous, current) => {
return assign(previous, {
Expand Down Expand Up @@ -65,9 +77,16 @@ const handleExpression = (state, context, expression) => {
}
return expression;
};

/**
* filters the plugins passed evaluating the dsiablePluginIf expression with the given context
* @memberof utils.PluginsUtils
* @param {Object} item the plugins
* @param {function} [state={}] The state to evaluate
* @param {Object} [plugins={}] the plugins object to get requires
* @return {Boolean} the result of the expression evaluation in the given context.
*/
const filterDisabledPlugins = (item, state = {}, plugins = {}) => {
const disablePluginIf = item && item.plugin && item.plugin.disablePluginIf || item.cfg.disablePluginIf;
const disablePluginIf = item && item.plugin && item.plugin.disablePluginIf || item.cfg && item.cfg.disablePluginIf;
if (disablePluginIf && !(item && item.cfg && item.cfg.skipAutoDisable)) {
return !handleExpression(state, plugins.requires, disablePluginIf);
}
Expand Down
7 changes: 7 additions & 0 deletions web/client/utils/__tests__/PluginUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ describe('PluginsUtils', () => {
{},
{}
)).toBe(false);

// check ignore other items, if any
expect(PluginsUtils.filterDisabledPlugins(
{},
{},
{}
)).toBe(true);
});
it('getMonitoredState', () => {
expect(PluginsUtils.getMonitoredState({maptype: {mapType: "leaflet"}}).mapType).toBe("leaflet");
Expand Down

0 comments on commit c34cd52

Please sign in to comment.