Repository: ckan/ckan

Issue:
Hide 'show more' link on search facets when irrelevent
i.e. don't show "show more formats" link when there are no more formats or tags or groups to show: http://beta.ckan.org/dataset



Discussion:
Duplicate, see #780 

This issue still exists - As an example 
http://master.ckan.org/dataset - Still shows "show more groups" and "show more formats" link when there are no more groups to display.

Also the "Show more..." links are much quite visually prominent compared to the facet titles and items. Suggest this should be less prominent. 

@johnmartin - Please could you have a look?  

FYI https://github.com/okfn/ckanext-iati/commit/ffe615f0d93

If we tweak `h.get_facet_items_dict` to actually support the limit param then we don't have to hack the ini file with `search.facets.default=11`

Maybe I dreamt it but I thought this had been fixed in another PR? @johnmartin please close this if so

@amercader I think you dreamt it. However I'm going to take a look at this today.

OK, I've taken a look at this... and I can update the templates to be a little nicer (using okfn/ckanext-iati@ffe615f as example). However when I give `h.get_facet_items_dict` a `limit` it just completely ignores it... so I'm not sure where to go from there. I'm unassigning myself and I'll bring this up in the next dev catchup.


Resolving patch:
diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py
--- a/ckan/lib/helpers.py
+++ b/ckan/lib/helpers.py
@@ -532,7 +532,7 @@ def default_group_type():
     return str(config.get('ckan.default.group_type', 'group'))
 
 
-def get_facet_items_dict(facet, limit=10, exclude_active=False):
+def get_facet_items_dict(facet, limit=None, exclude_active=False):
     '''Return the list of unselected facet items for the given facet, sorted
     by count.
 
@@ -564,12 +564,41 @@ def get_facet_items_dict(facet, limit=10, exclude_active=False):
         elif not exclude_active:
             facets.append(dict(active=True, **facet_item))
     facets = sorted(facets, key=lambda item: item['count'], reverse=True)
-    if c.search_facets_limits:
+    if c.search_facets_limits and limit is None:
         limit = c.search_facets_limits.get(facet)
-    if limit:
+    if limit is not None:
         return facets[:limit]
-    else:
-        return facets
+    return facets
+
+
+def has_more_facets(facet, limit=None, exclude_active=False):
+    '''
+    Returns True if there are more facet items for the given facet than the
+    limit.
+
+    Reads the complete list of facet items for the given facet from
+    c.search_facets, and filters out the facet items that the user has already
+    selected.
+
+    Arguments:
+    facet -- the name of the facet to filter.
+    limit -- the max. number of facet items.
+    exclude_active -- only return unselected facets.
+
+    '''
+    facets = []
+    for facet_item in c.search_facets.get(facet)['items']:
+        if not len(facet_item['name'].strip()):
+            continue
+        if not (facet, facet_item['name']) in request.params.items():
+            facets.append(dict(active=False, **facet_item))
+        elif not exclude_active:
+            facets.append(dict(active=True, **facet_item))
+    if c.search_facets_limits and limit is None:
+        limit = c.search_facets_limits.get(facet)
+    if limit is not None and len(facets) > limit:
+        return True
+    return False
 
 
 def unselected_facet_items(facet, limit=10):
@@ -1843,6 +1872,7 @@ def check_config_permission(permission):
     'list_dict_filter',
     'new_activities',
     'time_ago_from_timestamp',
+    'has_more_facets',
     # imported into ckan.lib.helpers
     'literal',
     'link_to',


Test patch: