Skip to content

Commit

Permalink
Revert "Added state to plugins configuration scope" (#1442)
Browse files Browse the repository at this point in the history
  • Loading branch information
MV88 authored Feb 13, 2017
1 parent 01f9a75 commit 2275490
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 63 deletions.
15 changes: 4 additions & 11 deletions web/client/components/plugins/PluginsContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ const PluginsUtils = require('../../utils/PluginsUtils');

const assign = require('object-assign');

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

const PluginsContainer = React.createClass({
propTypes: {
mode: React.PropTypes.string,
Expand All @@ -23,8 +21,7 @@ const PluginsContainer = React.createClass({
className: React.PropTypes.string,
style: React.PropTypes.object,
pluginsState: React.PropTypes.object,
defaultMode: React.PropTypes.string,
stateSelector: React.PropTypes.func
defaultMode: React.PropTypes.string
},
getDefaultProps() {
return {
Expand All @@ -36,8 +33,7 @@ const PluginsContainer = React.createClass({
id: "plugins-container",
className: "plugins-container",
style: {},
pluginsState: {},
stateSelector: () => ({})
pluginsState: {}
};
},
getInitialState() {
Expand All @@ -51,11 +47,8 @@ const PluginsContainer = React.createClass({
componentWillReceiveProps(newProps) {
this.loadPlugins(newProps.pluginsState);
},
getState(path) {
return this.props.stateSelector(path) || get(this.props.params, path);
},
getPluginDescriptor(plugin) {
return PluginsUtils.getPluginDescriptor(this.getState, this.props.plugins,
return PluginsUtils.getPluginDescriptor(this.props.plugins,
this.props.pluginsConfig[this.props.mode], plugin, this.state.loadedPlugins);
},
renderPlugins(plugins) {
Expand Down Expand Up @@ -85,7 +78,7 @@ const PluginsContainer = React.createClass({
},
loadPlugins(state) {
(this.props.pluginsConfig && this.props.pluginsConfig[this.props.mode] || [])
.map((plugin) => PluginsUtils.getPluginDescriptor(this.getState, this.props.plugins,
.map((plugin) => PluginsUtils.getPluginDescriptor(this.props.plugins,
this.props.pluginsConfig[this.props.mode], plugin, this.state.loadedPlugins))
.filter((plugin) => plugin.impl.loadPlugin).forEach((plugin) => {
if (!this.state.loadedPlugins[plugin.name]) {
Expand Down
7 changes: 2 additions & 5 deletions web/client/containers/Embedded.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ const {connect} = require('react-redux');

const url = require('url');
const urlQuery = url.parse(window.location.href, true).query;
const {get} = require('lodash');


const PluginsContainer = connect((state) => ({
mode: (urlQuery.mode || (state.browser && state.browser.mobile ? 'mobile' : 'desktop')),
pluginsState: state && state.controls || {},
stateSelector: (path) => {
return get(state, path);
}
pluginsState: state && state.controls || {}
}))(require('../components/plugins/PluginsContainer'));

const Embedded = React.createClass({
Expand Down
7 changes: 1 addition & 6 deletions web/client/containers/MapViewer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,10 @@ const urlQuery = url.parse(window.location.href, true).query;

const ConfigUtils = require('../utils/ConfigUtils');

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

const PluginsContainer = connect((state) => ({
pluginsConfig: state.plugins || ConfigUtils.getConfigProp('plugins') || null,
mode: (urlQuery.mode || (state.browser && state.browser.mobile ? 'mobile' : 'desktop')),
pluginsState: state && state.controls || {},
stateSelector: (path) => {
return get(state, path);
}
pluginsState: state && state.controls || {}
}))(require('../components/plugins/PluginsContainer'));

const MapViewer = React.createClass({
Expand Down
6 changes: 1 addition & 5 deletions web/client/containers/Page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ const {connect} = require('react-redux');

const url = require('url');
const urlQuery = url.parse(window.location.href, true).query;
const {get} = require('lodash');

const PluginsContainer = connect((state) => ({
mode: urlQuery.mode || ((urlQuery.mobile || (state.browser && state.browser.mobile)) ? 'mobile' : 'desktop'),
stateSelector: (path) => {
return get(state, path);
}
mode: urlQuery.mode || ((urlQuery.mobile || (state.browser && state.browser.mobile)) ? 'mobile' : 'desktop')
}))(require('../components/plugins/PluginsContainer'));

const Page = React.createClass({
Expand Down
6 changes: 1 addition & 5 deletions web/client/examples/plugins/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ const startApp = () => {
const {loadMapConfig} = require('../../actions/config');
const {loadLocale} = require('../../actions/locale');
const {loadPrintCapabilities} = require('../../actions/print');
const {get} = require('lodash');

const PluginsContainer = connect((state) => ({
pluginsState: state && state.controls || {},
stateSelector: (path) => {
return get(state, path);
}
pluginsState: state && state.controls || {}
}))(require('../../components/plugins/PluginsContainer'));

const {plugins} = require('./plugins');
Expand Down
62 changes: 31 additions & 31 deletions web/client/utils/PluginsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,32 +44,7 @@ const getMorePrioritizedContainer = (pluginImpl, plugins, priority) => {
}, {plugin: null, priority: priority});
};

/*eslint-disable */
const parseExpression = (state, requires, value) => {
const searchExpression = /^\{(.*?)\}$/;
const context = requires || {};
const expression = searchExpression.exec(value);
if (expression !== null) {
return eval(expression[1]);
}
return value;
};
/*eslint-enable */

const parsePluginConfig = (state, requires, cfg) => {
if (isArray(cfg)) {
return cfg.map((value) => parsePluginConfig(state, requires, value));
}
if (isObject(cfg)) {
return Object.keys(cfg).reduce((previous, current) => {
const value = cfg[current];
return assign(previous, {[current]: parsePluginConfig(state, requires, value)});
}, {});
}
return parseExpression(state, requires, cfg);
};

const getPluginItems = (state, plugins, pluginsConfig, name, id, isDefault, loadedPlugins) => {
const getPluginItems = (plugins, pluginsConfig, name, id, isDefault, loadedPlugins) => {
return Object.keys(plugins)
.filter((plugin) => plugins[plugin][name])
.filter((plugin) => {
Expand All @@ -86,15 +61,40 @@ const getPluginItems = (state, plugins, pluginsConfig, name, id, isDefault, load
item,
pluginCfg.override && pluginCfg.override[name] || {},
{
cfg: pluginCfg && parsePluginConfig(state, plugins.requires, pluginCfg.cfg || {}) || undefined
cfg: pluginCfg && pluginCfg.cfg || undefined
},
{
plugin: pluginImpl,
items: getPluginItems(state, plugins, pluginsConfig, pluginName, null, true, loadedPlugins)
items: getPluginItems(plugins, pluginsConfig, pluginName, null, true, loadedPlugins)
});
});
};

/*eslint-disable */
const parseExpression = (requires, value) => {
const searchExpression = /^\{(.*?)\}$/;
const context = requires || {};
const expression = searchExpression.exec(value);
if (expression !== null) {
return eval(expression[1]);
}
return value;
};
/*eslint-enable */


const parsePluginConfig = (requires, cfg) => {
if (isArray(cfg)) {
return cfg.map((value) => parsePluginConfig(requires, value));
}
if (isObject(cfg)) {
return Object.keys(cfg).reduce((previous, current) => {
const value = cfg[current];
return assign(previous, {[current]: parsePluginConfig(requires, value)});
}, {});
}
return parseExpression(requires, cfg);
};
const getReducers = (plugins) => Object.keys(plugins).map((name) => plugins[name].reducers)
.reduce((previous, current) => assign({}, previous, current), {});

Expand All @@ -106,7 +106,7 @@ const PluginsUtils = {
getReducers,
getPlugins: (plugins) => Object.keys(plugins).map((name) => plugins[name])
.reduce((previous, current) => assign({}, previous, omit(current, 'reducers')), {}),
getPluginDescriptor: (state, plugins, pluginsConfig, pluginDef, loadedPlugins = {}) => {
getPluginDescriptor: (plugins, pluginsConfig, pluginDef, loadedPlugins = {}) => {
const name = isObject(pluginDef) ? pluginDef.name : pluginDef;
const id = isObject(pluginDef) ? pluginDef.id : null;
const stateSelector = isObject(pluginDef) ? pluginDef.stateSelector : id || undefined;
Expand All @@ -120,8 +120,8 @@ const PluginsUtils = {
id: id || name,
name,
impl: includeLoaded(name, loadedPlugins, (impl.loadPlugin || impl.displayName) ? impl : impl(stateSelector)),
cfg: isObject(pluginDef) ? parsePluginConfig(state, plugins.requires, pluginDef.cfg) : {},
items: getPluginItems(state, plugins, pluginsConfig, name, id, isDefault, loadedPlugins)
cfg: isObject(pluginDef) ? parsePluginConfig(plugins.requires, pluginDef.cfg) : {},
items: getPluginItems(plugins, pluginsConfig, name, id, isDefault, loadedPlugins)
};
},
getMorePrioritizedContainer
Expand Down

0 comments on commit 2275490

Please sign in to comment.