7871051e23f4a79bb84e90b842b7995ec6aa4d9a
[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         onReload: "usernameChanged"
9     },
10     published: {
11         username: "",
12         permalink: false,
13         record: null,
14     },
15     components: [
16         {classes: "blerg-user-controls", components: [
17             {name: "chatterLink", kind: "blerg.Link", content: "[chatter]"},
18             {name: "stalkLink", kind: "blerg.Link", content: "[stalk]", showing: false, onNavigate: "startStalking"},
19             {name: "unstalkLink", kind: "blerg.Link", content: "[stop stalking]", showing: false, onNavigate: "stopStalking"},
20             {name: "rssLink", kind: "blerg.Link", components: [
21                 {noDom: true, content: "["},
22                 {kind: "Image", src: "/images/rss.png", attributes: {width: 16, height: 16}},
23                 {noDom: true, content: "RSS]"}
24             ]}
25         ]},
26         {name: "records"},
27         {name: "spinner", kind: "OldSchoolSpinner", showing: false},
28         {name: "emptyAccountMessage", showing: false, content: "Hey, there's nothing here!"},
29         {name: "userNotFoundMessage", classes: "blerg-error", showing: false, content: "User not found"},
30         {name: "loadMoreButton", kind: "onyx.Button", onclick: "loadMore", content: "Load More"},
31         {name: "api", kind: "blerg.API",
32          onItemsLoaded: "itemsLoaded",
33          onUserNotFound: "userNotFound",
34          onAPIError: "apiError",
35          onSubscriptionStatus: "gotStalkStatus"}
36     ],
37     statics: {
38         locationDetect: function(l) {
39             var m = l.hash.match(/^#([A-Za-z0-9_-]+)(?:\/(\d+))?$/);
40             if (m) {
41                 return {
42                     kind: "blerg.User",
43                     username: m[1],
44                     permalink: m[2] != undefined,
45                     record: parseInt(m[2])
46                 };
47             }
48         }
49     },
50     create: function() {
51         this.inherited(arguments);
52         this.usernameChanged();
53     },
54     usernameChanged: function() {
55         this.bubble('onSetTitle', {section: '@' + this.username});
56         this.$.records.destroyComponents();
57         this.lastRecord = null;
58         this.$.loadMoreButton.hide();
59         this.$.chatterLink.setHref('/#/ref/' + this.username);
60         this.$.rssLink.setHref('/rss/' + this.username);
61         this.getStalkStatus();
62
63         if (this.permalink) {
64             this.loadItems(this.record, this.record);
65         } else {
66             this.loadMore();
67         }
68     },
69     loadItems: function(from, to) {
70         this.inherited(arguments);
71         this.$.api.loadUserRecords(this.username, from, to);
72     },
73     itemsLoaded: function(inSender, inEvent) {
74         this.$.userNotFoundMessage.hide();
75         if (this.permalink) {
76             this.$.loadMoreButton.hide();
77         } else {
78             this.$.loadMoreButton.show();
79         }
80
81         for (var i = 0; i < inEvent.entries.length; i++) {
82             inEvent.entries[i].author = this.username;
83         }
84
85         this.addItems(inEvent.entries);
86         if (this.lastRecord == 0 || inEvent.entries.length == 0)
87             this.$.loadMoreButton.hide();
88     },
89     addItems: function(items) {
90         this.$.emptyAccountMessage.setShowing(items.length == 0);
91         this.inherited(arguments);
92     },
93     userNotFound: function() {
94         this.addItems([]);
95         this.$.emptyAccountMessage.hide();
96         this.$.userNotFoundMessage.show();
97     },
98     apiError: function() {
99         this.addItems([]);
100         alert('Unknown API Error');
101     },
102     getStalkStatus: function() {
103         if (!blerg.API.loggedIn) {
104             this.$.stalkLink.hide();
105             this.$.unstalkLink.hide();
106             return;
107         }
108         this.$.api.getSubscriptionStatus(this.username);
109     },
110     gotStalkStatus: function(inSender, inEvent) {
111         if (inEvent.subscribed) {
112             this.$.stalkLink.hide();
113             this.$.unstalkLink.show();
114         } else {
115             this.$.stalkLink.show();
116             this.$.unstalkLink.hide();
117         }
118     },
119     startStalking: function() {
120         this.$.api.subscribe(this.username);
121     },
122     stopStalking: function() {
123         this.$.api.unsubscribe(this.username);
124     }
125 });