[reledit] have a link-free landing zone for mouse-clicks (closes #343544) stable
authorAurelien Campeas <aurelien.campeas@logilab.fr>
Wed, 08 Jul 2009 19:02:29 +0200
branchstable
changeset 2330 8c70ca715fe9
parent 2329 8b5a1af6dc35
child 2331 4c02a761d879
[reledit] have a link-free landing zone for mouse-clicks (closes #343544)
web/data/cubicweb.edition.js
web/views/basecontrollers.py
web/views/editforms.py
--- a/web/data/cubicweb.edition.js	Wed Jul 08 17:50:42 2009 +0200
+++ b/web/data/cubicweb.edition.js	Wed Jul 08 19:02:29 2009 +0200
@@ -454,7 +454,8 @@
  * @param eid : the eid of the entity being edited
  * @param reload: boolean to reload page if true (when changing URL dependant data)
  */
-function inlineValidateAttributeForm(formid, rtype, eid, divid, reload, default_value) {
+function inlineValidateAttributeForm(formid, rtype, eid, divid, reload,
+                                     default_value, lzone) {
     try {
 	var form = getNode(formid);
 	if (typeof FCKeditorAPI != "undefined") {
@@ -467,7 +468,7 @@
 	}
 	var zipped = formContents(form);
 	var d = asyncRemoteExec('edit_field', 'apply', zipped[0], zipped[1],
-                                rtype, eid, default_value);
+                                rtype, eid, default_value, lzone);
     } catch (ex) {
 	log('got exception', ex);
 	return false;
@@ -494,14 +495,15 @@
     return false;
 }
 
-function inlineValidateRelationForm(formid, rtype, role, eid, divid, vid, default_value) {
+function inlineValidateRelationForm(formid, rtype, role, eid, divid, vid,
+                                    default_value, escape, lzone) {
     try {
 	var form = getNode(formid);
         var relname = rtype + ':' + eid;
         var newtarget = jQuery('[name=' + relname + ']').val();
 	var zipped = formContents(form);
 	var d = asyncRemoteExec('edit_relation', 'apply', zipped[0], zipped[1], rtype, role,
-                                eid, vid, default_value);
+                                eid, vid, default_value, escape, lzone);
     } catch (ex) {
 	log('got exception', ex);
 	return false;
--- a/web/views/basecontrollers.py	Wed Jul 08 17:50:42 2009 +0200
+++ b/web/views/basecontrollers.py	Wed Jul 08 19:02:29 2009 +0200
@@ -388,7 +388,7 @@
         return _validate_form(self.req, self.vreg)
 
     @jsonize
-    def js_edit_field(self, action, names, values, rtype, eid, default):
+    def js_edit_field(self, action, names, values, rtype, eid, default, lzone):
         success, args = self.validate_form(action, names, values)
         if success:
             # Any X,N where we don't seem to use N is an optimisation
@@ -396,25 +396,25 @@
             rset = self.req.execute('Any X,N WHERE X eid %%(x)s, X %s N' % rtype,
                                     {'x': eid}, 'x')
             entity = rset.get_entity(0, 0)
-            value = entity.printable_value(rtype)
-            return (success, args, value or default)
+            value = entity.printable_value(rtype) or default
+            return (success, args, lzone + value)
         else:
             return (success, args, None)
 
     @jsonize
-    def js_edit_relation(self, action, names, values,
-                         rtype, role, eid, vid, default):
+    def js_edit_relation(self, action, names, values, rtype,
+                         role, eid, vid, default, escape, lzone):
         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)
             if rset:
                 output = self.view(vid, rset)
-                if vid == 'textoutofcontext':
+                if escape == 'True':
                     output = xml_escape(output)
             else:
                 output = default
-            return (success, args, output)
+            return (success, args, lzone + output)
         else:
             return (success, args, None)
 
--- a/web/views/editforms.py	Wed Jul 08 17:50:42 2009 +0200
+++ b/web/views/editforms.py	Wed Jul 08 19:02:29 2009 +0200
@@ -91,21 +91,26 @@
     # FIXME editableField class could be toggleable from userprefs
 
     _ondblclick = "showInlineEditionForm(%(eid)s, '%(rtype)s', '%(divid)s')"
+    _defaultlandingzone = u'<img title="%s" class="needsvalidation" src="data/file.gif"/>'
+    _landingzonemsg = _('double click to edit this field')
 
     def cell_call(self, row, col, rtype=None, role='subject',
                   reload=False,             # controls reloading the whole page after change
                   rvid='textoutofcontext',  # vid to be applied to other side of rtype
                   escape=True,              # depending on the vid, will xml_escape or not
-                  default=None              # default value
+                  default=None,             # default value
+                  landing_zone=None         # prepend value with a separate html element to click onto
+                                            # (esp. needed when values are links)
                   ):
         """display field to edit entity's `rtype` relation on double-click"""
         assert rtype
         rschema = self.schema.rschema(rtype)
         entity = self.entity(row, col)
         if not default:
-            default = xml_escape(self.req._('not specified'))
+            default = xml_escape(self.req._('<no value>'))
         if rschema.is_final():
             value = entity.printable_value(rtype)
+            value = value.strip() or default
             if not entity.has_perm('update'):
                 self.w(value)
                 return
@@ -124,15 +129,15 @@
                                                        toeid=entity.eid):
             self.w(value)
             return
-        if not value.strip():
-            value = default
         # build form.
+        landing_zone = landing_zone or self._defaultlandingzone % self.req._(self._landingzonemsg)
+        value = landing_zone + value
         if rschema.is_final():
-            form = self._build_attribute_form(entity, value, rtype, role,
-                                              reload, row, col, default)
+            form = self._build_attribute_form(entity, value, rtype, role, reload,
+                                              row, col, default, landing_zone)
         else:
-            form = self._build_relation_form(entity, value, rtype, role,
-                                             row, col, rvid, default)
+            form = self._build_relation_form(entity, value, rtype, role, row, col,
+                                             rvid, default, escape, landing_zone)
         renderer = self.vreg.select_object('formrenderers', 'base', self.req,
                                       entity=entity,
                                       display_label=False, display_help=False,
@@ -141,12 +146,13 @@
                                       display_progress_div=False)
         self.w(form.form_render(renderer=renderer))
 
-    def _build_relation_form(self, entity, value, rtype, role, row, col, vid, default):
+    def _build_relation_form(self, entity, value, rtype, role, row, col, rvid, default, escape, lzone):
+        print rvid, escape
         divid = 'd%s' % make_uid('%s-%s' % (rtype, entity.eid))
-        event_data = {'divid' : divid, 'eid' : entity.eid, 'rtype' : rtype, 'vid' : vid,
-                      'default' : default, 'role' : role}
+        event_data = {'divid' : divid, 'eid' : entity.eid, 'rtype' : rtype, 'vid' : rvid,
+                      'default' : default, 'role' : role, 'escape' : escape, 'lzone' : lzone}
         onsubmit = ("return inlineValidateRelationForm('%(divid)s-form', '%(rtype)s', "
-                    "'%(role)s', '%(eid)s', '%(divid)s', '%(vid)s', '%(default)s');"
+                    "'%(role)s', '%(eid)s', '%(divid)s', '%(vid)s', '%(default)s', '%(escape)s', '%(lzone)s');"
                     % event_data)
         cancelclick = "cancelInlineEdit(%s,\'%s\',\'%s\')" % (
             entity.eid, rtype, divid)
@@ -162,13 +168,13 @@
                         ondblclick=self._ondblclick % event_data))
         return form
 
-    def _build_attribute_form(self, entity, value, rtype, role, reload, row, col, default):
+    def _build_attribute_form(self, entity, value, rtype, role, reload, row, col, default, lzone):
         eid = entity.eid
         divid = 'd%s' % make_uid('%s-%s' % (rtype, eid))
         event_data = {'divid' : divid, 'eid' : eid, 'rtype' : rtype,
-                      'reload' : dumps(reload), 'default' : default}
+                      'reload' : dumps(reload), 'default' : default, 'lzone' : lzone}
         onsubmit = ("return inlineValidateAttributeForm('%(divid)s-form', '%(rtype)s', "
-                    "'%(eid)s', '%(divid)s', %(reload)s, '%(default)s');")
+                    "'%(eid)s', '%(divid)s', %(reload)s, '%(default)s', '%(lzone)s');")
         buttons = [SubmitButton(stdmsgs.BUTTON_OK),
                    Button(stdmsgs.BUTTON_CANCEL,
                           onclick="cancelInlineEdit(%s,\'%s\',\'%s\')" % (