provide installation instructions
[extjs.git] / air / src / SystemMenu.js
1 /*\r
2  * Ext JS Library 0.30\r
3  * Copyright(c) 2006-2009, Ext JS, LLC.\r
4  * licensing@extjs.com\r
5  * \r
6  * http://extjs.com/license\r
7  */\r
8 \r
9 /**\r
10  * @class Ext.air.SystemMenu\r
11  * \r
12  * Provides platform independent handling of adding item to the application menu, creating the menu or \r
13  * items as needed. <br/><br/>\r
14  * \r
15  * This class also provides the ability to bind standard Ext.Action instances with NativeMenuItems\r
16  * \r
17  * @singleton\r
18  */\r
19 Ext.air.SystemMenu = function(){\r
20         var menu;\r
21         // windows\r
22         if(air.NativeWindow.supportsMenu && nativeWindow.systemChrome != air.NativeWindowSystemChrome.NONE) {\r
23         menu = new air.NativeMenu();\r
24         nativeWindow.menu = menu;\r
25     }\r
26     \r
27         // mac\r
28     if(air.NativeApplication.supportsMenu) {\r
29                 menu = air.NativeApplication.nativeApplication.menu;\r
30     }\r
31 \r
32     function find(menu, text){\r
33         for(var i = 0, len = menu.items.length; i < len; i++){\r
34             if(menu.items[i]['label'] == text){\r
35                 return menu.items[i];\r
36             }\r
37         }\r
38         return null;\r
39     }\r
40 \r
41     return {\r
42                 /**\r
43                  * Add items to one of the application menus\r
44                  * @param {String} text The application menu to add the actions to (e.g. 'File' or 'Edit').\r
45                  * @param {Array} actions An array of Ext.Action objects or menu item configs\r
46                  * @param {Number} mindex The index of the character in "text" which should be used for \r
47                  * keyboard access\r
48                  * @return air.NativeMenu The raw submenu\r
49                  */\r
50                 add: function(text, actions, mindex){\r
51 \r
52             var item = find(menu, text);\r
53             if(!item){\r
54                 item = menu.addItem(new air.NativeMenuItem(text));\r
55                 item.mnemonicIndex = mindex || 0;\r
56 \r
57                 item.submenu = new air.NativeMenu();\r
58                         }\r
59                         for (var i = 0, len = actions.length; i < len; i++) {\r
60                                 item.submenu.addItem(actions[i] == '-' ? new air.NativeMenuItem("", true) : Ext.air.MenuItem(actions[i]));\r
61                         }\r
62             return item.submenu;\r
63         },\r
64                 \r
65                 /**\r
66                  * Returns the application menu\r
67                  */\r
68                 get : function(){\r
69                         return menu;\r
70                 }\r
71         };      \r
72 }();\r
73 \r
74 // ability to bind native menu items to an Ext.Action\r
75 Ext.air.MenuItem = function(action){\r
76         if(!action.isAction){\r
77                 action = new Ext.Action(action);\r
78         }\r
79         var cfg = action.initialConfig;\r
80         var nativeItem = new air.NativeMenuItem(cfg.itemText || cfg.text);\r
81         \r
82         nativeItem.enabled = !cfg.disabled;\r
83 \r
84     if(!Ext.isEmpty(cfg.checked)){\r
85         nativeItem.checked = cfg.checked;\r
86     }\r
87 \r
88     var handler = cfg.handler;\r
89         var scope = cfg.scope;\r
90         \r
91         nativeItem.addEventListener(air.Event.SELECT, function(){\r
92                 handler.call(scope || window, cfg);\r
93         });\r
94         \r
95         action.addComponent({\r
96                 setDisabled : function(v){\r
97                         nativeItem.enabled = !v;\r
98                 },\r
99                 \r
100                 setText : function(v){\r
101                         nativeItem.label = v;\r
102                 },\r
103                 \r
104                 setVisible : function(v){\r
105                         // could not find way to hide in air so disable?\r
106                         nativeItem.enabled = !v;\r
107                 },\r
108                 \r
109                 setHandler : function(newHandler, newScope){\r
110                         handler = newHandler;\r
111                         scope = newScope;\r
112                 },\r
113                 // empty function\r
114                 on : function(){}\r
115         });\r
116         \r
117         return nativeItem;\r
118 }\r