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