Remove CoffeeScript class wrapper
This commit is contained in:
parent
7274c69bc6
commit
ca57f8a391
1 changed files with 78 additions and 81 deletions
|
@ -31,10 +31,7 @@ const methodInApp = {
|
||||||
quit: true
|
quit: true
|
||||||
}
|
}
|
||||||
|
|
||||||
const MenuItem = (function () {
|
const MenuItem = function (options) {
|
||||||
MenuItem.types = ['normal', 'separator', 'submenu', 'checkbox', 'radio']
|
|
||||||
|
|
||||||
function MenuItem (options) {
|
|
||||||
const {app, Menu} = require('electron')
|
const {app, Menu} = require('electron')
|
||||||
|
|
||||||
const click = options.click
|
const click = options.click
|
||||||
|
@ -48,37 +45,43 @@ const MenuItem = (function () {
|
||||||
this.enabled = options.enabled
|
this.enabled = options.enabled
|
||||||
this.visible = options.visible
|
this.visible = options.visible
|
||||||
this.checked = options.checked
|
this.checked = options.checked
|
||||||
|
|
||||||
this.submenu = options.submenu
|
this.submenu = options.submenu
|
||||||
if ((this.submenu != null) && this.submenu.constructor !== Menu) {
|
if (this.submenu != null && this.submenu.constructor !== Menu) {
|
||||||
this.submenu = Menu.buildFromTemplate(this.submenu)
|
this.submenu = Menu.buildFromTemplate(this.submenu)
|
||||||
}
|
}
|
||||||
if ((this.type == null) && (this.submenu != null)) {
|
if (this.type == null && this.submenu != null) {
|
||||||
this.type = 'submenu'
|
this.type = 'submenu'
|
||||||
}
|
}
|
||||||
if (this.type === 'submenu' && (this.submenu != null ? this.submenu.constructor : void 0) !== Menu) {
|
if (this.type === 'submenu' && (this.submenu == null || this.submenu.constructor !== Menu)) {
|
||||||
throw new Error('Invalid submenu')
|
throw new Error('Invalid submenu')
|
||||||
}
|
}
|
||||||
|
|
||||||
this.overrideReadOnlyProperty('type', 'normal')
|
this.overrideReadOnlyProperty('type', 'normal')
|
||||||
this.overrideReadOnlyProperty('role')
|
this.overrideReadOnlyProperty('role')
|
||||||
this.overrideReadOnlyProperty('accelerator')
|
this.overrideReadOnlyProperty('accelerator')
|
||||||
this.overrideReadOnlyProperty('icon')
|
this.overrideReadOnlyProperty('icon')
|
||||||
this.overrideReadOnlyProperty('submenu')
|
this.overrideReadOnlyProperty('submenu')
|
||||||
|
|
||||||
this.overrideProperty('label', '')
|
this.overrideProperty('label', '')
|
||||||
this.overrideProperty('sublabel', '')
|
this.overrideProperty('sublabel', '')
|
||||||
this.overrideProperty('enabled', true)
|
this.overrideProperty('enabled', true)
|
||||||
this.overrideProperty('visible', true)
|
this.overrideProperty('visible', true)
|
||||||
this.overrideProperty('checked', false)
|
this.overrideProperty('checked', false)
|
||||||
if (MenuItem.types.indexOf(this.type) === -1) {
|
|
||||||
throw new Error('Unknown menu type ' + this.type)
|
if (!MenuItem.types.includes(this.type)) {
|
||||||
|
throw new Error(`Unknown menu type ${this.type}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
this.commandId = ++nextCommandId
|
this.commandId = ++nextCommandId
|
||||||
this.click = (event, focusedWindow) => {
|
|
||||||
|
this.click = (focusedWindow) => {
|
||||||
// Manually flip the checked flags when clicked.
|
// Manually flip the checked flags when clicked.
|
||||||
if (this.type === 'checkbox' || this.type === 'radio') {
|
if (this.type === 'checkbox' || this.type === 'radio') {
|
||||||
this.checked = !this.checked
|
this.checked = !this.checked
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.role && rolesMap[this.role] && process.platform !== 'darwin' && (focusedWindow != null)) {
|
if (this.role && rolesMap[this.role] && process.platform !== 'darwin' && focusedWindow != null) {
|
||||||
const methodName = rolesMap[this.role]
|
const methodName = rolesMap[this.role]
|
||||||
if (methodInApp[methodName]) {
|
if (methodInApp[methodName]) {
|
||||||
return app[methodName]()
|
return app[methodName]()
|
||||||
|
@ -91,37 +94,31 @@ const MenuItem = (function () {
|
||||||
return webContents != null ? webContents[methodName]() : void 0
|
return webContents != null ? webContents[methodName]() : void 0
|
||||||
}
|
}
|
||||||
} else if (typeof click === 'function') {
|
} else if (typeof click === 'function') {
|
||||||
return click(this, focusedWindow, event)
|
return click(this, focusedWindow)
|
||||||
} else if (typeof this.selector === 'string' && process.platform === 'darwin') {
|
} else if (typeof this.selector === 'string' && process.platform === 'darwin') {
|
||||||
return Menu.sendActionToFirstResponder(this.selector)
|
return Menu.sendActionToFirstResponder(this.selector)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MenuItem.prototype.overrideProperty = function (name, defaultValue) {
|
MenuItem.types = ['normal', 'separator', 'submenu', 'checkbox', 'radio']
|
||||||
if (defaultValue == null) {
|
|
||||||
defaultValue = null
|
|
||||||
}
|
|
||||||
this[name] != null ? this[name] : this[name] = defaultValue
|
|
||||||
|
|
||||||
return this[name]
|
MenuItem.prototype.overrideProperty = function (name, defaultValue) {
|
||||||
}
|
|
||||||
|
|
||||||
MenuItem.prototype.overrideReadOnlyProperty = function (name, defaultValue) {
|
|
||||||
if (defaultValue == null) {
|
if (defaultValue == null) {
|
||||||
defaultValue = null
|
defaultValue = null
|
||||||
}
|
}
|
||||||
if (this[name] == null) {
|
if (this[name] == null) {
|
||||||
this[name] = defaultValue
|
this[name] = defaultValue
|
||||||
}
|
}
|
||||||
return Object.defineProperty(this, name, {
|
}
|
||||||
|
|
||||||
|
MenuItem.prototype.overrideReadOnlyProperty = function (name, defaultValue) {
|
||||||
|
this.overrideProperty(name, defaultValue)
|
||||||
|
Object.defineProperty(this, name, {
|
||||||
enumerable: true,
|
enumerable: true,
|
||||||
writable: false,
|
writable: false,
|
||||||
value: this[name]
|
value: this[name]
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return MenuItem
|
|
||||||
})()
|
|
||||||
|
|
||||||
module.exports = MenuItem
|
module.exports = MenuItem
|
||||||
|
|
Loading…
Reference in a new issue