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}. <a href="' + baseURL + '/\##{author}/#{record}" onclick="return qlink()">[permalink]</a> <a href="#" onclick="postPopup(\'@#{author}/#{record}: \'); return false">[reply]</a></div></div>'
10 var tagRecordTemplate = new Template(
11 '<div class="record">#{data}<div class="info">Posted by <a class="author ref" href="/\##{author}" onclick="return qlink()">@#{author}</a> on #{date}. <a href="' + baseURL + '/\##{author}/#{record}" onclick="return qlink()">[permalink]</a> <a href="#" onclick="postPopup(\'@#{author}/#{record}: \'); return false">[reply]</a></div></div>'
13 var latestRecordsTemplate = new Template(
14 '<div class="record"><a class="author ref" href="' + baseURL + '/\##{author}" onclick="return qlink()">@#{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;
34 this.requestFeedStatus();
35 this.feedStatusUpdateInterval = setInterval(this.requestFeedStatus.bind(this), 900000);
37 this.loggedIn = false;
43 LoginStatus.prototype.login = function(username, password) {
44 new Ajax.Request(baseURL + '/login', {
49 onSuccess: function(r) {
50 var j = r.responseText.evalJSON();
51 if (j && j.status == 'success') {
53 this.username = username;
54 document.cookie = "username=" + username;
55 $('login.password').value = '';
56 this.requestFeedStatus();
57 this.feedStatusUpdateInterval = setInterval(this.requestFeedStatus.bind(this), 900000);
60 alert("Could not log in");
61 $('login.username').focus();
64 onFailure: function(r) {
65 alert("Could not log in");
66 $('login.username').focus();
71 LoginStatus.prototype.logout = function() {
72 new Ajax.Request(baseURL + '/logout', {
74 username: this.username
76 onComplete: function(r) {
77 this.loggedIn = false;
78 document.cookie = "auth=; expires=1-Jan-1970 00:00:00 GMT";
80 clearInterval(this.feedStatusUpdateInterval);
83 document.cookie = "username=; expires=1-Jan-1970 00:00:00 GMT";
86 LoginStatus.prototype.update = function() {
88 $('userlink').href = baseURL + '/#' + this.username;
89 $('userlink').update('@' + this.username);
90 $('reflink').href = baseURL + '/#/ref/' + this.username;
100 LoginStatus.prototype.post = function(msg) {
101 if (!this.loggedIn) {
102 alert("You are not logged in!");
106 new Ajax.Request(baseURL + '/put', {
108 username: this.username,
111 onSuccess: function(r) {
112 var j = r.responseText.evalJSON();
113 if (j && j.status == 'success') {
114 $('post.content').value = '';
115 if (location.hash != '#' + this.username) {
116 qlink(this.username);
118 currentPager.itemCount++;
119 currentPager.reload();
122 alert('Post failed!');
125 onFailure: function(r) {
126 alert('Post failed!');
131 LoginStatus.prototype.requestFeedStatus = function() {
132 new Ajax.Request('/feedinfo', {
133 parameters: { username: this.username },
134 onSuccess: function(r) {
135 var j = r.responseText.evalJSON();
137 $('newFeedMessages').update('(' + j['new'] + ' new)');
139 $('newFeedMessages').update('');
145 // Base object for paged data
147 this.itemsPerPage = 10;
148 this.itemCache = new Hash();
149 this.pageStart = null;
152 Pager.prototype.updateState = function(m) {
156 Pager.prototype.show = function() {
160 Pager.prototype.hide = function() {
163 $('newer_link').hide();
164 $('older_link').hide();
167 Pager.prototype.olderPage = function() {
168 if (this.pageStart >= this.itemsPerPage) {
169 qlink(this.baseFrag + '/p' + (this.pageStart - this.itemsPerPage));
173 Pager.prototype.newerPage = function() {
174 if (this.pageStart + this.itemsPerPage < this.itemCount) {
175 qlink(this.baseFrag + '/p' + (this.pageStart + this.itemsPerPage));
179 Pager.prototype.addItems = function(items) {
180 items.each(function(v) {
181 if (!this.itemCache[v.id])
182 this.itemCache[v.id] = v;
186 Pager.prototype.displayItems = function() {
187 if (this.pageStart == undefined)
188 this.pageStart == this.itemCount - 1;
191 if (this.pageStart != undefined && this.itemCache[this.pageStart]) {
192 var end = (this.pageStart >= this.itemsPerPage ? this.pageStart - this.itemsPerPage + 1 : 0);
193 for (var i = this.pageStart; i >= end; i--) {
194 items.insert(this.itemCache[i].html);
197 items.insert("There doesn't seem to be anything here!");
200 if (this.pageStart < this.itemCount - 1) {
201 $('newer_link').href = baseURL + '/#' + this.baseFrag + '/p' + (this.pageStart + this.itemsPerPage);
202 $('newer_link').show();
204 $('newer_link').hide();
207 if (this.pageStart >= 10) {
208 $('older_link').href = baseURL + '/#' + this.baseFrag + '/p' + (this.pageStart - this.itemsPerPage);
209 $('older_link').show();
211 $('older_link').hide();
214 document.body.scrollTo();
217 Pager.prototype.reload = function() {
218 this.pageStart = null;
219 this.loadItems(null, null, Pager.prototype.showPageAt.bind(this, this.itemCount - 1));
222 Pager.prototype.showPageAt = function(r) {
223 var end = (r - 9 > 0 ? r - 9 : 0);
224 if (this.itemCache[r] && this.itemCache[end]) {
228 this.loadItems((r >= 49 ? r - 49 : 0), r, Pager.prototype.showPageAt.bind(this, r));
232 Pager.prototype.showRecord = function(r) {
233 if (this.itemCache[r]) {
234 $('older_link').hide();
235 $('newer_link').hide();
236 items.update(this.itemCache[r].html);
238 this.loadItems(r, r, Pager.prototype.showRecord.bind(this, r));
242 Pager.prototype.loadItems = function(from, to, continuation) { }
244 function displayError(msg) {
245 items.innerText = msg;
249 // Object for browsing tags
254 this.pageStart = parseInt(m[3]);
256 var url = baseURL + "/tag/";
260 url += 'H'; // apache is eating the hash, even encoded. Probably a security feature.
261 this.baseFrag = '/tag/' + this.tag;
265 this.baseFrag = '/ref/' + this.tag;
268 alert('Invalid tag type: ' + this.type);
273 new Ajax.Request(url, {
275 onSuccess: function(r) {
276 var j = r.responseText.evalJSON();
278 var maxid = j.length - 1;
279 j.each(function(v, i) {
281 mangleRecord(v, tagRecordTemplate)
285 this.pageStart = j.length - 1;
286 this.itemCount = j.length;
290 onFailure: function(r) {
296 Tag.prototype = new Pager();
297 Tag.prototype.constructor = Tag;
299 Tag.prototype.updateState = function(m) {
300 if (this.type != m[1] || this.tag != m[2])
303 this.pageStart = parseInt(m[3]) || this.itemCount - 1;
309 Tag.prototype.show = function() {
310 Pager.prototype.show.call(this);
312 var ctype = {ref: '@', tag: '#'}[this.type];
314 $$('[name=section]').each(function(v) {
315 v.update(' about ' + ctype + this.tag);
320 // Pager for browsing subscription feeds
323 this.username = loginStatus.username;
324 this.baseFrag = '/feed';
325 this.pageStart = parseInt(m[1]);
327 new Ajax.Request(baseURL + '/feed', {
330 username: loginStatus.username
332 onSuccess: function(r) {
333 var response = r.responseText.evalJSON();
335 var maxid = response.length - 1;
336 response.each(function(v, i) {
338 mangleRecord(v, tagRecordTemplate)
340 this.addItems(response);
342 this.pageStart = response.length - 1;
343 this.itemCount = response.length;
344 loginStatus.requestFeedStatus();
348 onFailure: function(r) {
353 Feed.prototype = new Pager();
354 Feed.prototype.constructor = Feed;
356 Feed.prototype.updateState = function(m) {
357 this.pageStart = parseInt(m[1]) || this.itemCount - 1;
363 Feed.prototype.show = function() {
364 Pager.prototype.show.call(this);
365 $$('[name=section]').each(function(v) {
366 v.update(' ' + loginStatus.username + "'s spycam");
371 function postPopup(initial) {
372 if (loginStatus.loggedIn || initial) {
373 var post = $('post');
374 if (post.visible()) {
379 $('post.content').value = initial;
380 } else if (!$('post.content').value && currentPager.username && currentPager.username != loginStatus.username) {
381 $('post.content').value = '@' + currentPager.username + ': ';
383 $('post.content').focus();
389 var username = $('signup.username').value;
390 var password = $('signup.password').value;
392 new Ajax.Request(baseURL + '/create', {
397 onSuccess: function(r) {
401 loginStatus.login(username, password);
403 onFailure: function(r) {
404 alert("Failed to create user");
409 function signup_cancel() {
415 var old_password = $('passwd.old_password').value;
416 var new_password = $('passwd.new_password').value;
418 new Ajax.Request(baseURL + '/passwd', {
420 username: loginStatus.username,
421 password: old_password,
422 new_password: new_password
424 onSuccess: function(r) {
425 if (r.responseJSON.status == 'success') {
426 alert('Password changed');
429 alert('Password change failed. Your password has NOT been changed.');
432 onFailure: function(r) {
433 alert('Password change error');
438 function passwd_cancel() {
440 $('navigation').show();
444 function subscribe() {
445 new Ajax.Request(baseURL + '/subscribe/' + currentPager.username, {
448 username: loginStatus.username
450 onSuccess: function(r) {
451 var response = r.responseText.evalJSON();
452 if (response.status == 'success') {
453 alert("You call " + currentPager.username + " and begin breathing heavily into the handset.");
454 $$('[name=user.subscribelink]').each(Element.hide);
455 $$('[name=user.unsubscribelink]').each(Element.show);
457 alert('Failed to subscribe. This is probably for the best');
460 onFailure: function(r) {
461 alert('Failed to subscribe. This is probably for the best');
466 function unsubscribe() {
467 new Ajax.Request(baseURL + '/unsubscribe/' + currentPager.username, {
470 username: loginStatus.username
472 onSuccess: function(r) {
473 var response = r.responseText.evalJSON();
474 if (response.status == 'success') {
475 alert("You come to your senses.");
476 $$('[name=user.subscribelink]').each(Element.show);
477 $$('[name=user.unsubscribelink]').each(Element.hide);
479 alert('You are unable to tear yourself away (because something failed on the server)');
482 onFailure: function(r) {
483 alert('You are unable to tear yourself away (because something failed on the server)');
488 var tickerTimer = null;
489 var tickerHead, tickerTail;
491 function tickerFader(a, b, p) {
495 a.style.lineHeight = (100 * p) + '%';
497 b.style.opacity = p2;
498 b.style.lineHeight = (100 * p2) + '%';
505 Bytex64.FX.run(tickerFader.curry(tickerHead, tickerTail), 0.5);
506 tickerHead = tickerHead.nextSibling;
507 tickerTail = tickerTail.nextSibling;
508 if (tickerHead == null) {
510 loadLatest.delay(10);
514 function startTicker() {
516 for (var elem = $('latest-posts').firstChild; elem != null; elem = elem.nextSibling) {
520 // Show the first five
521 tickerHead = $('latest-posts').firstChild;
522 for (var i = 0; i < 10 && tickerHead; i++) {
524 tickerHead = tickerHead.nextSibling;
526 tickerTail = $('latest-posts').firstChild;
527 tickerTimer = setInterval(ticker, 5000);
530 function stopTicker() {
532 clearInterval(tickerTimer);
536 function loadLatest() {
537 new Ajax.Request(baseURL + '/latest.json', {
539 onSuccess: function(r) {
540 var j = r.responseText.evalJSON();
542 $('latest-tags').update();
543 j.tags.each(function(v) {
544 var a = new Element('a', {href: baseURL + '/#/tag/' + v});
546 a.onclick = "return qlink()";
548 $('latest-tags').insert(a);
549 $('latest-tags').appendChild(document.createTextNode(' '));
552 $('latest-posts').update();
553 j.records.each(function(v) {
554 v.data = v.data.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
555 v.date = (new Date(v.timestamp * 1000)).toString();
556 var html = latestRecordsTemplate.evaluate(v);
557 $('latest-posts').insert(html);
564 function ExternalURLPost(m) {
565 this.title = decodeURIComponent(m[1]).replace(']','').replace('[','');
566 this.url = decodeURIComponent(m[2]);
569 ExternalURLPost.prototype.show = function() {
570 $('post.content').value = '[' + this.title + '](' + this.url + ')';