commit:9818631aecddd827374a0c01b3ec287e7b018bb1
author:Chip Black
committer:Chip Black
date:Sun Apr 22 12:13:22 2012 -0700
parents:8f3918fbe3d812782d70af32b0d122bcd16a4c65
Add User, port simplified paging

Add User to handle user pages.  Rework paging to simply append older items;
damn the memory cost.  Move postMangle to a Util class.
diff --git a/www/js/blerg.js b/www/js/blerg.js
line changes: +0/-112
index fc93a5e..8da3872
--- a/www/js/blerg.js
+++ b/www/js/blerg.js
@@ -365,118 +365,6 @@ User.prototype.loadItems = function(from, to, continuation) {
     });
 }
 
-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 = ['<p>'];
-    var endpush = null;
-    var listMode = false;
-    lines.each(function(l) {
-        if (l == '') {
-            if (out[out.length - 1] == '<br>') {
-                out[out.length - 1] = '<p>';
-            }
-            if (out[out.length - 1] == '</li>') {
-                out.push('</ul>');
-                out.push('<p>');
-                listMode = false;
-            }
-            return;
-        }
-
-        // Put quoted material into a special paragraph
-        if (l[0] == '>') {
-            var pi = out.lastIndexOf('<p>');
-            if (pi != -1) {
-                out[pi] = '<p class="quote">';
-                l = l.replace(/^>\s*/, '');
-            }
-        }
-
-        // Sanitize HTML input
-        l = l.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
-
-        // Turn HTTP URLs into links
-        l = l.replace(/(\s|^)(https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/([^\s"]*[^.!,;?()\s])?)?)/g, '$1<a href="$2">$2</a>');
-
-        // 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<a href="$3">$2</a> <a href="$3" onclick="play_audio(); return false"><img src="/images/play.png"></a>');
-
-        // 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<a href="$3">$2</a> <a href="$3" onclick="play_audio(); return false"><img src="/images/play.png"></a>');
-
-        // 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<a href="$3">$2</a> <a href="$3" onclick="play_video(); return false"><img src="/images/play.png"></a>');
-
-        // 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<a href="$3">$2</a> <a href="$3" onclick="play_video(); return false"><img src="/images/play.png"></a>');
-
-        // Regular markdown links
-        l = l.replace(/(\s|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?)\)/g, '$1<a href="$3">$2</a>');
-
-        // Turn *foo* into italics and **foo** into bold
-        l = l.replace(/([^\w\\]|^)\*\*(\w[^*]*)\*\*(\W|$)/g, '$1<b>$2</b>$3');
-        l = l.replace(/([^\w\\]|^)\*(\w[^*]*)\*(\W|$)/g, '$1<i>$2</i>$3');
-
-        // Turn refs and tags into links
-        l = l.replace(/(\s|^)#([A-Za-z0-9_-]+)/g, '$1<a href="#/tag/$2" class="ref" onclick="return qlink()">#$2</a>');
-        l = l.replace(/(\s|^)@([A-Za-z0-9_-]+)(\/\d+)?/g, '$1<a href="#$2$3" class="ref" onclick="return qlink()">@$2</a>');
-
-        // Create lists when lines begin with *
-        if (l[0] == '*') {
-            if (!listMode) {
-                var pi = out.lastIndexOf('<p>');
-                out[pi] = '<ul>';
-                listMode = true;
-            }
-            l = l.replace(/^\*\s*/, '');
-            out.push('<li>');
-            endpush = '</li>';
-        }
-
-        // Create headers when lines begin with = or #
-        if (l[0] == '=' || l[0] == '#') {
-            var m = l.match(/^([=#]+)/);
-            var depth = m[1].length;
-            if (depth <= 5) {
-                l = l.replace(/^[=#]+\s*/, '').replace(/\s*[=#]+$/, '');
-                out.push('<h' + depth + '>');
-                endpush = '</h' + depth + '>';
-            }
-        }
-
-        // Remove backslashes from escaped metachars
-        l = l.replace(/\\([*\[\]@#])/g, '$1');
-
-        out.push(l);
-        if (endpush) {
-            out.push(endpush);
-            endpush = null;
-        } else {
-            out.push('<br>');
-        }
-    });
-    while (out[out.length - 1] == '<br>' || out[out.length - 1] == '<p>')
-        out.pop();
-    if (listMode)
-        out.push('</ul>');
-
-    record.data = out.join('');
-    record.date = (new Date(record.timestamp * 1000)).toString();
-    record.html = template.evaluate(record);
-}
-
 function displayError(msg) {
     items.innerText = msg;
 }

diff --git a/www/jssrc/blerg/Blerg.js b/www/jssrc/blerg/Blerg.js
line changes: +1/-1
index a077b52..52de4e3
--- a/www/jssrc/blerg/Blerg.js
+++ b/www/jssrc/blerg/Blerg.js
@@ -31,7 +31,7 @@ enyo.kind({
 		['hash',   /^#\/feed(?:\/p(\d+))?$/, "blerg.Feed"],
 		['hash',   /^#([A-Za-z0-9_-]+)(?:\/(p)?(\d+))?$/, "blerg.User"]
 	],
-	pathHandlers: [ blerg.Welcome ],
+	pathHandlers: [ blerg.User, blerg.Welcome ],
 	rendered: function() {
 		this.inherited(arguments);
 

diff --git a/www/jssrc/blerg/Pager.js b/www/jssrc/blerg/Pager.js
line changes: +36/-0
index 0000000..9ae703b
--- /dev/null
+++ b/www/jssrc/blerg/Pager.js
@@ -0,0 +1,36 @@
+enyo.kind({
+	name: "blerg.Pager",
+	kind: "Control",
+	listKind: "Control",
+	lastRecord: null,
+	components: [
+		{name: "records"},
+		{name: "loadMoreButton", kind: "onyx.Button", onclick: "loadMore", content: "Load More"}
+	],
+	addItems: function(items) {
+		this.$.records.createComponents(items, {kind: this.listKind});
+		for (var i = 0; i < items.length; i++) {
+			var r = parseInt(items[i].record);
+			if (r < this.lastRecord || this.lastRecord == null)
+				this.lastRecord = r;
+		}
+		this.$.records.render();
+		if (this.lastRecord == 0)
+			this.$.loadMoreButton.hide();
+	},
+	loadMore: function() {
+		if (this.lastRecord == 0)
+			return;
+
+		if (this.lastRecord != null) {
+			var to = this.lastRecord - 1;
+			var from = this.lastRecord - 50;
+			if (from < 0)
+				from = 0;
+			this.loadItems(from, to);
+		} else {
+			this.loadItems();
+		}
+	},
+	loadItems: function(from, to) { }
+});

diff --git a/www/jssrc/blerg/Record.js b/www/jssrc/blerg/Record.js
line changes: +2/-2
index 2751042..7601de9
--- a/www/jssrc/blerg/Record.js
+++ b/www/jssrc/blerg/Record.js
@@ -9,7 +9,7 @@ enyo.kind({
 		record: null
 	},
 	components: [
-		{name: "data", noDom: true},
+		{name: "data", noDom: true, allowHtml: true},
 		{classes: "info", components: [
 			{noDom: true, content: "Posted "},
 			{name: "date", noDom: true},
@@ -26,7 +26,7 @@ enyo.kind({
 		this.updateLinks();
 	},
 	dataChanged: function() {
-		this.$.data.setContent(postMangle(this.data));
+		this.$.data.setContent(blerg.Util.blergFormat(this.data));
 	},
 	timestampChanged: function() {
 		this.$.date.setContent(new Date(this.timestamp * 1000).toString());

diff --git a/www/jssrc/blerg/User.js b/www/jssrc/blerg/User.js
line changes: +54/-0
index 0000000..ca0916a
--- /dev/null
+++ b/www/jssrc/blerg/User.js
@@ -0,0 +1,54 @@
+enyo.kind({
+	name: "blerg.User",
+	kind: "blerg.Pager",
+	listKind: "blerg.Record",
+	published: {
+		username: "",
+		permalink: false,
+		firstRecord: null,
+	},
+	statics: {
+		locationDetect: function(l) {
+			var m = l.hash.match(/^#([A-Za-z0-9_-]+)(?:\/(p)?(\d+))?$/);
+			if (m) {
+				return {
+					kind: "blerg.User",
+					username: m[1],
+					permalink: m[2] != 'p',
+					firstRecord: parseInt(m[3])
+				};
+			}
+		}
+	},
+	create: function() {
+		this.inherited(arguments);
+		this.usernameChanged();
+	},
+	usernameChanged: function() {
+		this.bubble('onSetTitle', {section: '@' + this.username});
+		this.$.records.destroyComponents();
+		this.lastRecord = null;
+		this.loadMore();
+	},
+	loadItems: function(from, to) {
+		this.inherited(arguments);
+
+		var url;
+		if (from != undefined && to != undefined) {
+			url = baseURL +  '/get/' + this.username + '/' + from + '-' + to;
+		} else {
+			url = baseURL +  '/get/' + this.username;
+		}
+
+		var req = new enyo.Ajax({
+			url: url
+		});
+		req.response(function(inSender, inResponse) {
+			for (var i = 0; i < inResponse.length; i++) {
+				inResponse[i].author = this.username;
+			}
+			this.addItems(inResponse);
+		}.bind(this));
+		req.go();
+	}
+});

diff --git a/www/jssrc/blerg/Util.js b/www/jssrc/blerg/Util.js
line changes: +113/-0
index 0000000..1bfab4c
--- /dev/null
+++ b/www/jssrc/blerg/Util.js
@@ -0,0 +1,113 @@
+blerg = window.blerg || {};
+
+blerg.Util = {};
+
+blerg.Util.blergFormat = function(text) {
+	var lines = text.split(/\r?\n/);
+	if (lines[lines.length - 1] == '')
+		lines.pop();
+
+	var out = ['<p>'];
+	var endpush = null;
+	var listMode = false;
+	lines.forEach(function(l) {
+		if (l == '') {
+			if (out[out.length - 1] == '<br>') {
+				out[out.length - 1] = '<p>';
+			}
+			if (out[out.length - 1] == '</li>') {
+				out.push('</ul>');
+				out.push('<p>');
+				listMode = false;
+			}
+			return;
+		}
+
+		// Put quoted material into a special paragraph
+		if (l[0] == '>') {
+			var pi = out.lastIndexOf('<p>');
+			if (pi != -1) {
+				out[pi] = '<p class="quote">';
+				l = l.replace(/^>\s*/, '');
+			}
+		}
+
+		// Sanitize HTML input
+		l = l.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
+
+		// Turn HTTP URLs into links
+		l = l.replace(/(\s|^)(https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/([^\s"]*[^.!,;?()\s])?)?)/g, '$1<a href="$2">$2</a>');
+
+		// 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<a href="$3">$2</a> <a href="$3" onclick="play_audio(); return false"><img src="/images/play.png"></a>');
+
+		// 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<a href="$3">$2</a> <a href="$3" onclick="play_audio(); return false"><img src="/images/play.png"></a>');
+
+		// 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<a href="$3">$2</a> <a href="$3" onclick="play_video(); return false"><img src="/images/play.png"></a>');
+
+		// 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<a href="$3">$2</a> <a href="$3" onclick="play_video(); return false"><img src="/images/play.png"></a>');
+		*/
+
+		// Regular markdown links
+		l = l.replace(/(\s|^)\[([^\]]+)\]\((https?:\/\/[a-zA-Z0-9.-]*[a-zA-Z0-9](\/[^)"]*?)?)\)/g, '$1<a href="$3">$2</a>');
+
+		// Turn *foo* into italics and **foo** into bold
+		l = l.replace(/([^\w\\]|^)\*\*(\w[^*]*)\*\*(\W|$)/g, '$1<b>$2</b>$3');
+		l = l.replace(/([^\w\\]|^)\*(\w[^*]*)\*(\W|$)/g, '$1<i>$2</i>$3');
+
+		// Turn refs and tags into links
+		l = l.replace(/(\s|^)#([A-Za-z0-9_-]+)/g, '$1<a href="#/tag/$2" class="ref" onclick="return qlink()">#$2</a>');
+		l = l.replace(/(\s|^)@([A-Za-z0-9_-]+)(\/\d+)?/g, '$1<a href="#$2$3" class="ref" onclick="return qlink()">@$2</a>');
+
+		// Create lists when lines begin with *
+		if (l[0] == '*') {
+			if (!listMode) {
+				var pi = out.lastIndexOf('<p>');
+				out[pi] = '<ul>';
+				listMode = true;
+			}
+			l = l.replace(/^\*\s*/, '');
+			out.push('<li>');
+			endpush = '</li>';
+		}
+
+		// Create headers when lines begin with = or #
+		if (l[0] == '=' || l[0] == '#') {
+			var m = l.match(/^([=#]+)/);
+			var depth = m[1].length;
+			if (depth <= 5) {
+				l = l.replace(/^[=#]+\s*/, '').replace(/\s*[=#]+$/, '');
+				out.push('<h' + depth + '>');
+				endpush = '</h' + depth + '>';
+			}
+		}
+
+		// Remove backslashes from escaped metachars
+		l = l.replace(/\\([*\[\]@#])/g, '$1');
+
+		out.push(l);
+		if (endpush) {
+			out.push(endpush);
+			endpush = null;
+		} else {
+			out.push('<br>');
+		}
+	});
+	while (out[out.length - 1] == '<br>' || out[out.length - 1] == '<p>')
+		out.pop();
+	if (listMode)
+		out.push('</ul>');
+
+	return out.join('');
+}

diff --git a/www/jssrc/blerg/Welcome.js b/www/jssrc/blerg/Welcome.js
line changes: +1/-1
index 1d7d9db..769450e
--- a/www/jssrc/blerg/Welcome.js
+++ b/www/jssrc/blerg/Welcome.js
@@ -26,7 +26,7 @@ enyo.kind({
 			{name: "latestTags", classes: "latest-tags"}
 		]}
 	],
-	rendered: function() {
+	create: function() {
 		this.inherited(arguments);
 
 		this.bubble("onSetTitle", {section: "Welcome!"});

diff --git a/www/jssrc/blerg/package.js b/www/jssrc/blerg/package.js
line changes: +3/-0
index c3c3013..dc52cd4
--- a/www/jssrc/blerg/package.js
+++ b/www/jssrc/blerg/package.js
@@ -1,5 +1,6 @@
 enyo.depends(
 	'API.js',
+	'Util.js',
 	'Link.js',
 	'Record.js',
 	'Title.js',
@@ -10,5 +11,7 @@ enyo.depends(
 	'SignupDialog.js',
 	'PasswdDialog.js',
 	'Welcome.js',
+	'Pager.js',
+	'User.js',
 	'Blerg.js'
 );