def _generate_action_form(self, action, content, clazz, attrs): action_name, action_args = self._parse_call(action) widget_url = self.widget.get_action_url(action_name, action_args) return tag('form', style='display:inline', action=widget_url, method='post', children=[ tag('button', type='submit', children=self.visit(content), clazz=clazz, fw_action_url=widget_url, attrs=attrs) ])
def visit_links(self, elem): clazz = 'fw-link' if elem.name == 'a' else 'fw-button' nattrs = elem.attrs.copy() if 'class' in elem.attrs: clazz += ' ' + elem.attrs['class'] if 'action' in elem.attrs: return self._generate_action_form(elem.attrs['action'], self.visit(elem.children), clazz, nattrs) elif 'href' in elem.attrs: href = elem.attrs['href'] if '://' in href: return elem return self._generate_href_form(href, self.visit(elem.children), clazz, elem.attrs.get('target'), nattrs) elif 'submit' in elem.attrs: return tag(elem.name, self.visit(elem.children), type='submit', clazz=clazz + ' fw-submit') else: return tag(elem.name, self.visit(elem.children), self.process_attrs(elem.attrs))
def _generate_href_form(self, action, content, clazz, target, attrs): url = self._parse_href(action) if target == '$this': del attrs['target'] if not isinstance(url, tuple): raise ValueError('link with target==$this not a tuple') top_name = self.fwml.widget.top.widget._widget_name widget, args = url str_url = fw.util.url(widget, args) if widget != self.fwml.widget._widget_name: raise ValueError('link with target==$this has to have same widget') href = fw.util.url(top_name, self._get_href_target_args(args)) return tag('a', href=href, children=content, clazz=clazz + ' fw-widget-link', #fw_widget_link=str_url, fw_link_target=self.fwml.widget.get_path(), attrs=attrs) else: if isinstance(url, tuple): url = fw.util.url(*url) return tag('a', href=url, children=content, clazz=clazz + ' fw-widget-link', attrs=attrs)
def visit_form(self, elem): if 'action' in elem.attrs and '(' in elem.attrs['action']: action = elem.attrs['action'] name, args = parse_call(action) std_args = {} form_args = {} for key, val in args.items(): try: std_args[key] = self.fwml._process_value(val) except (AttributeError, KeyError): form_args[key] = val[1:] url = self.widget.get_action_url(name, std_args, form_args) attrs = self.process_attrs(elem.attrs) if 'file' in elem.attrs: attrs.update(enctype="multipart/form-data", fw_no_js="true") attrs.update(action=url, method='post') return tag('form', self.visit(elem.children), attrs=attrs) return tag('form', children=self.visit(elem.children), attrs=self.process_attrs(elem.attrs))
def visit_script(self, elem): # Google Chrome sometimes behaves strangely when using script elements if elem.attrs.get('type', '').startswith('fw/'): return tag('fwscript', self.visit(elem.children), elem.attrs) else: return elem
def visit_root(self, elem): return tag('span', fw_widget_path=self.widget.get_path(), children=self.visit(elem.children))
def visit_input(self, elem): attrs = self.process_attrs(elem.attrs) if 'name' in attrs: attrs['name'] = '_field_' + attrs['name'] return tag(elem.name, self.visit(elem.children), attrs=attrs)