# HG changeset patch # User Sylvain Thénault # Date 1309526804 -7200 # Node ID 01b49ec8fe80facd1e3af737b033639ca07857bb # Parent 48abeac162fd6e587bbddef458d6eb53789b5d87 [facets] allow to specify on which rqlst's variable facets should be applied. Closes #1795915 diff -r 48abeac162fd -r 01b49ec8fe80 web/data/cubicweb.facets.js --- a/web/data/cubicweb.facets.js Fri Jul 01 15:26:43 2011 +0200 +++ b/web/data/cubicweb.facets.js Fri Jul 01 15:26:44 2011 +0200 @@ -102,7 +102,13 @@ 'ctxcomponents', 'breadcrumbs')); } } - var d = loadRemote('json', ajaxFuncArgs('filter_select_content', null, toupdate, rql)); + var mainvar = null; + var index = jQuery.inArray('mainvar', zipped[0]); + if (index > - 1) { + mainvar = zipped[1][index]; + } + + var d = loadRemote('json', ajaxFuncArgs('filter_select_content', null, toupdate, rql, mainvar)); d.addCallback(function(updateMap) { for (facetId in updateMap) { var values = updateMap[facetId]; diff -r 48abeac162fd -r 01b49ec8fe80 web/views/basecontrollers.py --- a/web/views/basecontrollers.py Fri Jul 01 15:26:43 2011 +0200 +++ b/web/views/basecontrollers.py Fri Jul 01 15:26:44 2011 +0200 @@ -504,8 +504,11 @@ facetbase.prepare_select(select, filtered_variable) update_map = {} for facetid in facetids: - facet = get_facet(self._cw, facetid, rqlst.children[0], mainvar) - update_map[facetid] = facet.possible_values() + facet = facetbase.get_facet(self._cw, facetid, select, filtered_variable) + try: + update_map[facetid] = facet.possible_values() + except facetbase.DontUpdateFacet: + continue return update_map def js_unregister_user_callback(self, cbname): diff -r 48abeac162fd -r 01b49ec8fe80 web/views/facets.py --- a/web/views/facets.py Fri Jul 01 15:26:43 2011 +0200 +++ b/web/views/facets.py Fri Jul 01 15:26:44 2011 +0200 @@ -28,7 +28,7 @@ from cubicweb.utils import json_dumps from cubicweb.web import component, facet as facetbase -def facets(req, rset, context): +def facets(req, rset, context, mainvar=None): """return the base rql and a list of widgets for facets applying to the given rset/context (cached version) """ @@ -37,29 +37,29 @@ except AttributeError: cache = req.__rset_facets = {} try: - return cache[(rset, context)] + return cache[(rset, context, mainvar)] except KeyError: - facets = cache[(rset, context)] = _facets(req, rset, context) + facets = _facets(req, rset, context, mainvar) + cache[(rset, context, mainvar)] = facets return facets -def _facets(req, rset, context): +def _facets(req, rset, context, mainvar): """return the base rql and a list of widgets for facets applying to the given rset/context """ # XXX done by selectors, though maybe necessary when rset has been hijacked # (e.g. contextview_selector matched) - rqlst = rset.syntax_tree() + origqlst = rset.syntax_tree() # union not yet supported - if len(rqlst.children) != 1: + if len(origqlst.children) != 1: return None, () - rqlst = rqlst.copy() - vreg = req.vreg - vreg.rqlhelper.annotate(rqlst) - mainvar, baserql = facetbase.prepare_facets_rqlst(rqlst, rset.args) - wdgs = [facet.get_widget() for facet in vreg['facets'].poss_visible_objects( - req, rset=rset, rqlst=rqlst.children[0], context=context, - filtered_variable=mainvar)] - return baserql, [wdg for wdg in wdgs if wdg is not None] + rqlst = origqlst.copy() + select = rqlst.children[0] + filtered_variable, baserql = facetbase.init_facets(rset, select, mainvar) + wdgs = [(facet, facet.get_widget()) for facet in req.vreg['facets'].poss_visible_objects( + req, rset=rset, rqlst=origqlst, select=select, context=context, + filtered_variable=filtered_variable)] + return baserql, [wdg for facet, wdg in wdgs if wdg is not None] @objectify_selector @@ -70,10 +70,10 @@ return 0 @objectify_selector -def has_facets(cls, req, rset=None, **kwargs): +def has_facets(cls, req, rset=None, mainvar=None, **kwargs): if rset is None: return 0 - return len(facets(req, rset, cls.__regid__)[1]) + return len(facets(req, rset, cls.__regid__, mainvar)[1]) def filter_hiddens(w, baserql, wdgs, **kwargs): @@ -92,7 +92,8 @@ def generate_form(self, w, rset, divid, vid, vidargs, paginate=False, cssclass='', **hiddens): """display a form to filter some view's content""" - baserql, wdgs = facets(self._cw, rset, self.__regid__) + mainvar = self.cw_extra_kwargs.get('mainvar') + baserql, wdgs = facets(self._cw, rset, self.__regid__, mainvar) if not wdgs: # may happen in contextview_selector matched return self._cw.add_js(self.needs_js) @@ -108,6 +109,8 @@ w(u'
' % (divid, cssclass, facetargs)) w(u'
') + if mainvar: + hiddens['mainvar'] = mainvar filter_hiddens(w, baserql, wdgs, **hiddens) self.layout_widgets(w, self.sorted_widgets(wdgs)) w(u'
\n')