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