comparison browser_side/xmlui.py @ 299:e4f586fc6101

server and browser side: updated callback system to follow core changes
author Goffi <goffi@goffi.org>
date Tue, 24 Dec 2013 02:00:30 +0100
parents fe83837d3491
children bfbd9d6eb901
comparison
equal deleted inserted replaced
298:e99f578c7179 299:e4f586fc6101
99 elif node_type=="label": 99 elif node_type=="label":
100 ctrl = Label(value+": ") 100 ctrl = Label(value+": ")
101 elif node_type=="string": 101 elif node_type=="string":
102 ctrl = TextBox() 102 ctrl = TextBox()
103 ctrl.setText(value) 103 ctrl.setText(value)
104 self.ctrl_list[name] = ({'node_type':node_type, 'control':ctrl}) 104 self.ctrl_list[name] = {'node_type':node_type, 'control':ctrl}
105 elif node_type=="password": 105 elif node_type=="password":
106 ctrl = PasswordTextBox() 106 ctrl = PasswordTextBox()
107 ctrl.setText(value) 107 ctrl.setText(value)
108 self.ctrl_list[name] = ({'node_type':node_type, 'control':ctrl}) 108 self.ctrl_list[name] = {'node_type':node_type, 'control':ctrl}
109 elif node_type=="textbox": 109 elif node_type=="textbox":
110 ctrl = TextArea() 110 ctrl = TextArea()
111 ctrl.setText(value) 111 ctrl.setText(value)
112 self.ctrl_list[name] = ({'node_type':node_type, 'control':ctrl}) 112 self.ctrl_list[name] = {'node_type':node_type, 'control':ctrl}
113 elif node_type=="bool": 113 elif node_type=="bool":
114 ctrl = CheckBox() 114 ctrl = CheckBox()
115 ctrl.setChecked(value=="true") 115 ctrl.setChecked(value=="true")
116 self.ctrl_list[name] = ({'node_type':node_type, 'control':ctrl}) 116 self.ctrl_list[name] = {'node_type':node_type, 'control':ctrl}
117 elif node_type=="list": 117 elif node_type=="list":
118 _options = [(option.getAttribute("label"), option.getAttribute("value")) for option in elem.getElementsByTagName("option")]
119 attr_map = {label: value for label, value in _options}
118 ctrl = ListBox() 120 ctrl = ListBox()
119 ctrl.setMultipleSelect(elem.getAttribute("multi")=='yes') 121 ctrl.setMultipleSelect(elem.getAttribute("multi")=='yes')
120 for option in elem.getElementsByTagName("option"): 122 for option in _options:
121 ctrl.addItem(option.getAttribute("value")) 123 ctrl.addItem(option[0])
122 ctrl.selectItem(value) 124 ctrl.selectItem(value)
123 self.ctrl_list[name] = ({'node_type':node_type, 'control':ctrl}) 125 self.ctrl_list[name] = {'node_type':node_type, 'control':ctrl, 'attr_map': attr_map}
124 elif node_type=="button": 126 elif node_type=="button":
125 callback_id = elem.getAttribute("callback_id") 127 callback_id = elem.getAttribute("callback_id")
126 ctrl = Button(value, self.onButtonPress) 128 ctrl = Button(value, self.onButtonPress)
127 ctrl.param_id = (callback_id,[field.getAttribute('name') for field in elem.getElementsByTagName("field_back")]) 129 ctrl.param_id = (callback_id,[field.getAttribute('name') for field in elem.getElementsByTagName("field_back")])
128 else: 130 else:
188 cat_dom = self.dom.parseString(xml_data) 190 cat_dom = self.dom.parseString(xml_data)
189 191
190 top=cat_dom.documentElement 192 top=cat_dom.documentElement
191 self.node_type = top.getAttribute("type") 193 self.node_type = top.getAttribute("type")
192 self.title = top.getAttribute("title") or self.title 194 self.title = top.getAttribute("title") or self.title
195 self.session_id = top.getAttribute("session_id") or None
196 self.submit_id = top.getAttribute("submit") or None
193 if top.nodeName != "sat_xmlui" or not self.node_type in ['form', 'param', 'window']: 197 if top.nodeName != "sat_xmlui" or not self.node_type in ['form', 'param', 'window']:
194 raise InvalidXMLUI 198 raise InvalidXMLUI
195 199
196 if self.node_type == 'param': 200 if self.node_type == 'param':
197 self.param_changed = set() 201 self.param_changed = set()
214 ##EVENTS## 218 ##EVENTS##
215 219
216 def onButtonPress(self, button): 220 def onButtonPress(self, button):
217 print "onButtonPress (%s)" % (button,) 221 print "onButtonPress (%s)" % (button,)
218 callback_id, fields = button.param_id 222 callback_id, fields = button.param_id
219 data = {"callback_id":callback_id}
220 for field in fields: 223 for field in fields:
221 ctrl = self.ctrl_list[field] 224 ctrl = self.ctrl_list[field]
222 if isinstance(ctrl['control'],ListBox): 225 if isinstance(ctrl['control'],ListBox):
223 data[field] = '\t'.join(ctrl['control'].getSelectedItemText()) 226 data[field] = '\t'.join(ctrl['control'].getSelectedItemText())
224 elif isinstance(ctrl['control'],CheckBox): 227 elif isinstance(ctrl['control'],CheckBox):
225 data[field] = "true" if ctrl['control'].isChecked() else "false" 228 data[field] = "true" if ctrl['control'].isChecked() else "false"
226 else: 229 else:
227 data[field] = ctrl['control'].getText() 230 data[field] = ctrl['control'].getText()
228 231
229 self.host.bridge.call('launchAction', None, "button", data) 232 self.host.launchAction(callback_id, None)
230 self.host.current_action_ids.add(id)
231 233
232 def onParamChange(self, widget): 234 def onParamChange(self, widget):
233 """Called when type is param and a widget to save is modified""" 235 """Called when type is param and a widget to save is modified"""
234 assert(self.node_type == "param") 236 assert(self.node_type == "param")
235 print "onParamChange:", widget 237 print "onParamChange:", widget
239 print "onFormSubmitted" 241 print "onFormSubmitted"
240 # FIXME: untested 242 # FIXME: untested
241 print "FIXME FIXME FIXME: Form submitting not managed yet" 243 print "FIXME FIXME FIXME: Form submitting not managed yet"
242 data = [] 244 data = []
243 for ctrl_name in self.ctrl_list: 245 for ctrl_name in self.ctrl_list:
244 ctrl = self.ctrl_list[ctrl_name] 246 escaped = u"%s%s" % (SAT_FORM_PREFIX, ctrl_name)
247 ctrl = self.ctrl_list[escaped]
245 if isinstance(ctrl['control'], ListBox): 248 if isinstance(ctrl['control'], ListBox):
246 data.append((ctrl_name, '\t'.join(ctrl['control'].getSelectedItemText()))) 249 data.append((escaped, '\t'.join([ctrl['attr_map'][label] for label in ctrl['control'].getSelectedItemText()])))
247 elif isinstance(ctrl['control'], CheckBox): 250 elif isinstance(ctrl['control'], CheckBox):
248 data.append((ctrl_name, "true" if ctrl['control'].isChecked() else "false")) 251 data.append((escaped, "true" if ctrl['control'].isChecked() else "false"))
249 else: 252 else:
250 data.append((ctrl_name, ctrl['control'].getText())) 253 data.append((escaped, ctrl['control'].getText()))
251 if 'action_back' in self.misc: #FIXME FIXME FIXME: WTF ! Must be cleaned 254 if 'action_back' in self.misc: #FIXME FIXME FIXME: WTF ! Must be cleaned
252 raise NotImplementedError 255 raise NotImplementedError
253 elif 'callback' in self.misc: 256 elif 'callback' in self.misc:
254 self.misc['callback'](data) 257 self.misc['callback'](data)
258 elif self.submit_id is not None:
259 data = dict(selected_values)
260 if self.session_id is not None:
261 data["session_id"] = self.session_id
262 self.host.launchAction(self.submit_id, data)
255 else: 263 else:
256 print ("WARNING: The form data is not sent back, the type is not managed properly") 264 print ("WARNING: The form data is not sent back, the type is not managed properly")
257 265
258 self.close() 266 self.close()
259 267