| 
									
										
										
										
											2013-05-30 20:16:54 +08:00
										 |  |  | BrowserWindow = require 'browser-window' | 
					
						
							| 
									
										
										
										
											2013-08-14 12:03:37 +08:00
										 |  |  | EventEmitter = require('events').EventEmitter | 
					
						
							|  |  |  | IDWeakMap = require 'id-weak-map' | 
					
						
							| 
									
										
										
										
											2013-05-30 20:16:54 +08:00
										 |  |  | MenuItem = require 'menu-item' | 
					
						
							| 
									
										
										
										
											2013-05-14 19:24:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-16 10:54:37 +08:00
										 |  |  | bindings = process.atomBinding 'menu' | 
					
						
							| 
									
										
										
										
											2013-05-16 19:34:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-16 10:54:37 +08:00
										 |  |  | Menu = bindings.Menu | 
					
						
							| 
									
										
										
										
											2013-05-14 19:24:52 +08:00
										 |  |  | Menu::__proto__ = EventEmitter.prototype | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | popup = Menu::popup | 
					
						
							|  |  |  | Menu::popup = (window) -> | 
					
						
							| 
									
										
										
										
											2013-05-15 19:19:35 +08:00
										 |  |  |   throw new TypeError('Invalid window') unless window?.constructor is BrowserWindow | 
					
						
							| 
									
										
										
										
											2013-05-14 19:24:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   popup.call this, window | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-16 20:06:25 +08:00
										 |  |  | Menu::append = (item) -> | 
					
						
							|  |  |  |   @insert @getItemCount(), item | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-16 19:34:23 +08:00
										 |  |  | Menu::insert = (pos, item) -> | 
					
						
							|  |  |  |   throw new TypeError('Invalid item') unless item?.constructor is MenuItem | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   switch item.type | 
					
						
							|  |  |  |     when 'normal' then @insertItem pos, item.commandId, item.label | 
					
						
							|  |  |  |     when 'checkbox' then @insertCheckItem pos, item.commandId, item.label | 
					
						
							|  |  |  |     when 'radio' then @insertRadioItem pos, item.commandId, item.label, item.groupId | 
					
						
							|  |  |  |     when 'separator' then @insertSeparator pos | 
					
						
							|  |  |  |     when 'submenu' then @insertSubMenu pos, item.commandId, item.label, item.submenu | 
					
						
							| 
									
										
										
										
											2013-05-14 22:17:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-16 19:34:23 +08:00
										 |  |  |   @setSublabel pos, item.sublabel if item.sublabel? | 
					
						
							| 
									
										
										
										
											2013-05-14 22:17:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-14 12:03:37 +08:00
										 |  |  |   unless @delegate? | 
					
						
							|  |  |  |     @commandsMap = {} | 
					
						
							|  |  |  |     @items = [] | 
					
						
							| 
									
										
										
										
											2013-05-16 20:06:25 +08:00
										 |  |  |     @delegate =
 | 
					
						
							| 
									
										
										
										
											2013-08-14 12:03:37 +08:00
										 |  |  |       isCommandIdChecked: (commandId) => @commandsMap[commandId]?.checked | 
					
						
							|  |  |  |       isCommandIdEnabled: (commandId) => @commandsMap[commandId]?.enabled | 
					
						
							|  |  |  |       isCommandIdVisible: (commandId) => @commandsMap[commandId]?.visible | 
					
						
							|  |  |  |       getAcceleratorForCommandId: (commandId) => @commandsMap[commandId]?.accelerator | 
					
						
							|  |  |  |       executeCommand: (commandId) => @commandsMap[commandId]?.click() | 
					
						
							|  |  |  |   @items.splice pos, 0, item | 
					
						
							|  |  |  |   @commandsMap[item.commandId] = item | 
					
						
							| 
									
										
										
										
											2013-05-16 19:34:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-16 10:54:37 +08:00
										 |  |  | Menu.setApplicationMenu = (menu) -> | 
					
						
							|  |  |  |   throw new TypeError('Invalid menu') unless menu?.constructor is Menu | 
					
						
							|  |  |  |   bindings.setApplicationMenu menu | 
					
						
							| 
									
										
										
										
											2013-05-16 19:34:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-16 17:25:02 +08:00
										 |  |  | Menu.sendActionToFirstResponder = bindings.sendActionToFirstResponder | 
					
						
							| 
									
										
										
										
											2013-05-16 10:54:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-16 22:43:58 +08:00
										 |  |  | Menu.buildFromTemplate = (template) -> | 
					
						
							|  |  |  |   throw new TypeError('Invalid template for Menu') unless Array.isArray template | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   menu = new Menu | 
					
						
							|  |  |  |   for item in template | 
					
						
							|  |  |  |     throw new TypeError('Invalid template for MenuItem') unless typeof item is 'object' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     item.submenu = Menu.buildFromTemplate item.submenu if item.submenu? | 
					
						
							| 
									
										
										
										
											2013-08-14 11:00:08 +08:00
										 |  |  |     menuItem = new MenuItem(item) | 
					
						
							|  |  |  |     menuItem[key] = value for key, value of item | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     menu.append menuItem | 
					
						
							| 
									
										
										
										
											2013-05-16 22:43:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   menu | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-14 19:24:52 +08:00
										 |  |  | module.exports = Menu |