Changelog update for 1.8
[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                 {tag: null, content: "["},
22                 {kind: "Image", src: "/images/rss.png", attributes: {width: 16, height: 16}},
23                 {tag: null, 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.$.loadMoreButton.hide();
72         this.$.api.loadUserRecords(this.username, from, to);
73     },
74     itemsLoaded: function(inSender, inEvent) {
75         this.$.userNotFoundMessage.hide();
76         if (this.permalink) {
77             this.$.loadMoreButton.hide();
78         } else {
79             this.$.loadMoreButton.show();
80         }
81
82         for (var i = 0; i < inEvent.entries.length; i++) {
83             inEvent.entries[i].author = this.username;
84         }
85
86         this.addItems(inEvent.entries);
87         if (this.lastRecord == 0 || inEvent.entries.length == 0)
88             this.$.loadMoreButton.hide();
89     },
90     addItems: function(items) {
91         this.$.emptyAccountMessage.setShowing(items.length == 0);
92         this.inherited(arguments);
93     },
94     userNotFound: function() {
95         this.addItems([]);
96         this.$.emptyAccountMessage.hide();
97         this.$.userNotFoundMessage.show();
98     },
99     apiError: function() {
100         this.addItems([]);
101         alert('Unknown API Error');
102     },
103     getStalkStatus: function() {
104         if (!blerg.API.loggedIn) {
105             this.$.stalkLink.hide();
106             this.$.unstalkLink.hide();
107             return;
108         }
109         this.$.api.getSubscriptionStatus(this.username);
110     },
111     gotStalkStatus: function(inSender, inEvent) {
112         if (inEvent.subscribed) {
113             this.$.stalkLink.hide();
114             this.$.unstalkLink.show();
115         } else {
116             this.$.stalkLink.show();
117             this.$.unstalkLink.hide();
118         }
119     },
120     startStalking: function() {
121         this.$.api.subscribe(this.username);
122     },
123     stopStalking: function() {
124         this.$.api.unsubscribe(this.username);
125     }
126 });