Mercurial > libervia-web
annotate browser_side/xmlui.py @ 312:b4781a350483
browser_side: display a "New message" button and add a "comment" icon for main entries in MicroblogPanel when the unibox is disabled
author | souliane <souliane@mailoo.org> |
---|---|
date | Mon, 30 Dec 2013 00:33:19 +0100 |
parents | e4f586fc6101 |
children | bfbd9d6eb901 |
rev | line source |
---|---|
143 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 Libervia: a Salut à Toi frontend | |
165 | 6 Copyright (C) 2011, 2012, 2013 Jérôme Poisson <goffi@goffi.org> |
143 | 7 |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU Affero General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU Affero General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU Affero General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 from pyjamas.ui.VerticalPanel import VerticalPanel | |
23 from pyjamas.ui.HorizontalPanel import HorizontalPanel | |
24 from pyjamas.ui.CellPanel import CellPanel | |
25 from pyjamas.ui.TabPanel import TabPanel | |
26 from pyjamas.ui.Grid import Grid | |
27 from pyjamas.ui.Label import Label | |
28 from pyjamas.ui.TextBoxBase import TextBoxBase | |
29 from pyjamas.ui.TextBox import TextBox | |
30 from pyjamas.ui.PasswordTextBox import PasswordTextBox | |
31 from pyjamas.ui.TextArea import TextArea | |
32 from pyjamas.ui.CheckBox import CheckBox | |
33 from pyjamas.ui.ListBox import ListBox | |
34 from pyjamas.ui.Button import Button | |
35 from nativedom import NativeDOM | |
36 | |
37 | |
38 class InvalidXMLUI(Exception): | |
39 pass | |
40 | |
41 class Pairs(Grid): | |
42 | |
43 def __init__(self): | |
247
fe83837d3491
browser_side: removed some trailing spaces
Goffi <goffi@goffi.org>
parents:
229
diff
changeset
|
44 Grid.__init__(self, 0, 0) |
143 | 45 self.row = 0 |
46 self.col = 0 | |
47 | |
48 def append(self, widget): | |
49 if self.col == 0: | |
50 self.resize(self.row+1, 2) | |
51 self.setWidget(self.row, self.col, widget) | |
52 self.col += 1 | |
53 if self.col == 2: | |
54 self.row +=1 | |
55 self.col = 0 | |
247
fe83837d3491
browser_side: removed some trailing spaces
Goffi <goffi@goffi.org>
parents:
229
diff
changeset
|
56 |
143 | 57 class XMLUI(VerticalPanel): |
247
fe83837d3491
browser_side: removed some trailing spaces
Goffi <goffi@goffi.org>
parents:
229
diff
changeset
|
58 |
143 | 59 def __init__(self, host, xml_data, title = None, options = None, misc = None, close_cb = None): |
60 print "XMLUI init" | |
61 VerticalPanel.__init__(self) | |
62 self.dom = NativeDOM() | |
63 self.host = host | |
64 self.title = title | |
65 self.options = options or [] | |
66 self.misc = misc or {} | |
67 self.close_cb = close_cb | |
68 self.__dest = "window" | |
69 self.ctrl_list = {} # usefull to access ctrl | |
70 self.constructUI(xml_data) | |
71 self.setSize('100%', '100%') | |
72 | |
73 def setCloseCb(self, close_cb): | |
74 self.close_cb = close_cb | |
75 | |
76 def close(self): | |
77 if self.close_cb: | |
78 self.close_cb() | |
79 else: | |
80 print "WARNING: no close method defined" | |
81 | |
82 def __parseElems(self, node, parent): | |
83 """Parse elements inside a <layout> tags, and add them to the parent""" | |
84 for elem in node.childNodes: | |
85 if elem.nodeName != "elem": | |
86 raise Exception("Unmanaged tag [%s]" % (elem.nodeName)) | |
87 node_id = elem.getAttribute("node_id") | |
88 name = elem.getAttribute("name") | |
89 node_type = elem.getAttribute("type") | |
90 value = elem.getAttribute("value") if elem.hasAttribute('value') else u'' | |
91 if node_type=="empty": | |
247
fe83837d3491
browser_side: removed some trailing spaces
Goffi <goffi@goffi.org>
parents:
229
diff
changeset
|
92 ctrl = Label('') |
143 | 93 elif node_type=="text": |
94 try: | |
95 value = elem.childNodes[0].wholeText | |
96 except IndexError: | |
97 print ("WARNING: text node has no child !") | |
98 ctrl = Label(value) | |
99 elif node_type=="label": | |
100 ctrl = Label(value+": ") | |
101 elif node_type=="string": | |
102 ctrl = TextBox() | |
103 ctrl.setText(value) | |
299
e4f586fc6101
server and browser side: updated callback system to follow core changes
Goffi <goffi@goffi.org>
parents:
247
diff
changeset
|
104 self.ctrl_list[name] = {'node_type':node_type, 'control':ctrl} |
143 | 105 elif node_type=="password": |
106 ctrl = PasswordTextBox() | |
107 ctrl.setText(value) | |
299
e4f586fc6101
server and browser side: updated callback system to follow core changes
Goffi <goffi@goffi.org>
parents:
247
diff
changeset
|
108 self.ctrl_list[name] = {'node_type':node_type, 'control':ctrl} |
143 | 109 elif node_type=="textbox": |
110 ctrl = TextArea() | |
111 ctrl.setText(value) | |
299
e4f586fc6101
server and browser side: updated callback system to follow core changes
Goffi <goffi@goffi.org>
parents:
247
diff
changeset
|
112 self.ctrl_list[name] = {'node_type':node_type, 'control':ctrl} |
143 | 113 elif node_type=="bool": |
114 ctrl = CheckBox() | |
115 ctrl.setChecked(value=="true") | |
299
e4f586fc6101
server and browser side: updated callback system to follow core changes
Goffi <goffi@goffi.org>
parents:
247
diff
changeset
|
116 self.ctrl_list[name] = {'node_type':node_type, 'control':ctrl} |
143 | 117 elif node_type=="list": |
299
e4f586fc6101
server and browser side: updated callback system to follow core changes
Goffi <goffi@goffi.org>
parents:
247
diff
changeset
|
118 _options = [(option.getAttribute("label"), option.getAttribute("value")) for option in elem.getElementsByTagName("option")] |
e4f586fc6101
server and browser side: updated callback system to follow core changes
Goffi <goffi@goffi.org>
parents:
247
diff
changeset
|
119 attr_map = {label: value for label, value in _options} |
143 | 120 ctrl = ListBox() |
121 ctrl.setMultipleSelect(elem.getAttribute("multi")=='yes') | |
299
e4f586fc6101
server and browser side: updated callback system to follow core changes
Goffi <goffi@goffi.org>
parents:
247
diff
changeset
|
122 for option in _options: |
e4f586fc6101
server and browser side: updated callback system to follow core changes
Goffi <goffi@goffi.org>
parents:
247
diff
changeset
|
123 ctrl.addItem(option[0]) |
229
e632f77c4219
bridge: asyncGetParamA takes a security_limit argument
souliane <souliane@mailoo.org>
parents:
218
diff
changeset
|
124 ctrl.selectItem(value) |
299
e4f586fc6101
server and browser side: updated callback system to follow core changes
Goffi <goffi@goffi.org>
parents:
247
diff
changeset
|
125 self.ctrl_list[name] = {'node_type':node_type, 'control':ctrl, 'attr_map': attr_map} |
143 | 126 elif node_type=="button": |
127 callback_id = elem.getAttribute("callback_id") | |
128 ctrl = Button(value, self.onButtonPress) | |
129 ctrl.param_id = (callback_id,[field.getAttribute('name') for field in elem.getElementsByTagName("field_back")]) | |
130 else: | |
131 print("FIXME FIXME FIXME: type [%s] is not implemented" % node_type) #FIXME ! | |
132 raise NotImplementedError | |
133 if self.node_type == 'param': | |
229
e632f77c4219
bridge: asyncGetParamA takes a security_limit argument
souliane <souliane@mailoo.org>
parents:
218
diff
changeset
|
134 if isinstance(ctrl, TextBoxBase): |
143 | 135 ctrl.addChangeListener(self.onParamChange) |
136 elif isinstance(ctrl, CheckBox): | |
137 ctrl.addClickListener(self.onParamChange) | |
229
e632f77c4219
bridge: asyncGetParamA takes a security_limit argument
souliane <souliane@mailoo.org>
parents:
218
diff
changeset
|
138 elif isinstance(ctrl, ListBox): |
e632f77c4219
bridge: asyncGetParamA takes a security_limit argument
souliane <souliane@mailoo.org>
parents:
218
diff
changeset
|
139 ctrl.addChangeListener(self.onParamChange) |
143 | 140 ctrl._param_category = self._current_category |
141 ctrl._param_name = name | |
142 parent.append(ctrl) | |
143 | |
144 def __parseChilds(self, current, elem, wanted = ['layout'], data = None): | |
145 """Recursively parse childNodes of an elemen | |
146 @param current: widget container with 'append' method | |
147 @param elem: element from which childs will be parsed | |
148 @param wanted: list of tag names that can be present in the childs to be SàT XMLUI compliant""" | |
149 for node in elem.childNodes: | |
150 if wanted and not node.nodeName in wanted: | |
151 raise InvalidXMLUI("ERROR: unexpected nodeName") | |
152 if node.nodeName == "layout": | |
153 node_type = node.getAttribute('type') | |
154 if node_type == "tabs": | |
218
4e6467efd6bf
browser_side: small improvements for parameters panel
souliane <souliane@mailoo.org>
parents:
165
diff
changeset
|
155 tab_cont = TabPanel() |
143 | 156 tab_cont.setStyleName('liberviaTabPanel') |
157 tab_cont.setHeight('100%') | |
158 self.__parseChilds(current, node, ['category'], tab_cont) | |
159 current.append(tab_cont) | |
160 if isinstance(current, CellPanel): | |
161 current.setCellHeight(tab_cont, '100%') | |
218
4e6467efd6bf
browser_side: small improvements for parameters panel
souliane <souliane@mailoo.org>
parents:
165
diff
changeset
|
162 if len(tab_cont.getChildren()) > 0: |
4e6467efd6bf
browser_side: small improvements for parameters panel
souliane <souliane@mailoo.org>
parents:
165
diff
changeset
|
163 tab_cont.selectTab(0) |
143 | 164 elif node_type == "vertical": |
165 self.__parseElems(node, current) | |
166 elif node_type == "pairs": | |
167 pairs = Pairs() | |
168 self.__parseElems(node, pairs) | |
169 current.append(pairs) | |
170 else: | |
171 print("WARNING: Unknown layout [%s], using default one" % (node_type,)) | |
172 self.__parseElems(node, current) | |
173 elif node.nodeName == "category": | |
247
fe83837d3491
browser_side: removed some trailing spaces
Goffi <goffi@goffi.org>
parents:
229
diff
changeset
|
174 name = node.getAttribute('name') |
fe83837d3491
browser_side: removed some trailing spaces
Goffi <goffi@goffi.org>
parents:
229
diff
changeset
|
175 label = node.getAttribute('label') |
143 | 176 if not name or not isinstance(data,TabPanel): |
247
fe83837d3491
browser_side: removed some trailing spaces
Goffi <goffi@goffi.org>
parents:
229
diff
changeset
|
177 raise InvalidXMLUI |
143 | 178 if self.node_type == 'param': |
179 self._current_category = name #XXX: awful hack because params need category and we don't keep parent | |
180 tab_cont = data | |
181 tab_body = VerticalPanel() | |
182 tab_cont.add(tab_body, label or name) | |
183 self.__parseChilds(tab_body, node, ['layout']) | |
184 else: | |
185 message=_("Unknown tag") | |
186 raise NotImplementedError(message) | |
187 | |
188 def constructUI(self, xml_data): | |
247
fe83837d3491
browser_side: removed some trailing spaces
Goffi <goffi@goffi.org>
parents:
229
diff
changeset
|
189 |
143 | 190 cat_dom = self.dom.parseString(xml_data) |
247
fe83837d3491
browser_side: removed some trailing spaces
Goffi <goffi@goffi.org>
parents:
229
diff
changeset
|
191 |
143 | 192 top=cat_dom.documentElement |
193 self.node_type = top.getAttribute("type") | |
194 self.title = top.getAttribute("title") or self.title | |
299
e4f586fc6101
server and browser side: updated callback system to follow core changes
Goffi <goffi@goffi.org>
parents:
247
diff
changeset
|
195 self.session_id = top.getAttribute("session_id") or None |
e4f586fc6101
server and browser side: updated callback system to follow core changes
Goffi <goffi@goffi.org>
parents:
247
diff
changeset
|
196 self.submit_id = top.getAttribute("submit") or None |
143 | 197 if top.nodeName != "sat_xmlui" or not self.node_type in ['form', 'param', 'window']: |
198 raise InvalidXMLUI | |
199 | |
200 if self.node_type == 'param': | |
201 self.param_changed = set() | |
202 | |
203 self.__parseChilds(self, cat_dom.documentElement) | |
247
fe83837d3491
browser_side: removed some trailing spaces
Goffi <goffi@goffi.org>
parents:
229
diff
changeset
|
204 |
143 | 205 if self.node_type == 'form': |
206 hpanel = HorizontalPanel() | |
207 hpanel.add(Button('Submit',self.onFormSubmitted)) | |
208 if not 'NO_CANCEL' in self.options: | |
209 hpanel.add(Button('Cancel',self.onFormCancelled)) | |
210 self.add(hpanel) | |
211 elif self.node_type == 'param': | |
212 assert(isinstance(self.children[0],TabPanel)) | |
213 hpanel = HorizontalPanel() | |
247
fe83837d3491
browser_side: removed some trailing spaces
Goffi <goffi@goffi.org>
parents:
229
diff
changeset
|
214 hpanel.add(Button('Cancel', lambda ignore: self.close())) |
fe83837d3491
browser_side: removed some trailing spaces
Goffi <goffi@goffi.org>
parents:
229
diff
changeset
|
215 hpanel.add(Button('Save', self.onSaveParams)) |
143 | 216 self.add(hpanel) |
217 | |
218 ##EVENTS## | |
219 | |
220 def onButtonPress(self, button): | |
221 print "onButtonPress (%s)" % (button,) | |
222 callback_id, fields = button.param_id | |
223 for field in fields: | |
224 ctrl = self.ctrl_list[field] | |
225 if isinstance(ctrl['control'],ListBox): | |
229
e632f77c4219
bridge: asyncGetParamA takes a security_limit argument
souliane <souliane@mailoo.org>
parents:
218
diff
changeset
|
226 data[field] = '\t'.join(ctrl['control'].getSelectedItemText()) |
143 | 227 elif isinstance(ctrl['control'],CheckBox): |
228 data[field] = "true" if ctrl['control'].isChecked() else "false" | |
229 else: | |
230 data[field] = ctrl['control'].getText() | |
231 | |
299
e4f586fc6101
server and browser side: updated callback system to follow core changes
Goffi <goffi@goffi.org>
parents:
247
diff
changeset
|
232 self.host.launchAction(callback_id, None) |
143 | 233 |
234 def onParamChange(self, widget): | |
235 """Called when type is param and a widget to save is modified""" | |
236 assert(self.node_type == "param") | |
237 print "onParamChange:", widget | |
238 self.param_changed.add(widget) | |
239 | |
240 def onFormSubmitted(self, button): | |
241 print "onFormSubmitted" | |
247
fe83837d3491
browser_side: removed some trailing spaces
Goffi <goffi@goffi.org>
parents:
229
diff
changeset
|
242 # FIXME: untested |
143 | 243 print "FIXME FIXME FIXME: Form submitting not managed yet" |
244 data = [] | |
245 for ctrl_name in self.ctrl_list: | |
299
e4f586fc6101
server and browser side: updated callback system to follow core changes
Goffi <goffi@goffi.org>
parents:
247
diff
changeset
|
246 escaped = u"%s%s" % (SAT_FORM_PREFIX, ctrl_name) |
e4f586fc6101
server and browser side: updated callback system to follow core changes
Goffi <goffi@goffi.org>
parents:
247
diff
changeset
|
247 ctrl = self.ctrl_list[escaped] |
143 | 248 if isinstance(ctrl['control'], ListBox): |
299
e4f586fc6101
server and browser side: updated callback system to follow core changes
Goffi <goffi@goffi.org>
parents:
247
diff
changeset
|
249 data.append((escaped, '\t'.join([ctrl['attr_map'][label] for label in ctrl['control'].getSelectedItemText()]))) |
143 | 250 elif isinstance(ctrl['control'], CheckBox): |
299
e4f586fc6101
server and browser side: updated callback system to follow core changes
Goffi <goffi@goffi.org>
parents:
247
diff
changeset
|
251 data.append((escaped, "true" if ctrl['control'].isChecked() else "false")) |
143 | 252 else: |
299
e4f586fc6101
server and browser side: updated callback system to follow core changes
Goffi <goffi@goffi.org>
parents:
247
diff
changeset
|
253 data.append((escaped, ctrl['control'].getText())) |
143 | 254 if 'action_back' in self.misc: #FIXME FIXME FIXME: WTF ! Must be cleaned |
255 raise NotImplementedError | |
256 elif 'callback' in self.misc: | |
257 self.misc['callback'](data) | |
299
e4f586fc6101
server and browser side: updated callback system to follow core changes
Goffi <goffi@goffi.org>
parents:
247
diff
changeset
|
258 elif self.submit_id is not None: |
e4f586fc6101
server and browser side: updated callback system to follow core changes
Goffi <goffi@goffi.org>
parents:
247
diff
changeset
|
259 data = dict(selected_values) |
e4f586fc6101
server and browser side: updated callback system to follow core changes
Goffi <goffi@goffi.org>
parents:
247
diff
changeset
|
260 if self.session_id is not None: |
e4f586fc6101
server and browser side: updated callback system to follow core changes
Goffi <goffi@goffi.org>
parents:
247
diff
changeset
|
261 data["session_id"] = self.session_id |
e4f586fc6101
server and browser side: updated callback system to follow core changes
Goffi <goffi@goffi.org>
parents:
247
diff
changeset
|
262 self.host.launchAction(self.submit_id, data) |
143 | 263 else: |
264 print ("WARNING: The form data is not sent back, the type is not managed properly") | |
265 | |
266 self.close() | |
247
fe83837d3491
browser_side: removed some trailing spaces
Goffi <goffi@goffi.org>
parents:
229
diff
changeset
|
267 |
143 | 268 def onFormCancelled(self, button): |
269 self.close() | |
270 | |
271 def onSaveParams(self, button): | |
272 print "onSaveParams" | |
273 for ctrl in self.param_changed: | |
274 if isinstance(ctrl, CheckBox): | |
275 value = "true" if ctrl.isChecked() else "false" | |
229
e632f77c4219
bridge: asyncGetParamA takes a security_limit argument
souliane <souliane@mailoo.org>
parents:
218
diff
changeset
|
276 elif isinstance(ctrl, ListBox): |
e632f77c4219
bridge: asyncGetParamA takes a security_limit argument
souliane <souliane@mailoo.org>
parents:
218
diff
changeset
|
277 value = '\t'.join(ctrl.getSelectedItemText()) |
143 | 278 else: |
279 value = ctrl.getText() | |
280 self.host.bridge.call('setParam', None, ctrl._param_name, value, ctrl._param_category) | |
281 self.close() |