diff --git a/lib/browser/api/menu.js b/lib/browser/api/menu.js index 1effc3ff761f..9864e189b26c 100644 --- a/lib/browser/api/menu.js +++ b/lib/browser/api/menu.js @@ -18,19 +18,19 @@ Menu.prototype._init = function () { this.groupsMap = {} this.items = [] this.delegate = { - isCommandIdChecked: id => this.commandsMap[id].checked, - isCommandIdEnabled: id => this.commandsMap[id].enabled, - isCommandIdVisible: id => this.commandsMap[id].visible, + isCommandIdChecked: id => this.commandsMap[id] ? this.commandsMap[id].checked : undefined, + isCommandIdEnabled: id => this.commandsMap[id] ? this.commandsMap[id].enabled : undefined, + isCommandIdVisible: id => this.commandsMap[id] ? this.commandsMap[id].visible : undefined, getAcceleratorForCommandId: (id, useDefaultAccelerator) => { const command = this.commandsMap[id] - if (command === null) return - if (command.accelerator !== null) return command.accelerator + if (!command) return + if (command.accelerator) return command.accelerator if (useDefaultAccelerator) return command.getDefaultRoleAccelerator() }, - getIconForCommandId: id => this.commandsMap[id].icon, + getIconForCommandId: id => this.commandsMap[id] ? this.commandsMap[id].icon : undefined, executeCommand: (event, id) => { const command = this.commandsMap[id] - if (command === null) return + if (!command) return command.click(event, BrowserWindow.getFocusedWindow(), webContents.getFocusedWebContents()) }, menuWillShow: () => { @@ -49,7 +49,7 @@ Menu.prototype.popup = function (window, x, y, positioningItem) { let [newX, newY, newPosition, newWindow] = [x, y, positioningItem, window] // menu.popup(x, y, positioningItem) - if (window !== null) { + if (!window) { // shift over values if (typeof window !== 'object' || window.constructor !== BrowserWindow) { [newPosition, newY, newX, newWindow] = [y, x, window, null] @@ -57,7 +57,7 @@ Menu.prototype.popup = function (window, x, y, positioningItem) { } // menu.popup(window, {}) - if (x !== null && typeof x === 'object') { + if (x && typeof x === 'object') { const opts = x newX = opts.x newY = opts.y @@ -69,7 +69,7 @@ Menu.prototype.popup = function (window, x, y, positioningItem) { if (typeof x !== 'number') newX = -1 if (typeof y !== 'number') newY = -1 if (typeof positioningItem !== 'number') newPosition = -1 - if (window === null) newWindow = BrowserWindow.getFocusedWindow() + if (!window) newWindow = BrowserWindow.getFocusedWindow() if (typeof asyncPopup !== 'boolean') asyncPopup = false this.popupAt(newWindow, newX, newY, newPosition, asyncPopup) @@ -77,12 +77,10 @@ Menu.prototype.popup = function (window, x, y, positioningItem) { // close an existing popup Menu.prototype.closePopup = function (window) { - if (window == null || window.constructor !== BrowserWindow) { + if (!window || window.constructor !== BrowserWindow) { window = BrowserWindow.getFocusedWindow() } - if (window != null) { - this.closePopupAt(window.id) - } + if (window) this.closePopupAt(window.id) } // return a MenuItem with the specified id @@ -105,7 +103,7 @@ Menu.prototype.append = function (item) { // insert a new menu item at a specified location Menu.prototype.insert = function (pos, item) { - if ((item != null ? item.constructor : void 0) !== MenuItem) { + if ((item ? item.constructor : void 0) !== MenuItem) { throw new TypeError('Invalid item') } @@ -113,9 +111,9 @@ Menu.prototype.insert = function (pos, item) { insertItemByType.call(this, item, pos) // set item properties - if (item.sublabel != null) this.setSublabel(pos, item.sublabel) - if (item.icon != null) this.setIcon(pos, item.icon) - if (item.role != null) this.setRole(pos, item.role) + if (item.sublabel) this.setSublabel(pos, item.sublabel) + if (item.icon) this.setIcon(pos, item.icon) + if (item.role) this.setRole(pos, item.role) // Make menu accessable to items. item.overrideReadOnlyProperty('menu', this) @@ -127,9 +125,9 @@ Menu.prototype.insert = function (pos, item) { // Force menuWillShow to be called Menu.prototype._callMenuWillShow = function () { - if (this.delegate != null) this.delegate.menuWillShow() + if (this.delegate) this.delegate.menuWillShow() this.items.forEach(item => { - if (item.submenu != null) item.submenu._callMenuWillShow() + if (item.submenu) item.submenu._callMenuWillShow() }) } @@ -142,13 +140,13 @@ Menu.sendActionToFirstResponder = bindings.sendActionToFirstResponder // set application menu with a preexisting menu Menu.setApplicationMenu = function (menu) { - if (!(menu === null || menu.constructor === Menu)) { + if (!(menu || menu.constructor === Menu)) { throw new TypeError('Invalid menu') } applicationMenu = menu if (process.platform === 'darwin') { - if (menu === null) return + if (!menu) return menu._callMenuWillShow() bindings.setApplicationMenu(menu) } else { @@ -205,7 +203,7 @@ function generateGroupId (items, pos) { // Returns the index of item according to |id|. function indexOfItemById (items, id) { - const foundItem = items.find(item => item.id === id) || null + const foundItem = items.find(item => item.id === id) || -1 return items.indexOf(foundItem) } @@ -218,7 +216,7 @@ function indexToInsertByPosition (items, position) { // warn if query doesn't exist if (idx === -1 && query !== 'endof') { - console.warn("Item with id '" + id + "' is not found") + console.warn(`Item with id ${id} is not found`) return items.length }