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