Fix handling of submenus in conversion of legacy context menu format

This commit is contained in:
Nathan Sobo 2014-10-02 10:47:03 -06:00
parent eff70b07d9
commit 6ce5356505
3 changed files with 39 additions and 13 deletions

View File

@ -150,3 +150,25 @@ describe "ContextMenuManager", ->
shouldDisplay = false
expect(contextMenu.templateForEvent(dispatchedEvent)).toEqual []
it "allows items to be specified in the legacy format for now", ->
contextMenu.add '.parent':
'A': 'a'
'Separator 1': '-'
'B':
'C': 'c'
'Separator 2': '-'
'D': 'd'
expect(contextMenu.templateForElement(parent)).toEqual [
{label: 'A', command: 'a'}
{type: 'separator'}
{
label: 'B'
submenu: [
{label: 'C', command: 'c'}
{type: 'separator'}
{label: 'D', command: 'd'}
]
}
]

View File

@ -85,7 +85,7 @@ class ContextMenuManager
for key, value of itemsBySelector
unless _.isArray(value)
Grim.deprecate("The format for declaring context menu items has changed. Please consult the documentation.")
itemsBySelector = @convertLegacyItems(itemsBySelector, devMode)
itemsBySelector = @convertLegacyItemsBySelector(itemsBySelector, devMode)
addedItemSets = []
@ -126,22 +126,27 @@ class ContextMenuManager
template
convertLegacyItems: (legacyItems, devMode) ->
convertLegacyItemsBySelector: (legacyItemsBySelector, devMode) ->
itemsBySelector = {}
for selector, commandsByLabel of legacyItems
itemsBySelector[selector] = items = []
for label, commandOrSubmenu of commandsByLabel
if typeof commandOrSubmenu is 'object'
items.push({label, submenu: @convertLegacyItems(commandOrSubmenu, devMode), devMode})
else if commandOrSubmenu is '-'
items.push({type: 'separator'})
else
items.push({label, command: commandOrSubmenu, devMode})
for selector, commandsByLabel of legacyItemsBySelector
itemsBySelector[selector] = @convertLegacyItems(commandsByLabel, devMode)
itemsBySelector
convertLegacyItems: (legacyItems, devMode) ->
items = []
for label, commandOrSubmenu of legacyItems
if typeof commandOrSubmenu is 'object'
items.push({label, submenu: @convertLegacyItems(commandOrSubmenu, devMode), devMode})
else if commandOrSubmenu is '-'
items.push({type: 'separator'})
else
items.push({label, command: commandOrSubmenu, devMode})
items
# Public: Request a context menu to be displayed.
#
# * `event` A DOM event.

View File

@ -42,7 +42,6 @@ normalizeLabel = (label) ->
else
label.replace(/\&/g, '')
cloneMenuItem = (item) ->
item = _.pick(item, 'type', 'label', 'command', 'submenu', 'commandDetail')
if item.submenu?