X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;f=www%2Fjs%2Fblerg.js;h=0d114cb22f70ef8d0f3e21a490aac81f14741241;hb=4720a1f90f7d42f3611f064cc5ba3d768a92b5c6;hp=9b1db74f346ba316edc5eb3a0d2bb8a03dcce7a1;hpb=a2ca84770dd9230c1c6d4e2f08b0d3638604d99a;p=blerg.git diff --git a/www/js/blerg.js b/www/js/blerg.js index 9b1db74..0d114cb 100644 --- a/www/js/blerg.js +++ b/www/js/blerg.js @@ -2,96 +2,6 @@ * BSD-style license. Please see the COPYING file for details. */ -// Config -var baseURL = ''; -var recordTemplate = new Template( - '
#{data}
Posted #{date}
' -); -var tagRecordTemplate = new Template( - '
#{data}
Posted by @#{author} on #{date}
' -); -var latestRecordsTemplate = new Template( - '
@#{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!"); @@ -111,8 +21,7 @@ LoginStatus.prototype.post = function(msg) { qlink(this.username); } else { currentPager.itemCount++; - currentPager.pageStart = null; - currentPager.loadItems(); + currentPager.reload(); } } else { alert('Post failed!'); @@ -124,625 +33,32 @@ LoginStatus.prototype.post = function(msg) { }); } +function passwd() { + var old_password = $('passwd.old_password').value; + var new_password = $('passwd.new_password').value; -// Base object for paged data -function Pager() { - this.itemsPerPage = 10; -} - -Pager.prototype.initPager = function() { - this.itemCache = new Hash(); - this.pageStart = null; -} - -Pager.prototype.olderPage = function() { - if (this.pageStart >= this.itemsPerPage) { - qlink(this.baseFrag + '/p' + (this.pageStart - this.itemsPerPage)); - } -} - -Pager.prototype.newerPage = function() { - if (this.pageStart + this.itemsPerPage < this.itemCount) { - qlink(this.baseFrag + '/p' + (this.pageStart + this.itemsPerPage)); - } -} - -Pager.prototype.addItems = function(items) { - items.each(function(v) { - if (!this.itemCache[v.id]) - this.itemCache[v.id] = v; - }.bind(this)); -} - -Pager.prototype.displayItems = function() { - if (this.pageStart == undefined) - this.pageStart == this.itemCount - 1; - items.update(); - - if (this.pageStart != undefined && this.itemCache[this.pageStart]) { - var end = (this.pageStart >= this.itemsPerPage ? this.pageStart - this.itemsPerPage + 1 : 0); - for (var i = this.pageStart; i >= end; i--) { - items.insert(this.itemCache[i].html); - } - } else { - items.insert("There doesn't seem to be anything here!"); - } - - if (this.pageStart < this.itemCount - 1) { - $('newer_link').href = '/#' + this.baseFrag + '/p' + (this.pageStart + this.itemsPerPage); - $('newer_link').show(); - } else { - $('newer_link').hide(); - } - - if (this.pageStart >= 10) { - $('older_link').href = '/#' + this.baseFrag + '/p' + (this.pageStart - this.itemsPerPage); - $('older_link').show(); - } else { - $('older_link').hide(); - } -} - -Pager.prototype.showPageAt = function(r) { - if (this.itemCache[r] && this.itemCache[r - 9]) { - this.pageStart = r; - this.displayItems(); - } else { - this.loadItems((r >= 49 ? r - 49 : 0), r); - } -} - -Pager.prototype.showRecord = function(r) { - this.showPageAt(r); -} - - -// Object to render user pages -function User(username) { - this.initPager(); - this.username = username; - this.baseFrag = username; - - $$('[name=user.subscribelink]').each(Element.hide); - $$('[name=user.unsubscribelink]').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.subscribelink]').each(Element.hide); - $$('[name=user.unsubscribelink]').each(Element.show); - } else { - $$('[name=user.subscribelink]').each(Element.show); - $$('[name=user.unsubscribelink]').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) - }); -} -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(); -} - -User.prototype.loadItems = function(from, to) { - var url; - if (from != undefined && to != undefined) { - url = baseURL + '/get/' + this.username + '/' + from + '-' + to; - this.pageStart = to; - } else { - url = baseURL + '/get/' + this.username; - } - - 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] = '