X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;f=www%2Fjs%2Fblerg.js;h=0d114cb22f70ef8d0f3e21a490aac81f14741241;hb=4720a1f90f7d42f3611f064cc5ba3d768a92b5c6;hp=eae9cbc387757d05d33a322f0b06998d18a4fe70;hpb=4fada9be5451cd415c6a9fb4786391f2996cddec;p=blerg.git diff --git a/www/js/blerg.js b/www/js/blerg.js index eae9cbc..0d114cb 100644 --- a/www/js/blerg.js +++ b/www/js/blerg.js @@ -2,93 +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}
' -); - -// 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!"); @@ -105,12 +18,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!'); @@ -122,375 +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) { - this.pageStart -= this.itemsPerPage; - this.displayItems(); - } -} - -Pager.prototype.newerPage = function() { - if (this.pageStart + this.itemsPerPage < this.itemCount) { - this.pageStart += this.itemsPerPage; - this.displayItems(); - } -} - -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').show(); - else - $('newer_link').hide(); - - if (this.pageStart >= 10) - $('older_link').show(); - else - $('older_link').hide(); -} - - -// Object to render user pages -function User(username) { - this.initPager(); - this.username = username; - - 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; -} - -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); - - // Sanitize HTML input - record.data = record.data.replace(/&/g, '&').replace(//g, '>'); - - // Turn HTTP URLs into links - record.data = record.data.replace(/(\s|^)(https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/([^\s"]*[^.!,;?()\s])?)?)/g, '$1$2'); - - // Turn markdown links into links - record.data = record.data.replace(/(\s|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?)\)/g, '$1$2'); - - // Turn *foo* into italics and **foo** into bold - record.data = record.data.replace(/(\s)\*\*([^*]+)\*\*(\s)/g, '$1$2$3'); - record.data = record.data.replace(/(\s)\*([^*]+)\*(\s)/g, '$1$2$3'); - - // Turn refs and tags into links - record.data = record.data.replace(/(\s|^)#([A-Za-z0-9_-]+)/g, '$1#$2'); - record.data = record.data.replace(/(\s|^)@([A-Za-z0-9_-]+)/g, '$1@$2'); - - // Turn newlines into linebreaks and paragraphs - record.data = record.data.replace(/\r?\n\r?\n/g, "

