[facets/table] vertically group facets by two when possible in order to have a more compact horizontal layout in table filter form
authorAurelien Campeas <aurelien.campeas@logilab.fr>
Thu, 19 May 2011 16:39:24 +0200
changeset 7402 826e5663a686
parent 7400 2391a6f526bf
child 7406 e772a2c57b00
[facets/table] vertically group facets by two when possible in order to have a more compact horizontal layout in table filter form
web/data/cubicweb.facets.css
web/facet.py
web/views/facets.py
web/views/tableview.py
--- a/web/data/cubicweb.facets.css	Thu May 19 11:50:54 2011 +0200
+++ b/web/data/cubicweb.facets.css	Thu May 19 16:39:24 2011 +0200
@@ -121,3 +121,13 @@
 div.facetTitleSelected {
   background: url("required.png") no-repeat right top;
 }
+
+table.filter {
+  background-color: #EBE8D9;
+  border: dotted grey 1px;
+}
+
+div.facet {
+ padding: none;
+ margin: .3em!important;
+}
--- a/web/facet.py	Thu May 19 11:50:54 2011 +0200
+++ b/web/facet.py	Thu May 19 16:39:24 2011 +0200
@@ -1,4 +1,4 @@
-# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of CubicWeb.
@@ -405,6 +405,10 @@
         """
         raise NotImplementedError
 
+    @property
+    def wdgclass(self):
+        raise NotImplementedError
+
 
 class VocabularyFacet(AbstractFacet):
     """This abstract class extend :class:`AbstractFacet` to use the
@@ -418,6 +422,10 @@
     """
     needs_update = True
 
+    @property
+    def wdgclass(self):
+        return FacetVocabularyWidget
+
     def get_widget(self):
         """Return the widget instance to use to display this facet.
 
@@ -427,7 +435,7 @@
         vocab = self.vocabulary()
         if len(vocab) <= 1:
             return None
-        wdg = FacetVocabularyWidget(self)
+        wdg = self.wdgclass(self)
         selected = frozenset(typed_eid(eid) for eid in self._cw.list_form_param(self.__regid__))
         for label, value in vocab:
             if value is None:
@@ -1051,6 +1059,9 @@
         self.facet = facet
         self.items = []
 
+    def height(self):
+        return len(self.items) + 1
+
     def append(self, item):
         self.items.append(item)
 
@@ -1084,6 +1095,9 @@
         self.facet = facet
         self.value = None
 
+    def height(self):
+        return 2
+
     def _render(self):
         title = xml_escape(self.facet.title)
         facetid = xml_escape(self.facet.__regid__)
@@ -1124,6 +1138,9 @@
         self.minvalue = minvalue
         self.maxvalue = maxvalue
 
+    def height(self):
+        return 3
+
     def _render(self):
         facet = self.facet
         facet._cw.add_js('jquery.ui.js')
@@ -1214,6 +1231,9 @@
         self.value = value
         self.selected = selected
 
+    def height(self):
+        return 1
+
     def _render(self):
         title = xml_escape(self.facet.title)
         facetid = xml_escape(self.facet.__regid__)
--- a/web/views/facets.py	Thu May 19 11:50:54 2011 +0200
+++ b/web/views/facets.py	Thu May 19 16:39:24 2011 +0200
@@ -1,4 +1,4 @@
-# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of CubicWeb.
@@ -203,6 +203,11 @@
     rtype = 'has_text'
     role = 'subject'
     order = 0
+
+    @property
+    def wdgclass(self):
+        return facet.FacetStringWidget
+
     @property
     def title(self):
         return self._cw._('has_text')
@@ -213,7 +218,7 @@
         default implentation expects a .vocabulary method on the facet and
         return a combobox displaying this vocabulary
         """
-        return facet.FacetStringWidget(self)
+        return self.wdgclass(self)
 
     def add_rql_restrictions(self):
         """add restriction for this facet into the rql syntax tree"""
--- a/web/views/tableview.py	Thu May 19 11:50:54 2011 +0200
+++ b/web/views/tableview.py	Thu May 19 16:39:24 2011 +0200
@@ -1,4 +1,4 @@
-# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of CubicWeb.
@@ -22,7 +22,7 @@
 
 from logilab.mtconverter import xml_escape
 
-from cubicweb.selectors import nonempty_rset, match_form_params
+from cubicweb.selectors import nonempty_rset
 from cubicweb.utils import make_uid, json_dumps
 from cubicweb.view import EntityView, AnyRsetView
 from cubicweb import tags
@@ -31,6 +31,7 @@
 from cubicweb.web.component import Link
 from cubicweb.web.htmlwidgets import (TableWidget, TableColumn, MenuWidget,
                                       PopupBoxMenu)
+from cubicweb.web import facet
 from cubicweb.web.facet import prepare_facets_rqlst, filter_hiddens
 
 class TableView(AnyRsetView):
@@ -42,6 +43,7 @@
     __regid__ = 'table'
     title = _('table')
     finalview = 'final'
+    wdg_stack_size = 8
 
     def form_filter(self, divid, displaycols, displayactions, displayfilter,
                     paginate, hidden=True):
@@ -81,12 +83,30 @@
         w(u'<input type="hidden" name="fromformfilter" value="1" />')
         filter_hiddens(w, facets=','.join(wdg.facet.__regid__ for wdg in fwidgets),
                        baserql=baserql)
+        # sort by vocab size
+        fwidgets.sort(key=lambda x: x.height())
         w(u'<table class="filter">\n')
+        widget_queue = []
+        queue_size = 0
+        widget_qty = len(fwidgets)
         w(u'<tr>\n')
         for wdg in fwidgets:
+            height = wdg.height()
+            if queue_size + height <= self.wdg_stack_size:
+                widget_queue.append(wdg)
+                queue_size += height
+                continue
             w(u'<td>')
-            wdg.render(w=w)
-            w(u'</td>\n')
+            for queued in widget_queue:
+                queued.render(w=w)
+            w(u'</td>')
+            widget_queue = [wdg]
+            queue_size = height
+        if widget_queue:
+            w(u'<td>')
+            for queued in widget_queue:
+                queued.render(w=w)
+            w(u'</td>')
         w(u'</tr>\n')
         w(u'</table>\n')
         w(u'</fieldset>\n')