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 // Sanitize HTML input
276 record.data = record.data.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
278 // Turn HTTP URLs into links
279 record.data = record.data.replace(/(\s|^)(https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/([^\s"]*[^.!,;?()\s])?)?)/g, '$1<a href="$2">$2</a>');
281 // Turn markdown links into links
282 record.data = record.data.replace(/(\s|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?)\)/g, '$1<a href="$3">$2</a>');
284 // Turn *foo* into italics and **foo** into bold
285 record.data = record.data.replace(/(\s)\*\*([^*]+)\*\*(\s)/g, '$1<b>$2</b>$3');
286 record.data = record.data.replace(/(\s)\*([^*]+)\*(\s)/g, '$1<i>$2</i>$3');
288 // Turn refs and tags into links
289 record.data = record.data.replace(/(\s|^)#([A-Za-z0-9_-]+)/g, '$1<a href="#tag/$2" class="ref">#$2</a>');
290 record.data = record.data.replace(/(\s|^)@([A-Za-z0-9_-]+)/g, '$1<a href="#$2" class="ref">@$2</a>');
292 // Turn newlines into linebreaks and paragraphs
293 record.data = record.data.replace(/\r?\n\r?\n/g, "<p>").replace(/\r?\n/g, "<br>");
295 record.date = (new Date(record.timestamp * 1000)).toString();
296 record.html = template.evaluate(record);
299 function displayError(msg) {
300 items.innerText = msg;
304 // Object for browsing tags
305 function Tag(type, tag) {
310 var url = baseURL + "/tag/";
314 url += 'H'; // apache is eating the hash, even encoded. Probably a security feature.
320 alert('Invalid tag type: ' + type);
325 new Ajax.Request(url, {
327 onSuccess: function(r) {
328 var j = r.responseText.evalJSON();
330 var maxid = j.length - 1;
331 j.each(function(v, i) {
333 mangleRecord(v, tagRecordTemplate)
336 this.pageStart = j.length - 1;
340 onFailure: function(r) {
346 Tag.prototype = new Pager();
347 Tag.prototype.constructor = Tag;
349 Tag.prototype.show = function() {
350 var ctype = {ref: '@', tag: '#'}[this.type];
352 $$('[name=section]').each(function(v) {
353 v.update(' about ' + ctype + this.tag);
357 $('older_link').hide();
358 $('newer_link').hide();
361 $('usercontrols').hide();
364 function postPopup() {
365 if (loginStatus.loggedIn) {
366 var post = $('post');
367 if (post.visible()) {
371 if (currentPager.username && currentPager.username != loginStatus.username && !$('post.content').value) {
372 $('post.content').value = '@' + currentPager.username + ': ';
374 $('post.content').focus();
380 var username = $('signup.username').value;
381 var password = $('signup.password').value;
383 new Ajax.Request(baseURL + '/create', {
388 onSuccess: function(r) {
390 location.href = '/#' + username;
393 loginStatus.login(username, password);
395 onFailure: function(r) {
396 alert("Failed to create user");
401 function signup_cancel() {
406 function newer_page() {
408 currentPager.newerPage();
411 function older_page() {
413 currentPager.olderPage();
417 new Ajax.Request(baseURL + '/subscribe/' + currentPager.username, {
420 username: loginStatus.username
422 onSuccess: function(r) {
423 var response = r.responseText.evalJSON();
424 if (response.status == 'success') {
425 alert("You call " + currentPager.username + " and begin breathing heavily into the handset.");
426 $$('[name=user.obsesslink]').each(Element.hide);
427 $$('[name=user.unobsesslink]').each(Element.show);
429 alert('Failed to obsess. This is probably for the best');
432 onFailure: function(r) {
433 alert('Failed to obsess. This is probably for the best');
438 function unobsess() {
439 new Ajax.Request(baseURL + '/unsubscribe/' + currentPager.username, {
442 username: loginStatus.username
444 onSuccess: function(r) {
445 var response = r.responseText.evalJSON();
446 if (response.status == 'success') {
447 alert("You come to your senses.");
448 $$('[name=user.obsesslink]').each(Element.show);
449 $$('[name=user.unobsesslink]').each(Element.hide);
451 alert('You are unable to tear yourself away (because something failed on the server)');
454 onFailure: function(r) {
455 alert('You are unable to tear yourself away (because something failed on the server)');
460 var resizePostContentTimeout = null;
461 function resizePostContent() {
462 if (resizePostContentTimeout)
463 clearTimeout(resizePostContentTimeout);
464 resizePostContentTimeout = setTimeout(function() {
465 var c = $('post.content');
466 var lines = Math.floor(c.value.length / (100 * (c.clientWidth / 1000))) + 1;
467 var m = c.value.match(/\r?\n/g);
473 c.style.height = (lines * 17) + "pt";
475 resizePostContentTimeout = null;
479 var tickerTimer = null;
480 var tickerHead, tickerTail;
482 function tickerFader(a, b, p) {
486 a.style.lineHeight = (100 * p) + '%';
488 b.style.opacity = p2;
489 b.style.lineHeight = (100 * p2) + '%';
496 Bytex64.FX.run(tickerFader.curry(tickerHead, tickerTail), 0.5);
497 tickerHead = tickerHead.nextSibling;
498 tickerTail = tickerTail.nextSibling;
499 if (tickerHead == null) {
501 loadLatest.delay(10);
505 function startTicker() {
507 for (var elem = $('latest-posts').firstChild; elem != null; elem = elem.nextSibling) {
511 // Show the first five
512 tickerHead = $('latest-posts').firstChild;
513 for (var i = 0; i < 10 && tickerHead; i++) {
515 tickerHead = tickerHead.nextSibling;
517 tickerTail = $('latest-posts').firstChild;
518 tickerTimer = setInterval(ticker, 5000);
521 function stopTicker() {
523 clearInterval(tickerTimer);
527 function loadLatest() {
528 new Ajax.Request(baseURL + '/latest.json', {
529 onSuccess: function(r) {
530 var j = r.responseText.evalJSON();
532 $('latest-tags').update();
533 j.tags.each(function(v) {
534 var a = new Element('a', {href: '/#tag/' + v});
537 $('latest-tags').insert(a);
538 $('latest-tags').appendChild(document.createTextNode(' '));
541 $('latest-posts').update();
542 j.records.each(function(v) {
543 v.data = v.data.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
544 v.date = (new Date(v.timestamp * 1000)).toString();
545 var html = latestRecordsTemplate.evaluate(v);
546 $('latest-posts').insert(html);
553 function hashSwitch() {
556 if (m = location.search.match(/^\?post\/([^/]+)\/(.+)/)) {
558 $('post.content').value = '[' + decodeURIComponent(m[1]).replace(']','').replace('[','') + '](' + decodeURIComponent(m[2]) + ')';
559 } else if (m = location.hash.match(/^#(ref|tag)\/([A-Za-z0-9_-]+)$/)) {
560 currentPager = new Tag(m[1], m[2]);
562 } else if (m = location.hash.match(/^#([A-Za-z0-9_-]+)(:(\d+))?$/)) {
563 if (!currentPager || currentPager.username != m[1])
564 currentPager = new User(m[1]);
566 loginStatus.update();
569 var r = parseInt(m[3]);
570 currentPager.showRecord = r;
571 if (currentPager.recordCache[r]) {
572 currentPager.displayItems();
574 currentPager.loadItems((r >= 49 ? r - 49 : 0), r);
577 currentPager.pageStart = currentPager.itemCount - 1;
578 currentPager.loadItems();
581 $$('[name=section]').each(function(v) { v.update('Welcome') });
585 $('newer_link').hide();
586 $('older_link').hide();
589 $('usercontrols').hide();
595 function hashCheck() {
596 if (location.hash != lastHash) {
597 lastHash = location.hash;
604 loginStatus = new LoginStatus();
606 lastHash = location.hash;
609 setInterval(hashCheck, 250);
611 document.body.observe('keyup', function(event) {
612 if (event.shiftKey && event.keyCode == '32') {
617 $('post.content').addEventListener('keyup', function(event) {
618 event.stopPropagation();