web/facet.py
changeset 7414 de802bcb6348
parent 7413 94ad9523abb7
child 7600 75d208ab8444
equal deleted inserted replaced
7413:94ad9523abb7 7414:de802bcb6348
  1064 
  1064 
  1065     def append(self, item):
  1065     def append(self, item):
  1066         self.items.append(item)
  1066         self.items.append(item)
  1067 
  1067 
  1068     def _render(self):
  1068     def _render(self):
       
  1069         w = self.w
  1069         title = xml_escape(self.facet.title)
  1070         title = xml_escape(self.facet.title)
  1070         facetid = xml_escape(self.facet.__regid__)
  1071         facetid = xml_escape(self.facet.__regid__)
  1071         self.w(u'<div id="%s" class="facet">\n' % facetid)
  1072         w(u'<div id="%s" class="facet">\n' % facetid)
  1072         self.w(u'<div class="facetTitle" cubicweb:facetName="%s">%s</div>\n' %
  1073         w(u'<div class="facetTitle" cubicweb:facetName="%s">%s</div>\n' %
  1073                (xml_escape(facetid), title))
  1074           (xml_escape(facetid), title))
  1074         if self.facet.support_and():
  1075         if self.facet.support_and():
  1075             _ = self.facet._cw._
  1076             _ = self.facet._cw._
  1076             self.w(u'''<select name="%s" class="radio facetOperator" title="%s">
  1077             w(u'''<select name="%s" class="radio facetOperator" title="%s">
  1077   <option value="OR">%s</option>
  1078   <option value="OR">%s</option>
  1078   <option value="AND">%s</option>
  1079   <option value="AND">%s</option>
  1079 </select>''' % (facetid + '_andor', _('and/or between different values'),
  1080 </select>''' % (facetid + '_andor', _('and/or between different values'),
  1080                 _('OR'), _('AND')))
  1081                 _('OR'), _('AND')))
  1081         cssclass = 'facetBody'
  1082         cssclass = 'facetBody'
  1082         if not self.facet.start_unfolded:
  1083         if not self.facet.start_unfolded:
  1083             cssclass += ' hidden'
  1084             cssclass += ' hidden'
  1084         if len(self.items) > 6:
  1085         if len(self.items) > 6:
  1085             cssclass += ' overflowed'
  1086             cssclass += ' overflowed'
  1086         self.w(u'<div class="%s">\n' % cssclass)
  1087         w(u'<div class="%s">\n' % cssclass)
  1087         for item in self.items:
  1088         for item in self.items:
  1088             item.render(w=self.w)
  1089             item.render(w=w)
  1089         self.w(u'</div>\n')
  1090         w(u'</div>\n')
  1090         self.w(u'</div>\n')
  1091         w(u'</div>\n')
  1091 
  1092 
  1092 
  1093 
  1093 class FacetStringWidget(HTMLWidget):
  1094 class FacetStringWidget(HTMLWidget):
  1094     def __init__(self, facet):
  1095     def __init__(self, facet):
  1095         self.facet = facet
  1096         self.facet = facet
  1097 
  1098 
  1098     def height(self):
  1099     def height(self):
  1099         return 3
  1100         return 3
  1100 
  1101 
  1101     def _render(self):
  1102     def _render(self):
       
  1103         w = self.w
  1102         title = xml_escape(self.facet.title)
  1104         title = xml_escape(self.facet.title)
  1103         facetid = xml_escape(self.facet.__regid__)
  1105         facetid = xml_escape(self.facet.__regid__)
  1104         self.w(u'<div id="%s" class="facet">\n' % facetid)
  1106         w(u'<div id="%s" class="facet">\n' % facetid)
  1105         self.w(u'<div class="facetTitle" cubicweb:facetName="%s">%s</div>\n' %
  1107         w(u'<div class="facetTitle" cubicweb:facetName="%s">%s</div>\n' %
  1106                (facetid, title))
  1108                (facetid, title))
  1107         self.w(u'<input name="%s" type="text" value="%s" />\n' % (facetid, self.value or u''))
  1109         w(u'<input name="%s" type="text" value="%s" />\n' % (facetid, self.value or u''))
  1108         self.w(u'</div>\n')
  1110         w(u'</div>\n')
  1109 
  1111 
  1110 
  1112 
  1111 class FacetRangeWidget(HTMLWidget):
  1113 class FacetRangeWidget(HTMLWidget):
  1112     formatter = 'function (value) {return value;}'
  1114     formatter = 'function (value) {return value;}'
  1113     onload = u'''
  1115     onload = u'''
  1140 
  1142 
  1141     def height(self):
  1143     def height(self):
  1142         return 3
  1144         return 3
  1143 
  1145 
  1144     def _render(self):
  1146     def _render(self):
       
  1147         w = self.w
  1145         facet = self.facet
  1148         facet = self.facet
  1146         facet._cw.add_js('jquery.ui.js')
  1149         facet._cw.add_js('jquery.ui.js')
  1147         facet._cw.add_css('jquery.ui.css')
  1150         facet._cw.add_css('jquery.ui.css')
  1148         sliderid = make_uid('theslider')
  1151         sliderid = make_uid('theslider')
  1149         facetid = xml_escape(self.facet.__regid__)
  1152         facetid = xml_escape(self.facet.__regid__)
  1153             'minvalue': self.minvalue,
  1156             'minvalue': self.minvalue,
  1154             'maxvalue': self.maxvalue,
  1157             'maxvalue': self.maxvalue,
  1155             'formatter': self.formatter,
  1158             'formatter': self.formatter,
  1156             })
  1159             })
  1157         title = xml_escape(self.facet.title)
  1160         title = xml_escape(self.facet.title)
  1158         self.w(u'<div id="%s" class="facet">\n' % facetid)
  1161         w(u'<div id="%s" class="facet">\n' % facetid)
  1159         self.w(u'<div class="facetTitle" cubicweb:facetName="%s">%s</div>\n' %
  1162         w(u'<div class="facetTitle" cubicweb:facetName="%s">%s</div>\n' %
  1160                (facetid, title))
  1163           (facetid, title))
  1161         cssclass = 'facetBody'
  1164         cssclass = 'facetBody'
  1162         if not self.facet.start_unfolded:
  1165         if not self.facet.start_unfolded:
  1163             cssclass += ' hidden'
  1166             cssclass += ' hidden'
  1164         self.w(u'<div class="%s">\n' % cssclass)
  1167         w(u'<div class="%s">\n' % cssclass)
  1165         self.w(u'<span id="%s_inf"></span> - <span id="%s_sup"></span>'
  1168         w(u'<span id="%s_inf"></span> - <span id="%s_sup"></span>'
  1166                % (sliderid, sliderid))
  1169           % (sliderid, sliderid))
  1167         self.w(u'<input type="hidden" name="%s_inf" value="%s" />'
  1170         w(u'<input type="hidden" name="%s_inf" value="%s" />'
  1168                % (facetid, self.minvalue))
  1171           % (facetid, self.minvalue))
  1169         self.w(u'<input type="hidden" name="%s_sup" value="%s" />'
  1172         w(u'<input type="hidden" name="%s_sup" value="%s" />'
  1170                % (facetid, self.maxvalue))
  1173           % (facetid, self.maxvalue))
  1171         self.w(u'<input type="hidden" name="min_%s_inf" value="%s" />'
  1174         w(u'<input type="hidden" name="min_%s_inf" value="%s" />'
  1172                % (facetid, self.minvalue))
  1175           % (facetid, self.minvalue))
  1173         self.w(u'<input type="hidden" name="max_%s_sup" value="%s" />'
  1176         w(u'<input type="hidden" name="max_%s_sup" value="%s" />'
  1174                % (facetid, self.maxvalue))
  1177           % (facetid, self.maxvalue))
  1175         self.w(u'<div id="%s"></div>' % sliderid)
  1178         w(u'<div id="%s"></div>' % sliderid)
  1176         self.w(u'</div>\n')
  1179         w(u'</div>\n')
  1177         self.w(u'</div>\n')
  1180         w(u'</div>\n')
  1178 
  1181 
  1179 
  1182 
  1180 class DateFacetRangeWidget(FacetRangeWidget):
  1183 class DateFacetRangeWidget(FacetRangeWidget):
  1181 
  1184 
  1182     formatter = 'function (value) {return (new Date(parseFloat(value))).strftime(DATE_FMT);}'
  1185     formatter = 'function (value) {return (new Date(parseFloat(value))).strftime(DATE_FMT);}'
  1204         self.label = label
  1207         self.label = label
  1205         self.value = value
  1208         self.value = value
  1206         self.selected = selected
  1209         self.selected = selected
  1207 
  1210 
  1208     def _render(self):
  1211     def _render(self):
       
  1212         w = self.w
  1209         cssclass = 'facetValue facetCheckBox'
  1213         cssclass = 'facetValue facetCheckBox'
  1210         if self.selected:
  1214         if self.selected:
  1211             cssclass += ' facetValueSelected'
  1215             cssclass += ' facetValueSelected'
  1212             imgsrc = self._cw.data_url(self.selected_img)
  1216             imgsrc = self._cw.data_url(self.selected_img)
  1213             imgalt = self._cw._('selected')
  1217             imgalt = self._cw._('selected')
  1214         else:
  1218         else:
  1215             imgsrc = self._cw.data_url(self.unselected_img)
  1219             imgsrc = self._cw.data_url(self.unselected_img)
  1216             imgalt = self._cw._('not selected')
  1220             imgalt = self._cw._('not selected')
  1217         self.w(u'<div class="%s" cubicweb:value="%s">\n'
  1221         w(u'<div class="%s" cubicweb:value="%s">\n'
  1218                % (cssclass, xml_escape(unicode(self.value))))
  1222           % (cssclass, xml_escape(unicode(self.value))))
  1219         self.w(u'<img src="%s" alt="%s"/>&#160;' % (imgsrc, imgalt))
  1223         w(u'<img src="%s" alt="%s"/>&#160;' % (imgsrc, imgalt))
  1220         self.w(u'<a href="javascript: {}">%s</a>' % xml_escape(self.label))
  1224         w(u'<a href="javascript: {}">%s</a>' % xml_escape(self.label))
  1221         self.w(u'</div>')
  1225         w(u'</div>')
  1222 
  1226 
  1223 
  1227 
  1224 class CheckBoxFacetWidget(HTMLWidget):
  1228 class CheckBoxFacetWidget(HTMLWidget):
  1225     selected_img = "black-check.png"
  1229     selected_img = "black-check.png"
  1226     unselected_img = "black-uncheck.png"
  1230     unselected_img = "black-uncheck.png"
  1233 
  1237 
  1234     def height(self):
  1238     def height(self):
  1235         return 2
  1239         return 2
  1236 
  1240 
  1237     def _render(self):
  1241     def _render(self):
       
  1242         w = self.w
  1238         title = xml_escape(self.facet.title)
  1243         title = xml_escape(self.facet.title)
  1239         facetid = xml_escape(self.facet.__regid__)
  1244         facetid = xml_escape(self.facet.__regid__)
  1240         self.w(u'<div id="%s" class="facet">\n' % facetid)
  1245         w(u'<div id="%s" class="facet">\n' % facetid)
  1241         cssclass = 'facetValue facetCheckBox'
  1246         cssclass = 'facetValue facetCheckBox'
  1242         if self.selected:
  1247         if self.selected:
  1243             cssclass += ' facetValueSelected'
  1248             cssclass += ' facetValueSelected'
  1244             imgsrc = self._cw.data_url(self.selected_img)
  1249             imgsrc = self._cw.data_url(self.selected_img)
  1245             imgalt = self._cw._('selected')
  1250             imgalt = self._cw._('selected')
  1246         else:
  1251         else:
  1247             imgsrc = self._cw.data_url(self.unselected_img)
  1252             imgsrc = self._cw.data_url(self.unselected_img)
  1248             imgalt = self._cw._('not selected')
  1253             imgalt = self._cw._('not selected')
  1249         self.w(u'<div class="%s" cubicweb:value="%s">\n'
  1254         w(u'<div class="%s" cubicweb:value="%s">\n'
  1250                % (cssclass, xml_escape(unicode(self.value))))
  1255           % (cssclass, xml_escape(unicode(self.value))))
  1251         self.w(u'<div class="facetCheckBoxWidget">')
  1256         w(u'<div class="facetCheckBoxWidget">')
  1252         self.w(u'<img src="%s" alt="%s" cubicweb:unselimg="true" />&#160;' % (imgsrc, imgalt))
  1257         w(u'<img src="%s" alt="%s" cubicweb:unselimg="true" />&#160;' % (imgsrc, imgalt))
  1253         self.w(u'<label class="facetTitle" cubicweb:facetName="%s"><a href="javascript: {}">%s</a></label>' % (facetid, title))
  1258         w(u'<label class="facetTitle" cubicweb:facetName="%s"><a href="javascript: {}">%s</a></label>' % (facetid, title))
  1254         self.w(u'</div>\n')
  1259         w(u'</div>\n')
  1255         self.w(u'</div>\n')
  1260         w(u'</div>\n')
  1256         self.w(u'</div>\n')
  1261         w(u'</div>\n')
  1257 
  1262 
  1258 
  1263 
  1259 class FacetSeparator(HTMLWidget):
  1264 class FacetSeparator(HTMLWidget):
  1260     def __init__(self, label=None):
  1265     def __init__(self, label=None):
  1261         self.label = label or u'&#160;'
  1266         self.label = label or u'&#160;'