# HG changeset patch # User sylvain.thenault@logilab.fr # Date 1239215666 -7200 # Node ID 8975c8e520a90607551507d2af5adb6323b68d88 # Parent 62be4ece45522d966aba86411ccace739031c80d refactor button handling diff -r 62be4ece4552 -r 8975c8e520a9 web/form.py --- a/web/form.py Wed Apr 08 20:30:58 2009 +0200 +++ b/web/form.py Wed Apr 08 20:34:26 2009 +0200 @@ -209,15 +209,14 @@ cssclass = None cssstyle = None cwtarget = None - buttons = None redirect_path = None set_error_url = True copy_nav_params = False + form_buttons = None # form buttons (button widgets instances) def __init__(self, req, rset=None, row=None, col=None, submitmsg=None, **kwargs): super(FieldsForm, self).__init__(req, rset, row=row, col=col) - self.buttons = kwargs.pop('buttons', []) for key, val in kwargs.items(): assert hasattr(self.__class__, key) and not key[0] == '_', key setattr(self, key, val) @@ -290,6 +289,7 @@ values.update(previous_values) for field in self.fields: for field in field.actual_fields(self): + field.form_init(self) value = self.form_field_value(field, values) context[field] = {'value': field.format_value(self.req, value), 'rawvalue': value, @@ -335,9 +335,6 @@ def form_field_vocabulary(self, field, limit=None): raise NotImplementedError - def form_buttons(self): - return self.buttons - class EntityFieldsForm(FieldsForm): __select__ = (match_kwargs('entity') | (one_line_rset & non_final_entity())) diff -r 62be4ece4552 -r 8975c8e520a9 web/formrenderers.py --- a/web/formrenderers.py Wed Apr 08 20:30:58 2009 +0200 +++ b/web/formrenderers.py Wed Apr 08 20:34:26 2009 +0200 @@ -169,8 +169,8 @@ def render_buttons(self, w, form): w(u'\n\n' % self.button_bar_class) - for button in form.form_buttons(): - w(u'\n' % button) + for button in form.form_buttons: + w(u'\n' % button.render(form)) w(u'
%s%s
') @@ -243,8 +243,7 @@ super(EntityFormRenderer, self)._render_fields(fields, w, form, values) def render_buttons(self, w, form): - buttons = form.form_buttons() - if len(buttons) == 3: + if len(form.form_buttons) == 3: w(""" -
@@ -254,7 +253,7 @@ %s
""" % tuple(buttons)) + """ % tuple(button.render(form) for button in form.form_buttons)) else: super(EntityFormRenderer, self).render_buttons(w, form) diff -r 62be4ece4552 -r 8975c8e520a9 web/formwidgets.py --- a/web/formwidgets.py Wed Apr 08 20:30:58 2009 +0200 +++ b/web/formwidgets.py Wed Apr 08 20:34:26 2009 +0200 @@ -9,6 +9,7 @@ from datetime import date from cubicweb.common import tags +from cubicweb.web import stdmsgs class FieldWidget(object): needs_js = () @@ -228,3 +229,56 @@ self.add_media(form) attrs = self._render_attrs(form, field)[-1] return tags.div(**attrs) + + +class Button(Input): + type = 'button' + def __init__(self, label=stdmsgs.BUTTON_OK, attrs=None, + setdomid=None, settabindex=None, + name='', value='', onclick=None, cwaction=None): + super(Button, self).__init__(attrs, setdomid, settabindex) + self.label = label + self.name = name + self.value = '' + self.onclick = onclick + self.cwaction = cwaction + self.attrs.setdefault('klass', 'validateButton') + + def render(self, form, field=None): + label = form.req._(self.label) + attrs = self.attrs.copy() + if self.cwaction: + assert self.onclick is None + attrs['onclick'] = "postForm('__action_%s', \'%s\', \'%s\')" % ( + self.cwaction, self.label, form.domid) + elif self.onclick: + attrs['onclick'] = self.onclick + if self.name: + attrs['name'] = name + if self.setdomid: + attrs['id'] = self.name + if self.settabindex and not 'tabindex' in attrs: + attrs['tabindex'] = form.req.next_tabindex() + return tags.input(value=label, type=self.type, **attrs) + + +class SubmitButton(Button): + type = 'submit' + +class ResetButton(Button): + type = 'reset' + +class ImgButton(object): + def __init__(self, domid, href, label, imgressource): + self.domid = domid + self.href = href + self.imgressource = imgressource + self.label = label + + def render(self, form, field=None): + self.imgsrc = form.req.external_resource(self.imgressource) + return '%(label)s%(label)s' % self.__dict__ + + +# XXX EntityLinkComboBoxWidget, AddComboBoxWidget, AutoCompletionWidget, +# StaticFileAutoCompletionWidget, RestrictedAutoCompletionWidget... diff -r 62be4ece4552 -r 8975c8e520a9 web/views/editforms.py --- a/web/views/editforms.py Wed Apr 08 20:30:58 2009 +0200 +++ b/web/views/editforms.py Wed Apr 08 20:34:26 2009 +0200 @@ -24,6 +24,7 @@ from cubicweb.web.form import (FieldNotFound, CompositeForm, EntityFieldsForm, FormMixIn) from cubicweb.web.formfields import guess_field +from cubicweb.web.formwidgets import Button, SubmitButton, ResetButton from cubicweb.web.formrenderers import (FormRenderer, EntityFormRenderer, EntityCompositeFormRenderer, EntityInlinedFormRenderer) @@ -61,11 +62,10 @@ % _('this action is not reversible!')) # XXX above message should have style of a warning w(u'

%s

\n' % _('Do you want to delete the following element(s) ?')) - form = CompositeForm(req, domid='deleteconf', action=self.build_url('edit'), - onsubmit=self.onsubmit, copy_nav_params=True) - # XXX tabindex - form.buttons.append(form.button_delete(label=stdmsgs.YES)) - form.buttons.append(form.button_cancel(label=stdmsgs.NO)) + form = CompositeForm(req, domid='deleteconf', copy_nav_params=True, + action=self.build_url('edit'), onsubmit=None, + form_buttons=[Button(stdmsgs.YES, cwaction='delete'), + Button(stdmsgs.NO, cwaction='cancel')]) done = set() w(u'