Fix multi-page for tags and feed
[blerg.git] / www / js / blerg.js
index 38e3f72..ba89815 100644 (file)
@@ -181,10 +181,13 @@ Pager.prototype.displayItems = function() {
     } else {
         $('older_link').hide();
     }
+
+    document.body.scrollTo();
 }
 
 Pager.prototype.showPageAt = function(r) {
-    if (this.itemCache[r] && this.itemCache[r - 9]) {
+    var end = (r - 9 > 0 ? r - 9 : 0);
+    if (this.itemCache[r] && this.itemCache[end]) {
         this.pageStart = r;
         this.displayItems();
     } else {
@@ -414,6 +417,7 @@ function Tag(type, tag) {
                 });
                 this.addItems(j);
                 this.pageStart = j.length - 1;
+                this.itemCount = j.length;
             }
             this.displayItems();
         }.bind(this),
@@ -445,6 +449,7 @@ Tag.prototype.show = function() {
 // Pager for browsing subscription feeds
 function Feed() {
     this.initPager();
+    this.username = loginStatus.username;
     this.baseFrag = '/feed';
 
     new Ajax.Request(baseURL + '/feed', {
@@ -462,6 +467,7 @@ function Feed() {
                 });
                 this.addItems(response);
                 this.pageStart = response.length - 1;
+                this.itemCount = response.length;
             }
             this.displayItems();
         }.bind(this),
@@ -648,7 +654,7 @@ function loadLatest() {
             j.tags.each(function(v) {
                 var a = new Element('a', {href: '/#/tag/' + v});
                 a.insert('#' + v);
-                a.onclick = qlink;
+                a.onclick = "return qlink()";
                 a.className = 'ref';
                 $('latest-tags').insert(a);
                 $('latest-tags').appendChild(document.createTextNode(' '));
@@ -685,13 +691,24 @@ function hashSwitch() {
     if (m = location.search.match(/^\?post\/([^/]+)\/(.+)/)) {
         $('post').show();
         $('post.content').value = '[' + decodeURIComponent(m[1]).replace(']','').replace('[','') + '](' + decodeURIComponent(m[2]) + ')';
-    } else if (m = location.hash.match(/^#\/(ref|tag)\/([A-Za-z0-9_-]+)$/)) {
-        currentPager = new Tag(m[1], m[2]);
+    } else if (m = location.hash.match(/^#\/(ref|tag)\/([A-Za-z0-9_-]+)(?:\/p(\d+))?$/)) {
+        if (!currentPager || currentPager.type != m[1] || currentPager.tag != m[2])
+            currentPager = new Tag(m[1], m[2]);
         currentPager.show();
-    } else if (m = location.hash.match(/^#\/feed/)) {
+
+        if (m[3]) {
+            var r = parseInt(m[3]);
+            currentPager.showPageAt(r);
+        }
+    } else if (m = location.hash.match(/^#\/feed(?:\/p(\d+))?$/)) {
         if (loginStatus.loggedIn) {
-            currentPager = new Feed();
+            if (currentPager && currentPager.username != loginStatus.username)
+                currentPager = new Feed();
             currentPager.show();
+            if (m[3]) {
+                var r = parseInt(m[3]);
+                currentPager.showPageAt(r);
+            }
         } else {
             location.href = baseURL;
         }