5bcdbd05f1e1e46834c982bbd32223f8b8b99c74
[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, onNavigate: "startStalking"},
18                         {name: "unstalkLink", kind: "blerg.Link", content: "[stop stalking]", showing: false, onNavigate: "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                  onSubscriptionStatus: "gotStalkStatus"}
30         ],
31         statics: {
32                 locationDetect: function(l) {
33                         var m = l.hash.match(/^#([A-Za-z0-9_-]+)(?:\/(\d+))?$/);
34                         if (m) {
35                                 return {
36                                         kind: "blerg.User",
37                                         username: m[1],
38                                         permalink: m[2] != undefined,
39                                         record: parseInt(m[2])
40                                 };
41                         }
42                 }
43         },
44         create: function() {
45                 this.inherited(arguments);
46                 this.usernameChanged();
47         },
48         usernameChanged: function() {
49                 this.bubble('onSetTitle', {section: '@' + this.username});
50                 this.$.records.destroyComponents();
51                 this.lastRecord = null;
52                 this.$.loadMoreButton.hide();
53                 this.$.chatterLink.setHref('/#/ref/' + this.username);
54                 this.$.rssLink.setHref('/rss/' + this.username);
55                 this.getStalkStatus();
56
57                 if (this.permalink) {
58                         this.loadItems(this.record, this.record);
59                 } else {
60                         this.loadMore();
61                 }
62         },
63         loadItems: function(from, to) {
64                 this.inherited(arguments);
65                 this.$.api.loadUserRecords(this.username, from, to);
66         },
67         itemsLoaded: function(inSender, inEvent) {
68                 if (this.permalink) {
69                         this.$.loadMoreButton.hide();
70                 } else {
71                         this.$.loadMoreButton.show();
72                 }
73
74                 for (var i = 0; i < inEvent.entries.length; i++) {
75                         inEvent.entries[i].author = this.username;
76                 }
77
78                 this.addItems(inEvent.entries);
79         },
80         getStalkStatus: function() {
81                 if (!blerg.API.loggedIn) {
82                         this.$.stalkLink.hide();
83                         this.$.unstalkLink.hide();
84                         return;
85                 }
86                 this.$.api.getSubscriptionStatus(this.username);
87         },
88         gotStalkStatus: function(inSender, inEvent) {
89                 if (inEvent.subscribed) {
90                         this.$.stalkLink.hide();
91                         this.$.unstalkLink.show();
92                 } else {
93                         this.$.stalkLink.show();
94                         this.$.unstalkLink.hide();
95                 }
96         },
97         startStalking: function() {
98                 this.$.api.subscribe(this.username);
99         },
100         stopStalking: function() {
101                 this.$.api.unsubscribe(this.username);
102         }
103 });