From 1b1998fe6a85b0c26b687e8ec32e371954758a0b Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Tue, 11 Apr 2017 13:49:35 +0200 Subject: [PATCH] Frame -> redux component Resovles #8392 Auditors: @bridiver @bbondy Test plan: - test should be green --- app/common/state/siteSettingsState.js | 15 +- app/common/state/siteState.js | 18 ++ app/common/state/tabMessageBoxState.js | 45 ++- app/renderer/components/common/messageBox.js | 123 ++++---- app/renderer/lib/domUtil.js | 7 + docs/windowActions.md | 10 +- js/actions/windowActions.js | 14 +- js/components/findbar.js | 6 +- js/components/frame.js | 221 +++++++++----- js/components/main.js | 54 +--- js/state/frameStateUtil.js | 15 +- js/stores/windowStore.js | 16 +- .../common/state/tabMessageBoxStateTest.js | 108 ++++++- .../app/renderer/components/messageBoxTest.js | 269 +++--------------- test/unit/js/components/frameTest.js | 75 ++++- 15 files changed, 562 insertions(+), 434 deletions(-) create mode 100644 app/common/state/siteState.js create mode 100644 app/renderer/lib/domUtil.js diff --git a/app/common/state/siteSettingsState.js b/app/common/state/siteSettingsState.js index 410b19dfacc..a391e65854b 100644 --- a/app/common/state/siteSettingsState.js +++ b/app/common/state/siteSettingsState.js @@ -1,6 +1,17 @@ const appConfig = require('../../../js/constants/appConfig') const siteSettings = require('../../../js/state/siteSettings') -module.exports.isNoScriptEnabled = (state, settings) => { - return siteSettings.activeSettings(settings, state, appConfig).noScript === true +const siteSettingsState = { + getAllSiteSettings: (state, frame) => { + if (frame && frame.get('isPrivate')) { + return state.get('siteSettings').mergeDeep(state.get('temporarySiteSettings')) + } + return state.get('siteSettings') + }, + + isNoScriptEnabled (state, settings) { + return siteSettings.activeSettings(settings, state, appConfig).noScript === true + } } + +module.exports = siteSettingsState diff --git a/app/common/state/siteState.js b/app/common/state/siteState.js new file mode 100644 index 00000000000..65d6c476a12 --- /dev/null +++ b/app/common/state/siteState.js @@ -0,0 +1,18 @@ +const assert = require('assert') +const { makeImmutable, isMap, isList } = require('./immutableUtil') + +const validateState = function (state) { + state = makeImmutable(state) + assert.ok(isMap(state), 'state must be an Immutable.Map') + assert.ok(isList(state.get('sites')), 'state must contain an Immutable.List of sites') + return state +} + +const siteState = { + getSites: (state) => { + state = validateState(state) + return state.get('sites') + } +} + +module.exports = siteState diff --git a/app/common/state/tabMessageBoxState.js b/app/common/state/tabMessageBoxState.js index 74a3c79a46e..a0d5b67606e 100644 --- a/app/common/state/tabMessageBoxState.js +++ b/app/common/state/tabMessageBoxState.js @@ -2,11 +2,26 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ +const assert = require('assert') const tabState = require('./tabState') -const {makeImmutable} = require('./immutableUtil') +const {makeImmutable, isList, isMap} = require('./immutableUtil') const messageBoxDetail = 'messageBoxDetail' +const validateState = function (state) { + state = makeImmutable(state) + assert.ok(isMap(state), 'state must be an Immutable.Map') + assert.ok(isList(state.get('tabs')), 'state must contain an Immutable.List of tabs') + return state +} + +const validateId = function (propName, id) { + assert.ok(id, `${propName} cannot be null`) + id = parseInt(id) + assert.ok(id === -1 || id > 0, `${propName} must be positive`) + return id +} + const tabMessageBoxState = { show: (state, action) => { state = makeImmutable(state) @@ -56,6 +71,34 @@ const tabMessageBoxState = { tabValue = tabValue.delete(messageBoxDetail) return tabState.updateTab(state, {tabValue, replace: true}) + }, + + getPropertyByTabId: (state, tabId, property) => { + state = validateState(state) + tabId = validateId('tabId', tabId) + const tab = tabState.getByTabId(state, tabId) + + if (tab) { + return tab.getIn([messageBoxDetail, property]) + } + + return undefined + }, + + getSuppress: (state, tabId) => { + return tabMessageBoxState.getPropertyByTabId(state, tabId, 'suppress') || false + }, + + getShowSuppress: (state, tabId) => { + return tabMessageBoxState.getPropertyByTabId(state, tabId, 'showSuppress') || false + }, + + getTitle: (state, tabId) => { + return tabMessageBoxState.getPropertyByTabId(state, tabId, 'title') || '' + }, + + getButtons: (state, tabId) => { + return tabMessageBoxState.getPropertyByTabId(state, tabId, 'buttons') || makeImmutable(['ok']) } } diff --git a/app/renderer/components/common/messageBox.js b/app/renderer/components/common/messageBox.js index 30389810077..5e38ea8cefe 100644 --- a/app/renderer/components/common/messageBox.js +++ b/app/renderer/components/common/messageBox.js @@ -3,20 +3,34 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ const React = require('react') -const ImmutableComponent = require('../immutableComponent') +const {StyleSheet, css} = require('aphrodite') + +// Components +const ReduxComponent = require('../reduxComponent') const Dialog = require('../../../../js/components/dialog') const Button = require('../../../../js/components/button') const SwitchControl = require('../../../../js/components/switchControl') + +// Actions const appActions = require('../../../../js/actions/appActions') + +// Constants const KeyCodes = require('../../../common/constants/keyCodes') const config = require('../../../../js/constants/config') + +// State +const tabState = require('../../../common/state/tabState') +const tabMessageBoxState = require('../../../common/state/tabMessageBoxState') +const frameStateUtil = require('../../../../js/state/frameStateUtil.js') + +// Utils const {makeImmutable} = require('../../../common/state/immutableUtil') -const {StyleSheet, css} = require('aphrodite') +// Styles const commonStyles = require('../styles/commonStyles') const globalStyles = require('../styles/global') -class MessageBox extends ImmutableComponent { +class MessageBox extends React.Component { constructor () { super() this.onKeyDown = this.onKeyDown.bind(this) @@ -26,105 +40,106 @@ class MessageBox extends ImmutableComponent { componentWillMount () { document.addEventListener('keydown', this.onKeyDown) } + componentWillUnmount () { document.removeEventListener('keydown', this.onKeyDown) } - get tabId () { - return this.props.tabId || '' - } - - get title () { - const msgBoxTitle = (this.props.detail && this.props.detail.get('title')) || '' - return msgBoxTitle.replace(config.braveExtensionId, 'Brave') - } - - get message () { - return (this.props.detail && this.props.detail.get('message')) || '' - } - - get buttons () { - return (this.props.detail && this.props.detail.get('buttons')) || makeImmutable(['ok']) - } - - get cancelId () { - return this.props.detail && this.props.detail.get('cancelId') - } - - get suppress () { - return (this.props.detail && this.props.detail.get('suppress')) || false - } - - get showSuppress () { - return (this.props.detail && this.props.detail.get('showSuppress')) || false - } - onKeyDown (e) { if (this.props.isActive) { switch (e.keyCode) { case KeyCodes.ENTER: - this.onDismiss(this.tabId) + this.onDismiss(this.props.tabId) break case KeyCodes.ESC: - this.onDismiss(this.tabId, this.cancelId) + this.onDismiss(this.props.tabId, this.props.cancelId) break } } } - onSuppressChanged (e) { - const detail = this.props.detail.toJS() - detail.suppress = !detail.suppress - appActions.tabMessageBoxUpdated(this.tabId, detail) + onSuppressChanged () { + const detail = { + buttons: this.props.buttons, + cancelId: this.props.cancelId, + message: this.props.message, + showSuppress: this.props.showSuppress, + suppress: !this.props.suppress, + title: this.props.title + } + + appActions.tabMessageBoxUpdated(this.props.tabId, detail) } onDismiss (tabId, buttonId) { const response = { - suppress: this.suppress, + suppress: this.props.suppress, result: true } - if (typeof this.cancelId === 'number') { - response.result = buttonId !== this.cancelId + if (typeof this.props.cancelId === 'number') { + response.result = buttonId !== this.props.cancelId } - appActions.tabMessageBoxDismissed(this.tabId, response) + appActions.tabMessageBoxDismissed(tabId, response) } get messageBoxButtons () { - const buttons = [] + const buttons = this.props.buttons || makeImmutable(['ok']) + const newButtons = [] - for (let index = (this.buttons.size - 1); index > -1; index--) { - buttons.push(