-// Base object for paged data
-function Pager() {
- this.itemsPerPage = 10;
-}
-
-Pager.prototype.initPager = function() {
- this.itemCache = new Hash();
- this.pageStart = null;
-}
-
-Pager.prototype.olderPage = function() {
- if (this.pageStart >= this.itemsPerPage) {
- this.pageStart -= this.itemsPerPage;
- this.displayItems();
- }
-}
-
-Pager.prototype.newerPage = function() {
- if (this.pageStart + this.itemsPerPage < this.itemCount) {
- this.pageStart += this.itemsPerPage;
- this.displayItems();
- }
-}
-
-Pager.prototype.addItems = function(items) {
- items.each(function(v) {
- if (!this.itemCache[v.id])
- this.itemCache[v.id] = v;
- }.bind(this));
-}
-
-Pager.prototype.displayItems = function() {
- if (this.pageStart == undefined)
- this.pageStart == this.itemCount - 1;
- items.update();
-
- if (this.pageStart != undefined && this.itemCache[this.pageStart]) {
- var end = (this.pageStart >= this.itemsPerPage ? this.pageStart - this.itemsPerPage + 1 : 0);
- for (var i = this.pageStart; i >= end; i--) {
- items.insert(this.itemCache[i].html);
- }
- } else {
- items.insert("There doesn't seem to be anything here!");
- }
-
- if (this.pageStart < this.itemCount - 1)
- $('newer_link').show();
- else
- $('newer_link').hide();
-
- if (this.pageStart >= 10)
- $('older_link').show();
- else
- $('older_link').hide();
-}
-
-
-// Object to render user pages
-function User(username) {
- this.initPager();
- this.username = username;
-
- new Ajax.Request(baseURL + '/info/' + username, {
- method: 'get',
- onSuccess: function(r) {
- var j = r.responseText.evalJSON();
- if (j) {
- this.itemCount = parseInt(j.record_count);
- this.displayItems();
- }
- }.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();
- $('reflink').href = '/#ref/' + this.username;
- $('rss').show();
- $('rsslink').href = '/rss/' + this.username;
-}
-
-User.prototype.loadItems = function(from, to) {
- var url;
- if (from != undefined && to != undefined) {
- url = baseURL + '/get/' + this.username + '/' + from + '-' + to;
- this.pageStart = to;
- } else {
- url = baseURL + '/get/' + this.username;
- }
-
- new Ajax.Request(url, {
- method: 'get',
- onSuccess: function(r) {
- var records = r.responseText.evalJSON();
- if (records && records.length > 0) {
- records.each(function(v) {
- v.id = v.record;
- mangleRecord(v, recordTemplate);
- });
- this.addItems(records);
- if (!this.pageStart)
- this.pageStart = records[0].recInt;
- }
- this.displayItems();
- }.bind(this),
- onFailure: function(r) {
- this.displayItems();
- }.bind(this),
- on404: function(r) {
- displayError('User not found');
- }
- });
-}
-
-function mangleRecord(record, template) {
- record.recInt = parseInt(record.record);
-
- // Sanitize HTML input
- record.data = record.data.replace('&', '&').replace('<', '<').replace('>', '>');
-
- // Turn HTTP URLs into links
- record.data = record.data.replace(/(\s|^)(https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/([^\s"]*[^.!,;?()\s])?)?)/g, '$1<a href="$2">$2</a>');
-
- // Turn markdown links into links
- record.data = record.data.replace(/(\s|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?)\)/, '$1<a href="$3">$2</a>');
-
- // Turn *foo* into italics and **foo** into bold
- record.data = record.data.replace(/(\s)\*\*([^*]+)\*\*(\s)/, '$1<b>$2</b>$3');
- record.data = record.data.replace(/(\s)\*([^*]+)\*(\s)/, '$1<i>$2</i>$3');
-
- // Turn refs and tags into links
- record.data = record.data.replace(/(\s|^)#(\w+)/g, '$1<a href="#tag/$2">#$2</a>');
- record.data = record.data.replace(/(\s|^)@(\w+)/g, '$1<a href="#$2">@$2</a>');
-
- // Turn newlines into linebreaks and paragraphs
- record.data = record.data.replace(/\r?\n\r?\n/g, "<p>").replace(/\r?\n/g, "<br>");
-
- record.date = (new Date(record.timestamp * 1000)).toString();
- record.html = template.evaluate(record);
-}
-
-function displayError(msg) {
- items.innerText = msg;
-}
-
-
-// Object for browsing tags
-function Tag(type, tag) {
- this.initPager();
- this.type = type;
- this.tag = tag;
-
- var url = baseURL + "/tag/";
- switch(type) {
- case 'tag':
- //url += '%23';
- url += 'H'; // apache is eating the hash, even encoded. Probably a security feature.
- break;
- case 'ref':
- url += '%40';
- break;
- default:
- alert('Invalid tag type: ' + type);
- return;
- }
- url += tag;
-
- new Ajax.Request(url, {
- method: 'get',
- onSuccess: function(r) {
- var j = r.responseText.evalJSON();
- if (j) {
- var maxid = j.length - 1;
- j.each(function(v, i) {
- v.id = maxid - i;
- mangleRecord(v, tagRecordTemplate)
- });
- this.addItems(j);
- this.pageStart = j.length - 1;
- }
- this.displayItems();
- }.bind(this),
- onFailure: function(r) {
- this.displayItems();
- }.bind(this)
- });
-
-}
-Tag.prototype = new Pager();
-Tag.prototype.constructor = Tag;
-
-Tag.prototype.show = function() {
- var ctype = {ref: '@', tag: '#'}[this.type];
-
- $$('[name=section]').each(function(v) {
- v.update(' about ' + ctype + this.tag);
- }.bind(this));
- $('welcome').hide();
- $('post').hide();
- $('older_link').hide();
- $('newer_link').hide();
- $('rss').hide();
- items.show();
-}
-
-function postPopup() {
- if (loginStatus.loggedIn) {
- var post = $('post');
- if (post.visible()) {
- post.hide();
- } else {
- post.show();
- if (currentPager.username && currentPager.username != loginStatus.username && !$('post.content').value) {
- $('post.content').value = '@' + currentPager.username + ': ';
- }
- $('post.content').focus();
- }
- }
-}
-
-function signup() {
- var username = $('signup.username').value;
- var password = $('signup.password').value;
-
- new Ajax.Request(baseURL + '/create', {