commit:15917511e7ec895e1caf6464a7deef76ed448f76
author:Chip Black
committer:Chip Black
date:Sun Apr 22 15:39:40 2012 -0700
parents:65a1f7dc82554486ffa9cc03eac29a7a1642f046
Rework API to provide global state for individual instances

Also add User controls
diff --git a/www/css/blerg.css b/www/css/blerg.css
line changes: +16/-0
index 24bb13e..ed87bc6
--- a/www/css/blerg.css
+++ b/www/css/blerg.css
@@ -155,6 +155,22 @@ h1, h2, h3 {
 	width: 100%;
 }
 
+.blerg-user-controls {
+	font-size: 14pt;
+	text-align: right;
+}
+
+.blerg-user-controls a {
+	color: inherit;
+	text-decoration: none;
+	margin-left: 4pt;
+}
+
+.blerg-user-controls a:hover {
+	text-decoration: underline;
+}
+
+
 .record {
 	margin: 8pt 0 24pt 0;
 	font-size: 14pt;

diff --git a/www/jssrc/blerg/API.js b/www/jssrc/blerg/API.js
line changes: +45/-10
index 2fb9561..1a6b013
--- a/www/jssrc/blerg/API.js
+++ b/www/jssrc/blerg/API.js
@@ -1,19 +1,36 @@
 var baseURL = '';
 
+// The API state is static so that any instance can use login-dependent API
+// calls
 enyo.kind({
 	name: "blerg.API",
-	loggedIn: false,
-	username: "",
+	kind: "Component",
+	statics: {
+		apiInitialized: false,
+		loggedIn: false,
+		username: "",
+	},
 	create: function() {
 		this.inherited(arguments);
+		if (blerg.API.apiInitialized) {
+			if (blerg.API.loggedIn) {
+				setTimeout(function() {
+					this.bubble('onLoginSuccessful', {username: blerg.API.username});
+				}.bind(this), 0);
+			}
+			return;
+		}
+
 		if (enyo.getCookie('auth') && enyo.getCookie('username')) {
-			this.loggedIn = true;
-			this.username = enyo.getCookie('username');
+			blerg.API.loggedIn = true;
+			blerg.API.username = enyo.getCookie('username');
 			// Defer the signal until everything's initialized
 			setTimeout(function() {
-				this.bubble('onLoginSuccessful', {username: this.username});
+				this.bubble('onLoginSuccessful', {username: blerg.API.username});
 			}.bind(this), 0);
 		}
+
+		blerg.API.apiInitialized = true;
 	},
 	login: function(username, password) {
 		var req = new enyo.Ajax({
@@ -22,8 +39,8 @@ enyo.kind({
 		});
 		req.response(function(inSender, inResponse) {
 			if (inResponse.status == 'success') {
-				this.loggedIn = true;
-				this.username = username;
+				blerg.API.loggedIn = true;
+				blerg.API.username = username;
 				enyo.setCookie('username', username);
 				this.bubble('onLoginSuccessful', {username: username});
 			} else {
@@ -42,18 +59,36 @@ enyo.kind({
 			method: 'POST'
 		});
 		req.response(function(inSender, inResponse) {
-			this.loggedIn = false;
+			blerg.API.loggedIn = false;
 			enyo.setCookie('auth', '', {"Max-Age": 0});
 			this.bubble('onLogoutSuccessful');
 		}.bind(this));
 		req.go({
-			username: this.username
+			username: blerg.API.username
 		});
 		enyo.setCookie('username', '', {"Max-Age": 0});
 	},
 	requestFeedStatus: function() {
-		if (!this.loggedIn)
+		if (!blerg.API.loggedIn)
 			throw new Error('Cannot request feed status when not logged in');
 		// TODO
+	},
+	loadUserRecords: function(username, from ,to) {
+		var url;
+		if (from != undefined && to != undefined) {
+			url = baseURL +  '/get/' + username + '/' + from + '-' + to;
+		} else {
+			url = baseURL +  '/get/' + username;
+		}
+
+		var req = new enyo.Ajax({
+			url: url
+		});
+		req.response(function(inSender, inResponse) {
+			this.bubble('onItemsLoaded', {
+				entries: inResponse
+			});
+		}.bind(this));
+		req.go();
 	}
 });

diff --git a/www/jssrc/blerg/User.js b/www/jssrc/blerg/User.js
line changes: +42/-21
index b977fa0..d2d0b83
--- a/www/jssrc/blerg/User.js
+++ b/www/jssrc/blerg/User.js
@@ -2,11 +2,31 @@ enyo.kind({
 	name: "blerg.User",
 	kind: "blerg.Pager",
 	listKind: "blerg.Record",
+	handlers: {
+		onLogin: "getStalkStatus",
+		onLogout: "getStalkStatus"
+	},
 	published: {
 		username: "",
 		permalink: false,
 		record: null,
 	},
+	components: [
+		{classes: "blerg-user-controls", components: [
+			{name: "chatterLink", kind: "blerg.Link", content: "[chatter]"},
+			{name: "stalkLink", kind: "blerg.Link", content: "[stalk]", showing: false, onclick: "startStalking"},
+			{name: "unstalkLink", kind: "blerg.Link", content: "[stop stalking]", showing: false, onclick: "stopStalking"},
+			{name: "rssLink", kind: "blerg.Link", components: [
+				{noDom: true, content: "["},
+				{kind: "Image", src: "/images/rss.png", attributes: {width: 16, height: 16}},
+				{noDom: true, content: "RSS]"}
+			]}
+		]},
+		{name: "records"},
+		{name: "loadMoreButton", kind: "onyx.Button", onclick: "loadMore", content: "Load More"},
+		{name: "api", kind: "blerg.API",
+		 onItemsLoaded: "itemsLoaded"}
+	],
 	statics: {
 		locationDetect: function(l) {
 			var m = l.hash.match(/^#([A-Za-z0-9_-]+)(?:\/(\d+))?$/);
@@ -29,6 +49,10 @@ enyo.kind({
 		this.$.records.destroyComponents();
 		this.lastRecord = null;
 		this.$.loadMoreButton.hide();
+		this.$.chatterLink.setHref('/#/ref/' + this.username);
+		this.$.rssLink.setHref('/rss/' + this.username);
+		this.getStalkStatus();
+
 		if (this.permalink) {
 			this.loadItems(this.record, this.record);
 		} else {
@@ -37,30 +61,27 @@ enyo.kind({
 	},
 	loadItems: function(from, to) {
 		this.inherited(arguments);
-
-		var url;
-		if (from != undefined && to != undefined) {
-			url = baseURL +  '/get/' + this.username + '/' + from + '-' + to;
+		this.$.api.loadUserRecords(this.username, from, to);
+	},
+	itemsLoaded: function(inSender, inEvent) {
+		if (this.permalink) {
+			this.$.loadMoreButton.hide();
 		} else {
-			url = baseURL +  '/get/' + this.username;
+			this.$.loadMoreButton.show();
 		}
 
-		var req = new enyo.Ajax({
-			url: url
-		});
-		req.response(function(inSender, inResponse) {
-			if (this.permalink) {
-				this.$.loadMoreButton.hide();
-			} else {
-				this.$.loadMoreButton.show();
-			}
-
-			for (var i = 0; i < inResponse.length; i++) {
-				inResponse[i].author = this.username;
-			}
+		for (var i = 0; i < inEvent.entries.length; i++) {
+			inEvent.entries[i].author = this.username;
+		}
 
-			this.addItems(inResponse);
-		}.bind(this));
-		req.go();
+		this.addItems(inEvent.entries);
+	},
+	getStalkStatus: function() {
+		if (!blerg.API.loggedIn) {
+			this.$.stalkLink.hide();
+			this.$.unstalkLink.hide();
+			return;
+		}
+		// Make an API call to determine status
 	}
 });