From 899926e450c2db3556dc8fc8cd61311a170353c2 Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Fri, 16 Jun 2017 19:04:21 +0200 Subject: [PATCH] Converts ContextMenu into redux component Resolves #9482 Auditors: @bsclifton @bridiver Test Plan: --- app/renderer/components/common/contextMenu.js | 85 +++++++++++++------ app/renderer/components/main/main.js | 5 +- 2 files changed, 61 insertions(+), 29 deletions(-) diff --git a/app/renderer/components/common/contextMenu.js b/app/renderer/components/common/contextMenu.js index f2af9f4ef50..1f89ecc5f63 100644 --- a/app/renderer/components/common/contextMenu.js +++ b/app/renderer/components/common/contextMenu.js @@ -7,6 +7,7 @@ const Immutable = require('immutable') // Components const ImmutableComponent = require('../immutableComponent') +const ReduxComponent = require('../reduxComponent') // Actions const windowActions = require('../../../../js/actions/windowActions') @@ -16,6 +17,7 @@ const keyCodes = require('../../../common/constants/keyCodes') // Utils const cx = require('../../../../js/lib/classSet') +const frameStateUtil = require('../../../../js/state/frameStateUtil') const {formatAccelerator, wrappingClamp} = require('../../../common/lib/formatUtil') const {separatorMenuItem} = require('../../../common/commonMenu') @@ -267,10 +269,12 @@ class ContextMenuSingle extends ImmutableComponent { /** * Represents a context menu including all submenus */ -class ContextMenu extends ImmutableComponent { - constructor () { - super() +class ContextMenu extends React.Component { + constructor (props) { + super(props) this.onKeyDown = this.onKeyDown.bind(this) + this.onClick = this.onClick.bind(this) + this.onAuxClick = this.onAuxClick.bind(this) } componentDidMount () { @@ -278,7 +282,7 @@ class ContextMenu extends ImmutableComponent { window.addEventListener('keyup', this.onKeyUp) if (this.node) { - this.node.addEventListener('auxclick', this.onAuxClick.bind(this)) + this.node.addEventListener('auxclick', this.onAuxClick) } } @@ -452,41 +456,72 @@ class ContextMenu extends ImmutableComponent { return (this.props.selectedIndex === null) ? false : this.props.selectedIndex.length > 1 } + mergeProps (state, ownProps) { + const currentWindow = state.get('currentWindow') + const activeFrame = frameStateUtil.getActiveFrame(currentWindow) || Immutable.Map() + const selectedIndex = currentWindow.getIn(['ui', 'contextMenu', 'selectedIndex'], null) + const contextMenuDetail = currentWindow.get('contextMenuDetail', Immutable.Map()) + + const props = {} + props.lastZoomPercentage = activeFrame.get('lastZoomPercentage') + props.contextMenuDetail = contextMenuDetail // TODO (nejc) only primitives + props.selectedIndex = typeof selectedIndex === 'object' && + Array.isArray(selectedIndex) && + selectedIndex.length > 0 + ? selectedIndex + : null + props.selectedIndexNorm = selectedIndex === null + ? [0] + : props.selectedIndex + props.selectedIndexChild = props.selectedIndex + ? this.props.selectedIndex[0] + : null + props.left = contextMenuDetail.get('left') + props.right = contextMenuDetail.get('right') + props.top = contextMenuDetail.get('top') + props.bottom = contextMenuDetail.get('bottom') + props.width = contextMenuDetail.get('width') + props.maxHeight = contextMenuDetail.get('maxHeight') + props.template = contextMenuDetail.get('template') + + return props + } + render () { - const selectedIndex = (this.props.selectedIndex === null) ? [0] : this.props.selectedIndex const styles = {} - if (this.props.contextMenuDetail.get('left') !== undefined) { - styles.left = this.props.contextMenuDetail.get('left') + if (this.props.left !== undefined) { + styles.left = this.props.left } - if (this.props.contextMenuDetail.get('right') !== undefined) { - styles.right = this.props.contextMenuDetail.get('right') + if (this.props.right !== undefined) { + styles.right = this.props.right } - if (this.props.contextMenuDetail.get('top') !== undefined) { - styles.marginTop = this.props.contextMenuDetail.get('top') + if (this.props.top !== undefined) { + styles.marginTop = this.props.top } - if (this.props.contextMenuDetail.get('bottom') !== undefined) { - styles.bottom = this.props.contextMenuDetail.get('bottom') + if (this.props.bottom !== undefined) { + styles.bottom = this.props.bottom } - if (this.props.contextMenuDetail.get('width') !== undefined) { - styles.width = this.props.contextMenuDetail.get('width') + if (this.props.width !== undefined) { + styles.width = this.props.width } - if (this.props.contextMenuDetail.get('maxHeight')) { - styles.maxHeight = this.props.contextMenuDetail.get('maxHeight') + if (this.props.maxHeight) { + styles.maxHeight = this.props.maxHeight } + return
{ this.node = node }} className={cx({ contextMenu: true, - reverseExpand: this.props.contextMenuDetail.get('right') !== undefined, - contextMenuScrollable: this.props.contextMenuDetail.get('maxHeight') !== undefined + reverseExpand: this.props.right !== undefined, + contextMenuScrollable: this.props.maxHeight !== undefined })} - onClick={this.onClick.bind(this)} + onClick={this.onClick} style={styles}> + template={this.props.template} + selectedIndex={this.props.selectedIndexChild} /> { this.openedSubmenuDetails.map((openedSubmenuDetail, i) => ) }
} } -module.exports = ContextMenu +module.exports = ReduxComponent.connect(ContextMenu) diff --git a/app/renderer/components/main/main.js b/app/renderer/components/main/main.js index f3720ebd488..b5fde33ee13 100644 --- a/app/renderer/components/main/main.js +++ b/app/renderer/components/main/main.js @@ -696,10 +696,7 @@ class Main extends ImmutableComponent { onClick={this.onClickWindow}> { contextMenuDetail - ? + ? : null } {