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 href="/\##{author}">@#{author}</a> on #{date}</div></div>'
21 // Object to keep track of login status
22 function LoginStatus() {
24 document.cookie.split(/;\s+/).each(function(v) {
26 cookies[kv[0]] = kv[1];
28 if (cookies.auth && cookies.username) {
30 this.username = cookies.username;
32 this.loggedIn = false;
38 LoginStatus.prototype.login = function(username, password) {
39 new Ajax.Request(baseURL + '/login', {
44 onSuccess: function(r) {
45 var j = r.responseText.evalJSON();
46 if (j && j.status == 'success') {
48 this.username = username;
49 document.cookie = "username=" + username;
50 $('login.password').value = '';
53 alert("Could not log in");
54 $('login.username').focus();
57 onFailure: function(r) {
58 alert("Could not log in");
59 $('login.username').focus();
64 LoginStatus.prototype.logout = function() {
65 new Ajax.Request(baseURL + '/logout', {
67 username: this.username
69 onSuccess: function(r) {
70 this.loggedIn = false;
71 document.cookie = "auth=; expires=1-Jan-1970 00:00:00 GMT";
75 document.cookie = "username=; expires=1-Jan-1970 00:00:00 GMT";
78 LoginStatus.prototype.update = function() {
80 $('userlink').href = '/#' + this.username;
81 $('userlink').update('@' + this.username);
82 $('reflink').href = '/#ref/' + this.username;
92 LoginStatus.prototype.post = function(msg) {
94 alert("You are not logged in!");
98 new Ajax.Request(baseURL + '/put', {
100 username: this.username,
103 onSuccess: function(r) {
104 var j = r.responseText.evalJSON();
105 if (j && j.status == 'success') {
106 $('post.content').value = '';
107 if (location.hash != '#' + this.username) {
108 location.href = '/#' + this.username;
111 currentPager.itemCount++;
112 currentPager.pageStart = null;
113 currentPager.loadItems();
116 alert('Post failed!');
119 onFailure: function(r) {
120 alert('Post failed!');
126 // Base object for paged data
128 this.itemsPerPage = 10;
131 Pager.prototype.initPager = function() {
132 this.itemCache = new Hash();
133 this.pageStart = null;
136 Pager.prototype.olderPage = function() {
137 if (this.pageStart >= this.itemsPerPage) {
138 this.pageStart -= this.itemsPerPage;
143 Pager.prototype.newerPage = function() {
144 if (this.pageStart + this.itemsPerPage < this.itemCount) {
145 this.pageStart += this.itemsPerPage;
150 Pager.prototype.addItems = function(items) {
151 items.each(function(v) {
152 if (!this.itemCache[v.id])
153 this.itemCache[v.id] = v;
157 Pager.prototype.displayItems = function() {
158 if (this.pageStart == undefined)
159 this.pageStart == this.itemCount - 1;
162 if (this.pageStart != undefined && this.itemCache[this.pageStart]) {
163 var end = (this.pageStart >= this.itemsPerPage ? this.pageStart - this.itemsPerPage + 1 : 0);
164 for (var i = this.pageStart; i >= end; i--) {
165 items.insert(this.itemCache[i].html);
168 items.insert("There doesn't seem to be anything here!");
171 if (this.pageStart < this.itemCount - 1)
172 $('newer_link').show();
174 $('newer_link').hide();
176 if (this.pageStart >= 10)
177 $('older_link').show();
179 $('older_link').hide();
183 // Object to render user pages
184 function User(username) {
186 this.username = username;
188 new Ajax.Request(baseURL + '/info/' + username, {
190 onSuccess: function(r) {
191 var j = r.responseText.evalJSON();
193 this.itemCount = parseInt(j.record_count);
199 User.prototype = new Pager();
200 User.prototype.constructor = User;
202 User.prototype.show = function() {
203 $$('[name=section]').each(function(v) { v.update(' @' + this.username) }.bind(this));
207 $('rsslink').href = '/rss/' + this.username;
210 User.prototype.loadItems = function(from, to) {
212 if (from != undefined && to != undefined) {
213 url = baseURL + '/get/' + this.username + '/' + from + '-' + to;
216 url = baseURL + '/get/' + this.username;
219 new Ajax.Request(url, {
221 onSuccess: function(r) {
222 var records = r.responseText.evalJSON();
223 if (records && records.length > 0) {
224 records.each(function(v) {
226 mangleRecord(v, recordTemplate);
228 this.addItems(records);
230 this.pageStart = records[0].recInt;
234 onFailure: function(r) {
238 displayError('User not found');
243 function mangleRecord(record, template) {
244 record.recInt = parseInt(record.record);
246 // Sanitize HTML input
247 record.data = record.data.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
249 // Turn HTTP URLs into links
250 record.data = record.data.replace(/(\s|^)(https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/([^\s"]*[^.!,;?()\s])?)?)/g, '$1<a href="$2">$2</a>');
252 // Turn markdown links into links
253 record.data = record.data.replace(/(\s|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?)\)/g, '$1<a href="$3">$2</a>');
255 // Turn *foo* into italics and **foo** into bold
256 record.data = record.data.replace(/(\s)\*\*([^*]+)\*\*(\s)/g, '$1<b>$2</b>$3');
257 record.data = record.data.replace(/(\s)\*([^*]+)\*(\s)/g, '$1<i>$2</i>$3');
259 // Turn refs and tags into links
260 record.data = record.data.replace(/(\s|^)#(\w+)/g, '$1<a href="#tag/$2">#$2</a>');
261 record.data = record.data.replace(/(\s|^)@(\w+)/g, '$1<a href="#$2">@$2</a>');
263 // Turn newlines into linebreaks and paragraphs
264 record.data = record.data.replace(/\r?\n\r?\n/g, "<p>").replace(/\r?\n/g, "<br>");
266 record.date = (new Date(record.timestamp * 1000)).toString();
267 record.html = template.evaluate(record);
270 function displayError(msg) {
271 items.innerText = msg;
275 // Object for browsing tags
276 function Tag(type, tag) {
281 var url = baseURL + "/tag/";
285 url += 'H'; // apache is eating the hash, even encoded. Probably a security feature.
291 alert('Invalid tag type: ' + type);
296 new Ajax.Request(url, {
298 onSuccess: function(r) {
299 var j = r.responseText.evalJSON();
301 var maxid = j.length - 1;
302 j.each(function(v, i) {
304 mangleRecord(v, tagRecordTemplate)
307 this.pageStart = j.length - 1;
311 onFailure: function(r) {
317 Tag.prototype = new Pager();
318 Tag.prototype.constructor = Tag;
320 Tag.prototype.show = function() {
321 var ctype = {ref: '@', tag: '#'}[this.type];
323 $$('[name=section]').each(function(v) {
324 v.update(' about ' + ctype + this.tag);
328 $('older_link').hide();
329 $('newer_link').hide();
334 function postPopup() {
335 if (loginStatus.loggedIn) {
336 var post = $('post');
337 if (post.visible()) {
341 if (currentPager.username && currentPager.username != loginStatus.username && !$('post.content').value) {
342 $('post.content').value = '@' + currentPager.username + ': ';
344 $('post.content').focus();
350 var username = $('signup.username').value;
351 var password = $('signup.password').value;
353 new Ajax.Request(baseURL + '/create', {
358 onSuccess: function(r) {
360 location.href = '/#' + username;
363 loginStatus.login(username, password);
365 onFailure: function(r) {
366 alert("Failed to create user");
371 function signup_cancel() {
376 function newer_page() {
378 currentPager.newerPage();
381 function older_page() {
383 currentPager.olderPage();
386 var resizePostContentTimeout = null;
387 function resizePostContent() {
388 if (resizePostContentTimeout)
389 clearTimeout(resizePostContentTimeout);
390 resizePostContentTimeout = setTimeout(function() {
391 var c = $('post.content');
392 var lines = Math.floor(c.value.length / (100 * (c.clientWidth / 1000))) + 1;
393 var m = c.value.match(/\r?\n/g);
399 c.style.height = (lines * 17) + "pt";
401 resizePostContentTimeout = null;
405 function loadNewThings() {
406 new Ajax.Request(baseURL + '/newtags.json', {
407 onSuccess: function(r) {
408 $('newtags').update();
409 r.responseText.evalJSON().each(function(v) {
410 var a = new Element('a', {href: '/#tag/' + v});
412 $('newtags').insert(a);
413 $('newtags').insert(new Element('br'));
418 new Ajax.Request(baseURL + '/newusers.json', {
419 onSuccess: function(r) {
420 $('newusers').update();
421 r.responseText.evalJSON().each(function(v) {
422 var a = new Element('a', {href: '/#' + v});
424 $('newusers').insert(a);
425 $('newusers').insert(new Element('br'));
431 function hashSwitch() {
433 if (m = location.search.match(/^\?post\/([^/]+)\/(.+)/)) {
435 $('post.content').value = '[' + decodeURIComponent(m[1]).replace(']','').replace('[','') + '](' + decodeURIComponent(m[2]) + ')';
436 } else if (m = location.hash.match(/^#(ref|tag)\/(\w+)$/)) {
437 currentPager = new Tag(m[1], m[2]);
439 } else if (m = location.hash.match(/^#(\w+)(:(\d+))?$/)) {
440 if (!currentPager || currentPager.username != m[1])
441 currentPager = new User(m[1]);
443 loginStatus.update();
446 var r = parseInt(m[3]);
447 currentPager.showRecord = r;
448 if (currentPager.recordCache[r]) {
449 currentPager.displayItems();
451 currentPager.loadItems((r >= 49 ? r - 49 : 0), r);
454 currentPager.pageStart = currentPager.itemCount - 1;
455 currentPager.loadItems();
458 $$('[name=section]').each(function(v) { v.update('Welcome') });
462 $('newer_link').hide();
463 $('older_link').hide();
471 function hashCheck() {
472 if (location.hash != lastHash) {
473 lastHash = location.hash;
480 loginStatus = new LoginStatus();
482 lastHash = location.hash;
485 setInterval(hashCheck, 250);
487 document.body.observe('keyup', function(event) {
488 if (event.shiftKey && event.keyCode == '32') {
493 $('post.content').addEventListener('keyup', function(event) {
494 event.stopPropagation();