Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Sort CountableWidget instances by counted number #1

Merged
merged 1 commit into from over 1 year ago

5 participants

David Batranu Alexandru Ghica Jens W. Klein Alin Voinea Antonio De Marinis
David Batranu
Collaborator
hman commented August 28, 2012

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

Alexandru Ghica
Collaborator

+1

Jens W. Klein
Owner

cool, +1

Alin Voinea
Collaborator

+1

Antonio De Marinis
Collaborator

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

Antonio De Marinis
Collaborator

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

Alin Voinea avoinea merged commit 3c16261 into from August 29, 2012
Alin Voinea avoinea closed this August 29, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Aug 28, 2012
David Batranu Adding posibility to sort CountableWidget instances by the counted re…
…sults number
7ecac71
This page is out of date. Refresh to see the latest.
2  docs/HISTORY.txt
@@ -3,6 +3,8 @@ Changelog
3 3
 
4 4
 5.0-dev - (unreleased)
5 5
 ----------------------
  6
+* Feature: Adding posibility to sort CountableWidget instances by the counted results
  7
+  [batradav]
6 8
 
7 9
 4.9 - (2012-08-27)
8 10
 ------------------
22  eea/facetednavigation/widgets/alphabetic/view.js
@@ -37,14 +37,15 @@ Faceted.AlphabeticalWidget = function(wid){
37 37
     js_widget.reset();
38 38
   });
39 39
   if(this.widget.hasClass('faceted-count')){
  40
+    var sortcountable = this.widget.hasClass('faceted-sortcountable');
40 41
     jQuery(Faceted.Events).bind(Faceted.Events.QUERY_INITIALIZED, function(evt){
41  
-      js_widget.count();
  42
+      js_widget.count(sortcountable);
42 43
     });
43 44
     jQuery(Faceted.Events).bind(Faceted.Events.FORM_DO_QUERY, function(evt, data){
44 45
       if(data.wid == js_widget.wid || data.wid == 'b_start'){
45 46
         return;
46 47
       }
47  
-      js_widget.count();
  48
+      js_widget.count(sortcountable);
48 49
     });
49 50
   }
50 51
 };
@@ -157,7 +158,7 @@ Faceted.AlphabeticalWidget.prototype = {
157 158
     this.do_query(this.selected[0]);
158 159
   },
159 160
 
