Make hgwebdir columns sortable.
authorThomas Arendsen Hein <thomas@intevation.de>
Mon, 01 May 2006 18:38:25 +0200
changeset 2173 d1943df604c4
parent 2172 eba364839c67
child 2174 3044a3fdae76
Make hgwebdir columns sortable.
mercurial/hgweb.py
templates/index.tmpl
templates/map
--- a/mercurial/hgweb.py	Mon May 01 10:04:25 2006 +0200
+++ b/mercurial/hgweb.py	Mon May 01 18:38:25 2006 +0200
@@ -1050,7 +1050,8 @@
                 if ui.configbool("web", "allow" + i, False):
                     yield {"type" : i, "node": nodeid, "url": url}
 
-        def entries(**map):
+        def entries(sortcolumn="", descending=False, **map):
+            rows = []
             parity = 0
             for name, path in self.repos:
                 u = ui.ui()
@@ -1069,17 +1070,36 @@
                 except OSError:
                     continue
 
-                yield dict(contact=(get("ui", "username") or # preferred
-                                    get("web", "contact") or # deprecated
-                                    get("web", "author", "unknown")), # also
-                           name=get("web", "name", name),
+                contact = (get("ui", "username") or # preferred
+                           get("web", "contact") or # deprecated
+                           get("web", "author", "")) # also
+                description = get("web", "description", "")
+                name = get("web", "name", name)
+                row = dict(contact=contact or "unknown",
+                           contact_sort=contact.upper() or "unknown",
+                           name=name,
+                           name_sort=name.upper(),
                            url=url,
-                           parity=parity,
-                           shortdesc=get("web", "description", "unknown"),
-                           lastupdate=d,
+                           description=description or "unknown",
+                           description_sort=description.upper() or "unknown",
+                           lastchange=d,
+                           lastchange_sort=d[1]-d[0],
                            archives=archivelist(u, "tip", url))
-
-                parity = 1 - parity
+                if not sortcolumn:
+                    # fast path for unsorted output
+                    row['parity'] = parity
+                    parity = 1 - parity
+                    yield row
+                else:
+                    rows.append((row["%s_sort" % sortcolumn], row))
+            if rows:
+                rows.sort()
+                if descending:
+                    rows.reverse()
+                for key, row in rows:
+                    row['parity'] = parity
+                    parity = 1 - parity
+                    yield row
 
         virtual = req.env.get("PATH_INFO", "").strip('/')
         if virtual:
@@ -1100,4 +1120,20 @@
                 req.write(staticfile(static, fname)
                           or tmpl("error", error="%r not found" % fname))
             else:
-                req.write(tmpl("index", entries=entries))
+                sortable = ["name", "description", "contact", "lastchange"]
+                sortcolumn = ""
+                if req.form.has_key('sort'):
+                    sortcolumn = req.form['sort'][0]
+                descending = sortcolumn.startswith('-')
+                if descending:
+                    sortcolumn = sortcolumn[1:]
+                if sortcolumn not in sortable:
+                    sortcolumn = ""
+
+                sort = [("sort_%s" % column,
+                         "%s%s" % ((not descending and column == sortcolumn)
+                                   and "-" or "", column))
+                        for column in sortable]
+                req.write(tmpl("index", entries=entries,
+                               sortcolumn=sortcolumn, descending=descending,
+                               **dict(sort)))
--- a/templates/index.tmpl	Mon May 01 10:04:25 2006 +0200
+++ b/templates/index.tmpl	Mon May 01 18:38:25 2006 +0200
@@ -7,10 +7,10 @@
 
 <table>
     <tr>
-        <td>Name</td>
-        <td>Description</td>
-        <td>Contact</td>
-        <td>Last change</td>
+        <td><a href="?sort=#sort_name#">Name</a></td>
+        <td><a href="?sort=#sort_description#">Description</a></td>
+        <td><a href="?sort=#sort_contact#">Contact</a></td>
+        <td><a href="?sort=#sort_lastchange#">Last change</a></td>
         <td>&nbsp;</td>
     <tr>
     #entries%indexentry#
--- a/templates/map	Mon May 01 10:04:25 2006 +0200
+++ b/templates/map	Mon May 01 18:38:25 2006 +0200
@@ -43,7 +43,7 @@
 filelogparent = '<tr><th>parent #rev#:</th><td><a href="?f=#node|short#;file=#file|urlescape#">#node|short#</a></td></tr>'
 filediffchild = '<tr><th class="child">child #rev#:</th><td class="child"><a href="?cs=#node|short#">#node|short#</a></td></tr>'
 filelogchild = '<tr><th>child #rev#:</th><td><a href="?f=#node|short#;file=#file|urlescape#">#node|short#</a></td></tr>'
-indexentry = '<tr class="parity#parity#"><td><a href="#url#">#name|escape#</a></td><td>#shortdesc#</td><td>#contact|obfuscate#</td><td class="age">#lastupdate|age# ago</td><td class="indexlinks"><a href="#url#?cl=tip;style=rss">RSS</a> #archives%archiveentry#</td></tr>'
+indexentry = '<tr class="parity#parity#"><td><a href="#url#">#name|escape#</a></td><td>#description#</td><td>#contact|obfuscate#</td><td class="age">#lastchange|age# ago</td><td class="indexlinks"><a href="#url#?cl=tip;style=rss">RSS</a> #archives%archiveentry#</td></tr>'
 index = index.tmpl
 archiveentry = '<a href="#url#?ca=#node|short#;type=#type|urlescape#">#type|escape#</a> '
 notfound = notfound.tmpl