Add new account center and account recovery frontends
[blerg.git] / www / jssrc / blerg / Controls.js
1 enyo.kind({
2     name: "blerg.Controls",
3     kind: "Control",
4     classes: "blerg-controls",
5     username: null,
6     postShowing: false,
7     published: {
8         loggedIn: false
9     },
10     handlers: {
11         onLogin: "login",
12         onLogout: "logout",
13         onPostVisibility: "postVisibilityUpdate"
14     },
15     components: [
16         {name: "loggedOutControls", components: [
17             {tag: "form", onkeyup: "loginKeyUp", classes: "login", components: [
18                 {kind: "onyx.Groupbox", components: [
19                     {kind: "onyx.InputDecorator", components: [
20                         {name: "username", kind: "onyx.Input", placeholder: "Username", attributes: {tabindex: 1}}
21                     ]},
22                     {kind: "onyx.InputDecorator", components: [
23                         {name: "password", kind: "onyx.Input", placeholder: "Password", type: "password", attributes: {tabindex: 2}}
24                     ]},
25                 ]},
26                 {kind: "onyx.Button", content: "Login", onclick: "loginClicked", attributes: {tabindex: 3}}
27             ]}
28         ]},
29         {name: "loggedInControls", showing: false, components: [
30             {name: "greeting", classes: "blerg-controls-greeting", components: [
31                 {tag: null, content: "Hello, "},
32                 {name: "userlink", tag: "a"},
33                 {tag: null, content: ". "},
34                 {kind: "blerg.Link", content: "Logout", onNavigate: "logoutClicked"},
35                 {tag: null, content: "."},
36                 {tag: "br"},
37                 {kind: "blerg.Link", content: "Account Center", href: '/#/account'},
38                 {tag: null, content: "."}
39             ]},
40             {classes: "blerg-controls-toolbar", components: [
41                 {name: "mentionButton", kind: "onyx.Button", content: "Feed Your Vanity", onclick: "chatterClicked"},
42                 {name: "feedButton", kind: "onyx.Button", classes: "feed-button", content: "Stalk Your Victims", onclick: "feedClicked"},
43                 {name: "spewButton", kind: "onyx.Button", classes: "spew-button", content: "Spew It!", onclick: "spewToggle"}
44             ]},
45         ]},
46         {name: "api", kind: "blerg.API",
47          onStatus: "gotStatus"},
48         {kind: "Signals", 
49          onClearNotification: "clearNotification"}
50     ],
51     showRSS: function(url) {
52         this.$.rssButton.show();
53         if (url)
54             this.$.rssButton.setAttribute('href', url);
55     },
56     hideRSS: function() {
57         this.$.rssButton.hide();
58     },
59     loggedInChanged: function() {
60         if (this.loggedIn) {
61             this.$.loggedOutControls.hide();
62             this.$.loggedInControls.show();
63         } else {
64             this.$.loggedOutControls.show();
65             this.$.loggedInControls.hide();
66         }
67     },
68     loginClicked: function(inSender, inEvent) {
69         this.bubble('onTryLogin', {
70             username: this.$.username.getValue(),
71             password: this.$.password.getValue()
72         });
73         inEvent.preventDefault();
74         return true;
75     },
76     loginKeyUp: function(inSender, inEvent) {
77         if (inEvent.keyCode == 13) {
78             if (this.$.username.hasFocus()) {
79                 this.$.password.focus();
80             } else {
81                 this.loginClicked(this, inEvent);
82             }
83             return true;
84         }
85     },
86     logoutClicked: function() {
87         this.bubble('onTryLogout');
88         return true;
89     },
90     login: function(inSender, inEvent) {
91         this.$.password.setValue('');
92         // TODO: Replace with regular blur() call in future enyo
93         this.$.password.node.blur();
94         this.setLoggedIn(true);
95         this.$.userlink.setAttribute('href', '/#' + inEvent.username);
96         this.$.userlink.setContent('@' + inEvent.username);
97         this.username = inEvent.username;
98
99         this.updateStatus();
100         this.feedStatusUpdateInterval = setInterval(function() {
101             this.updateStatus();
102         }.bind(this), 900000);
103     },
104     logout: function(inSender, inEvent) {
105         this.setLoggedIn(false);
106         clearInterval(this.feedStatusUpdateInterval);
107     },
108     changePasswordClicked: function() {
109         this.bubble('onShowChangePassword');
110     },
111     spewToggle: function(inSender, inEvent) {
112         this.postShowing = !this.postShowing;
113         this.bubble('onPostVisibility', {showing: this.postShowing});
114     },
115     postVisibilityUpdate: function(inSender, inEvent) {
116         this.postShowing = inEvent.showing;
117         this.$.spewButton.addRemoveClass('active', inEvent.showing);
118     },
119     updateStatus: function() {
120         this.$.api.getStatus();
121     },
122     gotStatus: function(inSender, inEvent) {
123         if ('mentioned' in inEvent) {
124             this.$.mentionButton.addRemoveClass('new', inEvent.mentioned);
125         }
126         if ('feed_new' in inEvent) {
127             this.$.feedButton.addRemoveClass('new', inEvent.feed_new > 0);
128             if (inEvent.feed_new > 0) {
129                 this.$.feedButton.setContent('Stalk Your Victims (' + inEvent.feed_new + ')');
130             } else {
131                 this.$.feedButton.setContent('Stalk Your Victims');
132             }
133         }
134     },
135     chatterClicked: function() {
136         window.location.href = '/#/ref/' + this.username;
137         this.bubble('onNavigate');
138     },
139     feedClicked: function() {
140         window.location.href = '/#/feed';
141         this.bubble('onNavigate');
142     },
143     clearNotification: function(inSender, inEvent) {
144         if (inEvent.type == 'feed') {
145             this.gotStatus(this, {feed_new: 0});
146         } else if (inEvent.type == 'mentioned') {
147             this.gotStatus(this, {mentioned: false});
148         }
149     }
150 });