160  
-  count: function(){
  161
+  count: function(sortcountable){
161 162
     var query = Faceted.SortedQuery();
162 163
     query.cid = this.wid;
163 164
     if(this.version){
@@ -167,12 +168,12 @@ Faceted.AlphabeticalWidget.prototype = {
167 168
     var context = this;
168 169
     jQuery(Faceted.Events).trigger(Faceted.Events.AJAX_START, {wid: context.wid});
169 170
     jQuery.getJSON(Faceted.BASEURL + '@@faceted_counter', query, function(data){
170  
-      context.count_update(data);
  171
+      context.count_update(data, sortcountable);
171 172
       jQuery(Faceted.Events).trigger(Faceted.Events.AJAX_STOP, {wid: context.wid});
172 173
     });
173 174
   },
174 175
 
175  
-  count_update: function(data){
  176
+  count_update: function(data, sortcountable){
176 177
     var context = this;
177 178
     context.letters.each(function(){
178 179
       var letter = jQuery(this);
@@ -182,6 +183,9 @@ Faceted.AlphabeticalWidget.prototype = {
182 183
       var value = data[key];
183 184
       value = value ? value : 0;
184 185
       letter.attr('title', value);
  186
+      if(sortcountable){
  187
+        letter.data('count', value);
  188
+      }
185 189
       if(!value){
186 190
         letter.addClass('faceted-alphabetic-letter-disabled');
187 191
       }else{
@@ -190,6 +194,14 @@ Faceted.AlphabeticalWidget.prototype = {
190 194
         });
191 195
       }
192 196
     });
  197
+    if(sortcountable){
  198
+      context.letters.detach().sort(function(x, y) {
  199
+        var a = jQuery(x).data('count');
  200
+        var b = jQuery(y).data('count');
  201
+        return b - a;
  202
+      });
  203
+    }
  204
+    jQuery('#' + context.wid, context.widget).append(context.letters);
193 205
   }
194 206
 };
195 207
 
4  eea/facetednavigation/widgets/alphabetic/widget.pt
... ...
@@ -1,13 +1,15 @@
1  
-<div class="faceted-widget faceted-alphabetic-widget" style="display: none" 
  1
+<div class="faceted-widget faceted-alphabetic-widget" style="display: none"
2 2
      i18n:domain="eea" tal:define="
3 3
   error_view nocall:context/@@faceted.widget.error;
4 4
   wid python:view.data.getId();
5 5
   hidden python:view.hidden;
6 6
   hidezerocount python:view.hidezerocount;
  7
+  sortcountable python:view.sortcountable;
7 8
   default python:view.default or '';
8 9
   css string:faceted-widget faceted-alphabetic-widget;
9 10
   css python:hidden and css + ' faceted-widget-hidden' or css;
10 11
   css python:view.countable and css + ' faceted-count' or css;
  12
+  css python:sortcountable and css + ' faceted-sortcountable' or css;
11 13
   css python:hidezerocount and css + ' faceted-zero-count-hidden' or css;"
12 14
   tal:attributes="id string:${wid}_widget; class css">
13 15
   <fieldset class="widget-fieldset">
15  eea/facetednavigation/widgets/alphabetic/widget.py
@@ -33,21 +33,6 @@
33 33
             description=_(u"Catalog index to use for search"),
34 34
         )
35 35
     ),
36  
-    BooleanField('count',
37  
-        schemata="countable",
38  
-        widget=BooleanWidget(
39  
-            label=_(u"Count results"),
40  
-            description=_(u'Display number of results per letter'),
41  
-        )
42  
-    ),
43  
-    BooleanField('hidezerocount',
44  
-        schemata="countable",
45  
-        widget=BooleanWidget(
46  
-            label=_(u"Hide items with zero results"),
47  
-            description=_(u"This option works only if 'count results' "
48  
-                           "is enabled"),
49  
-        )
50  
-    ),
51 36
     StringField('default',
52 37
         schemata="default",
53 38
         widget=StringWidget(
22  eea/facetednavigation/widgets/checkbox/view.js
@@ -53,14 +53,15 @@ Faceted.CheckboxesWidget = function(wid){
53 53
     js_widget.reset();
54 54
   });
55 55
   if(this.widget.hasClass('faceted-count')){
  56
+    var sortcountable = this.widget.hasClass('faceted-sortcountable');
56 57
     jQuery(Faceted.Events).bind(Faceted.Events.QUERY_INITIALIZED, function(evt){
57  
-      js_widget.count();
  58
+      js_widget.count(sortcountable);
58 59
     });
59 60
     jQuery(Faceted.Events).bind(Faceted.Events.FORM_DO_QUERY, function(evt, data){
60 61
       if(data.wid == js_widget.wid || data.wid == 'b_start'){
61 62
         return;
62 63
       }
63  
-      js_widget.count();
  64
+      js_widget.count(sortcountable);
64 65
     });
65 66
   }
66 67
 };
@@ -170,7 +171,7 @@ Faceted.CheckboxesWidget.prototype = {
170 171
     }
171 172
   },
172 173
 
