web/views/ajaxedit.py
branchstable
changeset 6924 b88221afe491
parent 6497 84fbd09f8c6b
child 6927 340aba0c8c9e
--- a/web/views/ajaxedit.py	Mon Jan 31 17:24:05 2011 +0100
+++ b/web/views/ajaxedit.py	Mon Jan 31 17:28:51 2011 +0100
@@ -22,9 +22,9 @@
 from cubicweb import role
 from cubicweb.view import View
 from cubicweb.selectors import match_form_params, match_kwargs
-from cubicweb.web.component import EditRelationMixIn
+from cubicweb.web import component, stdmsgs, formwidgets as fw
 
-class AddRelationView(EditRelationMixIn, View):
+class AddRelationView(component.EditRelationMixIn, View):
     """base class for view which let add entities linked by a given relation
 
     subclasses should define at least id, rtype and target class attributes.
@@ -36,7 +36,7 @@
     cw_property_defs = {} # don't want to inherit this from Box
     expected_kwargs = form_params = ('rtype', 'target')
 
-    build_js = EditRelationMixIn.build_reload_js_call
+    build_js = component.EditRelationMixIn.build_reload_js_call
 
     def cell_call(self, row, col, rtype=None, target=None, etype=None):
         self.rtype = rtype or self._cw.form['rtype']
@@ -73,3 +73,41 @@
             self.pagination(self._cw, rset, w=self.w)
             return rset.entities()
         super(AddRelationView, self).unrelated_entities(self)
+
+
+def ajaxCompositeForm(container, entity, rtype, okjs, canceljs,
+                      entityfkwargs=None):
+    """
+    * if entity is None, edit container (assert container.has_eid())
+    * if entity has not eid, will be created
+    * if container has not eid, will be created (see vcreview InsertionPoint)
+    """
+    req = container._cw
+    parentexists = entity is None or container.has_eid()
+    buttons = [fw.Button(onclick=okjs),
+               fw.Button(stdmsgs.BUTTON_CANCEL, onclick=canceljs)]
+    freg = req.vreg['forms']
+    # main form kwargs
+    mkwargs = dict(action='#', domid='%sForm%s' % (rtype, container.eid),
+                   form_buttons=buttons,
+                   onsubmit='javascript: %s; return false' % okjs)
+    # entity form kwargs
+    # use formtype=inlined to skip the generic relations edition section
+    fkwargs = dict(entity=entity or container, formtype='inlined')
+    if entityfkwargs is not None:
+        fkwargs.update(entityfkwargs)
+    # form values
+    formvalues = {}
+    if entity is not None: # creation
+        formvalues[rtype] = container.eid
+    if parentexists: # creation / edition
+        mkwargs.update(fkwargs)
+        # use formtype=inlined to avoid viewing the relation edition section
+        form = freg.select('edition', req, **mkwargs)
+    else: # creation of both container and comment entities
+        form = freg.select('composite', req, form_renderer_id='default',
+                            **mkwargs)
+        form.add_subform(freg.select('edition', req, entity=container,
+                                      mainform=False, mainentity=True))
+        form.add_subform(freg.select('edition', req, mainform=False, **fkwargs))
+    return form, formvalues