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