Remove CoffeeScript class wrapper
authorKevin Sawicki <kevinsawicki@gmail.com>
Tue, 21 Jun 2016 22:41:37 +0000 (15:41 -0700)
committerKevin Sawicki <kevinsawicki@gmail.com>
Wed, 22 Jun 2016 16:21:46 +0000 (09:21 -0700)
lib/browser/api/menu-item.js

index b0100b7..25d52f8 100644 (file)
@@ -31,97 +31,94 @@ const methodInApp = {
   quit: true
 }
 
-const MenuItem = (function () {
-  MenuItem.types = ['normal', 'separator', 'submenu', 'checkbox', 'radio']
-
-  function MenuItem (options) {
-    const {app, Menu} = require('electron')
-
-    const 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
-    if ((this.submenu != null) && this.submenu.constructor !== Menu) {
-      this.submenu = Menu.buildFromTemplate(this.submenu)
-    }
-    if ((this.type == null) && (this.submenu != null)) {
-      this.type = 'submenu'
-    }
-    if (this.type === 'submenu' && (this.submenu != null ? this.submenu.constructor : void 0) !== Menu) {
-      throw new Error('Invalid submenu')
-    }
-    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)
-    if (MenuItem.types.indexOf(this.type) === -1) {
-      throw new Error('Unknown menu type ' + this.type)
-    }
-    this.commandId = ++nextCommandId
-    this.click = (event, focusedWindow) => {
-      // Manually flip the checked flags when clicked.
-      if (this.type === 'checkbox' || this.type === 'radio') {
-        this.checked = !this.checked
-      }
+const MenuItem = function (options) {
+  const {app, Menu} = require('electron')
 
-      if (this.role && rolesMap[this.role] && process.platform !== 'darwin' && (focusedWindow != null)) {
-        const methodName = rolesMap[this.role]
-        if (methodInApp[methodName]) {
-          return app[methodName]()
-        } else if (typeof methodInBrowserWindow[methodName] === 'function') {
-          return methodInBrowserWindow[methodName](focusedWindow)
-        } else if (methodInBrowserWindow[methodName]) {
-          return focusedWindow[methodName]()
-        } else {
-          const {webContents} = focusedWindow
-          return webContents != null ? webContents[methodName]() : void 0
-        }
-      } else if (typeof click === 'function') {
-        return click(this, focusedWindow, event)
-      } else if (typeof this.selector === 'string' && process.platform === 'darwin') {
-        return Menu.sendActionToFirstResponder(this.selector)
-      }
-    }
+  const 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
+  if (this.submenu != null && this.submenu.constructor !== Menu) {
+    this.submenu = Menu.buildFromTemplate(this.submenu)
+  }
+  if (this.type == null && this.submenu != null) {
+    this.type = 'submenu'
+  }
+  if (this.type === 'submenu' && (this.submenu == null || this.submenu.constructor !== Menu)) {
+    throw new Error('Invalid submenu')
   }
 
-  MenuItem.prototype.overrideProperty = function (name, defaultValue) {
-    if (defaultValue == null) {
-      defaultValue = null
-    }
-    this[name] != null ? this[name] : this[name] = defaultValue
+  this.overrideReadOnlyProperty('type', 'normal')
+  this.overrideReadOnlyProperty('role')
+  this.overrideReadOnlyProperty('accelerator')
+  this.overrideReadOnlyProperty('icon')
+  this.overrideReadOnlyProperty('submenu')
 
-    return this[name]
+  this.overrideProperty('label', '')
+  this.overrideProperty('sublabel', '')
+  this.overrideProperty('enabled', true)
+  this.overrideProperty('visible', true)
+  this.overrideProperty('checked', false)
+
+  if (!MenuItem.types.includes(this.type)) {
+    throw new Error(`Unknown menu type ${this.type}`)
   }
 
-  MenuItem.prototype.overrideReadOnlyProperty = function (name, defaultValue) {
-    if (defaultValue == null) {
-      defaultValue = null
+  this.commandId = ++nextCommandId
+
+  this.click = (focusedWindow) => {
+    // Manually flip the checked flags when clicked.
+    if (this.type === 'checkbox' || this.type === 'radio') {
+      this.checked = !this.checked
     }
-    if (this[name] == null) {
-      this[name] = defaultValue
+
+    if (this.role && rolesMap[this.role] && process.platform !== 'darwin' && focusedWindow != null) {
+      const methodName = rolesMap[this.role]
+      if (methodInApp[methodName]) {
+        return app[methodName]()
+      } else if (typeof methodInBrowserWindow[methodName] === 'function') {
+        return methodInBrowserWindow[methodName](focusedWindow)
+      } else if (methodInBrowserWindow[methodName]) {
+        return focusedWindow[methodName]()
+      } else {
+        const {webContents} = focusedWindow
+        return webContents != null ? webContents[methodName]() : void 0
+      }
+    } else if (typeof click === 'function') {
+      return click(this, focusedWindow)
+    } else if (typeof this.selector === 'string' && process.platform === 'darwin') {
+      return Menu.sendActionToFirstResponder(this.selector)
     }
-    return Object.defineProperty(this, name, {
-      enumerable: true,
-      writable: false,
-      value: this[name]
-    })
   }
+}
+
+MenuItem.types = ['normal', 'separator', 'submenu', 'checkbox', 'radio']
 
-  return MenuItem
-})()
+MenuItem.prototype.overrideProperty = function (name, defaultValue) {
+  if (defaultValue == null) {
+    defaultValue = null
+  }
+  if (this[name] == null) {
+    this[name] = defaultValue
+  }
+}
+
+MenuItem.prototype.overrideReadOnlyProperty = function (name, defaultValue) {
+  this.overrideProperty(name, defaultValue)
+  Object.defineProperty(this, name, {
+    enumerable: true,
+    writable: false,
+    value: this[name]
+  })
+}
 
 module.exports = MenuItem