Use reselect to compute our activeContexts

This commit is contained in:
dannyrb committed 2019-06-17 14:20:32 -04:00
1 parent 88568b1ddd
commit 6050721d33
9 files changed
+255 -100

No files matched your search

-11
View File
@@ -49,17 +49,6 @@ appCommands.init(commandsManager);
if (window.config.hotkeys) {
hotkeysManager.setHotkeys(window.config.hotkeys, true);
}
// Force active contexts for now. These should be set in Viewer/ActiveViewer
store.dispatch({
type: 'ADD_ACTIVE_CONTEXT',
item: 'VIEWER',
});
store.dispatch({
type: 'ADD_ACTIVE_CONTEXT',
item: 'ACTIVE_VIEWPORT::CORNERSTONE',
});
// ~~~~ END APP SETUP
setupTools(store);
+2 -13
View File
@@ -5,22 +5,11 @@ import {
import ToolbarRow from './ToolbarRow';
import { connect } from 'react-redux';
const defaultPlugin = 'cornerstone';
import { getActiveContexts } from './../store/layout/selectors.js';
const mapStateToProps = state => {
const { layout, viewportSpecificData, activeViewportIndex } = state.viewports;
const pluginInLayout =
layout.viewports[activeViewportIndex] &&
layout.viewports[activeViewportIndex].plugin;
const pluginInViewportData =
viewportSpecificData[activeViewportIndex] &&
viewportSpecificData[activeViewportIndex].plugin;
const pluginInActiveViewport =
pluginInLayout || pluginInViewportData || defaultPlugin;
return {
pluginId: pluginInActiveViewport,
activeContexts: getActiveContexts(state),
leftSidebarOpen: state.ui.leftSidebarOpen,
rightSidebarOpen: state.ui.rightSidebarOpen,
};
@@ -5,7 +5,6 @@ import { connect } from 'react-redux';
const {
setViewportSpecificData,
clearViewportSpecificData,
// setActiveViewportSpecificData,
} = OHIF.redux.actions;
const mapStateToProps = state => {
@@ -27,9 +26,6 @@ const mapDispatchToProps = dispatch => {
clearViewportSpecificData: () => {
dispatch(clearViewportSpecificData());
},
// setActiveViewportSpecificData: viewport => {
// dispatch(setActiveViewportSpecificData(viewport));
// },
};
};
+35 -9
View File
@@ -15,7 +15,7 @@ class ToolbarRow extends Component {
rightSidebarOpen: PropTypes.bool.isRequired,
setLeftSidebarOpen: PropTypes.func,
setRightSidebarOpen: PropTypes.func,
pluginId: PropTypes.string,
activeContexts: PropTypes.arrayOf(PropTypes.string).isRequired,
};
static defaultProps = {
@@ -26,20 +26,13 @@ class ToolbarRow extends Component {
constructor(props) {
super(props);
const toolbarModules = extensionManager.modules[MODULE_TYPES.TOOLBAR];
const toolbarButtonDefinitions = toolbarModules[0].module.definitions;
const toolbarButtonDefinitions = _getVisibleToolbarButtons.call(this);
// TODO:
// ToolbarSection for Each ToolbarButton
// Render buttons
// On click, depending on Type, pass props to registered command in commands manager?
// If it's a tool that can be active... Mark it as active?
// - Tools that are on/off?
// - Tools that can be bound to multiple buttons?
// Normal ToolbarButtons...
// Maybe filtered by the active context(s)?
// Buttons can handle their own clicks. No reason for the container to do it?
// Just how high do we need to hoist this state?
// Why ToolbarRow instead of just Toolbar? Do we have any others?
this.state = {
@@ -48,6 +41,17 @@ class ToolbarRow extends Component {
};
}
componentDidUpdate(prevProps) {
const activeContextsChanged =
prevProps.activeContexts !== this.props.activeContexts;
if (activeContextsChanged) {
this.setState({
toolbarButtons: _getVisibleToolbarButtons.call(this),
});
}
}
onLeftSidebarValueChanged = value => {
this.props.setLeftSidebarOpen(!!value);
};
@@ -146,4 +150,26 @@ function _getButtonComponents(toolbarButtons, activeButtons) {
});
}
function _getVisibleToolbarButtons() {
const toolbarModules = extensionManager.modules[MODULE_TYPES.TOOLBAR];
const toolbarButtonDefinitions = [];
toolbarModules.forEach(extension => {
const { definitions, defaultContext } = extension.module;
definitions.forEach(definition => {
const context = definition.context || defaultContext;
console.log(context);
if (this.props.activeContexts.includes(context)) {
console.log('includes: ', definition.commandName);
toolbarButtonDefinitions.push(definition);
}
});
console.log(extension);
});
return toolbarButtonDefinitions;
}
export default ToolbarRow;
-19
View File
@@ -1,18 +1,3 @@
export const addActiveContext = state => ({
type: 'ADD_ACTIVE_CONTEXT',
state,
});
export const removeActiveContext = state => ({
type: 'REMOVE_ACTIVE_CONTEXT',
state,
});
export const clearActiveContexts = state => ({
type: 'CLEAR_ACTIVE_CONTEXTS',
state,
});
export const setLeftSidebarOpen = state => ({
type: 'SET_LEFT_SIDEBAR_OPEN',
state,
@@ -24,10 +9,6 @@ export const setRightSidebarOpen = state => ({
});
const actions = {
addActiveContext,
removeActiveContext,
clearActiveContexts,
//
setLeftSidebarOpen,
setRightSidebarOpen,
};
-17
View File
@@ -3,27 +3,10 @@ const defaultState = {
rightSidebarOpen: false,
labelling: {},
contextMenu: {},
activeContexts: [],
};
const ui = (state = defaultState, action) => {
switch (action.type) {
// ~ ACTIVE CONTEXTS
// https://redux.js.org/recipes/structuring-reducers/immutable-update-patterns#inserting-and-removing-items-in-arrays
case 'ADD_ACTIVE_CONTEXT': {
const shallowCopy = Object.assign({}, state);
shallowCopy.activeContexts = [...shallowCopy.activeContexts, action.item];
return shallowCopy;
}
case 'REMOVE_ACTIVE_CONTEXT': {
const shallowCopy = Object.assign({}, state);
shallowCopy.activeContexts = shallowCopy.activeContexts.filter(
item => item !== action.item
);
return shallowCopy;
}
case 'CLEAR_ACTIVE_CONTEXTS':
return Object.assign({}, state, { activeContexts: [] });
// ~ SIDEBAR
case 'SET_LEFT_SIDEBAR_OPEN':
return Object.assign({}, state, { leftSidebarOpen: action.state });
+28
View File
@@ -0,0 +1,28 @@
import { createSelector } from 'reselect';
const getActiveViewportIndex = state => state.viewports.activeViewportIndex;
const getLayoutViewports = state => state.viewports.layout.viewports;
const getViewportSpecificData = state => state.viewports.viewportSpecificData;
/**
* Think of this as a computed getter for our store. It lets us watch parts of
* our redux state, and only update/recalculate when those values change.
*/
export const getActiveContexts = createSelector(
[getActiveViewportIndex, getLayoutViewports, getViewportSpecificData],
(activeViewportIndex, layoutViewports, viewportSpecificData) => {
const activeContexts = ['VIEWER'];
const activeLayoutViewport = layoutViewports[activeViewportIndex] || {};
const activeViewportSpecificData =
viewportSpecificData[activeViewportIndex] || {};
const activeViewportPluginName =
activeLayoutViewport.plugin || activeViewportSpecificData.plugin;
if (activeViewportPluginName) {
const activeViewportExtension = `ACTIVE_VIEWPORT::${activeViewportPluginName.toUpperCase()}`;
activeContexts.push(activeViewportExtension);
}
return activeContexts;
}
);