# HG changeset patch # User Adrien Di Mascio # Date 1243923444 -7200 # Node ID fe437afc8fb7ed327481b6d0c0ead0fb2c3c483d # Parent 5abd684d5b9da8e361c4c09af65336546dbe5325 [facet] refactor values formatting in RangeFacets diff -r 5abd684d5b9d -r fe437afc8fb7 web/facet.py --- a/web/facet.py Sun May 31 19:53:41 2009 +0200 +++ b/web/facet.py Tue Jun 02 08:17:24 2009 +0200 @@ -517,6 +517,7 @@ return self.req.form.get('%s_sup' % self.id) def formatvalue(self, value): + """format `value` before in order to insert it in the RQL query""" return unicode(value) def add_rql_restrictions(self): @@ -538,13 +539,13 @@ @property def wdgclass(self): - fmt = self.req.property_value('ui.date-format') - self.req.html_headers.define_var('DATE_FMT', fmt) return DateFacetRangeWidget def formatvalue(self, value): + """format `value` before in order to insert it in the RQL query""" return '"%s"' % date.fromtimestamp(float(value) / 1000).strftime('%Y/%m/%d') + class HasRelationFacet(AbstractFacet): rtype = None # override me in subclass role = 'subject' # role of filtered entity in the relation @@ -557,7 +558,7 @@ return False def get_widget(self): - return CheckBoxFacetWidget(self.req, self, + return CheckBoxFacetWidget(self.req, self, '%s:%s' % (self.rtype, self), self.req.form.get(self.id)) @@ -625,32 +626,35 @@ class FacetRangeWidget(HTMLWidget): + formatter = 'function (value) {return value;}' onload = u''' + var _formatter = %(formatter)s; jQuery("#%(sliderid)s").slider({ - range: true, - min: %(minvalue)s, - max: %(maxvalue)s, + range: true, + min: %(minvalue)s, + max: %(maxvalue)s, values: [%(minvalue)s, %(maxvalue)s], stop: function(event, ui) { // submit when the user stops sliding var form = $('#%(sliderid)s').closest('form'); buildRQL.apply(null, evalJSON(form.attr('cubicweb:facetargs'))); }, - slide: function(event, ui) { - $('#%(sliderid)s_inf').html(ui.values[0]); - $('#%(sliderid)s_sup').html(ui.values[1]); - $('input[name=%(facetid)s_inf]').val(ui.values[0]); - $('input[name=%(facetid)s_sup]').val(ui.values[1]); - } + slide: function(event, ui) { + jQuery('#%(sliderid)s_inf').html(_formatter(ui.values[0])); + jQuery('#%(sliderid)s_sup').html(_formatter(ui.values[1])); + jQuery('input[name=%(facetid)s_inf]').val(ui.values[0]); + jQuery('input[name=%(facetid)s_sup]').val(ui.values[1]); + } }); + // use JS formatter to format value on page load + jQuery('#%(sliderid)s_inf').html(_formatter(jQuery('input[name=%(facetid)s_inf]').val())); + jQuery('#%(sliderid)s_sup').html(_formatter(jQuery('input[name=%(facetid)s_sup]').val())); ''' + #'# make emacs happier def __init__(self, facet, minvalue, maxvalue): self.facet = facet self.minvalue = minvalue self.maxvalue = maxvalue - def formatvalue(self, value): - return value - def _render(self): facet = self.facet facet.req.add_js('ui.slider.js') @@ -658,18 +662,18 @@ sliderid = make_uid('the slider') facetid = html_escape(self.facet.id) facet.req.html_headers.add_onload(self.onload % { - 'sliderid': sliderid, - 'facetid': facetid, - 'minvalue': self.minvalue, - 'maxvalue': self.maxvalue, - }) + 'sliderid': sliderid, + 'facetid': facetid, + 'minvalue': self.minvalue, + 'maxvalue': self.maxvalue, + 'formatter': self.formatter, + }) title = html_escape(self.facet.title) self.w(u'
\n' % facetid) self.w(u'
%s
\n' % (facetid, title)) - self.w(u'%s - %s' - % (sliderid, self.formatvalue(self.minvalue), - sliderid, self.formatvalue(self.maxvalue))) + self.w(u' - ' + % (sliderid, sliderid)) self.w(u'' % (facetid, self.minvalue)) self.w(u'' @@ -679,32 +683,13 @@ class DateFacetRangeWidget(FacetRangeWidget): - onload = u''' - jQuery("#%(sliderid)s").slider({ - range: true, - min: %(minvalue)s, - max: %(maxvalue)s, - values: [%(minvalue)s, %(maxvalue)s], - stop: function(event, ui) { // submit when the user stops sliding -UI = ui; - var form = $('#%(sliderid)s').closest('form'); - buildRQL.apply(null, evalJSON(form.attr('cubicweb:facetargs'))); - }, - slide: function(event, ui) { - $('#%(sliderid)s_inf').html( (new Date(ui.values[0])).strftime(DATE_FMT)); - $('#%(sliderid)s_sup').html( (new Date(ui.values[1])).strftime(DATE_FMT)); - $('input[name=%(facetid)s_inf]').val(ui.values[0]); - $('input[name=%(facetid)s_sup]').val(ui.values[1]); - } - }); -''' + formatter = 'function (value) {return (new Date(parseFloat(value))).strftime(DATE_FMT);}' def __init__(self, facet, minvalue, maxvalue): super(DateFacetRangeWidget, self).__init__(facet, datetime2ticks(minvalue), datetime2ticks(maxvalue)) - def formatvalue(self, value): - datefmt = self.facet.req.property_value('ui.date-format') - return ustrftime(date.fromtimestamp(float(value) / 1000), datefmt) + fmt = facet.req.property_value('ui.date-format') + facet.req.html_headers.define_var('DATE_FMT', fmt) class FacetItem(HTMLWidget):