From 3a567b3c5b790401b10e1964c92b676f314f64fa Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 29 Sep 2014 15:51:15 -0600 Subject: [PATCH] Call context menu item ::created hooks with the click event --- spec/context-menu-manager-spec.coffee | 18 ++++++++++++++ src/context-menu-manager.coffee | 35 +++++++++++++++------------ 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/spec/context-menu-manager-spec.coffee b/spec/context-menu-manager-spec.coffee index e4a342f1e..d26cbe175 100644 --- a/spec/context-menu-manager-spec.coffee +++ b/spec/context-menu-manager-spec.coffee @@ -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' diff --git a/src/context-menu-manager.coffee b/src/context-menu-manager.coffee index 786455afc..cefbeabde 100644 --- a/src/context-menu-manager.coffee +++ b/src/context-menu-manager.coffee @@ -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)