d2d0b83e1b5927689e0ad20d4587c4df4c946157
[blerg.git] / www / jssrc / blerg / User.js
1 enyo.kind({
2         name: "blerg.User",
3         kind: "blerg.Pager",
4         listKind: "blerg.Record",
5         handlers: {
6                 onLogin: "getStalkStatus",
7                 onLogout: "getStalkStatus"
8         },
9         published: {
10                 username: "",
11                 permalink: false,
12                 record: null,
13         },
14         components: [
15                 {classes: "blerg-user-controls", components: [
16                         {name: "chatterLink", kind: "blerg.Link", content: "[chatter]"},
17                         {name: "stalkLink", kind: "blerg.Link", content: "[stalk]", showing: false, onclick: "startStalking"},
18                         {name: "unstalkLink", kind: "blerg.Link", content: "[stop stalking]", showing: false, onclick: "stopStalking"},
19                         {name: "rssLink", kind: "blerg.Link", components: [
20                                 {noDom: true, content: "["},
21                                 {kind: "Image", src: "/images/rss.png", attributes: {width: 16, height: 16}},
22                                 {noDom: true, content: "RSS]"}
23                         ]}
24                 ]},
25                 {name: "records"},
26                 {name: "loadMoreButton", kind: "onyx.Button", onclick: "loadMore", content: "Load More"},
27                 {name: "api", kind: "blerg.API",
28                  onItemsLoaded: "itemsLoaded"}
29         ],
30         statics: {
31                 locationDetect: function(l) {
32                         var m = l.hash.match(/^#([A-Za-z0-9_-]+)(?:\/(\d+))?$/);
33                         if (m) {
34                                 return {
35                                         kind: "blerg.User",
36                                         username: m[1],
37                                         permalink: m[2] != undefined,
38                                         record: parseInt(m[2])
39                                 };
40                         }
41                 }
42         },
43         create: function() {
44                 this.inherited(arguments);
45                 this.usernameChanged();
46         },
47         usernameChanged: function() {
48                 this.bubble('onSetTitle', {section: '@' + this.username});
49                 this.$.records.destroyComponents();
50                 this.lastRecord = null;
51                 this.$.loadMoreButton.hide();
52                 this.$.chatterLink.setHref('/#/ref/' + this.username);
53                 this.$.rssLink.setHref('/rss/' + this.username);
54                 this.getStalkStatus();
55
56                 if (this.permalink) {
57                         this.loadItems(this.record, this.record);
58                 } else {
59                         this.loadMore();
60                 }
61         },
62         loadItems: function(from, to) {
63                 this.inherited(arguments);
64                 this.$.api.loadUserRecords(this.username, from, to);
65         },
66         itemsLoaded: function(inSender, inEvent) {
67                 if (this.permalink) {
68                         this.$.loadMoreButton.hide();
69                 } else {
70                         this.$.loadMoreButton.show();
71                 }
72
73                 for (var i = 0; i < inEvent.entries.length; i++) {
74                         inEvent.entries[i].author = this.username;
75                 }
76
77                 this.addItems(inEvent.entries);
78         },
79         getStalkStatus: function() {
80                 if (!blerg.API.loggedIn) {
81                         this.$.stalkLink.hide();
82                         this.$.unstalkLink.hide();
83                         return;
84                 }
85                 // Make an API call to determine status
86         }
87 });