merge tls-sprint
authorsylvain.thenault@logilab.fr
Tue, 12 May 2009 20:31:35 +0200
branchtls-sprint
changeset 1764 73dc7ad124aa
parent 1758 d46f59e5efd3 (current diff)
parent 1763 65ec5ccc8927 (diff)
child 1765 a25c7c73c8f6
merge
--- a/web/data/cubicweb.edition.js	Tue May 12 20:18:19 2009 +0200
+++ b/web/data/cubicweb.edition.js	Tue May 12 20:31:35 2009 +0200
@@ -443,13 +443,13 @@
 
 
 /*
- * called by live-edit forms to submit changes
+ * called by reledit forms to submit changes
  * @param formid : the dom id of the form used
  * @param rtype : the attribute being edited
  * @param eid : the eid of the entity being edited
  * @param reload: boolean to reload page if true (when changing URL dependant data)
  */
-function inlineValidateForm(formid, rtype, eid, divid, reload) {
+function inlineValidateAttributeForm(formid, rtype, eid, divid, reload) {
     try {
 	var form = getNode(formid);
 	if (typeof FCKeditorAPI != "undefined") {
@@ -488,6 +488,32 @@
     return false;
 }
 
+function inlineValidateRelationForm(formid, rtype, eid, divid, vid) {
+    try {
+	var form = getNode(formid);
+	var zipped = formContents(form);
+	var d = asyncRemoteExec('edit_relation', 'apply', zipped[0], zipped[1], rtype, eid, vid);
+    } catch (ex) {
+	log('got exception', ex);
+	return false;
+    }
+    d.addCallback(function (result, req) {
+        handleFormValidationResponse(formid, noop, result);
+	var fieldview = getNode(divid);
+        fieldview.innerHTML = result[2];
+	// switch inline form off only if no error
+	if (result[0]) {
+          // hide global error messages
+	  jQuery('div.errorMessage').remove();
+	  jQuery('#appMsg').hide();
+	  cancelInlineEdit(eid, rtype, divid);
+	}
+        return false;
+    });
+  return false;
+}
+
+
 /**** inline edition ****/
 function showInlineEditionForm(eid, rtype, divid) {
     jQuery('#' + divid).hide();
--- a/web/views/basecontrollers.py	Tue May 12 20:18:19 2009 +0200
+++ b/web/views/basecontrollers.py	Tue May 12 20:31:35 2009 +0200
@@ -387,7 +387,7 @@
         except Exception, err:
             self.req.cnx.rollback()
             self.exception('unexpected error in js_validateform')
-            return (False, self.req._(str(err)))
+            return (False, self.req._(str(err).decode('utf-8')))
         return (False, '???')
 
     @jsonize
@@ -404,6 +404,17 @@
             return (success, args, None)
 
     @jsonize
+    def js_edit_relation(self, action, names, values,
+                         rtype, eid, vid, role='subject'):
+        success, args = self.validate_form(action, names, values)
+        if success:
+            entity = self.req.eid_rset(eid).get_entity(0, 0)
+            rset = entity.related(rtype, role)
+            return (success, args, self.view(vid, rset))
+        else:
+            return (success, args, None)
+
+    @jsonize
     def js_i18n(self, msgids):
         """returns the translation of `msgid`"""
         return [self.req._(msgid) for msgid in msgids]
--- a/web/views/baseviews.py	Tue May 12 20:18:19 2009 +0200
+++ b/web/views/baseviews.py	Tue May 12 20:31:35 2009 +0200
@@ -68,7 +68,7 @@
     def cell_call(self, row, col, props=None, displaytime=False, format='text/html'):
         etype = self.rset.description[row][col]
         value = self.rset.rows[row][col]
-       
+
         if etype == 'String':
             entity, rtype = self.rset.related_entity(row, col)
             if entity is not None:
--- a/web/views/editforms.py	Tue May 12 20:18:19 2009 +0200
+++ b/web/views/editforms.py	Tue May 12 20:31:35 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,26 +89,59 @@
 
     # 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='textoutofcontext', default=u''):
         """display field to edit entity's `rtype` relation on double-click"""
         rschema = self.schema.rschema(rtype)
         entity = self.entity(row, col)
         if rschema.is_final():
             if getattr(entity, rtype) is None:
-                value = self.req._('not specified')
+                value = default or self.req._('not specified')
             else:
                 value = entity.printable_value(rtype)
         else:
             rset = entity.related(rtype, role)
-            value = self.view(vid, rset, 'null')
+            value = self.view(vid, rset, 'null') or default
         if not entity.has_perm('update'):
             self.w(value)
             return
+        if rschema.is_final():
+            form = self._build_attribute_form(entity, value, rtype, role, reload, row, col)
+        else:
+            form = self._build_relation_form(entity, value, rtype, role, row, col, vid)
+        form.form_add_hidden(u'__maineid', entity.eid)
+        renderer = FormRenderer(display_label=False, display_help=False,
+                                display_fields=[(rtype, role)],
+                                table_class='', button_bar_class='buttonbar',
+                                display_progress_div=False)
+        self.w(form.form_render(renderer=renderer))
+
+    def _build_relation_form(self, entity, value, rtype, role, row, col, vid):
+        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, 'vid' : vid}
+        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', '%(vid)s');" % event_data),
+                                form_buttons=[SubmitButton(),
+                                              Button(stdmsgs.BUTTON_CANCEL,
+                                                     onclick="cancelInlineEdit(%s,\'%s\',\'%s\')" %\
+                                                         (entity.eid, rtype, divid))])
+        form.append_field(RelationField(name=rtype, sort=True,
+                                        widget=Select(),
+                                        label=u' '))
+        self.w(tags.div(value, klass='editableField', id=divid,
+                        ondblclick=self.ondblclick % event_data))
+        return form
+
+    def _build_attribute_form(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,14 +155,9 @@
                                        domid='%s-form' % divid, action='#',
                                        cssstyle='display: none',
                                        onsubmit=self.onsubmit % event_data)
-        form.form_add_hidden(u'__maineid', entity.eid)
-        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))
+        return form
 
 
 class EditionFormView(FormViewMixIn, EntityView):