abstract out switch case from Menu.prototype.insert
This commit is contained in:
parent
7c0f7329d9
commit
b1e707d535
1 changed files with 53 additions and 55 deletions
|
@ -11,6 +11,7 @@ let groupIdIndex = 0
|
||||||
|
|
||||||
Object.setPrototypeOf(Menu.prototype, EventEmitter.prototype)
|
Object.setPrototypeOf(Menu.prototype, EventEmitter.prototype)
|
||||||
|
|
||||||
|
// cleaned up
|
||||||
Menu.prototype._init = function () {
|
Menu.prototype._init = function () {
|
||||||
this.commandsMap = {}
|
this.commandsMap = {}
|
||||||
this.groupsMap = {}
|
this.groupsMap = {}
|
||||||
|
@ -41,6 +42,7 @@ Menu.prototype._init = function () {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// create and show a popup
|
||||||
Menu.prototype.popup = function (window, x, y, positioningItem) {
|
Menu.prototype.popup = function (window, x, y, positioningItem) {
|
||||||
let asyncPopup
|
let asyncPopup
|
||||||
|
|
||||||
|
@ -78,6 +80,7 @@ Menu.prototype.popup = function (window, x, y, positioningItem) {
|
||||||
this.popupAt(window, x, y, positioningItem, asyncPopup)
|
this.popupAt(window, x, y, positioningItem, asyncPopup)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// close an existing popup
|
||||||
Menu.prototype.closePopup = function (window) {
|
Menu.prototype.closePopup = function (window) {
|
||||||
if (window == null || window.constructor !== BrowserWindow) {
|
if (window == null || window.constructor !== BrowserWindow) {
|
||||||
window = BrowserWindow.getFocusedWindow()
|
window = BrowserWindow.getFocusedWindow()
|
||||||
|
@ -87,11 +90,12 @@ Menu.prototype.closePopup = function (window) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// return a MenuItem with the specified id
|
||||||
Menu.prototype.getMenuItemById = function (id) {
|
Menu.prototype.getMenuItemById = function (id) {
|
||||||
const items = this.items
|
const items = this.items
|
||||||
|
|
||||||
let found = items.find(item => item.id === id) || null
|
let found = items.find(item => item.id === id) || null
|
||||||
for (let i = 0, length = items.length; !found && i < length; i++) {
|
for (let i = 0; !found && i < items.length; i++) {
|
||||||
if (items[i].submenu) {
|
if (items[i].submenu) {
|
||||||
found = items[i].submenu.getMenuItemById(id)
|
found = items[i].submenu.getMenuItemById(id)
|
||||||
}
|
}
|
||||||
|
@ -99,58 +103,21 @@ Menu.prototype.getMenuItemById = function (id) {
|
||||||
return found
|
return found
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// append a new MenuItem to an existing Menu
|
||||||
Menu.prototype.append = function (item) {
|
Menu.prototype.append = function (item) {
|
||||||
return this.insert(this.getItemCount(), item)
|
return this.insert(this.getItemCount(), item)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// insert a new menu item at a specified location
|
||||||
Menu.prototype.insert = function (pos, item) {
|
Menu.prototype.insert = function (pos, item) {
|
||||||
var base, name
|
|
||||||
if ((item != null ? item.constructor : void 0) !== MenuItem) {
|
if ((item != null ? item.constructor : void 0) !== MenuItem) {
|
||||||
throw new TypeError('Invalid item')
|
throw new TypeError('Invalid item')
|
||||||
}
|
}
|
||||||
switch (item.type) {
|
|
||||||
case 'normal':
|
|
||||||
this.insertItem(pos, item.commandId, item.label)
|
|
||||||
break
|
|
||||||
case 'checkbox':
|
|
||||||
this.insertCheckItem(pos, item.commandId, item.label)
|
|
||||||
break
|
|
||||||
case 'separator':
|
|
||||||
this.insertSeparator(pos)
|
|
||||||
break
|
|
||||||
case 'submenu':
|
|
||||||
this.insertSubMenu(pos, item.commandId, item.label, item.submenu)
|
|
||||||
break
|
|
||||||
case 'radio':
|
|
||||||
// Grouping radio menu items.
|
|
||||||
item.overrideReadOnlyProperty('groupId', generateGroupId(this.items, pos))
|
|
||||||
if ((base = this.groupsMap)[name = item.groupId] == null) {
|
|
||||||
base[name] = []
|
|
||||||
}
|
|
||||||
this.groupsMap[item.groupId].push(item)
|
|
||||||
|
|
||||||
// Setting a radio menu item should flip other items in the group.
|
// insert item depending on its type
|
||||||
v8Util.setHiddenValue(item, 'checked', item.checked)
|
insertItemByType.call(this, item, pos)
|
||||||
Object.defineProperty(item, 'checked', {
|
|
||||||
enumerable: true,
|
|
||||||
get: function () {
|
|
||||||
return v8Util.getHiddenValue(item, 'checked')
|
|
||||||
},
|
|
||||||
set: () => {
|
|
||||||
var j, len, otherItem, ref1
|
|
||||||
ref1 = this.groupsMap[item.groupId]
|
|
||||||
for (j = 0, len = ref1.length; j < len; j++) {
|
|
||||||
otherItem = ref1[j]
|
|
||||||
if (otherItem !== item) {
|
|
||||||
v8Util.setHiddenValue(otherItem, 'checked', false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return v8Util.setHiddenValue(item, 'checked', true)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
this.insertRadioItem(pos, item.commandId, item.label, item.groupId)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// set item properties
|
||||||
if (item.sublabel != null) this.setSublabel(pos, item.sublabel)
|
if (item.sublabel != null) this.setSublabel(pos, item.sublabel)
|
||||||
if (item.icon != null) this.setIcon(pos, item.icon)
|
if (item.icon != null) this.setIcon(pos, item.icon)
|
||||||
if (item.role != null) this.setRole(pos, item.role)
|
if (item.role != null) this.setRole(pos, item.role)
|
||||||
|
@ -164,7 +131,6 @@ Menu.prototype.insert = function (pos, item) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Force menuWillShow to be called
|
// Force menuWillShow to be called
|
||||||
// cleaned up
|
|
||||||
Menu.prototype._callMenuWillShow = function () {
|
Menu.prototype._callMenuWillShow = function () {
|
||||||
if (this.delegate != null) this.delegate.menuWillShow()
|
if (this.delegate != null) this.delegate.menuWillShow()
|
||||||
this.items.forEach(item => {
|
this.items.forEach(item => {
|
||||||
|
@ -172,7 +138,12 @@ Menu.prototype._callMenuWillShow = function () {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// cleaned up
|
// return application menu
|
||||||
|
Menu.getApplicationMenu = () => applicationMenu
|
||||||
|
|
||||||
|
Menu.sendActionToFirstResponder = bindings.sendActionToFirstResponder
|
||||||
|
|
||||||
|
// set application menu with a preexisting menu
|
||||||
Menu.setApplicationMenu = function (menu) {
|
Menu.setApplicationMenu = function (menu) {
|
||||||
if (!(menu === null || menu.constructor === Menu)) {
|
if (!(menu === null || menu.constructor === Menu)) {
|
||||||
throw new TypeError('Invalid menu')
|
throw new TypeError('Invalid menu')
|
||||||
|
@ -189,12 +160,7 @@ Menu.setApplicationMenu = function (menu) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// cleaned up
|
// build a menu by passing in a template
|
||||||
Menu.getApplicationMenu = () => applicationMenu
|
|
||||||
|
|
||||||
Menu.sendActionToFirstResponder = bindings.sendActionToFirstResponder
|
|
||||||
|
|
||||||
// cleaned up
|
|
||||||
Menu.buildFromTemplate = function (template) {
|
Menu.buildFromTemplate = function (template) {
|
||||||
if (!(template instanceof Array)) {
|
if (!(template instanceof Array)) {
|
||||||
throw new TypeError('Invalid template for Menu')
|
throw new TypeError('Invalid template for Menu')
|
||||||
|
@ -223,7 +189,7 @@ Menu.buildFromTemplate = function (template) {
|
||||||
|
|
||||||
/* Helper Methods */
|
/* Helper Methods */
|
||||||
|
|
||||||
// Search between separators to find a radio menu item and return its group id,
|
// Search between separators to find a radio menu item and return its group id
|
||||||
function generateGroupId (items, pos) {
|
function generateGroupId (items, pos) {
|
||||||
let i, item
|
let i, item
|
||||||
if (pos > 0) {
|
if (pos > 0) {
|
||||||
|
@ -241,18 +207,17 @@ function generateGroupId (items, pos) {
|
||||||
if (item.type === 'separator') { break }
|
if (item.type === 'separator') { break }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ++groupIdIndex
|
groupIdIndex += 1
|
||||||
|
return groupIdIndex
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the index of item according to |id|.
|
// Returns the index of item according to |id|.
|
||||||
// cleaned up
|
|
||||||
function indexOfItemById (items, id) {
|
function indexOfItemById (items, id) {
|
||||||
const foundItem = items.find(item => item.id === id) || null
|
const foundItem = items.find(item => item.id === id) || null
|
||||||
return items.indexOf(foundItem)
|
return items.indexOf(foundItem)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the index of where to insert the item according to |position|
|
// Returns the index of where to insert the item according to |position|
|
||||||
// cleaned up
|
|
||||||
function indexToInsertByPosition (items, position) {
|
function indexToInsertByPosition (items, position) {
|
||||||
if (!position) return items.length
|
if (!position) return items.length
|
||||||
|
|
||||||
|
@ -287,4 +252,37 @@ function indexToInsertByPosition (items, position) {
|
||||||
return (query in queries) ? queries[query](idx) : idx
|
return (query in queries) ? queries[query](idx) : idx
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// insert a new MenuItem depending on its type
|
||||||
|
function insertItemByType(item, pos) {
|
||||||
|
const types = {
|
||||||
|
'normal': () => this.insertItem(pos, item.commandId, item.label),
|
||||||
|
'checkbox': () => this.insertCheckItem(pos, item.commandId, item.label),
|
||||||
|
'separator':() => this.insertSeparator(pos),
|
||||||
|
'submenu': () => this.insertSubMenu(pos, item.commandId, item.label, item.submenu),
|
||||||
|
'radio': () => {
|
||||||
|
// Grouping radio menu items
|
||||||
|
item.overrideReadOnlyProperty('groupId', generateGroupId(this.items, pos))
|
||||||
|
if (this.groupsMap[item.groupId] == null) {
|
||||||
|
this.groupsMap[item.groupId] = []
|
||||||
|
}
|
||||||
|
this.groupsMap[item.groupId].push(item)
|
||||||
|
|
||||||
|
// Setting a radio menu item should flip other items in the group.
|
||||||
|
v8Util.setHiddenValue(item, 'checked', item.checked)
|
||||||
|
Object.defineProperty(item, 'checked', {
|
||||||
|
enumerable: true,
|
||||||
|
get: () => v8Util.getHiddenValue(item, 'checked'),
|
||||||
|
set: () => {
|
||||||
|
this.groupsMap[item.groupId].forEach(other => {
|
||||||
|
if (other !== item) v8Util.setHiddenValue(other, 'checked', false)
|
||||||
|
})
|
||||||
|
v8Util.setHiddenValue(item, 'checked', true)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
this.insertRadioItem(pos, item.commandId, item.label, item.groupId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
types[item.type]()
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = Menu
|
module.exports = Menu
|
||||||
|
|
Loading…
Reference in a new issue