Log out whether or not the server reports success
[blerg.git] / www / js / blerg.js
index 21d7d34..b0bc38f 100644 (file)
@@ -5,10 +5,10 @@
 // Config
 var baseURL = '';
 var recordTemplate = new Template(
-    '<div class="record">#{data}<div class="info">Posted #{date}. <a href="/\##{author}/#{record}">[permalink]</a></div></div>'
+    '<div class="record">#{data}<div class="info">Posted #{date}. <a href="/\##{author}/#{record}">[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}</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="/\##{author}/#{record}">[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>'
@@ -69,7 +69,7 @@ LoginStatus.prototype.logout = function() {
         parameters: {
             username: this.username
         },
-        onSuccess: function(r) {
+        onComplete: function(r) {
             this.loggedIn = false;
             document.cookie = "auth=; expires=1-Jan-1970 00:00:00 GMT";
             this.update();
@@ -209,18 +209,38 @@ Pager.prototype.showRecord = function(r) {
     }
 }
 
+Pager.prototype.loadItems = function(from, to, continuation) { }
+
 
 // Object to render user pages
 function User(username) {
     this.initPager();
     this.username = username;
     this.baseFrag = username;
+}
+User.prototype = new Pager();
+User.prototype.constructor = User;
+
+User.prototype.show = function() {
+    $$('[name=section]').each(function(v) { v.update(' @' + this.username) }.bind(this));
+    $('welcome').hide();
+    items.show();
+    $('rss').show();
+    $('rsslink').href = '/rss/' + this.username;
+    $$('[name=user.reflink]').each(function(e) {
+        e.href = '/#/ref/' + this.username;
+    }.bind(this));
+    $('usercontrols').show();
+}
+
+User.prototype.reload = function() {
+    this.pageStart = null;
 
     $$('[name=user.subscribelink]').each(Element.hide);
     $$('[name=user.unsubscribelink]').each(Element.hide);
 
     if (loginStatus.loggedIn) {
-        new Ajax.Request(baseURL + '/feedinfo/' + username, {
+        new Ajax.Request(baseURL + '/feedinfo/' + this.username, {
             method: 'post',
             parameters: {
                 username: loginStatus.username
@@ -238,7 +258,7 @@ function User(username) {
         });
     }
 
-    new Ajax.Request(baseURL + '/info/' + username, {
+    new Ajax.Request(baseURL + '/info/' + this.username, {
         method: 'get',
         onSuccess: function(r) {
             var j = r.responseText.evalJSON();
@@ -250,20 +270,6 @@ function User(username) {
         }.bind(this)
     });
 }
-User.prototype = new Pager();
-User.prototype.constructor = User;
-
-User.prototype.show = function() {
-    $$('[name=section]').each(function(v) { v.update(' @' + this.username) }.bind(this));
-    $('welcome').hide();
-    items.show();
-    $('rss').show();
-    $('rsslink').href = '/rss/' + this.username;
-    $$('[name=user.reflink]').each(function(e) {
-        e.href = '/#/ref/' + this.username;
-    }.bind(this));
-    $('usercontrols').show();
-}
 
 User.prototype.loadItems = function(from, to, continuation) {
     var url;
@@ -341,12 +347,12 @@ function mangleRecord(record, template) {
         l = l.replace(/(\s|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?)\)/g, '$1<a href="$3">$2</a>');
 
         // Turn *foo* into italics and **foo** into bold
-        l = l.replace(/(\W|^)\*\*(\w[^*]*)\*\*(\W|$)/g, '$1<b>$2</b>$3');
-        l = l.replace(/(\W|^)\*(\w[^*]*)\*(\W|$)/g, '$1<i>$2</i>$3');
+        l = l.replace(/([^\w\\]|^)\*\*(\w[^*]*)\*\*(\W|$)/g, '$1<b>$2</b>$3');
+        l = l.replace(/([^\w\\]|^)\*(\w[^*]*)\*(\W|$)/g, '$1<i>$2</i>$3');
 
         // Turn refs and tags into links
         l = l.replace(/(\s|^)#([A-Za-z0-9_-]+)/g, '$1<a href="#/tag/$2" class="ref" onclick="return qlink()">#$2</a>');
-        l = l.replace(/(\s|^)@([A-Za-z0-9_-]+)/g, '$1<a href="#$2" class="ref" onclick="return qlink()">@$2</a>');
+        l = l.replace(/(\s|^)@([A-Za-z0-9_-]+)(\/\d+)?/g, '$1<a href="#$2$3" class="ref" onclick="return qlink()">@$2</a>');
 
         // Create lists when lines begin with *
         if (l[0] == '*') {
@@ -360,17 +366,20 @@ function mangleRecord(record, template) {
             endpush = '</li>';
         }
 
-        // Create headers when lines begin with =
-        if (l[0] == '=') {
-            var m = l.match(/^(=+)/);
+        // Create headers when lines begin with = or #
+        if (l[0] == '=' || l[0] == '#') {
+            var m = l.match(/^([=#]+)/);
             var depth = m[1].length;
             if (depth <= 5) {
-                l = l.replace(/^=+\s*/, '').replace(/\s*=+$/, '');
+                l = l.replace(/^[=#]+\s*/, '').replace(/\s*[=#]+$/, '');
                 out.push('<h' + depth + '>');
                 endpush = '</h' + depth + '>';
             }
         }
 
+        // Remove backslashes from escaped metachars
+        l = l.replace(/\\([*\[\]@#])/g, '$1');
+
         out.push(l);
         if (endpush) {
             out.push(endpush);
@@ -505,14 +514,16 @@ Feed.prototype.show = function() {
 }
 
 
-function postPopup() {
-    if (loginStatus.loggedIn) {
+function postPopup(initial) {
+    if (loginStatus.loggedIn || initial) {
         var post = $('post');
         if (post.visible()) {
             post.hide();
         } else {
             post.show();
-            if (currentPager.username && currentPager.username != loginStatus.username && !$('post.content').value) {
+            if (initial) {
+                $('post.content').value = initial;
+            } else if (!$('post.content').value && currentPager.username && currentPager.username != loginStatus.username) {
                 $('post.content').value = '@' + currentPager.username + ': ';
             }
             $('post.content').focus();
@@ -711,6 +722,8 @@ function hashSwitch() {
         if (m[3]) {
             var r = parseInt(m[3]);
             currentPager.showPageAt(r);
+        } else {
+            currentPager.showPageAt(currentPager.itemCount - 1);
         }
     } else if (m = location.hash.match(/^#\/feed(?:\/p(\d+))?$/)) {
         if (loginStatus.loggedIn) {
@@ -720,6 +733,8 @@ function hashSwitch() {
             if (m[3]) {
                 var r = parseInt(m[3]);
                 currentPager.showPageAt(r);
+            } else {
+                currentPager.showPageAt(currentPager.itemCount - 1);
             }
         } else {
             location.href = baseURL;
@@ -737,8 +752,9 @@ function hashSwitch() {
             } else {
                 currentPager.showRecord(r);
             }
+        } else {
+            currentPager.reload();
         }
-        // Do nothing otherwise, the User constructor takes care of it
     } else {
         $$('[name=section]').each(function(v) { v.update('Welcome') });
         $('signup').hide();