Call context menu item ::created hooks with the click event

This commit is contained in:
Nathan Sobo 2014-09-29 15:51:15 -06:00
parent c5b395579b
commit 3a567b3c5b
2 changed files with 38 additions and 15 deletions

View File

@ -104,6 +104,24 @@ describe "ContextMenuManager", ->
contextMenu.devMode = true
expect(contextMenu.templateForElement(grandchild)).toEqual [{label: 'A', command: 'a'}, {label: 'B', command: 'b'}]
it "allows items to be associated with `created` hooks which are invoked on template construction with the item and event", ->
createdEvent = null
item = {
label: 'A',
command: 'a',
created: (event) ->
@command = 'b'
createdEvent = event
}
contextMenu.add('.grandchild': [item])
dispatchedEvent = {target: grandchild}
expect(contextMenu.templateForEvent(dispatchedEvent)).toEqual [{label: 'A', command: 'b'}]
expect(item.command).toBe 'a' # doesn't modify original item template
expect(createdEvent).toBe dispatchedEvent
describe "executeBuildHandlers", ->
menuTemplate = [
label: 'label'

View File

@ -19,17 +19,17 @@ SequenceCount = 0
module.exports =
class ContextMenuManager
constructor: ({@resourcePath, @devMode}) ->
@definitions = {'.overlayer': []} # TODO: Remove once color picker package stops touching private data
@activeElement = null
@itemSets = []
@definitions = {'.overlayer': []} # TODO: Remove once color picker package stops touching private data
# @devModeDefinitions['.workspace'] = [
# label: 'Inspect Element'
# command: 'application:inspect'
# executeAtBuild: (e) ->
# @commandOptions = x: e.pageX, y: e.pageY
# ]
@add '.workspace': [{
label: 'Inspect Element'
command: 'application:inspect'
created: (item, event) ->
{pageX, pageY} = event
item.commandOptions = {x: pageX, y: pageY}
}]
atom.keymaps.onDidLoadBundledKeymaps => @loadPlatformItems()
@ -53,12 +53,12 @@ class ContextMenuManager
devMode = arguments[2]?.devMode
return @add(@convertLegacyItems(legacyItems, devMode))
itemsBySelector = _.deepClone(arguments[0])
itemsBySelector = arguments[0]
devMode = arguments[1]?.devMode ? false
addedItemSets = []
for selector, items of itemsBySelector
itemSet = new ContextMenuItemSet(selector, items)
itemSet = new ContextMenuItemSet(selector, items.slice())
addedItemSets.push(itemSet)
@itemSets.push(itemSet)
@ -66,9 +66,12 @@ class ContextMenuManager
for itemSet in addedItemSets
@itemSets.splice(@itemSets.indexOf(itemSet), 1)
templateForElement: (element) ->
templateForElement: (target) ->
@templateForEvent({target})
templateForEvent: (event) ->
template = []
currentTarget = element
currentTarget = event.target
while currentTarget?
matchingItemSets =
@ -79,8 +82,10 @@ class ContextMenuManager
for {items} in matchingItemSets
for item in items
continue if item.devMode and not @devMode
item = _.pick(item, 'type', 'label', 'command')
MenuHelpers.merge(template, item)
item = _.clone(item)
item.created?(event)
templateItem = _.pick(item, 'type', 'label', 'command', 'submenu', 'commandOptions')
MenuHelpers.merge(template, templateItem)
currentTarget = currentTarget.parentElement
@ -107,7 +112,7 @@ class ContextMenuManager
# * `event` A DOM event.
showForEvent: (event) ->
@activeElement = event.target
menuTemplate = @templateForElement(@activeElement)
menuTemplate = @templateForEvent(event)
return unless menuTemplate?.length > 0
# @executeBuildHandlers(event, menuTemplate)