Add new message bits to web
[blerg.git] / www / js / blerg.js
index d0135b8..83a8a88 100644 (file)
@@ -5,13 +5,13 @@
 // Config
 var baseURL = '';
 var recordTemplate = new Template(
-    '<div class="record">#{data}<div class="info">Posted #{date}. <a href="/\##{author}/#{record}" onclick="return qlink()">[permalink]</a> <a href="#" onclick="postPopup(\'@#{author}/#{record}: \'); return false">[reply]</a></div></div>'
+    '<div class="record">#{data}<div class="info">Posted #{date}. <a href="' + baseURL + '/\##{author}/#{record}" onclick="return qlink()">[permalink]</a> <a href="#" onclick="postPopup(\'@#{author}/#{record}: \'); return false">[reply]</a></div></div>'
 );
 var tagRecordTemplate = new Template(
-    '<div class="record">#{data}<div class="info">Posted by <a class="author ref" href="/\##{author}" onclick="return qlink()">@#{author}</a> on #{date}. <a href="/\##{author}/#{record}" onclick="return qlink()">[permalink]</a> <a href="#" onclick="postPopup(\'@#{author}/#{record}: \'); return false">[reply]</a></div></div>'
+    '<div class="record">#{data}<div class="info">Posted by <a class="author ref" href="/\##{author}" onclick="return qlink()">@#{author}</a> on #{date}. <a href="' + baseURL + '/\##{author}/#{record}" onclick="return qlink()">[permalink]</a> <a href="#" onclick="postPopup(\'@#{author}/#{record}: \'); return false">[reply]</a></div></div>'
 );
 var latestRecordsTemplate = new Template(
-    '<div class="record"><a class="author ref" href="/\##{author}" onclick="return qlink()">@#{author}</a> #{data}</div>'
+    '<div class="record"><a class="author ref" href="' + baseURL + '/\##{author}" onclick="return qlink()">@#{author}</a> #{data}</div>'
 );
 
 // Page elements
@@ -31,6 +31,8 @@ function LoginStatus() {
     if (cookies.auth && cookies.username) {
         this.loggedIn = true;
         this.username = cookies.username;
+        this.requestFeedStatus();
+        this.feedStatusUpdateInterval = setInterval(this.requestFeedStatus.bind(this), 900000);
     } else {
         this.loggedIn = false;
         this.username = null;
@@ -51,6 +53,8 @@ LoginStatus.prototype.login = function(username, password) {
                 this.username = username;
                 document.cookie = "username=" + username;
                 $('login.password').value = '';
+                this.requestFeedStatus();
+                this.feedStatusUpdateInterval = setInterval(this.requestFeedStatus.bind(this), 900000);
                 this.update();
             } else {
                 alert("Could not log in");
@@ -73,6 +77,7 @@ LoginStatus.prototype.logout = function() {
             this.loggedIn = false;
             document.cookie = "auth=; expires=1-Jan-1970 00:00:00 GMT";
             this.update();
+            clearInterval(this.feedStatusUpdateInterval);
         }.bind(this)
     });
     document.cookie = "username=; expires=1-Jan-1970 00:00:00 GMT";
@@ -80,9 +85,9 @@ LoginStatus.prototype.logout = function() {
 
 LoginStatus.prototype.update = function() {
     if (this.loggedIn) {
-        $('userlink').href = '/#' + this.username;
+        $('userlink').href = baseURL + '/#' + this.username;
         $('userlink').update('@' + this.username);
-        $('reflink').href = '/#/ref/' + this.username;
+        $('reflink').href = baseURL + '/#/ref/' + this.username;
         $('login').hide();
         $('logout').show();
     } else {
@@ -123,6 +128,19 @@ LoginStatus.prototype.post = function(msg) {
     });
 }
 
+LoginStatus.prototype.requestFeedStatus = function() {
+    new Ajax.Request('/feedinfo', {
+        parameters: { username: this.username },
+        onSuccess: function(r) {
+            var j = r.responseText.evalJSON();
+            if (j['new'] > 0) {
+                $('newFeedMessages').update('(' + j['new'] + ' new)');
+            } else {
+                $('newFeedMessages').update('');
+            }
+        }
+    });
+}
 
 // Base object for paged data
 function Pager() {
@@ -180,14 +198,14 @@ Pager.prototype.displayItems = function() {
     }
 
     if (this.pageStart < this.itemCount - 1) {
-        $('newer_link').href = '/#' + this.baseFrag + '/p' + (this.pageStart + this.itemsPerPage);
+        $('newer_link').href = baseURL + '/#' + this.baseFrag + '/p' + (this.pageStart + this.itemsPerPage);
         $('newer_link').show();
     } else {
         $('newer_link').hide();
     }
 
     if (this.pageStart >= 10) {
-        $('older_link').href = '/#' + this.baseFrag + '/p' + (this.pageStart - this.itemsPerPage);
+        $('older_link').href = baseURL + '/#' + this.baseFrag + '/p' + (this.pageStart - this.itemsPerPage);
         $('older_link').show();
     } else {
         $('older_link').hide();
@@ -253,13 +271,13 @@ User.prototype.show = function() {
     $('rss').show();
     $('rsslink').href = '/rss/' + this.username;
     $$('[name=user.reflink]').each(function(e) {
-        e.href = '/#/ref/' + this.username;
+        e.href = baseURL + '/#/ref/' + this.username;
     }.bind(this));
     $('usercontrols').show();
 
-    if (this.permalink && this.pageStart) {
+    if (this.permalink && this.pageStart >= 0) {
         this.showRecord(this.pageStart);
-    } else if (this.pageStart) {
+    } else if (this.pageStart >= 0) {
         this.showPageAt(this.pageStart);
     } else {
         this.reload();
@@ -311,6 +329,9 @@ User.prototype.reload = function() {
 }
 
 User.prototype.loadItems = function(from, to, continuation) {
+    if (to < 0)
+        return;
+
     var url;
     if (from != undefined && to != undefined) {
         url = baseURL + '/get/' + this.username + '/' + from + '-' + to;
@@ -537,6 +558,7 @@ function Feed(m) {
                 if (!this.pageStart)
                     this.pageStart = response.length - 1;
                 this.itemCount = response.length;
+                loginStatus.requestFeedStatus();
             }
             this.displayItems();
         }.bind(this),
@@ -719,12 +741,13 @@ function stopTicker() {
 
 function loadLatest() {
     new Ajax.Request(baseURL + '/latest.json', {
+        method: 'GET',
         onSuccess: function(r) {
             var j = r.responseText.evalJSON();
 
             $('latest-tags').update();
             j.tags.each(function(v) {
-                var a = new Element('a', {href: '/#/tag/' + v});
+                var a = new Element('a', {href: baseURL + '/#/tag/' + v});
                 a.insert('#' + v);
                 a.onclick = "return qlink()";
                 a.className = 'ref';