").replace(/\r?\n/g, "
"); - - record.date = (new Date(record.timestamp * 1000)).toString(); - record.html = template.evaluate(record); -} - -function displayError(msg) { - items.innerText = msg; -} - - -// Object for browsing tags -function Tag(type, tag) { - this.initPager(); - this.type = type; - this.tag = tag; - - var url = baseURL + "/tag/"; - switch(type) { - case 'tag': - //url += '%23'; - url += 'H'; // apache is eating the hash, even encoded. Probably a security feature. - break; - case 'ref': - url += '%40'; - break; - default: - alert('Invalid tag type: ' + type); - return; - } - url += tag; - - new Ajax.Request(url, { - method: 'get', - onSuccess: function(r) { - var j = r.responseText.evalJSON(); - if (j) { - var maxid = j.length - 1; - j.each(function(v, i) { - v.id = maxid - i; - mangleRecord(v, tagRecordTemplate) - }); - this.addItems(j); - this.pageStart = j.length - 1; - } - this.displayItems(); - }.bind(this), - onFailure: function(r) { - this.displayItems(); - }.bind(this) - }); - -} -Tag.prototype = new Pager(); -Tag.prototype.constructor = Tag; - -Tag.prototype.show = function() { - var ctype = {ref: '@', tag: '#'}[this.type]; - - $$('[name=section]').each(function(v) { - v.update(' about ' + ctype + this.tag); - }.bind(this)); - $('welcome').hide(); - $('post').hide(); - $('older_link').hide(); - $('newer_link').hide(); - $('rss').hide(); - items.show(); -} - -function postPopup() { - if (loginStatus.loggedIn) { - var post = $('post'); - if (post.visible()) { - post.hide(); - } else { - post.show(); - if (currentPager.username && currentPager.username != loginStatus.username && !$('post.content').value) { - $('post.content').value = '@' + currentPager.username + ': '; - } - $('post.content').focus(); - } - } -} - -function signup() { - var username = $('signup.username').value; - var password = $('signup.password').value; - - new Ajax.Request(baseURL + '/create', { + new Ajax.Request(baseURL + '/passwd', { parameters: { - username: username, - password: password + username: loginStatus.username, + password: old_password, + new_password: new_password }, onSuccess: function(r) { - $('signup').hide(); - location.href = '/#' + username; - hashSwitch(); - - loginStatus.login(username, password); + if (r.responseJSON.status == 'success') { + alert('Password changed'); + passwd_cancel(); + } else { + alert('Password change failed. Your password has NOT been changed.'); + } }, onFailure: function(r) { - alert("Failed to create user"); - } - }); -} - -function signup_cancel() { - $('signup').hide(); - hashSwitch(); -} - -function newer_page() { - if (currentPager) - currentPager.newerPage(); -} - -function older_page() { - if (currentPager) - currentPager.olderPage(); -} - -var resizePostContentTimeout = null; -function resizePostContent() { - if (resizePostContentTimeout) - clearTimeout(resizePostContentTimeout); - resizePostContentTimeout = setTimeout(function() { - var c = $('post.content'); - var lines = Math.floor(c.value.length / (100 * (c.clientWidth / 1000))) + 1; - var m = c.value.match(/\r?\n/g); - if (m) - lines += m.length; - if (lines <= 3) { - c.style.height = ""; - } else { - c.style.height = (lines * 17) + "pt"; - } - resizePostContentTimeout = null; - }, 150); -} - -function loadNewThings() { - new Ajax.Request(baseURL + '/newtags.json', { - onSuccess: function(r) { - $('newtags').update(); - r.responseText.evalJSON().each(function(v) { - var a = new Element('a', {href: '/#tag/' + v}); - a.insert('#' + v); - $('newtags').insert(a); - $('newtags').insert(new Element('br')); - }); - } - }); - - new Ajax.Request(baseURL + '/newusers.json', { - onSuccess: function(r) { - $('newusers').update(); - r.responseText.evalJSON().each(function(v) { - var a = new Element('a', {href: '/#' + v}); - a.insert('@' + v); - $('newusers').insert(a); - $('newusers').insert(new Element('br')); - }); + alert('Password change error'); } }); } -function hashSwitch() { - var m; - if (m = location.search.match(/^\?post\/([^/]+)\/(.+)/)) { - $('post').show(); - $('post.content').value = '[' + decodeURIComponent(m[1]).replace(']','').replace('[','') + '](' + decodeURIComponent(m[2]) + ')'; - } else if (m = location.hash.match(/^#(ref|tag)\/([A-Za-z0-9_-]+)$/)) { - currentPager = new Tag(m[1], m[2]); - currentPager.show(); - } else if (m = location.hash.match(/^#([A-Za-z0-9_-]+)(:(\d+))?$/)) { - if (!currentPager || currentPager.username != m[1]) - currentPager = new User(m[1]); - currentPager.show(); - loginStatus.update(); - - if (m[3]) { - var r = parseInt(m[3]); - currentPager.showRecord = r; - if (currentPager.recordCache[r]) { - currentPager.displayItems(); - } else { - currentPager.loadItems((r >= 49 ? r - 49 : 0), r); - } - } else { - currentPager.pageStart = currentPager.itemCount - 1; - currentPager.loadItems(); - } - } else { - $$('[name=section]').each(function(v) { v.update('Welcome') }); - $('signup').hide(); - items.update(); - items.hide(); - $('newer_link').hide(); - $('older_link').hide(); - $('welcome').show(); - $('rss').hide(); - loadNewThings(); - } -} - -var lastHash; -function hashCheck() { - if (location.hash != lastHash) { - lastHash = location.hash; - hashSwitch(); - } -} - -function init() { - items = $('items'); - loginStatus = new LoginStatus(); - - lastHash = location.hash; - hashSwitch(); - - setInterval(hashCheck, 250); - - document.body.observe('keyup', function(event) { - if (event.shiftKey && event.keyCode == '32') { - postPopup(); - event.stop(); - } - }); - $('post.content').addEventListener('keyup', function(event) { - event.stopPropagation(); - }, true); +function passwd_cancel() { + $('passwd').hide(); + $('navigation').show(); + urlSwitch(); }