[ui] Backport and adapt code from SAEM to hide annotation behind the cardinality field
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 08 Jul 2016 16:43:14 +0200
changeset 1406 2d0c34a4169d
parent 1405 c8bc72d54540
child 1407 c52594a26fcc
[ui] Backport and adapt code from SAEM to hide annotation behind the cardinality field There is now a pop-up allowing to edit the annotation. If some annotation exists, pop-up is displayed open from the start. Disable this functionnality for first level content types, because for them it feels better to see the annotation as a first level attribute. Existing views already display the annotation properly when it exists. Closes #14157055
data/cubes.seda.css
data/cubes.seda.js
i18n/en.po
i18n/fr.po
views/__init__.py
views/widgets.py
xsd2uicfg.py
--- a/data/cubes.seda.css	Fri Jul 08 16:31:32 2016 +0200
+++ b/data/cubes.seda.css	Fri Jul 08 16:43:14 2016 +0200
@@ -16,4 +16,9 @@
 
 div.btn-group {
   margin-bottom: 1em;
-}
\ No newline at end of file
+}
+
+.metaField .control-label {
+  font-weight: normal;
+  width: 20%;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/data/cubes.seda.js	Fri Jul 08 16:43:14 2016 +0200
@@ -0,0 +1,29 @@
+// copyright 2015-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+// contact http://www.logilab.fr -- mailto:contact@logilab.fr
+//
+// This program is free software: you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License as published by the Free
+// Software Foundation, either version 2.1 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+// details.
+//
+// You should have received a copy of the GNU Lesser General Public License along
+// with this program. If not, see <http://www.gnu.org/licenses/>.
+
+seda = {
+    toggleFormMetaVisibility: function(domid) {
+        $node = cw.jqNode(domid);
+        $node.toggleClass('hidden');
+        $a = $node.parent().children('a');
+        if ($a.attr('class') == 'icon-list-add') {
+            $a.attr('class', 'icon-up-open');
+        } else {
+            $a.attr('class', 'icon-list-add');
+        }
+    },
+
+};
--- a/i18n/en.po	Fri Jul 08 16:31:32 2016 +0200
+++ b/i18n/en.po	Fri Jul 08 16:43:14 2016 +0200
@@ -7248,6 +7248,9 @@
 msgid "service_level"
 msgstr ""
 
+msgid "show/hide meta fields"
+msgstr ""
+
 msgid "source"
 msgstr ""
 
--- a/i18n/fr.po	Fri Jul 08 16:31:32 2016 +0200
+++ b/i18n/fr.po	Fri Jul 08 16:43:14 2016 +0200
@@ -7256,6 +7256,9 @@
 msgid "service_level"
 msgstr ""
 
+msgid "show/hide meta fields"
+msgstr "afficher / cacher le champ d'aide à la saisie"
+
 msgid "source"
 msgstr "valeur"
 
--- a/views/__init__.py	Fri Jul 08 16:31:32 2016 +0200
+++ b/views/__init__.py	Fri Jul 08 16:43:14 2016 +0200
@@ -20,18 +20,22 @@
 
 from cubicweb import tags, neg_role
 from cubicweb.utils import UStringIO
-from cubicweb.web import formwidgets as fw
+from cubicweb.web import formwidgets as fw, formfields as ff
 # XXX alias to avoid side effect: uicfg will become our uicfg submodule. Not necessary with 3.23?
 from cubicweb.web.views import uicfg as cwuicfg
 
 from cubes.seda.xsd import XSDM_MAPPING
+from cubes.seda.xsd2uicfg import FIRST_LEVEL_ETYPES
+from cubes.seda.views.widgets import SEDAMetaField
 
 _ = unicode
 
 pvs = cwuicfg.primaryview_section
 afs = cwuicfg.autoform_section
+aff = cwuicfg.autoform_field
 affk = cwuicfg.autoform_field_kwargs
 rec = cwuicfg.reledit_ctrl
+
 pvs.tag_subject_of(('*', 'container', '*'), 'hidden')
 pvs.tag_object_of(('*', 'container', '*'), 'hidden')
 afs.tag_subject_of(('*', 'container', '*'), 'main', 'hidden')
@@ -41,6 +45,12 @@
 pvs.tag_subject_of(('*', 'scheme_relation_type', '*'), 'hidden')
 pvs.tag_subject_of(('*', 'scheme_entity_type', '*'), 'hidden')
 
+aff.tag_attribute(('*', 'user_cardinality'), SEDAMetaField)
+afs.tag_attribute(('*', 'user_annotation'), 'main', 'hidden')
+for etype in FIRST_LEVEL_ETYPES:
+    aff.tag_attribute((etype, 'user_cardinality'), ff.StringField)
+    afs.tag_attribute((etype, 'user_annotation'), 'main', 'attributes')
+
 
 def rtags_from_xsd_element(etype, element_name):
     """Return primary view section and display control rtags, generated from information in the XSD
--- a/views/widgets.py	Fri Jul 08 16:31:32 2016 +0200
+++ b/views/widgets.py	Fri Jul 08 16:43:14 2016 +0200
@@ -17,7 +17,7 @@
 
 from logilab.common.decorators import monkeypatch
 
-from cubicweb import tags
+from cubicweb import tags, utils
 from cubicweb.uilib import js
 from cubicweb.web import formfields as ff, formwidgets as fw
 from cubicweb.web.views import ajaxcontroller, formrenderers
@@ -174,3 +174,60 @@
         w(u' [<a href="javascript: %(removejs)s;$.noop();">%(removemsg)s</a>]'
           % values)
     w(u'</div>')
+
+
+class SEDAMetaField(ff.StringField):
+    """Extends user_cardinality field to allow edition of a `user_annotation` all together.
+    """
+
+    @staticmethod
+    def annotation_field(form):
+        """Return the field for `user_annotation` entity. """
+        eschema = form.edited_entity.e_schema
+        rschema = eschema.subjrels['user_annotation']
+        return ff.guess_field(eschema, rschema, 'subject', form._cw)
+
+    def actual_fields(self, form):
+        """Overriden from :class:`Field`"""
+        yield self
+        yield self.annotation_field(form)
+
+    def render(self, form, renderer):
+        """Overriden from :class:`Field` to render fields in a div which is hidden by default"""
+        form._cw.add_js('cubes.seda.js')
+        wdgs = [self.get_widget(form).render(form, self, renderer)]
+        self._render_hidden_section(wdgs, form, renderer)
+        return u'\n'.join(wdgs)
+
+    def _render_hidden_section(self, wdgs, form, renderer):
+        divid = '%s-advanced' % self.input_name(form)
+        if form.edited_entity.has_eid() and form.edited_entity.user_annotation:
+            hidden = ''
+            icon = 'icon-up-open'
+        else:
+            hidden = ' hidden'
+            icon = 'icon-list-add'
+        wdgs.append(tags.a(u'', onclick=unicode(js.seda.toggleFormMetaVisibility(divid)),
+                           href='javascript:$.noop()', title=form._cw._('show/hide meta fields'),
+                           klass=icon + ' metaFieldSwitch'))
+        wdgs.append(u'<div id="{0}" class="metaField{1}">'.format(divid, hidden))
+        wdgs.append(self._render_subfield(form, self.annotation_field(form), renderer))
+        wdgs.append(u'</div>')
+
+    @staticmethod
+    def _render_subfield(form, field, renderer):
+        """Render a sub-field: label + widget + help + EOL"""
+        data = utils.UStringIO()
+        w = data.write
+        w(u'<div class="row">')
+        w(renderer.render_label(form, field))
+        w(u'<div class="col-md-9">')
+        w(field.render(form, renderer))
+        w(u'</div>')
+        w(u'</div>')
+        w(u'<div class="row">')
+        w(u'<div class="col-md-offset-3 col-md-9">')
+        w(renderer.render_help(form, field))
+        w(u'</div>')
+        w(u'</div>')
+        return data.getvalue()
--- a/xsd2uicfg.py	Fri Jul 08 16:31:32 2016 +0200
+++ b/xsd2uicfg.py	Fri Jul 08 16:43:14 2016 +0200
@@ -25,8 +25,10 @@
 from xsd2yams import CodeGenerator
 
 
-LEVEL_ETYPES = set(('SEDABinaryDataObjectType', 'SEDAPhysicalDataObjectType',
-                    'SEDAArchiveUnitType'))
+FIRST_LEVEL_ETYPES = set(('SEDAArchiveTransfer',
+                          'SEDABinaryDataObject', 'SEDAPhysicalDataObject',
+                          'SEDAArchiveUnit',
+                          'SEDAContent'))
 
 RTYPES_IN_TAB = set((
     'seda_binary_data_object',