web/views/editforms.py
branchtls-sprint
changeset 1759 61d026ced19f
parent 1750 9df5e65c5f79
child 1760 6b97d286eb5a
--- a/web/views/editforms.py	Tue May 12 18:16:11 2009 +0200
+++ b/web/views/editforms.py	Tue May 12 18:53:41 2009 +0200
@@ -20,7 +20,8 @@
 from cubicweb.common import tags
 from cubicweb.web import INTERNAL_FIELD_VALUE, stdmsgs, eid_param
 from cubicweb.web.form import CompositeForm, EntityFieldsForm, FormViewMixIn
-from cubicweb.web.formwidgets import Button, SubmitButton, ResetButton
+from cubicweb.web.formfields import RelationField
+from cubicweb.web.formwidgets import Button, SubmitButton, ResetButton, Select
 from cubicweb.web.formrenderers import (FormRenderer, EntityFormRenderer,
                                         EntityCompositeFormRenderer,
                                         EntityInlinedFormRenderer)
@@ -88,12 +89,12 @@
 
     # FIXME editableField class could be toggleable from userprefs
 
-    onsubmit = ("return inlineValidateForm('%(divid)s-form', '%(rtype)s', "
+    onsubmit = ("return inlineValidateAttributeForm('%(divid)s-form', '%(rtype)s', "
                 "'%(eid)s', '%(divid)s', %(reload)s);")
     ondblclick = "showInlineEditionForm(%(eid)s, '%(rtype)s', '%(divid)s')"
 
     def cell_call(self, row, col, rtype=None, role='subject', reload=False,
-                  vid='autolimited'):
+                  vid='list'):
         """display field to edit entity's `rtype` relation on double-click"""
         rschema = self.schema.rschema(rtype)
         entity = self.entity(row, col)
@@ -104,10 +105,43 @@
                 value = entity.printable_value(rtype)
         else:
             rset = entity.related(rtype, role)
-            value = self.view(vid, rset, 'null')
+            value = ' %s ' % self.view(vid, rset, 'null')
         if not entity.has_perm('update'):
             self.w(value)
             return
+        if rschema.is_final():
+            self._edit_attribute(entity, value, rtype, role, reload, row, col)
+        else:
+            self._edit_relation(entity, value, rtype, role, reload, row, col)
+
+    def _edit_relation(self, entity, value, rtype, role, reload, row, col):
+        rtype = 'person_in_charge'
+        entity = self.entity(row, col)
+        divid = 'd%s' % make_uid('%s-%s' % (rtype, entity.eid))
+        event_data = {'divid' : divid, 'eid' : entity.eid, 'rtype' : rtype,
+                      'reload' : dumps(reload)}
+        form = EntityFieldsForm(self.req, None, entity=entity, action='#',
+                                domid='%s-form' % divid,
+                                cssstyle='display: none',
+                                onsubmit=("return inlineValidateRelationForm('%(divid)s-form', '%(rtype)s', "
+                                          "'%(eid)s', '%(divid)s', %(reload)s);" % event_data),
+                                form_buttons=[SubmitButton(),
+                                              Button(stdmsgs.BUTTON_CANCEL,
+                                                     onclick="cancelInlineEdit(%s,\'%s\',\'%s\')" %\
+                                                         (entity.eid, rtype, divid))])
+        form.form_add_hidden(u'__maineid', entity.eid)
+        form.append_field(RelationField(name=rtype, sort=True,
+                                        widget=Select(dict(size=1)),
+                                        label=u' '))
+        renderer = FormRenderer(display_label=False, display_help=False,
+                                display_fields=[(rtype, role)],
+                                table_class='', button_bar_class='buttonbar',
+                                display_progress_div=False)
+        self.w(tags.div(value, klass='editableField', id=divid,
+                        ondblclick=self.ondblclick % event_data))
+        self.w(form.form_render(renderer=renderer))
+
+    def _edit_attribute(self, entity, value, rtype, role, reload, row, col):
         eid = entity.eid
         divid = 'd%s' % make_uid('%s-%s' % (rtype, eid))
         event_data = {'divid' : divid, 'eid' : eid, 'rtype' : rtype,
@@ -121,7 +155,7 @@
                                        domid='%s-form' % divid, action='#',
                                        cssstyle='display: none',
                                        onsubmit=self.onsubmit % event_data)
-        form.form_add_hidden(u'__maineid', entity.eid)
+        form.form_add_hidden(u'__maineid', eid)
         renderer = FormRenderer(display_label=False, display_help=False,
                                 display_fields=[(rtype, role)],
                                 table_class='', button_bar_class='buttonbar',