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>'
17 LoginStatus.prototype.post = function(msg) {
19 alert("You are not logged in!");
23 new Ajax.Request(baseURL + '/put', {
25 username: this.username,
28 onSuccess: function(r) {
29 var j = r.responseText.evalJSON();
30 if (j && j.status == 'success') {
31 $('post.content').value = '';
32 if (location.hash != '#' + this.username) {
35 currentPager.itemCount++;
36 currentPager.reload();
39 alert('Post failed!');
42 onFailure: function(r) {
43 alert('Post failed!');
48 // Base object for paged data
50 this.itemsPerPage = 10;
51 this.itemCache = new Hash();
52 this.pageStart = null;
55 Pager.prototype.updateState = function(m) {
59 Pager.prototype.show = function() {
63 Pager.prototype.hide = function() {
66 $('newer_link').hide();
67 $('older_link').hide();
70 Pager.prototype.olderPage = function() {
71 if (this.pageStart >= this.itemsPerPage) {
72 qlink(this.baseFrag + '/p' + (this.pageStart - this.itemsPerPage));
76 Pager.prototype.newerPage = function() {
77 if (this.pageStart + this.itemsPerPage < this.itemCount) {
78 qlink(this.baseFrag + '/p' + (this.pageStart + this.itemsPerPage));
82 Pager.prototype.addItems = function(items) {
83 items.each(function(v) {
84 if (!this.itemCache[v.id])
85 this.itemCache[v.id] = v;
89 Pager.prototype.displayItems = function() {
90 if (this.pageStart == undefined)
91 this.pageStart == this.itemCount - 1;
94 if (this.pageStart != undefined && this.itemCache[this.pageStart]) {
95 var end = (this.pageStart >= this.itemsPerPage ? this.pageStart - this.itemsPerPage + 1 : 0);
96 for (var i = this.pageStart; i >= end; i--) {
97 items.insert(this.itemCache[i].html);
100 items.insert("There doesn't seem to be anything here!");
103 if (this.pageStart < this.itemCount - 1) {
104 $('newer_link').href = baseURL + '/#' + this.baseFrag + '/p' + (this.pageStart + this.itemsPerPage);
105 $('newer_link').show();
107 $('newer_link').hide();
110 if (this.pageStart >= 10) {
111 $('older_link').href = baseURL + '/#' + this.baseFrag + '/p' + (this.pageStart - this.itemsPerPage);
112 $('older_link').show();
114 $('older_link').hide();
117 document.body.scrollTo();
120 Pager.prototype.reload = function() {
121 this.pageStart = null;
122 this.loadItems(null, null, Pager.prototype.showPageAt.bind(this, this.itemCount - 1));
125 Pager.prototype.showPageAt = function(r) {
126 var end = (r - 9 > 0 ? r - 9 : 0);
127 if (this.itemCache[r] && this.itemCache[end]) {
131 this.loadItems((r >= 49 ? r - 49 : 0), r, Pager.prototype.showPageAt.bind(this, r));
135 Pager.prototype.showRecord = function(r) {
136 if (this.itemCache[r]) {
137 $('older_link').hide();
138 $('newer_link').hide();
139 items.update(this.itemCache[r].html);
141 this.loadItems(r, r, Pager.prototype.showRecord.bind(this, r));
145 Pager.prototype.loadItems = function(from, to, continuation) { }
147 function displayError(msg) {
148 items.innerText = msg;
151 // Pager for browsing subscription feeds
154 this.username = loginStatus.username;
155 this.baseFrag = '/feed';
156 this.pageStart = parseInt(m[1]);
158 new Ajax.Request(baseURL + '/feed', {
161 username: loginStatus.username
163 onSuccess: function(r) {
164 var response = r.responseText.evalJSON();
166 var maxid = response.length - 1;
167 response.each(function(v, i) {
169 mangleRecord(v, tagRecordTemplate)
171 this.addItems(response);
173 this.pageStart = response.length - 1;
174 this.itemCount = response.length;
175 loginStatus.requestFeedStatus();
179 onFailure: function(r) {
184 Feed.prototype = new Pager();
185 Feed.prototype.constructor = Feed;
187 Feed.prototype.updateState = function(m) {
188 this.pageStart = parseInt(m[1]) || this.itemCount - 1;
194 Feed.prototype.show = function() {
195 Pager.prototype.show.call(this);
196 $$('[name=section]').each(function(v) {
197 v.update(' ' + loginStatus.username + "'s spycam");
202 function postPopup(initial) {
203 if (loginStatus.loggedIn || initial) {
204 var post = $('post');
205 if (post.visible()) {
210 $('post.content').value = initial;
211 } else if (!$('post.content').value && currentPager.username && currentPager.username != loginStatus.username) {
212 $('post.content').value = '@' + currentPager.username + ': ';
214 $('post.content').focus();
220 var username = $('signup.username').value;
221 var password = $('signup.password').value;
223 new Ajax.Request(baseURL + '/create', {
228 onSuccess: function(r) {
232 loginStatus.login(username, password);
234 onFailure: function(r) {
235 alert("Failed to create user");
240 function signup_cancel() {
246 var old_password = $('passwd.old_password').value;
247 var new_password = $('passwd.new_password').value;
249 new Ajax.Request(baseURL + '/passwd', {
251 username: loginStatus.username,
252 password: old_password,
253 new_password: new_password
255 onSuccess: function(r) {
256 if (r.responseJSON.status == 'success') {
257 alert('Password changed');
260 alert('Password change failed. Your password has NOT been changed.');
263 onFailure: function(r) {
264 alert('Password change error');
269 function passwd_cancel() {
271 $('navigation').show();
275 function subscribe() {
276 new Ajax.Request(baseURL + '/subscribe/' + currentPager.username, {
279 username: loginStatus.username
281 onSuccess: function(r) {
282 var response = r.responseText.evalJSON();
283 if (response.status == 'success') {
284 alert("You call " + currentPager.username + " and begin breathing heavily into the handset.");
285 $$('[name=user.subscribelink]').each(Element.hide);
286 $$('[name=user.unsubscribelink]').each(Element.show);
288 alert('Failed to subscribe. This is probably for the best');
291 onFailure: function(r) {
292 alert('Failed to subscribe. This is probably for the best');
297 function unsubscribe() {
298 new Ajax.Request(baseURL + '/unsubscribe/' + currentPager.username, {
301 username: loginStatus.username
303 onSuccess: function(r) {
304 var response = r.responseText.evalJSON();
305 if (response.status == 'success') {
306 alert("You come to your senses.");
307 $$('[name=user.subscribelink]').each(Element.show);
308 $$('[name=user.unsubscribelink]').each(Element.hide);
310 alert('You are unable to tear yourself away (because something failed on the server)');
313 onFailure: function(r) {
314 alert('You are unable to tear yourself away (because something failed on the server)');
319 var tickerTimer = null;
320 var tickerHead, tickerTail;
322 function tickerFader(a, b, p) {
326 a.style.lineHeight = (100 * p) + '%';
328 b.style.opacity = p2;
329 b.style.lineHeight = (100 * p2) + '%';
336 Bytex64.FX.run(tickerFader.curry(tickerHead, tickerTail), 0.5);
337 tickerHead = tickerHead.nextSibling;
338 tickerTail = tickerTail.nextSibling;
339 if (tickerHead == null) {
341 loadLatest.delay(10);
345 function startTicker() {
347 for (var elem = $('latest-posts').firstChild; elem != null; elem = elem.nextSibling) {
351 // Show the first five
352 tickerHead = $('latest-posts').firstChild;
353 for (var i = 0; i < 10 && tickerHead; i++) {
355 tickerHead = tickerHead.nextSibling;
357 tickerTail = $('latest-posts').firstChild;
358 tickerTimer = setInterval(ticker, 5000);
361 function stopTicker() {
363 clearInterval(tickerTimer);
367 function loadLatest() {
368 new Ajax.Request(baseURL + '/latest.json', {
370 onSuccess: function(r) {
371 var j = r.responseText.evalJSON();
373 $('latest-tags').update();
374 j.tags.each(function(v) {
375 var a = new Element('a', {href: baseURL + '/#/tag/' + v});
377 a.onclick = "return qlink()";
379 $('latest-tags').insert(a);
380 $('latest-tags').appendChild(document.createTextNode(' '));
383 $('latest-posts').update();
384 j.records.each(function(v) {
385 v.data = v.data.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
386 v.date = (new Date(v.timestamp * 1000)).toString();
387 var html = latestRecordsTemplate.evaluate(v);
388 $('latest-posts').insert(html);
395 function ExternalURLPost(m) {
396 this.title = decodeURIComponent(m[1]).replace(']','').replace('[','');
397 this.url = decodeURIComponent(m[2]);
400 ExternalURLPost.prototype.show = function() {
401 $('post.content').value = '[' + this.title + '](' + this.url + ')';