From 6343a9c86ba3a914702d6f5dabb560db45692915 Mon Sep 17 00:00:00 2001 From: David Read Date: Wed, 25 Apr 2012 16:00:27 +0100 Subject: [PATCH] [release-v1.6.1][#2325]: Bootstrapify alphabet pager. Cleaned out old non-bootstrap pager CSS. --- ckan/lib/alphabet_paginate.py | 45 ++++++++++++++-------- ckan/public/css/style.css | 24 +----------- ckan/tests/lib/test_alphabet_pagination.py | 21 ++++++++-- 3 files changed, 48 insertions(+), 42 deletions(-) diff --git a/ckan/lib/alphabet_paginate.py b/ckan/lib/alphabet_paginate.py index 0afa88f3ff1..b75bf27e83b 100644 --- a/ckan/lib/alphabet_paginate.py +++ b/ckan/lib/alphabet_paginate.py @@ -1,6 +1,7 @@ ''' -Based on webhelpers.paginator, but each page is for items beginning - with a particular letter. +Based on webhelpers.paginator, but: + * each page is for items beginning with a particular letter + * output is suitable for Bootstrap Example: c.page = h.Page( @@ -43,7 +44,12 @@ def __init__(self, collection, alpha_attribute, page, other_text, paging_thresho self.other_text = other_text self.paging_threshold = paging_threshold self.controller_name = controller_name - self.available = dict( (c,0,) for c in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ) + + self.letters = [char for char in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'] + [self.other_text] + + # Work out which alphabet letters are 'available' i.e. have some results + # because we grey-out those which aren't. + self.available = dict( (c,0,) for c in self.letters ) for c in self.collection: if isinstance(c, unicode): x = c[0] @@ -51,35 +57,42 @@ def __init__(self, collection, alpha_attribute, page, other_text, paging_thresho x = c[self.alpha_attribute][0] else: x = getattr(c, self.alpha_attribute)[0] + x = x.upper() + if x not in self.letters: + x = self.other_text self.available[x] = self.available.get(x, 0) + 1 def pager(self, q=None): '''Returns pager html - for navigating between the pages. e.g. Something like this: -
- A - B - C + ''' if self.item_count < self.paging_threshold: return '' pages = [] page = q or self.page - letters = [char for char in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'] + [self.other_text] - for letter in letters: + for letter in self.letters: + href = url_for(controller=self.controller_name, action='index', page=letter) + link = HTML.a(href=href, c=letter) if letter != page: if self.available.get(letter, 0): - page_element = HTML.a(class_='pager_link', href=url_for(controller=self.controller_name, action='index', page=letter),c=letter) + li_class = '' else: - page_element = HTML.span(class_="pager_empty", c=letter) + li_class = 'disabled' else: - page_element = HTML.span(class_='pager_curpage', c=letter) + li_class = 'active' + attributes = {'class_': li_class} if li_class else {} + page_element = HTML.li(link, **attributes) pages.append(page_element) - div = HTML.tag('div', class_='pager', *pages) + ul = HTML.tag('ul', *pages) + div = HTML.div(ul, class_='pagination pagination-alphabet') return div diff --git a/ckan/public/css/style.css b/ckan/public/css/style.css index dffb4d6a1b8..c0a3e92cba0 100644 --- a/ckan/public/css/style.css +++ b/ckan/public/css/style.css @@ -430,28 +430,8 @@ ul.no-break li { /* ============== */ /* = Pagination = */ /* ============== */ -.pager { - width: 100%; - text-align: center; - margin: 0 0 1.2em 0; - clear: both; -} -.pager span, .pager a { - text-decoration: none; - margin: 0em; - border: none; - padding: 0.3em 0.1em; -} -.pager a:hover, .pager a:active { - color: #fff; - background-color: #c22; -} -.pager span.pager_dotdot { - color: #aaa; -} -.pager span.pager_curpage { - font-weight: bold; - border: 1px solid #ddd; +.pagination-alphabet a { + padding: 0 6px; } /* ====== */ diff --git a/ckan/tests/lib/test_alphabet_pagination.py b/ckan/tests/lib/test_alphabet_pagination.py index a1bf6ae4749..8afc27642b4 100644 --- a/ckan/tests/lib/test_alphabet_pagination.py +++ b/ckan/tests/lib/test_alphabet_pagination.py @@ -1,5 +1,7 @@ import re +from nose.tools import assert_equal + from ckan.tests import * from ckan.tests import regex_related from ckan.lib.create_test_data import CreateTestData @@ -28,6 +30,16 @@ def setup_class(cls): def teardown_class(cls): model.repo.rebuild_db() + def test_00_model(self): + query = model.Session.query(model.Package) + page = h.AlphaPage( + collection=query, + alpha_attribute='title', + page='A', + other_text=other, + ) + assert_equal(page.available, {'Other': 20, 'A': 10, 'C': 10, 'B': 10, 'E': 0, 'D': 10, 'G': 0, 'F': 0, 'I': 0, 'H': 0, 'K': 0, 'J': 0, 'M': 0, 'L': 0, 'O': 0, 'N': 0, 'Q': 0, 'P': 0, 'S': 0, 'R': 0, 'U': 0, 'T': 0, 'W': 0, 'V': 0, 'Y': 0, 'X': 0, 'Z': 0}) + def test_01_package_page(self): query = model.Session.query(model.Package) page = h.AlphaPage( @@ -37,11 +49,12 @@ def test_01_package_page(self): other_text=other, ) pager = page.pager() - assert pager.startswith('
'), pager - assert 'A' in pager, pager + assert pager.startswith('