Commits

Anonymous committed efb989c

[svn] Updated PHPbb style links code to use htmlgen

Comments (0)

Files changed (2)

webhelpers/pagination/__init__.py

     def padding__get(self):
         return self._padding
 
+    padding = property(padding__get, padding__set)
+
     def pages__get(self):
         return [self.paginator[page_number] for page_number in 
             range(self.first.number, self.last.number+1)]
     pages = property(pages__get)
+
+    def __add__(self, window):
+        if window.paginator != self.paginator:
+            raise AttributeError("Window/paginator mismatch")
+        assert self.last >= window.first
+        return Window(self.page.next, padding=self.padding+1)
+

webhelpers/pagination/links.py

 """Pagination Link Generators"""
+from webhelpers.htmlgen import html
 
-def pagelist(page, pages):
+def pagelist(page):
     """PHPbb style Pagination Links
     
-    Example (Using Kid)::
-    
-        <div py:def="display_pager(url, page, pages)" class="pager">Page ${page} of ${pages}:
-            <span py:if="page != 1">
-            <a href="${url + str(page-1)}">&lt;&lt; Previous</a>
-            </span>
-            <span py:for="i in pagelist(page, pages)"> 
-                    <span py:if="i == None">...</span>
-                            <a py:if="i != page and i != None" href="${url + str(i)}">${i}</a>
-                            <span py:if="i == page">${i}</span>
-                    </span>
-                    <span py:if="page != pages">
-                             <a href="${url + str(page+1)}">Next &gt;&gt;</a>
-                    </span>
-            </span>
-        </div>
+    This returns HTML source to be included into a page. The html is generated
+    with htmlgen
     """
-    page = max(min(page, pages), 1)
-    topstart = pages < 1 + 3 and pages or 4
-    botmid = page-1 < 1 and 1 or page - 1
-    topmid = page+2 < pages and page+2 or pages
-    bottop = pages-2 < 1 and 1 or pages - 2
-    startpastmid = topstart >= botmid
-    midpasttop = topmid >= bottop
-    if startpastmid:
-        if midpasttop:
-            display = range(1, pages+1)
+    paginator = page.paginator
+
+    first_page = paginator.first
+    first_window = first_page.window(padding=3)
+
+    page_window = page.window(padding=1)
+
+    last_page = paginator.last
+    last_window = last_page.window(padding=3)
+
+    first_past_page = first_window.last >= page_window.first
+    page_past_last = page_window.last >= last_window.first
+
+    def combine_pages(page_list):
+        seen = {}
+        result = []
+        for page in page_list:
+            if page in seen: continue
+            seen[page] = 1
+            result.append(page)
+        return result
+
+    if first_past_page:
+        if page_past_last:
+            display = first_window.pages
         else:
-            display = range(1, topmid+1) + [None] + range(bottop, pages+1)
+            first_window.last = page_window.last
+            display = (first_window.pages + [None] + last_window.pages)
     else:
-        if midpasttop:
-            display = range(1, topstart) + [None] + range(botmid-1, pages+1)
+        if page_past_last:
+            page_window.last = last_window.last
+            display = (first_window.pages + [None] + page_window.pages)
         else:
-            display = (
-                    range(1, topstart) + [None] + range(botmid, topmid) + [None] +
-                    range(bottop, pages+1))
-    return display
+            display = (first_window.pages + [None] + page_window.pages + [None]
+                    + last_window.pages)
+
+    print display
+
+    pager_c = []
+    for i in display:
+        if i is None:
+            pager_c.append(html.span(c='...'))
+        elif i == page:
+            pager_c.append(html.span(c=[i]))
+        else:
+            pager_c.append(html.a(href=i, c=[i]))
+
+    pager = html.div(class_='pager', c=pager_c)
+
+    return pager
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.