28 from cubicweb.utils import json_dumps |
28 from cubicweb.utils import json_dumps |
29 from cubicweb.web import component, facet as facetbase |
29 from cubicweb.web import component, facet as facetbase |
30 |
30 |
31 def facets(req, rset, context, mainvar=None): |
31 def facets(req, rset, context, mainvar=None): |
32 """return the base rql and a list of widgets for facets applying to the |
32 """return the base rql and a list of widgets for facets applying to the |
33 given rset/context (cached version) |
33 given rset/context (cached version of :func:`_facet`) |
|
34 |
|
35 :param req: A :class:`~cubicweb.req.RequestSessionBase` object |
|
36 :param rset: A :class:`~cubicweb.rset.ResultSet` |
|
37 :param context: A string that match the ``__regid__`` of a ``FacetFilter`` |
|
38 :param mainvar: A string that match a select var from the rset |
34 """ |
39 """ |
35 try: |
40 try: |
36 cache = req.__rset_facets |
41 cache = req.__rset_facets |
37 except AttributeError: |
42 except AttributeError: |
38 cache = req.__rset_facets = {} |
43 cache = req.__rset_facets = {} |
44 return facets |
49 return facets |
45 |
50 |
46 def _facets(req, rset, context, mainvar): |
51 def _facets(req, rset, context, mainvar): |
47 """return the base rql and a list of widgets for facets applying to the |
52 """return the base rql and a list of widgets for facets applying to the |
48 given rset/context |
53 given rset/context |
|
54 |
|
55 :param req: A :class:`~cubicweb.req.RequestSessionBase` object |
|
56 :param rset: A :class:`~cubicweb.rset.ResultSet` |
|
57 :param context: A string that match the ``__regid__`` of a ``FacetFilter`` |
|
58 :param mainvar: A string that match a select var from the rset |
49 """ |
59 """ |
|
60 ### initialisation |
50 # XXX done by selectors, though maybe necessary when rset has been hijacked |
61 # XXX done by selectors, though maybe necessary when rset has been hijacked |
51 # (e.g. contextview_selector matched) |
62 # (e.g. contextview_selector matched) |
52 origqlst = rset.syntax_tree() |
63 origqlst = rset.syntax_tree() |
53 # union not yet supported |
64 # union not yet supported |
54 if len(origqlst.children) != 1: |
65 if len(origqlst.children) != 1: |
|
66 req.debug('facette disabled on union request %s', origqlst) |
55 return None, () |
67 return None, () |
56 rqlst = origqlst.copy() |
68 rqlst = origqlst.copy() |
57 select = rqlst.children[0] |
69 select = rqlst.children[0] |
58 filtered_variable, baserql = facetbase.init_facets(rset, select, mainvar) |
70 filtered_variable, baserql = facetbase.init_facets(rset, select, mainvar) |
59 wdgs = [(facet, facet.get_widget()) for facet in req.vreg['facets'].poss_visible_objects( |
71 ### Selection |
60 req, rset=rset, rqlst=origqlst, select=select, context=context, |
72 possible_facets = req.vreg['facets'].poss_visible_objects( |
61 filtered_variable=filtered_variable)] |
73 req, |
|
74 rset=rset, |
|
75 rqlst=origqlst, |
|
76 select=select, |
|
77 context=context, |
|
78 filtered_variable=filtered_variable) |
|
79 wdgs = [(facet, facet.get_widget()) for facet in possible_facets] |
62 return baserql, [wdg for facet, wdg in wdgs if wdg is not None] |
80 return baserql, [wdg for facet, wdg in wdgs if wdg is not None] |
63 |
81 |
64 |
82 |
65 @objectify_selector |
83 @objectify_selector |
66 def contextview_selector(cls, req, rset=None, row=None, col=None, view=None, |
84 def contextview_selector(cls, req, rset=None, row=None, col=None, view=None, |
92 w(u'<input type="hidden" name="%s" value="%s" />' % ( |
110 w(u'<input type="hidden" name="%s" value="%s" />' % ( |
93 key, xml_escape(val))) |
111 key, xml_escape(val))) |
94 |
112 |
95 |
113 |
96 class FacetFilterMixIn(object): |
114 class FacetFilterMixIn(object): |
|
115 """Mixin Class to generate Facet Filter Form |
|
116 |
|
117 To generate the form, you need to explicitly the following methode with the |
|
118 right argument: |
|
119 |
|
120 .. automethod:: generate_form |
|
121 |
|
122 The most useful function to overwrite is: |
|
123 |
|
124 .. automethod:: layout_widgets |
|
125 |
|
126 """ |
97 needs_js = ['cubicweb.ajax.js', 'cubicweb.facets.js'] |
127 needs_js = ['cubicweb.ajax.js', 'cubicweb.facets.js'] |
98 needs_css = ['cubicweb.facets.css'] |
128 needs_css = ['cubicweb.facets.css'] |
99 roundcorners = True |
129 roundcorners = True |
100 |
130 |
101 def generate_form(self, w, rset, divid, vid, vidargs, |
131 def generate_form(self, w, rset, divid, vid, vidargs, |
102 paginate=False, cssclass='', **hiddens): |
132 paginate=False, cssclass='', **hiddens): |
103 """display a form to filter some view's content""" |
133 """display a form to filter some view's content |
|
134 |
|
135 :param w: Write function |
|
136 |
|
137 :param rset: ResultSet to be filtered |
|
138 |
|
139 :param divid: Dom ID of the div where the rendering of the view is done. |
|
140 :type divid: string |
|
141 |
|
142 :param vid: ID of the view display in the div |
|
143 :type vid: string |
|
144 |
|
145 :param paginate: Is the view paginated ? |
|
146 :type paginate: boolean |
|
147 |
|
148 :param cssclass: Additional css classes to put on the form. |
|
149 :type cssclass: string |
|
150 |
|
151 :param hiddens: other hidden parametters to include in the forms. |
|
152 :type hiddens: dict from extra keyword argument |
|
153 """ |
104 mainvar = self.cw_extra_kwargs.get('mainvar') |
154 mainvar = self.cw_extra_kwargs.get('mainvar') |
105 baserql, wdgs = facets(self._cw, rset, self.__regid__, mainvar) |
155 baserql, wdgs = facets(self._cw, rset, self.__regid__, mainvar) |
106 assert wdgs |
156 assert wdgs |
107 self._cw.add_js(self.needs_js) |
157 self._cw.add_js(self.needs_js) |
108 self._cw.add_css(self.needs_css) |
158 self._cw.add_css(self.needs_css) |