Skip to content

Commit

Permalink
Improves bookmark folder render for context menu
Browse files Browse the repository at this point in the history
Resolves brave#10054

Auditors:

Test Plan:
  • Loading branch information
NejcZdovc committed Aug 4, 2017
1 parent d7c40a2 commit d60717a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 27 deletions.
6 changes: 6 additions & 0 deletions app/common/state/contextMenuState.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* 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 Immutable = require('immutable')
const assert = require('assert')
const { makeImmutable, isMap } = require('./immutableUtil')

Expand Down Expand Up @@ -33,6 +34,11 @@ const api = {
return windowState
},

getContextMenu: (windowState) => {
windowState = validateState(windowState)
return windowState.get('contextMenuDetail', Immutable.Map())
},

selectedIndex: (windowState) => {
const selectedIndex = windowState.getIn(['ui', 'contextMenu', 'selectedIndex'])
return (typeof selectedIndex === 'object' &&
Expand Down
47 changes: 28 additions & 19 deletions app/renderer/components/common/contextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ContextMenuItem extends ImmutableComponent {
return this.props.contextMenuItem.get('items')
}
get hasSubmenu () {
return this.submenu && this.submenu.size > 0
return (this.submenu && this.submenu.size > 0) || this.props.contextMenuItem.has('folderKey')
}
get isMulti () {
return this.items && this.items.size > 0
Expand All @@ -49,6 +49,22 @@ class ContextMenuItem extends ImmutableComponent {
get hasAccelerator () {
return this.accelerator !== null
}

getNewCoordinate (e) {
let node = e.target
while (node && node.classList && !node.classList.contains('contextMenuItem')) {
node = node.parentNode
}
let parentNode = node.parentNode
while (parentNode && parentNode.classList && !parentNode.classList.contains('contextMenu')) {
parentNode = parentNode.parentNode
}
const parentBoundingRect = parentNode.getBoundingClientRect()
const boundingRect = node.getBoundingClientRect()

return boundingRect.top - parentBoundingRect.top - 1 + parentNode.scrollTop
}

onClick (clickAction, shouldHide, e) {
e.stopPropagation()
if (clickAction) {
Expand Down Expand Up @@ -93,28 +109,21 @@ class ContextMenuItem extends ImmutableComponent {
}

onMouseEnter (e) {
let openedSubmenuDetails = this.props.contextMenuDetail.get('openedSubmenuDetails')
openedSubmenuDetails = openedSubmenuDetails
? openedSubmenuDetails.splice(this.props.submenuIndex, this.props.contextMenuDetail.get('openedSubmenuDetails').size)
: new Immutable.List()

if (this.hasSubmenu) {
let node = e.target
while (node && node.classList && !node.classList.contains('contextMenuItem')) {
node = node.parentNode
}
let parentNode = node.parentNode
while (parentNode && parentNode.classList && !parentNode.classList.contains('contextMenu')) {
parentNode = parentNode.parentNode
}
const parentBoundingRect = parentNode.getBoundingClientRect()
const boundingRect = node.getBoundingClientRect()
if (this.props.contextMenuItem.has('folderKey')) {
const y = this.getNewCoordinate(e)
windowActions.onShowBookmarkFolderMenu(this.props.contextMenuItem.get('folderKey'), y, null, this.props.submenuIndex)
} else if (this.hasSubmenu) {
let openedSubmenuDetails = this.props.contextMenuDetail.get('openedSubmenuDetails')
openedSubmenuDetails = openedSubmenuDetails
? openedSubmenuDetails.splice(this.props.submenuIndex, this.props.contextMenuDetail.get('openedSubmenuDetails').size)
: new Immutable.List()
const y = this.getNewCoordinate(e)
openedSubmenuDetails = openedSubmenuDetails.push(Immutable.fromJS({
y: boundingRect.top - parentBoundingRect.top - 1 + parentNode.scrollTop,
y: y,
template: this.submenu
}))
windowActions.setContextMenuDetail(this.props.contextMenuDetail.set('openedSubmenuDetails', openedSubmenuDetails))
}
windowActions.setContextMenuDetail(this.props.contextMenuDetail.set('openedSubmenuDetails', openedSubmenuDetails))
}
getLabelForItem (item) {
const label = item.get('label')
Expand Down
25 changes: 19 additions & 6 deletions app/renderer/reducers/contextMenuReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ const bookmarkItemsInit = (state, items) => {
}
}
if (isFolder) {
templateItem.submenu = showBookmarkFolderInit(state, siteKey)
templateItem.folderKey = siteKey
}
template.push(templateItem)
}
Expand Down Expand Up @@ -509,11 +509,24 @@ const onShowBookmarkFolderMenu = (state, action) => {
state = validateState(state)

const menuTemplate = showBookmarkFolderInit(state, action.get('bookmarkKey'))
state = contextMenuState.setContextMenu(state, makeImmutable({
left: action.get('left'),
top: action.get('top'),
template: menuTemplate
}))
if (action.get('submenuIndex') != null) {
let contextMenu = contextMenuState.getContextMenu(state)
let openedSubmenuDetails = contextMenu.get('openedSubmenuDetails', Immutable.List())

openedSubmenuDetails = openedSubmenuDetails
.splice(action.get('submenuIndex'), openedSubmenuDetails.size)
.push(makeImmutable({
y: action.get('left'),
template: menuTemplate
}))
state = contextMenuState.setContextMenu(state, contextMenu.set('openedSubmenuDetails', openedSubmenuDetails))
} else {
state = contextMenuState.setContextMenu(state, makeImmutable({
left: action.get('left'),
top: action.get('top'),
template: menuTemplate
}))
}

return state
}
Expand Down
5 changes: 3 additions & 2 deletions js/actions/windowActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1230,12 +1230,13 @@ const windowActions = {
})
},

onShowBookmarkFolderMenu: function (bookmarkKey, left, top) {
onShowBookmarkFolderMenu: function (bookmarkKey, left, top, submenuIndex) {
dispatch({
actionType: windowConstants.WINDOW_ON_SHOW_BOOKMARK_FOLDER_MENU,
bookmarkKey,
left,
top
top,
submenuIndex
})
},

Expand Down

0 comments on commit d60717a

Please sign in to comment.