Add bookmarklet support, clean up hashSwitch()
[blerg.git] / www / js / blerg.js
index bae782e..d6081eb 100644 (file)
@@ -8,7 +8,7 @@ var recordTemplate = new Template(
     '<div class="record">#{data}<div class="info">Posted #{date}</div></div>'
 );
 var tagRecordTemplate = new Template(
-    '<div class="record">#{data}<div class="info">Posted by <a href="\##{author}">@#{author}</a> on #{date}</div></div>'
+    '<div class="record">#{data}<div class="info">Posted by <a href="/\##{author}">@#{author}</a> on #{date}</div></div>'
 );
 
 // Page elements
@@ -77,7 +77,7 @@ LoginStatus.prototype.logout = function() {
 
 LoginStatus.prototype.update = function() {
     if (this.loggedIn) {
-        $('userlink').href = '#' + this.username;
+        $('userlink').href = '/#' + this.username;
         $('userlink').update('@' + this.username);
         $('login').hide();
         $('logout').show();
@@ -202,7 +202,7 @@ User.prototype.show = function() {
     $$('[name=section]').each(function(v) { v.update(' @' + this.username) }.bind(this));
     $('welcome').hide();
     items.show();
-    $('reflink').href = '#ref/' + this.username;
+    $('reflink').href = '/#ref/' + this.username;
     $('rss').show();
     $('rsslink').href = '/rss/' + this.username;
 }
@@ -399,29 +399,30 @@ function resizePostContent() {
 }
 
 function hashSwitch() {
-    var m = location.hash.match(/^#((ref|tag)\/)?(\w+)(:(\d+))?/);
-    if (m) {
-        if (m[1]) {
-            currentPager = new Tag(m[2], m[3]);
-            currentPager.show();
-        } else {
-            if (!currentPager || currentPager.username != m[3])
-                currentPager = new User(m[3]);
-            currentPager.show();
-            loginStatus.update();
-
-            if (m[5]) {
-                var r = parseInt(m[5]);
-                currentPager.showRecord = r;
-                if (currentPager.recordCache[r]) {
-                    currentPager.displayItems();
-                } else {
-                    currentPager.loadItems((r >= 49 ? r - 49 : 0), r);
-                }
+    var m;
+    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)\/(\w+)$/)) {
+        currentPager = new Tag(m[1], m[2]);
+        currentPager.show();
+    } else if (m = location.hash.match(/^#(\w+)(:(\d+))?$/)) {
+        if (!currentPager || currentPager.username != m[1])
+            currentPager = new User(m[1]);
+        currentPager.show();
+        loginStatus.update();
+
+        if (m[3]) {
+            var r = parseInt(m[3]);
+            currentPager.showRecord = r;
+            if (currentPager.recordCache[r]) {
+                currentPager.displayItems();
             } else {
-                currentPager.pageStart = currentPager.itemCount - 1;
-                currentPager.loadItems();
+                currentPager.loadItems((r >= 49 ? r - 49 : 0), r);
             }
+        } else {
+            currentPager.pageStart = currentPager.itemCount - 1;
+            currentPager.loadItems();
         }
     } else {
         $$('[name=section]').each(function(v) { v.update('Welcome') });