Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Sort CountableWidget instances by counted number #1

Merged
merged 1 commit into from

5 participants

@david-batranu
Collaborator

This allows widgets based on CountableWidget (Alphabetic, Checkbox, Radio, Select) to be sorted by the counted results for each entry.

@alecghica
Collaborator

+1

@jensens
Owner

cool, +1

@avoinea
Collaborator

+1

@demarant
Collaborator

this is a featured I also missed a lot!
+1

@demarant
Collaborator

alin you get the honor to merge it :) i need to party now

@avoinea avoinea merged commit 3c16261 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 28, 2012
  1. @david-batranu
This page is out of date. Refresh to see the latest.
View
2  docs/HISTORY.txt
@@ -3,6 +3,8 @@ Changelog
5.0-dev - (unreleased)
----------------------
+* Feature: Adding posibility to sort CountableWidget instances by the counted results
+ [batradav]
4.9 - (2012-08-27)
------------------
View
22 eea/facetednavigation/widgets/alphabetic/view.js
@@ -37,14 +37,15 @@ Faceted.AlphabeticalWidget = function(wid){
js_widget.reset();
});
if(this.widget.hasClass('faceted-count')){
+ var sortcountable = this.widget.hasClass('faceted-sortcountable');
jQuery(Faceted.Events).bind(Faceted.Events.QUERY_INITIALIZED, function(evt){
- js_widget.count();
+ js_widget.count(sortcountable);
});
jQuery(Faceted.Events).bind(Faceted.Events.FORM_DO_QUERY, function(evt, data){
if(data.wid == js_widget.wid || data.wid == 'b_start'){
return;
}
- js_widget.count();
+ js_widget.count(sortcountable);
});
}
};
@@ -157,7 +158,7 @@ Faceted.AlphabeticalWidget.prototype = {
this.do_query(this.selected[0]);
},
- count: function(){
+ count: function(sortcountable){
var query = Faceted.SortedQuery();
query.cid = this.wid;
if(this.version){
@@ -167,12 +168,12 @@ Faceted.AlphabeticalWidget.prototype = {
var context = this;
jQuery(Faceted.Events).trigger(Faceted.Events.AJAX_START, {wid: context.wid});
jQuery.getJSON(Faceted.BASEURL + '@@faceted_counter', query, function(data){
- context.count_update(data);
+ context.count_update(data, sortcountable);
jQuery(Faceted.Events).trigger(Faceted.Events.AJAX_STOP, {wid: context.wid});
});
},
- count_update: function(data){
+ count_update: function(data, sortcountable){
var context = this;
context.letters.each(function(){
var letter = jQuery(this);
@@ -182,6 +183,9 @@ Faceted.AlphabeticalWidget.prototype = {
var value = data[key];
value = value ? value : 0;
letter.attr('title', value);
+ if(sortcountable){
+ letter.data('count', value);
+ }
if(!value){
letter.addClass('faceted-alphabetic-letter-disabled');
}else{
@@ -190,6 +194,14 @@ Faceted.AlphabeticalWidget.prototype = {
});
}
});
+ if(sortcountable){
+ context.letters.detach().sort(function(x, y) {
+ var a = jQuery(x).data('count');
+ var b = jQuery(y).data('count');
+ return b - a;
+ });
+ }
+ jQuery('#' + context.wid, context.widget).append(context.letters);
}
};
View
4 eea/facetednavigation/widgets/alphabetic/widget.pt
@@ -1,13 +1,15 @@
-<div class="faceted-widget faceted-alphabetic-widget" style="display: none"
+<div class="faceted-widget faceted-alphabetic-widget" style="display: none"
i18n:domain="eea" tal:define="
error_view nocall:context/@@faceted.widget.error;
wid python:view.data.getId();
hidden python:view.hidden;
hidezerocount python:view.hidezerocount;
+ sortcountable python:view.sortcountable;
default python:view.default or '';
css string:faceted-widget faceted-alphabetic-widget;
css python:hidden and css + ' faceted-widget-hidden' or css;
css python:view.countable and css + ' faceted-count' or css;
+ css python:sortcountable and css + ' faceted-sortcountable' or css;
css python:hidezerocount and css + ' faceted-zero-count-hidden' or css;"
tal:attributes="id string:${wid}_widget; class css">
<fieldset class="widget-fieldset">
View
15 eea/facetednavigation/widgets/alphabetic/widget.py
@@ -33,21 +33,6 @@
description=_(u"Catalog index to use for search"),
)
),
- BooleanField('count',
- schemata="countable",
- widget=BooleanWidget(
- label=_(u"Count results"),
- description=_(u'Display number of results per letter'),
- )
- ),
- BooleanField('hidezerocount',
- schemata="countable",
- widget=BooleanWidget(
- label=_(u"Hide items with zero results"),
- description=_(u"This option works only if 'count results' "
- "is enabled"),
- )
- ),
StringField('default',
schemata="default",
widget=StringWidget(
View
22 eea/facetednavigation/widgets/checkbox/view.js
@@ -53,14 +53,15 @@ Faceted.CheckboxesWidget = function(wid){
js_widget.reset();
});
if(this.widget.hasClass('faceted-count')){
+ var sortcountable = this.widget.hasClass('faceted-sortcountable');
jQuery(Faceted.Events).bind(Faceted.Events.QUERY_INITIALIZED, function(evt){
- js_widget.count();
+ js_widget.count(sortcountable);
});
jQuery(Faceted.Events).bind(Faceted.Events.FORM_DO_QUERY, function(evt, data){
if(data.wid == js_widget.wid || data.wid == 'b_start'){
return;
}
- js_widget.count();
+ js_widget.count(sortcountable);
});
}
};
@@ -170,7 +171,7 @@ Faceted.CheckboxesWidget.prototype = {
}
},
- count: function(){
+ count: function(sortcountable){
var query = Faceted.SortedQuery();
query.cid = this.wid;
if(this.version){
@@ -180,12 +181,12 @@ Faceted.CheckboxesWidget.prototype = {
var context = this;
jQuery(Faceted.Events).trigger(Faceted.Events.AJAX_START, {wid: context.wid});
jQuery.getJSON(Faceted.BASEURL + '@@faceted_counter', query, function(data){
- context.count_update(data);
+ context.count_update(data, sortcountable);
jQuery(Faceted.Events).trigger(Faceted.Events.AJAX_STOP, {wid: context.wid});
});
},
- count_update: function(data){
+ count_update: function(data, sortcountable){
var context = this;
var lis = jQuery('li', context.widget);
jQuery(lis).each(function(){
@@ -205,6 +206,9 @@ Faceted.CheckboxesWidget.prototype = {
var value = data[key];
value = value ? value : 0;
span.text('(' + data[key] + ')');
+ if(sortcountable){
+ li.data('count', value);
+ }
if(!value){
li.addClass('faceted-checkbox-item-disabled');
if(context.widget.hasClass('faceted-zero-count-hidden')){
@@ -218,6 +222,14 @@ Faceted.CheckboxesWidget.prototype = {
});
}
});
+ if(sortcountable){
+ lis.detach().sort(function(x, y) {
+ var a = jQuery(x).data('count');
+ var b = jQuery(y).data('count');
+ return b - a;
+ });
+ }
+ jQuery('ul', context.widget).append(lis);
// Update expand/colapse
context.fieldset.trigger('widget-refresh');
}
View
4 eea/facetednavigation/widgets/checkbox/widget.pt
@@ -1,13 +1,15 @@
-<div class="faceted-widget faceted-checkboxes-widget" style="display: none"
+<div class="faceted-widget faceted-checkboxes-widget" style="display: none"
i18n:domain="eea" tal:define="
error_view nocall:context/@@faceted.widget.error;
wid python:view.data.getId();
hidden python:view.hidden;
hidezerocount python:view.hidezerocount;
+ sortcountable python:view.sortcountable;
css string:faceted-widget faceted-checkboxes-widget;
css python:hidden and css + ' faceted-widget-hidden' or css;
css python:view.countable and css + ' faceted-count' or css;
css python:hidezerocount and css + ' faceted-zero-count-hidden' or css;
+ css python:sortcountable and css + ' faceted-sortcountable' or css;
maxitems python:view.data.get('maxitems', 0) or 0;"
tal:attributes="id string:${wid}_widget; class css">
View
16 eea/facetednavigation/widgets/checkbox/widget.py
@@ -73,22 +73,6 @@
description=_(u"Sort options reversed"),
)
),
- BooleanField('count',
- schemata="countable",
- widget=BooleanWidget(
- label=_(u"Count results"),
- description=_(u"Display number of results near each option"),
- )
- ),
- BooleanField('hidezerocount',
- schemata="countable",
- widget=BooleanWidget(
- label=_(u'Hide items with zero results'),
- description=_(u"This option works only if 'count results' "
- "is enabled"),
- i18n_domain="eea"
- )
- ),
LinesField('default',
schemata="default",
widget=LinesWidget(
View
24 eea/facetednavigation/widgets/radio/view.js
@@ -41,14 +41,15 @@ Faceted.RadioWidget = function(wid){
js_widget.reset();
});
if(this.widget.hasClass('faceted-count')){
+ var sortcountable = this.widget.hasClass('faceted-sortcountable');
jQuery(Faceted.Events).bind(Faceted.Events.QUERY_INITIALIZED, function(evt){
- js_widget.count();
+ js_widget.count(sortcountable);
});
jQuery(Faceted.Events).bind(Faceted.Events.FORM_DO_QUERY, function(evt, data){
if(data.wid == js_widget.wid || data.wid == 'b_start'){
return;
}
- js_widget.count();
+ js_widget.count(sortcountable);
});
}
@@ -170,7 +171,7 @@ Faceted.RadioWidget.prototype = {
this.do_query();
},
- count: function(){
+ count: function(sortcountable){
var query = Faceted.SortedQuery();
query.cid = this.wid;
if(this.version){
@@ -180,12 +181,12 @@ Faceted.RadioWidget.prototype = {
var context = this;
jQuery(Faceted.Events).trigger(Faceted.Events.AJAX_START, {wid: context.wid});
jQuery.getJSON(Faceted.BASEURL + '@@faceted_counter', query, function(data){
- context.count_update(data);
+ context.count_update(data, sortcountable);
jQuery(Faceted.Events).trigger(Faceted.Events.AJAX_STOP, {wid: context.wid});
});
},
- count_update: function(data){
+ count_update: function(data, sortcountable){
var context = this;
var lis = jQuery('li', context.widget);
jQuery(lis).each(function(){
@@ -201,10 +202,13 @@ Faceted.RadioWidget.prototype = {
li.append(jQuery('<span>'));
span = jQuery('span', li);
}
-
+ if(sortcountable){
+ li.data('count', value);
+ }
var value = data[key];
value = value ? value : 0;
span.text('(' + value + ')');
+
if(!value){
li.addClass('faceted-radio-item-disabled');
if(context.widget.hasClass('faceted-zero-count-hidden')){
@@ -218,6 +222,14 @@ Faceted.RadioWidget.prototype = {
});
}
});
+ if(sortcountable){
+ lis.detach().sort(function(x, y) {
+ var a = jQuery(x).data('count');
+ var b = jQuery(y).data('count');
+ return b - a;
+ });
+ }
+ jQuery('ul', context.widget).append(lis);
// Update expand/colapse
context.fieldset.trigger('widget-refresh');
}
View
4 eea/facetednavigation/widgets/radio/widget.pt
@@ -1,14 +1,16 @@
-<div class="faceted-widget faceted-radio-widget" style="display: none"
+<div class="faceted-widget faceted-radio-widget" style="display: none"
i18n:domain="eea" tal:define="
error_view nocall:context/@@faceted.widget.error;
wid python:view.data.getId();
hidden python:view.hidden;
hidezerocount python:view.hidezerocount;
+ sortcountable python:view.sortcountable;
default python:view.default or '';
css string:faceted-widget faceted-radio-widget;
css python:hidden and css + ' faceted-widget-hidden' or css;
css python:view.countable and css + ' faceted-count' or css;
css python:hidezerocount and css + ' faceted-zero-count-hidden' or css;
+ css python:sortcountable and css + ' faceted-sortcountable' or css;
maxitems python:view.data.get('maxitems', 0) or 0;"
tal:attributes="id string:${wid}_widget; class css">
View
17 eea/facetednavigation/widgets/radio/widget.py
@@ -63,23 +63,6 @@
i18n_domain="eea"
)
),
- BooleanField('count',
- schemata="countable",
- widget=BooleanWidget(
- label=_(u'Count results'),
- description=_(u'Display number of results near each option'),
- i18n_domain="eea"
- )
- ),
- BooleanField('hidezerocount',
- schemata="countable",
- widget=BooleanWidget(
- label=_(u'Hide items with zero results'),
- description=_(u"This option works only if 'count results' "
- "is enabled"),
- i18n_domain="eea"
- )
- ),
StringField('default',
schemata="default",
widget=StringWidget(
View
22 eea/facetednavigation/widgets/select/view.js
@@ -41,14 +41,15 @@ Faceted.SelectWidget = function(wid){
js_widget.reset();
});
if(this.widget.hasClass('faceted-count')){
+ var sortcountable = this.widget.hasClass('faceted-sortcountable');
jQuery(Faceted.Events).bind(Faceted.Events.QUERY_INITIALIZED, function(evt){
- js_widget.count();
+ js_widget.count(sortcountable);
});
jQuery(Faceted.Events).bind(Faceted.Events.FORM_DO_QUERY, function(evt, data){
if(data.wid == js_widget.wid || data.wid == 'b_start'){
return;
}
- js_widget.count();
+ js_widget.count(sortcountable);
});
}
};
@@ -158,7 +159,7 @@ Faceted.SelectWidget.prototype = {
this.do_query();
},
- count: function(){
+ count: function(sortcountable){
var query = Faceted.SortedQuery();
query.cid = this.wid;
if(this.version){
@@ -168,12 +169,12 @@ Faceted.SelectWidget.prototype = {
var context = this;
jQuery(Faceted.Events).trigger(Faceted.Events.AJAX_START, {wid: context.wid});
jQuery.getJSON(Faceted.BASEURL + '@@faceted_counter', query, function(data){
- context.count_update(data);
+ context.count_update(data, sortcountable);
jQuery(Faceted.Events).trigger(Faceted.Events.AJAX_STOP, {wid: context.wid});
});
},
- count_update: function(data){
+ count_update: function(data, sortcountable){
var context = this;
var options = jQuery('option', context.widget);
jQuery(options).each(function(){
@@ -188,11 +189,22 @@ Faceted.SelectWidget.prototype = {
option_txt += ' (' + value + ')';
option.html(option_txt);
+ if(sortcountable){
+ option.data('count', value);
+ }
if(!value){
option.attr('disabled', 'disabled');
option.addClass('faceted-select-item-disabled');
}
});
+ if(sortcountable){
+ options.detach().sort(function(x, y) {
+ var a = jQuery(x).data('count');
+ var b = jQuery(y).data('count');
+ return b - a;
+ });
+ }
+ jQuery('select', context.widget).append(options);
}
};
View
4 eea/facetednavigation/widgets/select/widget.pt
@@ -1,13 +1,15 @@
-<div class="faceted-widget faceted-select-widget" style="display: none"
+<div class="faceted-widget faceted-select-widget" style="display: none"
i18n:domain="eea" tal:define="
error_view nocall:context/@@faceted.widget.error;
wid python:view.data.getId();
hidden python:view.hidden;
hidezerocount python:view.hidezerocount;
+ sortcountable python:view.sortcountable;
default python:view.default or '';
css string:faceted-widget faceted-select-widget;
css python:hidden and css + ' faceted-widget-hidden' or css;
css python:view.countable and css + ' faceted-count' or css;
+ css python:sortcountable and css + ' faceted-sortcountable' or css;
css python:hidezerocount and css + ' faceted-zero-count-hidden' or css;"
tal:attributes="id string:${wid}_widget; class css">
View
16 eea/facetednavigation/widgets/select/widget.py
@@ -49,22 +49,6 @@
description=_(u"Sort options reversed"),
)
),
- BooleanField('count',
- schemata="countable",
- widget=BooleanWidget(
- label=_(u"Count results"),
- description=_(u"Display number of results near each option"),
- )
- ),
- BooleanField('hidezerocount',
- schemata="countable",
- widget=BooleanWidget(
- label=_(u'Hide items with zero results'),
- description=_(u"This option works only if 'count results' "
- "is enabled"),
- i18n_domain="eea"
- )
- ),
StringField('default',
schemata="default",
widget=StringWidget(
View
16 eea/facetednavigation/widgets/tagscloud/widget.py
@@ -139,22 +139,6 @@
i18n_domain="eea"
)
),
- BooleanField('count',
- schemata="countable",
- widget=BooleanWidget(
- label=_(u'Count results'),
- description=_(u"Display number of results near each tag"),
- )
- ),
- BooleanField('hidezerocount',
- schemata="countable",
- widget=BooleanWidget(
- label=_(u'Hide items with zero results'),
- description=_(u"This option works only if 'count results' "
- "is enabled"),
- i18n_domain="eea"
- )
- ),
StringField('default',
schemata="default",
widget=StringWidget(
View
36 eea/facetednavigation/widgets/widget.py
@@ -291,9 +291,39 @@ def __call__(self, **kwargs):
"""
return self.template
+
+CountableWidgetSchema = Schema((
+ BooleanField('count',
+ schemata="countable",
+ widget=BooleanWidget(
+ label=_(u"Count results"),
+ description=_(u"Display number of results near each option"),
+ )
+ ),
+ BooleanField('sortcountable',
+ schemata="countable",
+ widget=BooleanWidget(
+ label=_(u"Sort by countable"),
+ description=_(u"Use the results counter for sorting"),
+ )
+ ),
+ BooleanField('hidezerocount',
+ schemata="countable",
+ widget=BooleanWidget(
+ label=_(u'Hide items with zero results'),
+ description=_(u"This option works only if 'count results' "
+ "is enabled"),
+ i18n_domain="eea"
+ )
+ ),
+))
+
class CountableWidget(Widget):
""" Defines useful methods for countable widgets
"""
+ # Widget properties
+ edit_schema = Widget.edit_schema.copy() + CountableWidgetSchema.copy()
+
@property
def countable(self):
""" Count results?
@@ -301,6 +331,12 @@ def countable(self):
return bool(safeToInt(self.data.get('count', 0)))
@property
+ def sortcountable(self):
+ """ Sort results by countable value?
+ """
+ return bool(safeToInt(self.data.get('sortcountable', 0)))
+
+ @property
def hidezerocount(self):
""" Hide items that return no result?
"""
Something went wrong with that request. Please try again.