electron/lib/browser/api/menu-item.js

113 lines
3.4 KiB
JavaScript
Raw Normal View History

'use strict'
var MenuItem, methodInBrowserWindow, nextCommandId, rolesMap
2016-01-12 02:40:23 +00:00
nextCommandId = 0
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Maps role to methods of webContents
2016-01-12 02:40:23 +00:00
rolesMap = {
undo: 'undo',
redo: 'redo',
cut: 'cut',
copy: 'copy',
paste: 'paste',
selectall: 'selectAll',
minimize: 'minimize',
2016-03-07 23:50:33 +00:00
close: 'close',
delete: 'delete'
}
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Maps methods that should be called directly on the BrowserWindow instance
2016-01-12 02:40:23 +00:00
methodInBrowserWindow = {
minimize: true,
close: true
}
2016-01-12 02:40:23 +00:00
MenuItem = (function () {
MenuItem.types = ['normal', 'separator', 'submenu', 'checkbox', 'radio']
2016-01-12 02:40:23 +00:00
function MenuItem (options) {
var click, ref
const Menu = require('electron').Menu
2016-03-29 01:00:30 +00:00
click = options.click
this.selector = options.selector
this.type = options.type
this.role = options.role
this.label = options.label
this.sublabel = options.sublabel
this.accelerator = options.accelerator
this.icon = options.icon
this.enabled = options.enabled
this.visible = options.visible
this.checked = options.checked
this.submenu = options.submenu
2016-01-12 02:40:23 +00:00
if ((this.submenu != null) && this.submenu.constructor !== Menu) {
this.submenu = Menu.buildFromTemplate(this.submenu)
2016-01-12 02:40:23 +00:00
}
if ((this.type == null) && (this.submenu != null)) {
this.type = 'submenu'
2016-01-12 02:40:23 +00:00
}
if (this.type === 'submenu' && ((ref = this.submenu) != null ? ref.constructor : void 0) !== Menu) {
throw new Error('Invalid submenu')
2016-01-12 02:40:23 +00:00
}
this.overrideReadOnlyProperty('type', 'normal')
this.overrideReadOnlyProperty('role')
this.overrideReadOnlyProperty('accelerator')
this.overrideReadOnlyProperty('icon')
this.overrideReadOnlyProperty('submenu')
this.overrideProperty('label', '')
this.overrideProperty('sublabel', '')
this.overrideProperty('enabled', true)
this.overrideProperty('visible', true)
this.overrideProperty('checked', false)
2016-01-12 02:40:23 +00:00
if (MenuItem.types.indexOf(this.type) === -1) {
throw new Error('Unknown menu type ' + this.type)
2016-01-12 02:40:23 +00:00
}
this.commandId = ++nextCommandId
this.click = (focusedWindow) => {
// Manually flip the checked flags when clicked.
var methodName, ref1, ref2
if ((ref1 = this.type) === 'checkbox' || ref1 === 'radio') {
this.checked = !this.checked
}
if (this.role && rolesMap[this.role] && process.platform !== 'darwin' && (focusedWindow != null)) {
methodName = rolesMap[this.role]
if (methodInBrowserWindow[methodName]) {
return focusedWindow[methodName]()
} else {
return (ref2 = focusedWindow.webContents) != null ? ref2[methodName]() : void 0
2016-01-12 02:40:23 +00:00
}
} else if (typeof click === 'function') {
return click(this, focusedWindow)
} else if (typeof this.selector === 'string' && process.platform === 'darwin') {
return Menu.sendActionToFirstResponder(this.selector)
}
}
2016-01-12 02:40:23 +00:00
}
MenuItem.prototype.overrideProperty = function (name, defaultValue) {
2016-01-12 02:40:23 +00:00
if (defaultValue == null) {
defaultValue = null
2016-01-12 02:40:23 +00:00
}
return this[name] != null ? this[name] : this[name] = defaultValue
}
2016-01-12 02:40:23 +00:00
MenuItem.prototype.overrideReadOnlyProperty = function (name, defaultValue) {
2016-01-12 02:40:23 +00:00
if (defaultValue == null) {
defaultValue = null
2016-01-12 02:40:23 +00:00
}
if (this[name] == null) {
this[name] = defaultValue
2016-01-12 02:40:23 +00:00
}
return Object.defineProperty(this, name, {
enumerable: true,
writable: false,
value: this[name]
})
}
2016-01-12 02:40:23 +00:00
return MenuItem
})()
2016-01-12 02:40:23 +00:00
module.exports = MenuItem