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 e0fadad
Show file tree
Hide file tree
Showing 4 changed files with 58 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 @@ -33,6 +33,12 @@ const api = {
return windowState
},

getContextMenu: (windowState) => {
windowState = validateState(windowState)

return windowState.get('contextMenuDetail')
},

selectedIndex: (windowState) => {
const selectedIndex = windowState.getIn(['ui', 'contextMenu', 'selectedIndex'])
return (typeof selectedIndex === 'object' &&
Expand Down
49 changes: 30 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,24 @@ class ContextMenuItem extends ImmutableComponent {
get hasAccelerator () {
return this.accelerator !== null
}

getNewCoordinates (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 {
y: boundingRect.top - parentBoundingRect.top - 1 + parentNode.scrollTop
}
}

onClick (clickAction, shouldHide, e) {
e.stopPropagation()
if (clickAction) {
Expand Down Expand Up @@ -93,28 +111,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 coordinates = this.getNewCoordinates(e)
windowActions.onShowBookmarkFolderMenu(this.props.contextMenuItem.get('folderKey'), coordinates.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 coordinates = this.getNewCoordinates(e)
openedSubmenuDetails = openedSubmenuDetails.push(Immutable.fromJS({
y: boundingRect.top - parentBoundingRect.top - 1 + parentNode.scrollTop,
y: coordinates.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 e0fadad

Please sign in to comment.