3 var recordTemplate = new Template(
4 '<div class="record">#{data}<div class="info">Posted #{date}</div></div>'
6 var tagRecordTemplate = new Template(
7 '<div class="record">#{data}<div class="info">Posted by <a href="\##{author}">@#{author}</a> on #{date}</div></div>'
17 // Object to keep track of login status
18 function LoginStatus() {
20 document.cookie.split(/;\s+/).each(function(v) {
22 cookies[kv[0]] = kv[1];
24 if (cookies.auth && cookies.username) {
26 this.username = cookies.username;
28 this.loggedIn = false;
34 LoginStatus.prototype.login = function(username, password) {
35 new Ajax.Request(baseURL + '/login', {
40 onSuccess: function(r) {
41 var j = r.responseText.evalJSON();
42 if (j && j.status == 'success') {
44 this.username = username;
45 document.cookie = "username=" + username;
46 $('login.password').value = '';
49 alert("Could not log in");
50 $('login.username').focus();
53 onFailure: function(r) {
54 alert("Could not log in");
55 $('login.username').focus();
60 LoginStatus.prototype.logout = function() {
61 new Ajax.Request(baseURL + '/logout', {
63 username: this.username,
65 onSuccess: function(r) {
66 this.loggedIn = false;
67 document.cookie = "auth=; expires=1-Jan-1970 00:00:00 GMT";
71 document.cookie = "username=; expires=1-Jan-1970 00:00:00 GMT";
74 LoginStatus.prototype.update = function() {
76 $('userlink').href = '#' + this.username;
77 $('userlink').update('@' + this.username);
87 LoginStatus.prototype.post = function(msg) {
89 alert("You are not logged in!");
93 new Ajax.Request(baseURL + '/put', {
95 username: this.username,
98 onSuccess: function(r) {
99 var j = r.responseText.evalJSON();
100 if (j && j.status == 'success') {
101 $('post.content').value = '';
102 if (location.hash != '#' + this.username) {
103 location.hash = this.username;
106 currentPager.itemCount++;
107 currentPager.pageStart = null;
108 currentPager.loadItems();
111 alert('Post failed!');
114 onFailure: function(r) {
115 alert('Post failed!');
121 // Base object for paged data
123 this.itemsPerPage = 10;
126 Pager.prototype.initPager = function() {
127 this.itemCache = new Hash();
128 this.pageStart = null;
131 Pager.prototype.olderPage = function() {
132 if (this.pageStart >= this.itemsPerPage) {
133 this.pageStart -= this.itemsPerPage;
138 Pager.prototype.newerPage = function() {
139 if (this.pageStart + this.itemsPerPage < this.itemCount) {
140 this.pageStart += this.itemsPerPage;
145 Pager.prototype.addItems = function(items) {
146 items.each(function(v) {
147 if (!this.itemCache[v.id])
148 this.itemCache[v.id] = v;
152 Pager.prototype.displayItems = function() {
153 if (this.pageStart == undefined)
154 this.pageStart == this.itemCount - 1;
157 if (this.pageStart != undefined && this.itemCache[this.pageStart]) {
158 var end = (this.pageStart >= this.itemsPerPage ? this.pageStart - this.itemsPerPage + 1 : 0);
159 for (var i = this.pageStart; i >= end; i--) {
160 items.insert(this.itemCache[i].html);
163 items.insert("There doesn't seem to be anything here!");
166 if (this.pageStart < this.itemCount - 1)
167 $('newer_link').show();
169 $('newer_link').hide();
171 if (this.pageStart >= 10)
172 $('older_link').show();
174 $('older_link').hide();
178 // Object to render user pages
179 function User(username) {
181 this.username = username;
183 new Ajax.Request(baseURL + '/info/' + username, {
185 onSuccess: function(r) {
186 var j = r.responseText.evalJSON();
188 this.itemCount = parseInt(j.record_count);
194 User.prototype = new Pager();
195 User.prototype.constructor = User;
197 User.prototype.show = function() {
198 $$('[name=section]').each(function(v) { v.update(' @' + this.username) }.bind(this));
201 $('reflink').href = '#ref/' + this.username;
203 $('rsslink').href = '/rss/' + this.username;
206 User.prototype.loadItems = function(from, to) {
208 if (from != undefined && to != undefined) {
209 url = baseURL + '/get/' + this.username + '/' + from + '-' + to;
212 url = baseURL + '/get/' + this.username;
215 new Ajax.Request(url, {
217 onSuccess: function(r) {
218 var records = r.responseText.evalJSON();
219 if (records && records.length > 0) {
220 records.each(function(v) {
222 mangleRecord(v, recordTemplate);
224 this.addItems(records);
226 this.pageStart = records[0].recInt;
230 onFailure: function(r) {
234 displayError('User not found');
239 function mangleRecord(record, template) {
240 record.recInt = parseInt(record.record);
242 // Sanitize HTML input
243 record.data = record.data.replace('&', '&').replace('<', '<').replace('>', '>');
245 // Turn HTTP URLs into links
246 record.data = record.data.replace(/(\s|^)(https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/(\S*[^.!,;?()\s])?)?)/g, '$1<a href="$2">$2</a>');
248 // Turn markdown links into links
249 record.data = record.data.replace(/(\s|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)]*?)?)\)/, '$1<a href="$3">$2</a>');
251 // Turn refs and tags into links
252 record.data = record.data.replace(/(\s|^)#(\w+)/g, '$1<a href="#tag/$2">#$2</a>');
253 record.data = record.data.replace(/(\s|^)@(\w+)/g, '$1<a href="#$2">@$2</a>');
255 // Turn newlines into linebreaks and paragraphs
256 record.data = record.data.replace(/\r?\n\r?\n/g, "<p>").replace(/\r?\n/g, "<br>");
258 record.date = (new Date(record.timestamp * 1000)).toString();
259 record.html = template.evaluate(record);
262 function displayError(msg) {
263 items.innerText = msg;
267 // Object for browsing tags
268 function Tag(type, tag) {
273 var url = baseURL + "/tag/";
277 url += 'H'; // apache is eating the hash, even encoded. Probably a security feature.
283 alert('Invalid tag type: ' + type);
288 new Ajax.Request(url, {
290 onSuccess: function(r) {
291 var j = r.responseText.evalJSON();
293 var maxid = j.length - 1;
294 j.each(function(v, i) {
296 mangleRecord(v, tagRecordTemplate)
299 this.pageStart = j.length - 1;
303 onFailure: function(r) {
309 Tag.prototype = new Pager();
310 Tag.prototype.constructor = Tag;
312 Tag.prototype.show = function() {
313 var ctype = {ref: '@', tag: '#'}[this.type];
315 $$('[name=section]').each(function(v) {
316 v.update(' about ' + ctype + this.tag);
320 $('older_link').hide();
321 $('newer_link').hide();
326 function postPopup() {
327 if (loginStatus.loggedIn) {
328 var post = $('post');
329 if (post.visible()) {
333 if (currentPager.username && currentPager.username != loginStatus.username && !$('post.content').value) {
334 $('post.content').value = '@' + currentPager.username + ': ';
336 $('post.content').focus();
342 var username = $('signup.username').value;
343 var password = $('signup.password').value;
345 new Ajax.Request(baseURL + '/create', {
350 onSuccess: function(r) {
352 location.hash = username;
355 loginStatus.login(username, password);
357 onFailure: function(r) {
358 alert("Failed to create user");
363 function signup_cancel() {
368 function newer_page() {
370 currentPager.newerPage();
373 function older_page() {
375 currentPager.olderPage();
378 var resizePostContentTimeout = null;
379 function resizePostContent() {
380 if (resizePostContentTimeout)
381 clearTimeout(resizePostContentTimeout);
382 resizePostContentTimeout = setTimeout(function() {
383 var c = $('post.content');
384 var lines = Math.floor(c.value.length / (100 * (c.clientWidth / 1000))) + 1;
385 var m = c.value.match(/\r?\n/g);
391 c.style.height = (lines * 17) + "pt";
393 resizePostContentTimeout = null;
397 function hashSwitch() {
398 var m = location.hash.match(/^#((ref|tag)\/)?(\w+)(:(\d+))?/);
401 currentPager = new Tag(m[2], m[3]);
404 if (!currentPager || currentPager.username != m[3])
405 currentPager = new User(m[3]);
407 loginStatus.update();
410 var r = parseInt(m[5]);
411 currentPager.showRecord = r;
412 if (currentPager.recordCache[r]) {
413 currentPager.displayItems();
415 currentPager.loadItems((r >= 49 ? r - 49 : 0), r);
418 currentPager.pageStart = currentPager.itemCount - 1;
419 currentPager.loadItems();
423 $$('[name=section]').each(function(v) { v.update('Welcome') });
427 $('newer_link').hide();
428 $('older_link').hide();
435 function hashCheck() {
436 if (location.hash != lastHash) {
437 lastHash = location.hash;
444 loginStatus = new LoginStatus();
446 lastHash = location.hash;
449 setInterval(hashCheck, 250);
451 document.body.observe('keyup', function(event) {
452 if (event.shiftKey && event.keyCode == '32') {