web/views/tableview.py
changeset 7600 75d208ab8444
parent 7415 777c47312bc7
child 7801 67f9677c1253
child 7814 1ec9fe1dfba9
--- a/web/views/tableview.py	Fri Jul 01 12:48:39 2011 +0200
+++ b/web/views/tableview.py	Fri Jul 01 13:39:45 2011 +0200
@@ -15,110 +15,48 @@
 #
 # You should have received a copy of the GNU Lesser General Public License along
 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
-"""generic table view, including filtering abilities"""
+"""generic table view, including filtering abilities using facets"""
 
 __docformat__ = "restructuredtext en"
 _ = unicode
 
 from logilab.mtconverter import xml_escape
 
+from cubicweb import NoSelectableObject, tags
 from cubicweb.selectors import nonempty_rset
 from cubicweb.utils import make_uid, json_dumps
 from cubicweb.view import EntityView, AnyRsetView
-from cubicweb import tags
 from cubicweb.uilib import toggle_action, limitsize, htmlescape
-from cubicweb.web import jsonize
-from cubicweb.web.component import Link
+from cubicweb.web import jsonize, component, facet
 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):
-    """The table view accepts any non-empty rset. It uses
-    introspection on the result set to compute column names and the
-    proper way to display the cells.
+    """The table view accepts any non-empty rset. It uses introspection on the
+    result set to compute column names and the proper way to display the cells.
+
     It is however highly configurable and accepts a wealth of options.
     """
     __regid__ = 'table'
     title = _('table')
     finalview = 'final'
-    wdg_stack_size = 8
 
     def form_filter(self, divid, displaycols, displayactions, displayfilter,
                     paginate, hidden=True):
-        rqlst = self.cw_rset.syntax_tree()
-        # union not yet supported
-        if len(rqlst.children) != 1:
+        try:
+            filterform = self._cw.vreg['views'].select(
+                'facet.filtertable', self._cw, rset=self.cw_rset)
+        except NoSelectableObject:
             return ()
-        rqlst = rqlst.copy()
-        self._cw.vreg.rqlhelper.annotate(rqlst)
-        mainvar, baserql = prepare_facets_rqlst(rqlst, self.cw_rset.args)
-        wdgs = [facet.get_widget() for facet in self._cw.vreg['facets'].poss_visible_objects(
-            self._cw, rset=self.cw_rset, rqlst=rqlst.children[0], context='tablefilter',
-            filtered_variable=mainvar)]
-        wdgs = [wdg for wdg in wdgs if wdg is not None]
-        if wdgs:
-            self._generate_form(divid, baserql, wdgs, hidden,
-                               vidargs={'paginate': paginate,
-                                        'displaycols': displaycols,
-                                        'displayactions': displayactions,
-                                        'displayfilter': displayfilter})
-            return self.show_hide_actions(divid, not hidden)
-        return ()
-
-    def _generate_form(self, divid, baserql, fwidgets, hidden=True, vidargs={}):
-        """display a form to filter table's content. This should only
-        occur when a context eid is given
-        """
-        w = self.w
-        self._cw.add_css('cubicweb.facets.css')
-        self._cw.add_js( ('cubicweb.ajax.js', 'cubicweb.facets.js'))
-        self._cw.html_headers.define_var('facetLoadingMsg',
-                                         self._cw._('facet-loading-msg'))
-        # drop False / None values from vidargs
-        vidargs = dict((k, v) for k, v in vidargs.iteritems() if v)
-        w(u'<form method="post" cubicweb:facetargs="%s" action="">' %
-          xml_escape(json_dumps([divid, self.__regid__, False, vidargs])))
-        w(u'<fieldset id="%sForm" class="%s">' % (divid, hidden and 'hidden' or ''))
-        w(u'<input type="hidden" name="divid" value="%s" />' % divid)
-        w(u'<input type="hidden" name="fromformfilter" value="1" />')
-        filter_hiddens(w, facets=','.join(wdg.facet.__regid__ for wdg in fwidgets),
-                       baserql=baserql)
-        self._build_form_table(fwidgets)
-
-    def _facet_widget_sort(self, fwidgets):
-        fwidgets.sort(key=lambda x: x.height())
-
-    def _build_form_table(self, fwidgets):
-        # sort by widget height
-        w = self.w
-        self._facet_widget_sort(fwidgets)
-        w(u'<table class="filter">\n')
-        widget_queue = []
-        queue_size = 0
-        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>')
-            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')
-        w(u'</form>\n')
+        vidargs = {'paginate': paginate,
+                   'displaycols': displaycols,
+                   'displayactions': displayactions,
+                   'displayfilter': displayfilter}
+        cssclass = hidden and 'hidden' or ''
+        filterform.render(self.w, vid=self.__regid__, divid=divid,
+                          vidargs=vidargs, cssclass=cssclass)
+        return self.show_hide_actions(divid, not hidden)
 
     def main_var_index(self):
         """returns the index of the first non-attribute variable among the RQL
@@ -241,7 +179,7 @@
                             ident='%sActions' % divid)
         box.append(menu)
         for url, label, klass, ident in actions:
-            menu.append(Link(url, label, klass=klass, id=ident))
+            menu.append(component.Link(url, label, klass=klass, id=ident))
         box.render(w=self.w)
         self.w(u'<div class="clear"/>')