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" href="/\##{author}">@#{author}</a> on #{date}</div></div>'
13 var latestRecordsTemplate = new Template(
14 '<div class="record"><a class="author" 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 new Ajax.Request(baseURL + '/info/' + username, {
193 onSuccess: function(r) {
194 var j = r.responseText.evalJSON();
196 this.itemCount = parseInt(j.record_count);
202 User.prototype = new Pager();
203 User.prototype.constructor = User;
205 User.prototype.show = function() {
206 $$('[name=section]').each(function(v) { v.update(' @' + this.username) }.bind(this));
210 $('rsslink').href = '/rss/' + this.username;
213 User.prototype.loadItems = function(from, to) {
215 if (from != undefined && to != undefined) {
216 url = baseURL + '/get/' + this.username + '/' + from + '-' + to;
219 url = baseURL + '/get/' + this.username;
222 new Ajax.Request(url, {
224 onSuccess: function(r) {
225 var records = r.responseText.evalJSON();
226 if (records && records.length > 0) {
227 records.each(function(v) {
229 mangleRecord(v, recordTemplate);
231 this.addItems(records);
233 this.pageStart = records[0].recInt;
237 onFailure: function(r) {
241 displayError('User not found');
246 function mangleRecord(record, template) {
247 record.recInt = parseInt(record.record);
249 // Sanitize HTML input
250 record.data = record.data.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
252 // Turn HTTP URLs into links
253 record.data = record.data.replace(/(\s|^)(https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/([^\s"]*[^.!,;?()\s])?)?)/g, '$1<a href="$2">$2</a>');
255 // Turn markdown links into links
256 record.data = record.data.replace(/(\s|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?)\)/g, '$1<a href="$3">$2</a>');
258 // Turn *foo* into italics and **foo** into bold
259 record.data = record.data.replace(/(\s)\*\*([^*]+)\*\*(\s)/g, '$1<b>$2</b>$3');
260 record.data = record.data.replace(/(\s)\*([^*]+)\*(\s)/g, '$1<i>$2</i>$3');
262 // Turn refs and tags into links
263 record.data = record.data.replace(/(\s|^)#([A-Za-z0-9_-]+)/g, '$1<a href="#tag/$2">#$2</a>');
264 record.data = record.data.replace(/(\s|^)@([A-Za-z0-9_-]+)/g, '$1<a href="#$2">@$2</a>');
266 // Turn newlines into linebreaks and paragraphs
267 record.data = record.data.replace(/\r?\n\r?\n/g, "<p>").replace(/\r?\n/g, "<br>");
269 record.date = (new Date(record.timestamp * 1000)).toString();
270 record.html = template.evaluate(record);
273 function displayError(msg) {
274 items.innerText = msg;
278 // Object for browsing tags
279 function Tag(type, tag) {
284 var url = baseURL + "/tag/";
288 url += 'H'; // apache is eating the hash, even encoded. Probably a security feature.
294 alert('Invalid tag type: ' + type);
299 new Ajax.Request(url, {
301 onSuccess: function(r) {
302 var j = r.responseText.evalJSON();
304 var maxid = j.length - 1;
305 j.each(function(v, i) {
307 mangleRecord(v, tagRecordTemplate)
310 this.pageStart = j.length - 1;
314 onFailure: function(r) {
320 Tag.prototype = new Pager();
321 Tag.prototype.constructor = Tag;
323 Tag.prototype.show = function() {
324 var ctype = {ref: '@', tag: '#'}[this.type];
326 $$('[name=section]').each(function(v) {
327 v.update(' about ' + ctype + this.tag);
331 $('older_link').hide();
332 $('newer_link').hide();
337 function postPopup() {
338 if (loginStatus.loggedIn) {
339 var post = $('post');
340 if (post.visible()) {
344 if (currentPager.username && currentPager.username != loginStatus.username && !$('post.content').value) {
345 $('post.content').value = '@' + currentPager.username + ': ';
347 $('post.content').focus();
353 var username = $('signup.username').value;
354 var password = $('signup.password').value;
356 new Ajax.Request(baseURL + '/create', {
361 onSuccess: function(r) {
363 location.href = '/#' + username;
366 loginStatus.login(username, password);
368 onFailure: function(r) {
369 alert("Failed to create user");
374 function signup_cancel() {
379 function newer_page() {
381 currentPager.newerPage();
384 function older_page() {
386 currentPager.olderPage();
389 var resizePostContentTimeout = null;
390 function resizePostContent() {
391 if (resizePostContentTimeout)
392 clearTimeout(resizePostContentTimeout);
393 resizePostContentTimeout = setTimeout(function() {
394 var c = $('post.content');
395 var lines = Math.floor(c.value.length / (100 * (c.clientWidth / 1000))) + 1;
396 var m = c.value.match(/\r?\n/g);
402 c.style.height = (lines * 17) + "pt";
404 resizePostContentTimeout = null;
408 var tickerTimer = null;
409 var tickerHead, tickerTail;
411 function tickerFader(a, b, p) {
415 a.style.lineHeight = (100 * p) + '%';
417 b.style.opacity = p2;
418 b.style.lineHeight = (100 * p2) + '%';
423 function fadeOut(p) {
428 Bytex64.FX.run(tickerFader.curry(tickerHead, tickerTail), 0.5);
429 tickerHead = tickerHead.nextSibling;
430 tickerTail = tickerTail.nextSibling;
431 if (tickerHead == null) {
433 loadLatest.delay(10);
437 function startTicker() {
439 for (var elem = $('latest-posts').firstChild; elem != null; elem = elem.nextSibling) {
443 // Show the first five
444 tickerHead = $('latest-posts').firstChild;
445 for (var i = 0; i < 5 && tickerHead; i++) {
447 tickerHead = tickerHead.nextSibling;
449 tickerTail = $('latest-posts').firstChild;
450 tickerTimer = setInterval(ticker, 5000);
453 function stopTicker() {
455 clearInterval(tickerTimer);
459 function loadLatest() {
460 new Ajax.Request(baseURL + '/latest.json', {
461 onSuccess: function(r) {
462 var j = r.responseText.evalJSON();
464 $('latest-tags').update();
465 j.tags.each(function(v) {
466 var a = new Element('a', {href: '/#tag/' + v});
468 $('latest-tags').insert(a);
469 $('latest-tags').appendChild(document.createTextNode(' '));
472 $('latest-posts').update();
473 j.records.each(function(v) {
474 v.data = v.data.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
475 v.date = (new Date(v.timestamp * 1000)).toString();
476 var html = latestRecordsTemplate.evaluate(v);
477 $('latest-posts').insert(html);
484 function hashSwitch() {
487 if (m = location.search.match(/^\?post\/([^/]+)\/(.+)/)) {
489 $('post.content').value = '[' + decodeURIComponent(m[1]).replace(']','').replace('[','') + '](' + decodeURIComponent(m[2]) + ')';
490 } else if (m = location.hash.match(/^#(ref|tag)\/([A-Za-z0-9_-]+)$/)) {
491 currentPager = new Tag(m[1], m[2]);
493 } else if (m = location.hash.match(/^#([A-Za-z0-9_-]+)(:(\d+))?$/)) {
494 if (!currentPager || currentPager.username != m[1])
495 currentPager = new User(m[1]);
497 loginStatus.update();
500 var r = parseInt(m[3]);
501 currentPager.showRecord = r;
502 if (currentPager.recordCache[r]) {
503 currentPager.displayItems();
505 currentPager.loadItems((r >= 49 ? r - 49 : 0), r);
508 currentPager.pageStart = currentPager.itemCount - 1;
509 currentPager.loadItems();
512 $$('[name=section]').each(function(v) { v.update('Welcome') });
516 $('newer_link').hide();
517 $('older_link').hide();
525 function hashCheck() {
526 if (location.hash != lastHash) {
527 lastHash = location.hash;
534 loginStatus = new LoginStatus();
536 lastHash = location.hash;
539 setInterval(hashCheck, 250);
541 document.body.observe('keyup', function(event) {
542 if (event.shiftKey && event.keyCode == '32') {
547 $('post.content').addEventListener('keyup', function(event) {
548 event.stopPropagation();