Mercurial > libervia-web
comparison src/browser/sat_browser/base_menu.py @ 662:ebb602d8b3f2 frontends_multi_profiles
browser_side: replace all instances of 'str' with 'unicode'
author | souliane <souliane@mailoo.org> |
---|---|
date | Tue, 03 Mar 2015 06:51:13 +0100 |
parents | 6d3142b782c3 |
children | 849ffb24d5bf |
comparison
equal
deleted
inserted
replaced
661:2664fe93ceb3 | 662:ebb602d8b3f2 |
---|---|
34 from pyjamas import Window | 34 from pyjamas import Window |
35 | 35 |
36 import re | 36 import re |
37 | 37 |
38 | 38 |
39 unicode = str # FIXME: pyjamas workaround | |
40 | |
41 | |
39 class MenuCmd(object): | 42 class MenuCmd(object): |
40 """Return an object with an "execute" method that can be set to a menu item callback""" | 43 """Return an object with an "execute" method that can be set to a menu item callback""" |
41 | 44 |
42 def __init__(self, object_, handler=None, data=None): | 45 def __init__(self, object_, handler=None, data=None): |
43 """ | 46 """ |
44 @param object_ (object): a callable or a class instance | 47 @param object_ (object): a callable or a class instance |
45 @param handler (str): method name if object_ is a class instance | 48 @param handler (unicode): method name if object_ is a class instance |
46 @param data (dict): data to pass as the callback argument | 49 @param data (dict): data to pass as the callback argument |
47 """ | 50 """ |
48 if handler is None: | 51 if handler is None: |
49 assert(callable(object_)) | 52 assert(callable(object_)) |
50 self.callback = object_ | 53 self.callback = object_ |
78 want the items of a sub-menu to be displayed in the parent menu. It was | 81 want the items of a sub-menu to be displayed in the parent menu. It was |
79 needed to break the naive relation of "one MenuBar = one category".""" | 82 needed to break the naive relation of "one MenuBar = one category".""" |
80 | 83 |
81 def __init__(self, name=None, item=None, menu=None, flat_level=0): | 84 def __init__(self, name=None, item=None, menu=None, flat_level=0): |
82 """ | 85 """ |
83 @param name (str): node name | 86 @param name (unicode): node name |
84 @param item (MenuItem): associated menu item | 87 @param item (MenuItem): associated menu item |
85 @param menu (GenericMenuBar): associated menu bar | 88 @param menu (GenericMenuBar): associated menu bar |
86 @param flat_level (int): sub-menus until that level see their items | 89 @param flat_level (int): sub-menus until that level see their items |
87 displayed in the parent menu bar, instead of in a callback popup. | 90 displayed in the parent menu bar, instead of in a callback popup. |
88 """ | 91 """ |
94 | 97 |
95 def _getOrCreateCategory(self, path, path_i18n=None, types=None, create=False, sub_menu=None): | 98 def _getOrCreateCategory(self, path, path_i18n=None, types=None, create=False, sub_menu=None): |
96 """Return the requested category. If create is True, path_i18n and | 99 """Return the requested category. If create is True, path_i18n and |
97 types are specified, recursively create the category and its parent. | 100 types are specified, recursively create the category and its parent. |
98 | 101 |
99 @param path (list[str]): path to the category | 102 @param path (list[unicode]): path to the category |
100 @param path_i18n (list[str]): internationalized path to the category | 103 @param path_i18n (list[unicode]): internationalized path to the category |
101 @param types (list[str]): types of the category and its parents | 104 @param types (list[unicode]): types of the category and its parents |
102 @param create (bool): if True, create the category | 105 @param create (bool): if True, create the category |
103 @param sub_menu (GenericMenuBar): instance to popup as the category | 106 @param sub_menu (GenericMenuBar): instance to popup as the category |
104 sub-menu, if it is created. Otherwise keep the previous sub-menu. | 107 sub-menu, if it is created. Otherwise keep the previous sub-menu. |
105 @return: MenuNode or None | 108 @return: MenuNode or None |
106 """ | 109 """ |
120 | 123 |
121 def getCategories(self, target_path=None): | 124 def getCategories(self, target_path=None): |
122 """Return all the categories of the current node, or those of the | 125 """Return all the categories of the current node, or those of the |
123 sub-category which is specified by target_path. | 126 sub-category which is specified by target_path. |
124 | 127 |
125 @param target_path (list[str]): path to the target node | 128 @param target_path (list[unicode]): path to the target node |
126 @return: list[MenuNode] | 129 @return: list[MenuNode] |
127 """ | 130 """ |
128 assert(self.menu) # this method applies to category nodes | 131 assert(self.menu) # this method applies to category nodes |
129 if target_path: | 132 if target_path: |
130 assert(isinstance(target_path, list)) | 133 assert(isinstance(target_path, list)) |
133 return [child for child in self.children if child.menu] | 136 return [child for child in self.children if child.menu] |
134 | 137 |
135 def addMenuItem(self, path, path_i18n, types, callback=None, asHTML=False): | 138 def addMenuItem(self, path, path_i18n, types, callback=None, asHTML=False): |
136 """Recursively add a new node, which could be a category or a leaf node. | 139 """Recursively add a new node, which could be a category or a leaf node. |
137 | 140 |
138 @param path (list[str], str): path to the item | 141 @param path (list[unicode], unicode): path to the item |
139 @param path_i18n (list[str], str): internationalized path to the item | 142 @param path_i18n (list[unicode], unicode): internationalized path to the item |
140 @param types (list[str], str): types of the item and its parents | 143 @param types (list[unicode], unicode): types of the item and its parents |
141 @param callback (MenuCmd, PluginMenuCmd or GenericMenuBar): instance to | 144 @param callback (MenuCmd, PluginMenuCmd or GenericMenuBar): instance to |
142 execute as a leaf's callback or to popup as a category sub-menu | 145 execute as a leaf's callback or to popup as a category sub-menu |
143 @param asHTML (boolean): True to display the UI item as HTML | 146 @param asHTML (boolean): True to display the UI item as HTML |
144 """ | 147 """ |
145 log.info("addMenuItem: %s %s %s %s" % (path, path_i18n, types, callback)) | 148 log.info("addMenuItem: %s %s %s %s" % (path, path_i18n, types, callback)) |
146 | 149 |
147 leaf_node = hasattr(callback, "execute") | 150 leaf_node = hasattr(callback, "execute") |
148 category = isinstance(callback, GenericMenuBar) | 151 category = isinstance(callback, GenericMenuBar) |
149 assert(not leaf_node or not category) | 152 assert(not leaf_node or not category) |
150 | 153 |
151 path = [path] if isinstance(path, str) else path | 154 path = [path] if isinstance(path, unicode) else path |
152 path_i18n = [path_i18n] if isinstance(path_i18n, str) else path_i18n | 155 path_i18n = [path_i18n] if isinstance(path_i18n, unicode) else path_i18n |
153 types = [types for dummy in range(len(path_i18n))] if isinstance(types, str) else types | 156 types = [types for dummy in range(len(path_i18n))] if isinstance(types, unicode) else types |
154 | 157 |
155 if category: | 158 if category: |
156 return self._getOrCreateCategory(path, path_i18n, types, True, callback) | 159 return self._getOrCreateCategory(path, path_i18n, types, True, callback) |
157 | 160 |
158 if len(path) == len(path_i18n) - 1: | 161 if len(path) == len(path_i18n) - 1: |
162 return parent.addItem(path_i18n[-1], asHTML=asHTML, popup=callback) | 165 return parent.addItem(path_i18n[-1], asHTML=asHTML, popup=callback) |
163 | 166 |
164 def addCategory(self, path, path_i18n, types, menu_bar=None): | 167 def addCategory(self, path, path_i18n, types, menu_bar=None): |
165 """Recursively add a new category. | 168 """Recursively add a new category. |
166 | 169 |
167 @param path (list[str], str): path to the category | 170 @param path (list[unicode], unicode): path to the category |
168 @param path_i18n (list[str], str): internationalized path to the category | 171 @param path_i18n (list[unicode], unicode): internationalized path to the category |
169 @param types (list[str], str): types of the category and its parents | 172 @param types (list[unicode], unicode): types of the category and its parents |
170 @param menu_bar (GenericMenuBar): instance to popup as the category sub-menu. | 173 @param menu_bar (GenericMenuBar): instance to popup as the category sub-menu. |
171 """ | 174 """ |
172 if menu_bar: | 175 if menu_bar: |
173 assert(isinstance(menu_bar, GenericMenuBar)) | 176 assert(isinstance(menu_bar, GenericMenuBar)) |
174 else: | 177 else: |
179 """Add a single child to the current node. | 182 """Add a single child to the current node. |
180 | 183 |
181 @param item: see MenuBar.addItem | 184 @param item: see MenuBar.addItem |
182 @param asHTML: see MenuBar.addItem | 185 @param asHTML: see MenuBar.addItem |
183 @param popup: see MenuBar.addItem | 186 @param popup: see MenuBar.addItem |
184 @param name (str): the item node's name | 187 @param name (unicode): the item node's name |
185 """ | 188 """ |
186 if item is None: # empty string is allowed to set a separator | 189 if item is None: # empty string is allowed to set a separator |
187 return None | 190 return None |
188 item = MenuBar.addItem(self.menu, item, asHTML, popup) | 191 item = MenuBar.addItem(self.menu, item, asHTML, popup) |
189 node_menu = item.getSubMenu() # node eventually uses it's own menu | 192 node_menu = item.getSubMenu() # node eventually uses it's own menu |
210 menus = self.menu.host.menus.get(type_, []) | 213 menus = self.menu.host.menus.get(type_, []) |
211 for action_id, path, path_i18n in menus: | 214 for action_id, path, path_i18n in menus: |
212 if len(path) != len(path_i18n): | 215 if len(path) != len(path_i18n): |
213 log.error("inconsistency between menu paths") | 216 log.error("inconsistency between menu paths") |
214 continue | 217 continue |
215 if isinstance(action_id, str): | 218 if isinstance(action_id, unicode): |
216 callback = PluginMenuCmd(self.menu.host, action_id, menu_data) | 219 callback = PluginMenuCmd(self.menu.host, action_id, menu_data) |
217 elif callable(action_id): | 220 elif callable(action_id): |
218 callback = MenuCmd(action_id, data=menu_data) | 221 callback = MenuCmd(action_id, data=menu_data) |
219 else: | 222 else: |
220 raise exceptions.InternalError | 223 raise exceptions.InternalError |
246 @classmethod | 249 @classmethod |
247 def getCategoryHTML(cls, menu_name_i18n, type_): | 250 def getCategoryHTML(cls, menu_name_i18n, type_): |
248 """Build the html to be used for displaying a category item. | 251 """Build the html to be used for displaying a category item. |
249 | 252 |
250 Inheriting classes may overwrite this method. | 253 Inheriting classes may overwrite this method. |
251 @param menu_name_i18n (str): internationalized category name | 254 @param menu_name_i18n (unicode): internationalized category name |
252 @param type_ (str): category type | 255 @param type_ (unicode): category type |
253 @return: str | 256 @return: unicode |
254 """ | 257 """ |
255 return menu_name_i18n | 258 return menu_name_i18n |
256 | 259 |
257 def setStyleName(self, style): | 260 def setStyleName(self, style): |
258 # XXX: pyjamas set the style to object string representation! | 261 # XXX: pyjamas set the style to object string representation! |