X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;f=www%2Fjs%2Fblerg.js;h=2125c5bef86f5faace6e856f7249cf3f2d4bd543;hb=4c3823106abb40d31797b51eeb047018698afc12;hp=9f92594ba51b22777d5855bf0390d4854d0bb3b9;hpb=f4bf2d2efc566e9f165b97bda72cd5a27e0d2b19;p=blerg.git diff --git a/www/js/blerg.js b/www/js/blerg.js index 9f92594..2125c5b 100644 --- a/www/js/blerg.js +++ b/www/js/blerg.js @@ -5,93 +5,15 @@ // Config var baseURL = ''; var recordTemplate = new Template( - '
#{data}
Posted #{date}
' + '
#{data}
Posted #{date}. [permalink] [reply]
' ); var tagRecordTemplate = new Template( - '
#{data}
Posted by @#{author} on #{date}
' + '
#{data}
Posted by @#{author} on #{date}. [permalink] [reply]
' ); var latestRecordsTemplate = new Template( - '
@#{author} #{data}
' + '
@#{author} #{data}
' ); -// Page elements -var items; - -// Other globals -var currentPager; -var loginStatus; - -// Object to keep track of login status -function LoginStatus() { - var cookies = {}; - document.cookie.split(/;\s+/).each(function(v) { - kv = v.split('='); - cookies[kv[0]] = kv[1]; - }); - if (cookies.auth && cookies.username) { - this.loggedIn = true; - this.username = cookies.username; - } else { - this.loggedIn = false; - this.username = null; - } - this.update(); -} - -LoginStatus.prototype.login = function(username, password) { - new Ajax.Request(baseURL + '/login', { - parameters: { - username: username, - password: password - }, - onSuccess: function(r) { - var j = r.responseText.evalJSON(); - if (j && j.status == 'success') { - this.loggedIn = true; - this.username = username; - document.cookie = "username=" + username; - $('login.password').value = ''; - this.update(); - } else { - alert("Could not log in"); - $('login.username').focus(); - } - }.bind(this), - onFailure: function(r) { - alert("Could not log in"); - $('login.username').focus(); - } - }); -} - -LoginStatus.prototype.logout = function() { - new Ajax.Request(baseURL + '/logout', { - parameters: { - username: this.username - }, - onSuccess: function(r) { - this.loggedIn = false; - document.cookie = "auth=; expires=1-Jan-1970 00:00:00 GMT"; - this.update(); - }.bind(this) - }); - document.cookie = "username=; expires=1-Jan-1970 00:00:00 GMT"; -} - -LoginStatus.prototype.update = function() { - if (this.loggedIn) { - $('userlink').href = '/#' + this.username; - $('userlink').update('@' + this.username); - $('reflink').href = '/#ref/' + this.username; - $('login').hide(); - $('logout').show(); - } else { - $('post').hide(); - $('login').show(); - $('logout').hide(); - } -} - LoginStatus.prototype.post = function(msg) { if (!this.loggedIn) { alert("You are not logged in!"); @@ -108,12 +30,10 @@ LoginStatus.prototype.post = function(msg) { if (j && j.status == 'success') { $('post.content').value = ''; if (location.hash != '#' + this.username) { - location.href = '/#' + this.username; - hashSwitch(); + qlink(this.username); } else { currentPager.itemCount++; - currentPager.pageStart = null; - currentPager.loadItems(); + currentPager.reload(); } } else { alert('Post failed!'); @@ -125,28 +45,37 @@ LoginStatus.prototype.post = function(msg) { }); } - // Base object for paged data function Pager() { this.itemsPerPage = 10; -} - -Pager.prototype.initPager = function() { this.itemCache = new Hash(); this.pageStart = null; } +Pager.prototype.updateState = function(m) { + return false; +} + +Pager.prototype.show = function() { + items.show(); +} + +Pager.prototype.hide = function() { + items.hide(); + items.update(); + $('newer_link').hide(); + $('older_link').hide(); +} + Pager.prototype.olderPage = function() { if (this.pageStart >= this.itemsPerPage) { - this.pageStart -= this.itemsPerPage; - this.displayItems(); + qlink(this.baseFrag + '/p' + (this.pageStart - this.itemsPerPage)); } } Pager.prototype.newerPage = function() { if (this.pageStart + this.itemsPerPage < this.itemCount) { - this.pageStart += this.itemsPerPage; - this.displayItems(); + qlink(this.baseFrag + '/p' + (this.pageStart + this.itemsPerPage)); } } @@ -171,225 +100,79 @@ Pager.prototype.displayItems = function() { items.insert("There doesn't seem to be anything here!"); } - if (this.pageStart < this.itemCount - 1) + if (this.pageStart < this.itemCount - 1) { + $('newer_link').href = baseURL + '/#' + this.baseFrag + '/p' + (this.pageStart + this.itemsPerPage); $('newer_link').show(); - else + } else { $('newer_link').hide(); + } - if (this.pageStart >= 10) + if (this.pageStart >= 10) { + $('older_link').href = baseURL + '/#' + this.baseFrag + '/p' + (this.pageStart - this.itemsPerPage); $('older_link').show(); - else + } else { $('older_link').hide(); -} - - -// Object to render user pages -function User(username) { - this.initPager(); - this.username = username; - - $$('[name=user.obsesslink]').each(Element.hide); - $$('[name=user.unobsesslink]').each(Element.hide); - - if (loginStatus.loggedIn) { - new Ajax.Request(baseURL + '/feedinfo/' + username, { - method: 'post', - parameters: { - username: loginStatus.username - }, - onSuccess: function(r) { - var json = r.responseText.evalJSON(); - if (json.subscribed) { - $$('[name=user.obsesslink]').each(Element.hide); - $$('[name=user.unobsesslink]').each(Element.show); - } else { - $$('[name=user.obsesslink]').each(Element.show); - $$('[name=user.unobsesslink]').each(Element.hide); - } - } - }); } - new Ajax.Request(baseURL + '/info/' + username, { - method: 'get', - onSuccess: function(r) { - var j = r.responseText.evalJSON(); - if (j) { - this.itemCount = parseInt(j.record_count); - this.displayItems(); - } - }.bind(this) - }); + document.body.scrollTo(); } -User.prototype = new Pager(); -User.prototype.constructor = User; -User.prototype.show = function() { - $$('[name=section]').each(function(v) { v.update(' @' + this.username) }.bind(this)); - $('welcome').hide(); - items.show(); - $('rss').show(); - $('rsslink').href = '/rss/' + this.username; - $$('[name=user.reflink]').each(function(e) { - e.href = '/#ref/' + this.username; - }.bind(this)); - $('usercontrols').show(); +Pager.prototype.reload = function() { + this.pageStart = null; + this.loadItems(null, null, Pager.prototype.showPageAt.bind(this, this.itemCount - 1)); } -User.prototype.loadItems = function(from, to) { - var url; - if (from != undefined && to != undefined) { - url = baseURL + '/get/' + this.username + '/' + from + '-' + to; - this.pageStart = to; +Pager.prototype.showPageAt = function(r) { + var end = (r - 9 > 0 ? r - 9 : 0); + if (this.itemCache[r] && this.itemCache[end]) { + this.pageStart = r; + this.displayItems(); } else { - url = baseURL + '/get/' + this.username; + this.loadItems((r >= 49 ? r - 49 : 0), r, Pager.prototype.showPageAt.bind(this, r)); } - - new Ajax.Request(url, { - method: 'get', - onSuccess: function(r) { - var records = r.responseText.evalJSON(); - if (records && records.length > 0) { - records.each(function(v) { - v.id = v.record; - mangleRecord(v, recordTemplate); - }); - this.addItems(records); - if (!this.pageStart) - this.pageStart = records[0].recInt; - } - this.displayItems(); - }.bind(this), - onFailure: function(r) { - this.displayItems(); - }.bind(this), - on404: function(r) { - displayError('User not found'); - } - }); } -function mangleRecord(record, template) { - record.recInt = parseInt(record.record); - - var lines = record.data.split(/\r?\n/); - if (lines[lines.length - 1] == '') - lines.pop(); - - var out = ['

']; - var endpush = null; - var listMode = false; - lines.each(function(l) { - if (l == '') { - if (out[out.length - 1] == '
') { - out[out.length - 1] = '

'; - } - if (out[out.length - 1] == '') { - out.push(''); - out.push('

'); - listMode = false; - } - return; - } - - if (l[0] == '>') { - var pi = out.lastIndexOf('

'); - if (pi != -1) { - out[pi] = '

'; - l = l.replace(/^>\s*/, ''); - } - } - if (l[0] == '*') { - if (!listMode) { - var pi = out.lastIndexOf('

'); - out[pi] = '