Rework API to provide global state for individual instances
[blerg.git] / www / jssrc / blerg / User.js
index ca0916a..d2d0b83 100644 (file)
@@ -2,20 +2,40 @@ enyo.kind({
        name: "blerg.User",
        kind: "blerg.Pager",
        listKind: "blerg.Record",
+       handlers: {
+               onLogin: "getStalkStatus",
+               onLogout: "getStalkStatus"
+       },
        published: {
                username: "",
                permalink: false,
-               firstRecord: null,
+               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_-]+)(?:\/(p)?(\d+))?$/);
+                       var m = l.hash.match(/^#([A-Za-z0-9_-]+)(?:\/(\d+))?$/);
                        if (m) {
                                return {
                                        kind: "blerg.User",
                                        username: m[1],
-                                       permalink: m[2] != 'p',
-                                       firstRecord: parseInt(m[3])
+                                       permalink: m[2] != undefined,
+                                       record: parseInt(m[2])
                                };
                        }
                }
@@ -28,27 +48,40 @@ enyo.kind({
                this.bubble('onSetTitle', {section: '@' + this.username});
                this.$.records.destroyComponents();
                this.lastRecord = null;
-               this.loadMore();
+               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 {
+                       this.loadMore();
+               }
        },
        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) {
-                       for (var i = 0; i < inResponse.length; i++) {
-                               inResponse[i].author = this.username;
-                       }
-                       this.addItems(inResponse);
-               }.bind(this));
-               req.go();
+               for (var i = 0; i < inEvent.entries.length; i++) {
+                       inEvent.entries[i].author = this.username;
+               }
+
+               this.addItems(inEvent.entries);
+       },
+       getStalkStatus: function() {
+               if (!blerg.API.loggedIn) {
+                       this.$.stalkLink.hide();
+                       this.$.unstalkLink.hide();
+                       return;
+               }
+               // Make an API call to determine status
        }
 });