This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 973
Rework alert/confirm to be tab-modal (instead of application-modal) #7107
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d19ce04
Rework alert/confirm to be tab-modal (instead of application-modal).
bsclifton 70d86f8
Reworked code (huge thanks to @bridiver)
bsclifton f553910
Final touches for tab-modal alert/confirm:
bsclifton 9118cf5
Update MessageBox component to substitute "Brave" for the extension ID
bsclifton 8e67c8a
Fixes per peer review :)
bsclifton b0a8f28
Updated importer to use new tab-modal alert
bsclifton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* 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/. */ | ||
|
||
'use strict' | ||
|
||
const appConstants = require('../../../js/constants/appConstants') | ||
const tabMessageBox = require('../tabMessageBox') | ||
const tabMessageBoxState = require('../../common/state/tabMessageBoxState') | ||
|
||
const tabMessageBoxReducer = (state, action) => { | ||
switch (action.actionType) { | ||
case appConstants.APP_SET_STATE: | ||
state = tabMessageBox.init(state, action) | ||
break | ||
case appConstants.APP_TAB_UPDATED: | ||
state = tabMessageBox.onTabUpdated(state, action) | ||
break | ||
case appConstants.APP_TAB_CLOSED: | ||
state = tabMessageBox.onTabClosed(state, action) | ||
break | ||
case appConstants.APP_TAB_MESSAGE_BOX_SHOWN: | ||
state = tabMessageBoxState.show(state, action) | ||
break | ||
case appConstants.APP_TAB_MESSAGE_BOX_DISMISSED: | ||
state = tabMessageBox.close(state, action) | ||
break | ||
case appConstants.APP_TAB_MESSAGE_BOX_UPDATED: | ||
state = tabMessageBoxState.update(state, action) | ||
break | ||
} | ||
return state | ||
} | ||
|
||
module.exports = tabMessageBoxReducer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
const appActions = require('../../js/actions/appActions') | ||
const tabMessageBoxState = require('../common/state/tabMessageBoxState') | ||
const {makeImmutable} = require('../common/state/immutableUtil') | ||
|
||
// callbacks for alert, confirm, etc. | ||
let messageBoxCallbacks = {} | ||
|
||
const cleanupCallback = (tabId) => { | ||
if (messageBoxCallbacks[tabId]) { | ||
delete messageBoxCallbacks[tabId] | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
const tabMessageBox = { | ||
init: (state, action) => { | ||
process.on('window-alert', (webContents, extraData, title, message, defaultPromptText, | ||
shouldDisplaySuppressCheckbox, isBeforeUnloadDialog, isReload, cb) => { | ||
const tabId = webContents.getId() | ||
const detail = { | ||
message, | ||
title, | ||
buttons: ['ok'], | ||
suppress: false, | ||
showSuppress: shouldDisplaySuppressCheckbox | ||
} | ||
|
||
tabMessageBox.show(tabId, detail, cb) | ||
}) | ||
|
||
process.on('window-confirm', (webContents, extraData, title, message, defaultPromptText, | ||
shouldDisplaySuppressCheckbox, isBeforeUnloadDialog, isReload, cb) => { | ||
const tabId = webContents.getId() | ||
const detail = { | ||
message, | ||
title, | ||
buttons: ['ok', 'cancel'], | ||
cancelId: 1, | ||
suppress: false, | ||
showSuppress: shouldDisplaySuppressCheckbox | ||
} | ||
|
||
tabMessageBox.show(tabId, detail, cb) | ||
}) | ||
|
||
process.on('window-prompt', (webContents, extraData, title, message, defaultPromptText, | ||
shouldDisplaySuppressCheckbox, isBeforeUnloadDialog, isReload, cb) => { | ||
console.warn('window.prompt is not supported yet') | ||
let suppress = false | ||
cb(false, '', suppress) | ||
}) | ||
|
||
return state | ||
}, | ||
|
||
show: (tabId, detail, cb) => { | ||
if (cb) { | ||
messageBoxCallbacks[tabId] = cb | ||
} | ||
setImmediate(() => { | ||
appActions.tabMessageBoxShown(tabId, detail) | ||
}) | ||
}, | ||
|
||
close: (state, action) => { | ||
action = makeImmutable(action) | ||
const tabId = action.get('tabId') | ||
const detail = action.get('detail') | ||
const cb = messageBoxCallbacks[tabId] | ||
let suppress = false | ||
let result = true | ||
state = tabMessageBoxState.removeDetail(state, action) | ||
if (cb) { | ||
cleanupCallback(tabId) | ||
if (detail) { | ||
if (detail.has('suppress')) { | ||
suppress = detail.get('suppress') | ||
} | ||
if (detail.has('result')) { | ||
result = detail.get('result') | ||
} | ||
cb(result, '', suppress) | ||
} else { | ||
cb(false, '', false) | ||
} | ||
} | ||
return state | ||
}, | ||
|
||
onTabClosed: (state, action) => { | ||
action = makeImmutable(action) | ||
const tabId = action.getIn(['tabValue', 'tabId']) | ||
if (tabId) { | ||
// remove callback; call w/ defaults | ||
const cb = messageBoxCallbacks[tabId] | ||
if (cb) { | ||
cleanupCallback(tabId) | ||
cb(false, '', false) | ||
} | ||
} | ||
return state | ||
}, | ||
|
||
onTabUpdated: (state, action) => { | ||
action = makeImmutable(action) | ||
const tabId = action.getIn(['tabValue', 'tabId']) | ||
const detail = tabMessageBoxState.getDetail(state, tabId) | ||
if (detail && detail.get('opener')) { | ||
const url = action.getIn(['tabValue', 'url']) | ||
// check if user has navigated away from site which opened the alert | ||
if (url && url !== detail.get('opener')) { | ||
const removeAction = makeImmutable({tabId: tabId}) | ||
// remove detail from state | ||
state = tabMessageBoxState.removeDetail(state, removeAction) | ||
// remove callback; call w/ defaults | ||
const cb = messageBoxCallbacks[tabId] | ||
if (cb) { | ||
cleanupCallback(tabId) | ||
cb(false, '', false) | ||
} | ||
} | ||
} | ||
return state | ||
}, | ||
|
||
getCallbacks: () => { | ||
return messageBoxCallbacks | ||
} | ||
} | ||
|
||
module.exports = tabMessageBox |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* 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 tabState = require('./tabState') | ||
const {makeImmutable} = require('./immutableUtil') | ||
|
||
const messageBoxDetail = 'messageBoxDetail' | ||
|
||
const tabMessageBoxState = { | ||
show: (state, action) => { | ||
state = makeImmutable(state) | ||
action = makeImmutable(action) | ||
const tabId = action.get('tabId') | ||
let tabValue = tabId && tabState.getByTabId(state, tabId) | ||
|
||
if (!tabValue) { | ||
return state | ||
} | ||
|
||
let detail = action.get('detail') | ||
if (!detail || detail.size === 0) { | ||
tabValue = tabValue.delete(messageBoxDetail) | ||
} else { | ||
detail = detail.set('opener', tabValue.get('url')) | ||
tabValue = tabValue.set(messageBoxDetail, detail) | ||
} | ||
return tabState.updateTab(state, {tabValue, replace: true}) | ||
}, | ||
|
||
getDetail: (state, tabId) => { | ||
if (typeof tabId !== 'number') { | ||
return null | ||
} | ||
|
||
const tabValue = tabState.getByTabId(state, tabId) | ||
if (tabValue) { | ||
return tabValue.get(messageBoxDetail) || null | ||
} | ||
return null | ||
}, | ||
|
||
update: (state, action) => { | ||
return tabMessageBoxState.show(state, action) | ||
}, | ||
|
||
removeDetail: (state, action) => { | ||
state = makeImmutable(state) | ||
action = makeImmutable(action) | ||
const tabId = action.get('tabId') | ||
let tabValue = tabId && tabState.getByTabId(state, tabId) | ||
|
||
if (!tabValue) { | ||
return state | ||
} | ||
|
||
tabValue = tabValue.delete(messageBoxDetail) | ||
return tabState.updateTab(state, {tabValue, replace: true}) | ||
} | ||
} | ||
|
||
module.exports = tabMessageBoxState |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use const