173  
-  count: function(){
  174
+  count: function(sortcountable){
174 175
     var query = Faceted.SortedQuery();
175 176
     query.cid = this.wid;
176 177
     if(this.version){
@@ -180,12 +181,12 @@ Faceted.CheckboxesWidget.prototype = {
180 181
     var context = this;
181 182
     jQuery(Faceted.Events).trigger(Faceted.Events.AJAX_START, {wid: context.wid});
182 183
     jQuery.getJSON(Faceted.BASEURL + '@@faceted_counter', query, function(data){
183  
-      context.count_update(data);
  184
+      context.count_update(data, sortcountable);
184 185
       jQuery(Faceted.Events).trigger(Faceted.Events.AJAX_STOP, {wid: context.wid});
185 186
     });
186 187
   },
187 188
 
188  
-  count_update: function(data){
  189
+  count_update: function(data, sortcountable){
189 190
     var context = this;
190 191
     var lis = jQuery('li', context.widget);
191 192
     jQuery(lis).each(function(){
@@ -205,6 +206,9 @@ Faceted.CheckboxesWidget.prototype = {
205 206
       var value = data[key];
206 207
       value = value ? value : 0;
207 208
       span.text('(' + data[key] + ')');
  209
+      if(sortcountable){
  210
+        li.data('count', value);
  211
+      }
208 212
       if(!value){
209 213
         li.addClass('faceted-checkbox-item-disabled');
210 214
         if(context.widget.hasClass('faceted-zero-count-hidden')){
@@ -218,6 +222,14 @@ Faceted.CheckboxesWidget.prototype = {
218 222
         });
219 223
       }
220 224
     });
  225
+    if(sortcountable){
  226
+      lis.detach().sort(function(x, y) {
  227
+        var a = jQuery(x).data('count');
  228
+        var b = jQuery(y).data('count');
  229
+        return b - a;
  230
+      });
  231
+    }
  232
+    jQuery('ul', context.widget).append(lis);
221 233
     // Update expand/colapse
222 234
     context.fieldset.trigger('widget-refresh');
223 235
   }
4  eea/facetednavigation/widgets/checkbox/widget.pt
... ...
@@ -1,13 +1,15 @@
1  
-<div class="faceted-widget faceted-checkboxes-widget" style="display: none" 
  1
+<div class="faceted-widget faceted-checkboxes-widget" style="display: none"
2 2
      i18n:domain="eea" tal:define="
3 3
   error_view nocall:context/@@faceted.widget.error;
4 4
   wid python:view.data.getId();
5 5
   hidden python:view.hidden;
6 6
   hidezerocount python:view.hidezerocount;
  7
+  sortcountable python:view.sortcountable;
7 8
   css string:faceted-widget faceted-checkboxes-widget;
8 9
   css python:hidden and css + ' faceted-widget-hidden' or css;
9 10
   css python:view.countable and css + ' faceted-count' or css;
10 11
   css python:hidezerocount and css + ' faceted-zero-count-hidden' or css;
  12
+  css python:sortcountable and css + ' faceted-sortcountable' or css;
11 13
   maxitems python:view.data.get('maxitems', 0) or 0;"
12 14
   tal:attributes="id string:${wid}_widget; class css">
13 15
 
16  eea/facetednavigation/widgets/checkbox/widget.py
@@ -73,22 +73,6 @@
73 73
             description=_(u"Sort options reversed"),
74 74
         )
75 75
     ),
76  
-    BooleanField('count',
77  
-        schemata="countable",
78  
-        widget=BooleanWidget(
79  
-            label=_(u"Count results"),
80  
-            description=_(u"Display number of results near each option"),
81  
-        )
82  
-    ),
83  
-    BooleanField('hidezerocount',
84  
-        schemata="countable",
85  
-        widget=BooleanWidget(
86  
-            label=_(u'Hide items with zero results'),
87  
-            description=_(u"This option works only if 'count results' "
88  
-                           "is enabled"),
89  
-            i18n_domain="eea"
90  
-        )
91  
-    ),
92 76
     LinesField('default',
93 77
         schemata="default",
94 78
         widget=LinesWidget(
24  eea/facetednavigation/widgets/radio/view.js
@@ -41,14 +41,15 @@ Faceted.RadioWidget = function(wid){
41 41
     js_widget.reset();
42 42
   });
43 43
   if(this.widget.hasClass('faceted-count')){
  44
+    var sortcountable = this.widget.hasClass('faceted-sortcountable');
44 45
     jQuery(Faceted.Events).bind(Faceted.Events.QUERY_INITIALIZED, function(evt){
45  
-      js_widget.count();
  46
+      js_widget.count(sortcountable);
46 47
     });
47 48
     jQuery(Faceted.Events).bind(Faceted.Events.FORM_DO_QUERY, function(evt, data){
48 49
       if(data.wid == js_widget.wid || data.wid == 'b_start'){
49 50
         return;
50 51
       }
51  
-      js_widget.count();
  52
+      js_widget.count(sortcountable);
52 53
     });
53 54
   }
54 55
 
@@ -170,7 +171,7 @@ Faceted.RadioWidget.prototype = {
170 171
     this.do_query();
171 172
   },
172 173
 
173  
-  count: function(){
  174
+  count: function(sortcountable){
174 175
     var query = Faceted.SortedQuery();
175 176
     query.cid = this.wid;
176 177
     if(this.version){
@@ -180,12 +181,12 @@ Faceted.RadioWidget.prototype = {
180 181
     var context = this;
181 182
     jQuery(Faceted.Events).trigger(Faceted.Events.AJAX_START, {wid: context.wid});
182 183
     jQuery.getJSON(Faceted.BASEURL + '@@faceted_counter', query, function(data){
183  
-      context.count_update(data);
  184
+      context.count_update(data, sortcountable);
184 185
       jQuery(Faceted.Events).trigger(Faceted.Events.AJAX_STOP, {wid: context.wid});
185 186
     });
186 187
   },
187 188
 
188  
-  count_update: function(data){
  189
+  count_update: function(data, sortcountable){
189 190
     var context = this;
190 191
     var lis = jQuery('li', context.widget);
191 192
     jQuery(lis).each(function(){
@@ -201,10 +202,13 @@ Faceted.RadioWidget.prototype = {
201 202
         li.append(jQuery('<span>'));
202 203
         span = jQuery('span', li);
203 204
       }
204  
-
  205
+      if(sortcountable){
  206
+        li.data('count', value);
  207
+      }
205 208
       var value = data[key];
206 209
       value = value ? value : 0;
207 210
       span.text('(' + value + ')');
  211
+
208 212
       if(!value){
209 213
         li.addClass('faceted-radio-item-disabled');
210 214
         if(context.widget.hasClass('faceted-zero-count-hidden')){
@@ -218,6 +222,14 @@ Faceted.RadioWidget.prototype = {
218 222
         });
219 223
       }
220 224
     });
  225
+    if(sortcountable){
  226
+      lis.detach().sort(function(x, y) {
  227
+        var a = jQuery(x).data('count');
  228
+        var b = jQuery(y).data('count');
  229
+        return b - a;
  230
+      });
  231
+    }
  232
+    jQuery('ul', context.widget).append(lis);
221 233
     // Update expand/colapse
222 234
     context.fieldset.trigger('widget-refresh');
223 235
   }
4  eea/facetednavigation/widgets/radio/widget.pt
... ...
@@ -1,14 +1,16 @@
1  
-<div class="faceted-widget faceted-radio-widget" style="display: none" 
  1
+<div class="faceted-widget faceted-radio-widget" style="display: none"
2 2
      i18n:domain="eea" tal:define="
3 3
   error_view nocall:context/@@faceted.widget.error;
4 4
   wid python:view.data.getId();
5 5
   hidden python:view.hidden;
6 6
   hidezerocount python:view.hidezerocount;
  7
+  sortcountable python:view.sortcountable;
7 8
   default python:view.default or '';
8 9
   css string:faceted-widget faceted-radio-widget;
9 10
   css python:hidden and css + ' faceted-widget-hidden' or css;
10 11
   css python:view.countable and css + ' faceted-count' or css;
11 12
   css python:hidezerocount and css + ' faceted-zero-count-hidden' or css;
  13
+  css python:sortcountable and css + ' faceted-sortcountable' or css;
12 14
   maxitems python:view.data.get('maxitems', 0) or 0;"
13 15
   tal:attributes="id string:${wid}_widget; class css">
14 16
 
17  eea/facetednavigation/widgets/radio/widget.py
@@ -63,23 +63,6 @@
63 63
             i18n_domain="eea"
64 64
         )
65 65
     ),
66  
-    BooleanField('count',
67  
-        schemata="countable",
68  
-        widget=BooleanWidget(
69  
-            label=_(u'Count results'),
70  
-            description=_(u'Display number of results near each option'),
71  
-            i18n_domain="eea"
72  
-        )
73  
-    ),
74  
-    BooleanField('hidezerocount',
75  
-        schemata="countable",
76  
-        widget=BooleanWidget(
77  
-            label=_(u'Hide items with zero results'),
78  
-            description=_(u"This option works only if 'count results' "
79  
-                           "is enabled"),
80  
-            i18n_domain="eea"
81  
-        )
82  
-    ),
83 66
     StringField('default',
84 67
         schemata="default",
85 68
         widget=StringWidget(
22  eea/facetednavigation/widgets/select/view.js
@@ -41,14 +41,15 @@ Faceted.SelectWidget = function(wid){
41 41
     js_widget.reset();
42 42
   });
43 43
   if(this.widget.hasClass('faceted-count')){
  44
+    var sortcountable = this.widget.hasClass('faceted-sortcountable');
44 45
     jQuery(Faceted.Events).bind(Faceted.Events.QUERY_INITIALIZED, function(evt){
45  
-      js_widget.count();
  46
+      js_widget.count(sortcountable);
46 47
     });
47 48
     jQuery(Faceted.Events).bind(Faceted.Events.FORM_DO_QUERY, function(evt, data){
48 49
       if(data.wid == js_widget.wid || data.wid == 'b_start'){
49 50
         return;
50 51
       }
51  
-      js_widget.count();
  52
+      js_widget.count(sortcountable);
52 53
     });
53 54
   }
54 55
 };
@@ -158,7 +159,7 @@ Faceted.SelectWidget.prototype = {
158 159
     this.do_query();
159 160
   },
160 161
 
161  
-  count: function(){
  162
+  count: function(sortcountable){
162 163
     var query = Faceted.SortedQuery();
163 164
     query.cid = this.wid;
164 165
     if(this.version){
@@ -168,12 +169,12 @@ Faceted.SelectWidget.prototype = {
168 169
     var context = this;
169 170
     jQuery(Faceted.Events).trigger(Faceted.Events.AJAX_START, {wid: context.wid});
170 171
     jQuery.getJSON(Faceted.BASEURL + '@@faceted_counter', query, function(data){
171  
-      context.count_update(data);
  172
+      context.count_update(data, sortcountable);
172 173
       jQuery(Faceted.Events).trigger(Faceted.Events.AJAX_STOP, {wid: context.wid});
173 174
     });
174 175
   },
175 176
 
176  
-  count_update: function(data){
  177
+  count_update: function(data, sortcountable){
177 178
     var context = this;
178 179
     var options = jQuery('option', context.widget);
179 180
     jQuery(options).each(function(){
@@ -188,11 +189,22 @@ Faceted.SelectWidget.prototype = {
188 189
       option_txt += ' (' + value + ')';
189 190
 
190 191
       option.html(option_txt);
  192
+      if(sortcountable){
  193
+        option.data('count', value);
  194
+      }
191 195
       if(!value){
192 196
         option.attr('disabled', 'disabled');
193 197
         option.addClass('faceted-select-item-disabled');
194 198
       }
195 199
     });
  200
+    if(sortcountable){
  201
+      options.detach().sort(function(x, y) {
  202
+        var a = jQuery(x).data('count');
  203
+        var b = jQuery(y).data('count');
  204
+        return b - a;
  205
+      });
  206
+    }
  207
+    jQuery('select', context.widget).append(options);
196 208
   }
197 209
 };
198 210
 
4  eea/facetednavigation/widgets/select/widget.pt
... ...
@@ -1,13 +1,15 @@
1  
-<div class="faceted-widget faceted-select-widget" style="display: none" 
  1
+<div class="faceted-widget faceted-select-widget" style="display: none"
2 2
      i18n:domain="eea" tal:define="
3 3
   error_view nocall:context/@@faceted.widget.error;
4 4
   wid python:view.data.getId();
5 5
   hidden python:view.hidden;
6 6
   hidezerocount python:view.hidezerocount;
  7
+  sortcountable python:view.sortcountable;
7 8
   default python:view.default or '';
8 9
   css string:faceted-widget faceted-select-widget;
9 10
   css python:hidden and css + ' faceted-widget-hidden' or css;
10 11
   css python:view.countable and css + ' faceted-count' or css;
  12
+  css python:sortcountable and css + ' faceted-sortcountable' or css;
11 13
   css python:hidezerocount and css + ' faceted-zero-count-hidden' or css;"
12 14
   tal:attributes="id string:${wid}_widget; class css">
13 15
 
16  eea/facetednavigation/widgets/select/widget.py
@@ -49,22 +49,6 @@
49 49
             description=_(u"Sort options reversed"),
50 50
         )
51 51
     ),
52  
-    BooleanField('count',
53  
-        schemata="countable",
54  
-        widget=BooleanWidget(
55  
-            label=_(u"Count results"),
56  
-            description=_(u"Display number of results near each option"),
57  
-        )
58  
-    ),
59  
-    BooleanField('hidezerocount',
60  
-        schemata="countable",
61  
-        widget=BooleanWidget(
62  
-            label=_(u'Hide items with zero results'),
63  
-            description=_(u"This option works only if 'count results' "
64  
-                           "is enabled"),
65  
-            i18n_domain="eea"
66  
-        )
67  
-    ),
68 52
     StringField('default',
69 53
         schemata="default",
70 54
         widget=StringWidget(
16  eea/facetednavigation/widgets/tagscloud/widget.py
@@ -139,22 +139,6 @@
139 139
             i18n_domain="eea"
140 140
         )
141 141
     ),
142  
-    BooleanField('count',
143  
-        schemata="countable",
144  
-        widget=BooleanWidget(
145  
-            label=_(u'Count results'),
146  
-            description=_(u"Display number of results near each tag"),
147  
-        )
148  
-    ),
149  
-    BooleanField('hidezerocount',
150  
-        schemata="countable",
151  
-        widget=BooleanWidget(
152  
-            label=_(u'Hide items with zero results'),
153  
-            description=_(u"This option works only if 'count results' "
154  
-                           "is enabled"),
155  
-            i18n_domain="eea"
156  
-        )
157  
-    ),
158 142
     StringField('default',
159 143
         schemata="default",
160 144
         widget=StringWidget(
36  eea/facetednavigation/widgets/widget.py
@@ -291,9 +291,39 @@ def __call__(self, **kwargs):
291 291
         """
292 292
         return self.template
293 293
 
  294
+
  295
+CountableWidgetSchema = Schema((
  296
+    BooleanField('count',
  297
+        schemata="countable",
  298
+        widget=BooleanWidget(
  299
+            label=_(u"Count results"),
  300
+            description=_(u"Display number of results near each option"),
  301
+        )
  302
+    ),
  303
+    BooleanField('sortcountable',
  304
+        schemata="countable",
  305
+        widget=BooleanWidget(
  306
+            label=_(u"Sort by countable"),
  307
+            description=_(u"Use the results counter for sorting"),
  308
+        )
  309
+    ),
  310
+    BooleanField('hidezerocount',
  311
+        schemata="countable",
  312
+        widget=BooleanWidget(
  313
+            label=_(u'Hide items with zero results'),
  314
+            description=_(u"This option works only if 'count results' "
  315
+                           "is enabled"),
  316
+            i18n_domain="eea"
  317
+        )
  318
+    ),
  319
+))
  320
+
294 321
 class CountableWidget(Widget):
295 322
     """ Defines useful methods for countable widgets
296 323
     """
  324
+    # Widget properties
  325
+    edit_schema = Widget.edit_schema.copy() + CountableWidgetSchema.copy()
  326
+
297 327
     @property
298 328
     def countable(self):
299 329
         """ Count results?
@@ -301,6 +331,12 @@ def countable(self):
301 331
         return bool(safeToInt(self.data.get('count', 0)))
302 332
 
303 333
     @property
  334
+    def sortcountable(self):
  335
+        """ Sort results by countable value?
  336
+        """
  337
+        return bool(safeToInt(self.data.get('sortcountable', 0)))
  338
+
  339
+    @property
304 340
     def hidezerocount(self):
305 341
         """ Hide items that return no result?
306 342
         """
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.