Add password change functionality
[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", onsubmit: "loginClicked", 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", 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: "Change Password", onNavigate: "changePasswordClicked"},
38                 {tag: null, content: "."}
39             ]},
40             {classes: "blerg-controls-toolbar", components: [
41                 {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          onFeedInfo: "gotFeedInfo"}
48     ],
49     showRSS: function(url) {
50         this.$.rssButton.show();
51         if (url)
52             this.$.rssButton.setAttribute('href', url);
53     },
54     hideRSS: function() {
55         this.$.rssButton.hide();
56     },
57     loggedInChanged: function() {
58         if (this.loggedIn) {
59             this.$.loggedOutControls.hide();
60             this.$.loggedInControls.show();
61         } else {
62             this.$.loggedOutControls.show();
63             this.$.loggedInControls.hide();
64         }
65     },
66     loginClicked: function(inSender, inEvent) {
67         this.bubble('onTryLogin', {
68             username: this.$.username.getValue(),
69             password: this.$.password.getValue()
70         });
71         inEvent.preventDefault();
72         return true;
73     },
74     logoutClicked: function() {
75         this.bubble('onTryLogout');
76         return true;
77     },
78     login: function(inSender, inEvent) {
79         this.$.password.setValue('');
80         this.setLoggedIn(true);
81         this.$.userlink.setAttribute('href', '/#' + inEvent.username);
82         this.$.userlink.setContent('@' + inEvent.username);
83         this.username = inEvent.username;
84
85         this.$.api.getFeedInfo();
86         this.feedStatusUpdateInterval = setInterval(function() {
87             this.$.api.getFeedInfo();
88         }.bind(this), 900000);
89     },
90     logout: function(inSender, inEvent) {
91         this.setLoggedIn(false);
92         clearInterval(this.feedStatusUpdateInterval);
93     },
94     changePasswordClicked: function() {
95         this.bubble('onShowChangePassword');
96     },
97     spewToggle: function(inSender, inEvent) {
98         this.postShowing = !this.postShowing;
99         this.bubble('onPostVisibility', {showing: this.postShowing});
100     },
101     postVisibilityUpdate: function(inSender, inEvent) {
102         this.postShowing = inEvent.showing;
103         this.$.spewButton.addRemoveClass('active', inEvent.showing);
104     },
105     gotFeedInfo: function(inSender, inEvent) {
106         this.$.feedButton.addRemoveClass('new', inEvent.new > 0);
107         if (inEvent.new > 0) {
108             this.$.feedButton.setContent('Stalk Your Victims (' + inEvent.new + ')');
109         } else {
110             this.$.feedButton.setContent('Stalk Your Victims');
111         }
112     },
113     chatterClicked: function() {
114         window.location.href = '/#/ref/' + this.username;
115         this.bubble('onNavigate');
116     },
117     feedClicked: function() {
118         window.location.href = '/#/feed';
119         this.bubble('onNavigate');
120     }
121 });