1 /* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
2 * BSD-style license. Please see the COPYING file for details.
7 var recordTemplate = new Template(
8 '<div class="record">#{data}<div class="info">Posted #{date}</div></div>'
10 var tagRecordTemplate = new Template(
11 '<div class="record">#{data}<div class="info">Posted by <a class="author ref" href="/\##{author}">@#{author}</a> on #{date}</div></div>'
13 var latestRecordsTemplate = new Template(
14 '<div class="record"><a class="author ref" href="/\##{author}">@#{author}</a> #{data}</div>'
24 // Object to keep track of login status
25 function LoginStatus() {
27 document.cookie.split(/;\s+/).each(function(v) {
29 cookies[kv[0]] = kv[1];
31 if (cookies.auth && cookies.username) {
33 this.username = cookies.username;
35 this.loggedIn = false;
41 LoginStatus.prototype.login = function(username, password) {
42 new Ajax.Request(baseURL + '/login', {
47 onSuccess: function(r) {
48 var j = r.responseText.evalJSON();
49 if (j && j.status == 'success') {
51 this.username = username;
52 document.cookie = "username=" + username;
53 $('login.password').value = '';
56 alert("Could not log in");
57 $('login.username').focus();
60 onFailure: function(r) {
61 alert("Could not log in");
62 $('login.username').focus();
67 LoginStatus.prototype.logout = function() {
68 new Ajax.Request(baseURL + '/logout', {
70 username: this.username
72 onSuccess: function(r) {
73 this.loggedIn = false;
74 document.cookie = "auth=; expires=1-Jan-1970 00:00:00 GMT";
78 document.cookie = "username=; expires=1-Jan-1970 00:00:00 GMT";
81 LoginStatus.prototype.update = function() {
83 $('userlink').href = '/#' + this.username;
84 $('userlink').update('@' + this.username);
85 $('reflink').href = '/#ref/' + this.username;
95 LoginStatus.prototype.post = function(msg) {
97 alert("You are not logged in!");
101 new Ajax.Request(baseURL + '/put', {
103 username: this.username,
106 onSuccess: function(r) {
107 var j = r.responseText.evalJSON();
108 if (j && j.status == 'success') {
109 $('post.content').value = '';
110 if (location.hash != '#' + this.username) {
111 location.href = '/#' + this.username;
114 currentPager.itemCount++;
115 currentPager.pageStart = null;
116 currentPager.loadItems();
119 alert('Post failed!');
122 onFailure: function(r) {
123 alert('Post failed!');
129 // Base object for paged data
131 this.itemsPerPage = 10;
134 Pager.prototype.initPager = function() {
135 this.itemCache = new Hash();
136 this.pageStart = null;
139 Pager.prototype.olderPage = function() {
140 if (this.pageStart >= this.itemsPerPage) {
141 this.pageStart -= this.itemsPerPage;
146 Pager.prototype.newerPage = function() {
147 if (this.pageStart + this.itemsPerPage < this.itemCount) {
148 this.pageStart += this.itemsPerPage;
153 Pager.prototype.addItems = function(items) {
154 items.each(function(v) {
155 if (!this.itemCache[v.id])
156 this.itemCache[v.id] = v;
160 Pager.prototype.displayItems = function() {
161 if (this.pageStart == undefined)
162 this.pageStart == this.itemCount - 1;
165 if (this.pageStart != undefined && this.itemCache[this.pageStart]) {
166 var end = (this.pageStart >= this.itemsPerPage ? this.pageStart - this.itemsPerPage + 1 : 0);
167 for (var i = this.pageStart; i >= end; i--) {
168 items.insert(this.itemCache[i].html);
171 items.insert("There doesn't seem to be anything here!");
174 if (this.pageStart < this.itemCount - 1)
175 $('newer_link').show();
177 $('newer_link').hide();
179 if (this.pageStart >= 10)
180 $('older_link').show();
182 $('older_link').hide();
186 // Object to render user pages
187 function User(username) {
189 this.username = username;
191 $$('[name=user.obsesslink]').each(Element.hide);
192 $$('[name=user.unobsesslink]').each(Element.hide);
194 if (loginStatus.loggedIn) {
195 new Ajax.Request(baseURL + '/feedinfo/' + username, {
198 username: loginStatus.username
200 onSuccess: function(r) {
201 var json = r.responseText.evalJSON();
202 if (json.subscribed) {
203 $$('[name=user.obsesslink]').each(Element.hide);
204 $$('[name=user.unobsesslink]').each(Element.show);
206 $$('[name=user.obsesslink]').each(Element.show);
207 $$('[name=user.unobsesslink]').each(Element.hide);
213 new Ajax.Request(baseURL + '/info/' + username, {
215 onSuccess: function(r) {
216 var j = r.responseText.evalJSON();
218 this.itemCount = parseInt(j.record_count);
224 User.prototype = new Pager();
225 User.prototype.constructor = User;
227 User.prototype.show = function() {
228 $$('[name=section]').each(function(v) { v.update(' @' + this.username) }.bind(this));
232 $('rsslink').href = '/rss/' + this.username;
233 $$('[name=user.reflink]').each(function(e) {
234 e.href = '/#ref/' + this.username;
236 $('usercontrols').show();
239 User.prototype.loadItems = function(from, to) {
241 if (from != undefined && to != undefined) {
242 url = baseURL + '/get/' + this.username + '/' + from + '-' + to;
245 url = baseURL + '/get/' + this.username;
248 new Ajax.Request(url, {
250 onSuccess: function(r) {
251 var records = r.responseText.evalJSON();
252 if (records && records.length > 0) {
253 records.each(function(v) {
255 mangleRecord(v, recordTemplate);
257 this.addItems(records);
259 this.pageStart = records[0].recInt;
263 onFailure: function(r) {
267 displayError('User not found');
272 function mangleRecord(record, template) {
273 record.recInt = parseInt(record.record);
275 var lines = record.data.split(/\r?\n/);
276 if (lines[lines.length - 1] == '')
281 var listMode = false;
282 lines.each(function(l) {
284 if (out[out.length - 1] == '<br>') {
285 out[out.length - 1] = '<p>';
287 if (out[out.length - 1] == '</li>') {
296 var pi = out.lastIndexOf('<p>');
298 out[pi] = '<p class="quote">';
299 l = l.replace(/^>\s*/, '');
304 var pi = out.lastIndexOf('<p>');
308 l = l.replace(/^\*\s*/, '');
313 var m = l.match(/^(=+)/);
314 var depth = m[1].length;
316 l = l.replace(/^=+\s*/, '').replace(/\s*=+$/, '');
317 out.push('<h' + depth + '>');
318 endpush = '</h' + depth + '>';
322 // Sanitize HTML input
323 l = l.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
325 // Turn HTTP URLs into links
326 l = l.replace(/(\s|^)(https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/([^\s"]*[^.!,;?()\s])?)?)/g, '$1<a href="$2">$2</a>');
328 // Turn markdown links into links
329 l = l.replace(/(\s|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?)\)/g, '$1<a href="$3">$2</a>');
331 // Turn *foo* into italics and **foo** into bold
332 l = l.replace(/(\s)\*\*([^*]+)\*\*(\s)/g, '$1<b>$2</b>$3');
333 l = l.replace(/(\s)\*([^*]+)\*(\s)/g, '$1<i>$2</i>$3');
335 // Turn refs and tags into links
336 l = l.replace(/(\s|^)#([A-Za-z0-9_-]+)/g, '$1<a href="#tag/$2" class="ref">#$2</a>');
337 l = l.replace(/(\s|^)@([A-Za-z0-9_-]+)/g, '$1<a href="#$2" class="ref">@$2</a>');
347 while (out[out.length - 1] == '<br>' || out[out.length - 1] == '<p>')
350 record.data = out.join('');
351 record.date = (new Date(record.timestamp * 1000)).toString();
352 record.html = template.evaluate(record);
355 function displayError(msg) {
356 items.innerText = msg;
360 // Object for browsing tags
361 function Tag(type, tag) {
366 var url = baseURL + "/tag/";
370 url += 'H'; // apache is eating the hash, even encoded. Probably a security feature.
376 alert('Invalid tag type: ' + type);
381 new Ajax.Request(url, {
383 onSuccess: function(r) {
384 var j = r.responseText.evalJSON();
386 var maxid = j.length - 1;
387 j.each(function(v, i) {
389 mangleRecord(v, tagRecordTemplate)
392 this.pageStart = j.length - 1;
396 onFailure: function(r) {
402 Tag.prototype = new Pager();
403 Tag.prototype.constructor = Tag;
405 Tag.prototype.show = function() {
406 var ctype = {ref: '@', tag: '#'}[this.type];
408 $$('[name=section]').each(function(v) {
409 v.update(' about ' + ctype + this.tag);
413 $('older_link').hide();
414 $('newer_link').hide();
417 $('usercontrols').hide();
420 function postPopup() {
421 if (loginStatus.loggedIn) {
422 var post = $('post');
423 if (post.visible()) {
427 if (currentPager.username && currentPager.username != loginStatus.username && !$('post.content').value) {
428 $('post.content').value = '@' + currentPager.username + ': ';
430 $('post.content').focus();
436 var username = $('signup.username').value;
437 var password = $('signup.password').value;
439 new Ajax.Request(baseURL + '/create', {
444 onSuccess: function(r) {
446 location.href = '/#' + username;
449 loginStatus.login(username, password);
451 onFailure: function(r) {
452 alert("Failed to create user");
457 function signup_cancel() {
462 function newer_page() {
464 currentPager.newerPage();
467 function older_page() {
469 currentPager.olderPage();
473 new Ajax.Request(baseURL + '/subscribe/' + currentPager.username, {
476 username: loginStatus.username
478 onSuccess: function(r) {
479 var response = r.responseText.evalJSON();
480 if (response.status == 'success') {
481 alert("You call " + currentPager.username + " and begin breathing heavily into the handset.");
482 $$('[name=user.obsesslink]').each(Element.hide);
483 $$('[name=user.unobsesslink]').each(Element.show);
485 alert('Failed to obsess. This is probably for the best');
488 onFailure: function(r) {
489 alert('Failed to obsess. This is probably for the best');
494 function unobsess() {
495 new Ajax.Request(baseURL + '/unsubscribe/' + currentPager.username, {
498 username: loginStatus.username
500 onSuccess: function(r) {
501 var response = r.responseText.evalJSON();
502 if (response.status == 'success') {
503 alert("You come to your senses.");
504 $$('[name=user.obsesslink]').each(Element.show);
505 $$('[name=user.unobsesslink]').each(Element.hide);
507 alert('You are unable to tear yourself away (because something failed on the server)');
510 onFailure: function(r) {
511 alert('You are unable to tear yourself away (because something failed on the server)');
516 var resizePostContentTimeout = null;
517 function resizePostContent() {
518 if (resizePostContentTimeout)
519 clearTimeout(resizePostContentTimeout);
520 resizePostContentTimeout = setTimeout(function() {
521 var c = $('post.content');
522 var lines = Math.floor(c.value.length / (100 * (c.clientWidth / 1000))) + 1;
523 var m = c.value.match(/\r?\n/g);
529 c.style.height = (lines * 17) + "pt";
531 resizePostContentTimeout = null;
535 var tickerTimer = null;
536 var tickerHead, tickerTail;
538 function tickerFader(a, b, p) {
542 a.style.lineHeight = (100 * p) + '%';
544 b.style.opacity = p2;
545 b.style.lineHeight = (100 * p2) + '%';
552 Bytex64.FX.run(tickerFader.curry(tickerHead, tickerTail), 0.5);
553 tickerHead = tickerHead.nextSibling;
554 tickerTail = tickerTail.nextSibling;
555 if (tickerHead == null) {
557 loadLatest.delay(10);
561 function startTicker() {
563 for (var elem = $('latest-posts').firstChild; elem != null; elem = elem.nextSibling) {
567 // Show the first five
568 tickerHead = $('latest-posts').firstChild;
569 for (var i = 0; i < 10 && tickerHead; i++) {
571 tickerHead = tickerHead.nextSibling;
573 tickerTail = $('latest-posts').firstChild;
574 tickerTimer = setInterval(ticker, 5000);
577 function stopTicker() {
579 clearInterval(tickerTimer);
583 function loadLatest() {
584 new Ajax.Request(baseURL + '/latest.json', {
585 onSuccess: function(r) {
586 var j = r.responseText.evalJSON();
588 $('latest-tags').update();
589 j.tags.each(function(v) {
590 var a = new Element('a', {href: '/#tag/' + v});
593 $('latest-tags').insert(a);
594 $('latest-tags').appendChild(document.createTextNode(' '));
597 $('latest-posts').update();
598 j.records.each(function(v) {
599 v.data = v.data.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
600 v.date = (new Date(v.timestamp * 1000)).toString();
601 var html = latestRecordsTemplate.evaluate(v);
602 $('latest-posts').insert(html);
609 function hashSwitch() {
612 if (m = location.search.match(/^\?post\/([^/]+)\/(.+)/)) {
614 $('post.content').value = '[' + decodeURIComponent(m[1]).replace(']','').replace('[','') + '](' + decodeURIComponent(m[2]) + ')';
615 } else if (m = location.hash.match(/^#(ref|tag)\/([A-Za-z0-9_-]+)$/)) {
616 currentPager = new Tag(m[1], m[2]);
618 } else if (m = location.hash.match(/^#([A-Za-z0-9_-]+)(:(\d+))?$/)) {
619 if (!currentPager || currentPager.username != m[1])
620 currentPager = new User(m[1]);
622 loginStatus.update();
625 var r = parseInt(m[3]);
626 currentPager.showRecord = r;
627 if (currentPager.itemCache[r]) {
628 currentPager.displayItems();
630 currentPager.loadItems((r >= 49 ? r - 49 : 0), r);
633 currentPager.pageStart = currentPager.itemCount - 1;
634 currentPager.loadItems();
637 $$('[name=section]').each(function(v) { v.update('Welcome') });
641 $('newer_link').hide();
642 $('older_link').hide();
645 $('usercontrols').hide();
651 function hashCheck() {
652 if (location.hash != lastHash) {
653 lastHash = location.hash;
660 loginStatus = new LoginStatus();
662 lastHash = location.hash;
665 setInterval(hashCheck, 250);
667 document.body.observe('keyup', function(event) {
668 if (event.shiftKey && event.keyCode == '32') {
673 $('post.content').addEventListener('keyup', function(event) {
674 event.stopPropagation();