Add Tag handler. Also convert all tabs to spaces.
[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         if (this.lastRecord == 0)
80             this.$.loadMoreButton.hide();
81     },
82     getStalkStatus: function() {
83         if (!blerg.API.loggedIn) {
84             this.$.stalkLink.hide();
85             this.$.unstalkLink.hide();
86             return;
87         }
88         this.$.api.getSubscriptionStatus(this.username);
89     },
90     gotStalkStatus: function(inSender, inEvent) {
91         if (inEvent.subscribed) {
92             this.$.stalkLink.hide();
93             this.$.unstalkLink.show();
94         } else {
95             this.$.stalkLink.show();
96             this.$.unstalkLink.hide();
97         }
98     },
99     startStalking: function() {
100         this.$.api.subscribe(this.username);
101     },
102     stopStalking: function() {
103         this.$.api.unsubscribe(this.username);
104     }
105 });