X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;f=www%2Fjs%2Fblerg.js;h=fc93a5ee08573afdf87cceb3867252e6b44836aa;hb=55431b133192e59138a51591d7ef4ef20b6ceb52;hp=d0135b8abda05ff5591979b11c02b0253946704e;hpb=36bef82c002dfa65064635cb8d3fae3660a6cde2;p=blerg.git diff --git a/www/js/blerg.js b/www/js/blerg.js index d0135b8..fc93a5e 100644 --- a/www/js/blerg.js +++ b/www/js/blerg.js @@ -5,13 +5,13 @@ // Config var baseURL = ''; var recordTemplate = new Template( - '
#{data}
Posted #{date}. [permalink] [reply]
' + '
#{data}
Posted #{date}. [permalink] [reply]
' ); var tagRecordTemplate = new Template( - '
#{data}
Posted by @#{author} on #{date}. [permalink] [reply]
' + '
#{data}
Posted by @#{author} on #{date}. [permalink] [reply]
' ); var latestRecordsTemplate = new Template( - '
@#{author} #{data}
' + '
@#{author} #{data}
' ); // Page elements @@ -31,6 +31,8 @@ function LoginStatus() { if (cookies.auth && cookies.username) { this.loggedIn = true; this.username = cookies.username; + this.requestFeedStatus(); + this.feedStatusUpdateInterval = setInterval(this.requestFeedStatus.bind(this), 900000); } else { this.loggedIn = false; this.username = null; @@ -51,6 +53,8 @@ LoginStatus.prototype.login = function(username, password) { this.username = username; document.cookie = "username=" + username; $('login.password').value = ''; + this.requestFeedStatus(); + this.feedStatusUpdateInterval = setInterval(this.requestFeedStatus.bind(this), 900000); this.update(); } else { alert("Could not log in"); @@ -73,6 +77,7 @@ LoginStatus.prototype.logout = function() { this.loggedIn = false; document.cookie = "auth=; expires=1-Jan-1970 00:00:00 GMT"; this.update(); + clearInterval(this.feedStatusUpdateInterval); }.bind(this) }); document.cookie = "username=; expires=1-Jan-1970 00:00:00 GMT"; @@ -80,9 +85,9 @@ LoginStatus.prototype.logout = function() { LoginStatus.prototype.update = function() { if (this.loggedIn) { - $('userlink').href = '/#' + this.username; + $('userlink').href = baseURL + '/#' + this.username; $('userlink').update('@' + this.username); - $('reflink').href = '/#/ref/' + this.username; + $('reflink').href = baseURL + '/#/ref/' + this.username; $('login').hide(); $('logout').show(); } else { @@ -123,6 +128,19 @@ LoginStatus.prototype.post = function(msg) { }); } +LoginStatus.prototype.requestFeedStatus = function() { + new Ajax.Request('/feedinfo', { + parameters: { username: this.username }, + onSuccess: function(r) { + var j = r.responseText.evalJSON(); + if (j['new'] > 0) { + $('newFeedMessages').update('(' + j['new'] + ' new)'); + } else { + $('newFeedMessages').update(''); + } + } + }); +} // Base object for paged data function Pager() { @@ -180,14 +198,14 @@ Pager.prototype.displayItems = function() { } if (this.pageStart < this.itemCount - 1) { - $('newer_link').href = '/#' + this.baseFrag + '/p' + (this.pageStart + this.itemsPerPage); + $('newer_link').href = baseURL + '/#' + 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').href = baseURL + '/#' + this.baseFrag + '/p' + (this.pageStart - this.itemsPerPage); $('older_link').show(); } else { $('older_link').hide(); @@ -253,13 +271,13 @@ User.prototype.show = function() { $('rss').show(); $('rsslink').href = '/rss/' + this.username; $$('[name=user.reflink]').each(function(e) { - e.href = '/#/ref/' + this.username; + e.href = baseURL + '/#/ref/' + this.username; }.bind(this)); $('usercontrols').show(); - if (this.permalink && this.pageStart) { + if (this.permalink && this.pageStart >= 0) { this.showRecord(this.pageStart); - } else if (this.pageStart) { + } else if (this.pageStart >= 0) { this.showPageAt(this.pageStart); } else { this.reload(); @@ -311,6 +329,9 @@ User.prototype.reload = function() { } User.prototype.loadItems = function(from, to, continuation) { + if (to < 0) + return; + var url; if (from != undefined && to != undefined) { url = baseURL + '/get/' + this.username + '/' + from + '-' + to; @@ -383,6 +404,25 @@ function mangleRecord(record, template) { l = l.replace(/(\s|^)(https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/([^\s"]*[^.!,;?()\s])?)?)/g, '$1$2'); // Turn markdown links into links + var re; + + // Craft a regex that finds URLs that end in the extensions specified by BlergMedia.audioExtensions. + re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9]\\/[^)"]*?\\.(' + BlergMedia.audioExtensions.join('|') + '))\\)', 'g'); + l = l.replace(re, '$1$2 '); + + // Ditto, but use the extended markdown link syntax to specify the format + re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\\/[^)"]*?)?)\\s+audio:(' + BlergMedia.audioExtensions.join('|') + ')\\)', 'g'); + l = l.replace(re, '$1$2 '); + + // Craft a regex that finds URLs that end in the extensions specified by BlergMedia.videoExtensions. + re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9]\\/[^)"]*?\\.(' + BlergMedia.videoExtensions.join('|') + '))\\)', 'g'); + l = l.replace(re, '$1$2 '); + + // Ditto, but use the extended markdown link syntax to specify the format + re = new RegExp('(\\s|^)\\[([^\\]]+)\\]\\((https?:\\/\\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\\/[^)"]*?)?)\\s+video:(' + BlergMedia.videoExtensions.join('|') + ')\\)', 'g'); + l = l.replace(re, '$1$2 '); + + // Regular markdown links l = l.replace(/(\s|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?)\)/g, '$1$2'); // Turn *foo* into italics and **foo** into bold @@ -537,6 +577,7 @@ function Feed(m) { if (!this.pageStart) this.pageStart = response.length - 1; this.itemCount = response.length; + loginStatus.requestFeedStatus(); } this.displayItems(); }.bind(this), @@ -606,6 +647,36 @@ function signup_cancel() { urlSwitch(); } +function passwd() { + var old_password = $('passwd.old_password').value; + var new_password = $('passwd.new_password').value; + + new Ajax.Request(baseURL + '/passwd', { + parameters: { + username: loginStatus.username, + password: old_password, + new_password: new_password + }, + onSuccess: function(r) { + 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('Password change error'); + } + }); +} + +function passwd_cancel() { + $('passwd').hide(); + $('navigation').show(); + urlSwitch(); +} + function subscribe() { new Ajax.Request(baseURL + '/subscribe/' + currentPager.username, { method: 'post', @@ -650,25 +721,6 @@ function unsubscribe() { }); } -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); -} - var tickerTimer = null; var tickerHead, tickerTail; @@ -719,12 +771,13 @@ function stopTicker() { function loadLatest() { new Ajax.Request(baseURL + '/latest.json', { + method: 'GET', onSuccess: function(r) { var j = r.responseText.evalJSON(); $('latest-tags').update(); j.tags.each(function(v) { - var a = new Element('a', {href: '/#/tag/' + v}); + var a = new Element('a', {href: baseURL + '/#/tag/' + v}); a.insert('#' + v); a.onclick = "return qlink()"; a.className = 'ref'; @@ -744,34 +797,6 @@ function loadLatest() { }); } -function qlink(loc) { - if (loc) { - location.hash = loc; - } else if (event && event.target) { - location.href = event.target.href; - } else { - // Bogus qlink - return; - } - lastHash = location.hash; - urlSwitch(); - return false; -} - -function Welcome() { - loadLatest(); -} - -Welcome.prototype.show = function() { - $$('[name=section]').each(function(v) { v.update('Welcome') }); - $('welcome').show(); -} - -Welcome.prototype.hide = function() { - stopTicker(); - $('welcome').hide(); -} - function ExternalURLPost(m) { this.title = decodeURIComponent(m[1]).replace(']','').replace('[',''); this.url = decodeURIComponent(m[2]); @@ -781,66 +806,3 @@ ExternalURLPost.prototype.show = function() { $('post.content').value = '[' + this.title + '](' + this.url + ')'; $('post').show(); } - -var urlmap = [ - ['search', /^\?post\/([^/]+)\/(.+)/, ExternalURLPost], - ['hash', /^#\/(ref|tag)\/([A-Za-z0-9_-]+)(?:\/p(\d+))?$/, Tag], - ['hash', /^#\/feed(?:\/p(\d+))?$/, Feed], - ['hash', /^#([A-Za-z0-9_-]+)(?:\/(p)?(\d+))?$/, User] -]; - -function urlSwitch() { - var m; - var pageconstructor; - - for (var i = 0; i < urlmap.length; i++) { - if (m = location[urlmap[i][0]].match(urlmap[i][1])) { - pageconstructor = urlmap[i][2]; - break; - } - } - if (i == urlmap.length) - pageconstructor = Welcome; - - if (currentPager && currentPager instanceof pageconstructor) { - // updateState returns true if the state has been successfully updated. - // Otherwise, we continue and create a new instance. - if (currentPager.updateState(m)) - return; - } - - if (currentPager && currentPager.hide) - currentPager.hide(); - - currentPager = new pageconstructor(m); - if (currentPager.show) - currentPager.show(); -} - -var lastHash; -function hashCheck() { - if (location.hash != lastHash) { - lastHash = location.hash; - urlSwitch(); - } -} - -function init() { - items = $('items'); - loginStatus = new LoginStatus(); - - lastHash = location.hash; - urlSwitch(); - - 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); -}