21d4a5995e0fd64f6f6ae69278d1c6505a2aa42b
[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         onClearFeedStatus: "clearFeedStatus"
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             ]}
29         ]},
30         {name: "loggedInControls", showing: false, components: [
31             {name: "greeting", classes: "blerg-controls-greeting", components: [
32                 {tag: null, content: "Hello, "},
33                 {name: "userlink", tag: "a"},
34                 {tag: null, content: ". "},
35                 {kind: "blerg.Link", content: "Logout", onNavigate: "logoutClicked"},
36                 {tag: null, content: "."},
37                 {tag: "br"},
38                 {kind: "blerg.Link", content: "Change Password", onNavigate: "changePasswordClicked"},
39                 {tag: null, content: "."}
40             ]},
41             {classes: "blerg-controls-toolbar", components: [
42                 {kind: "onyx.Button", content: "Feed Your Vanity", onclick: "chatterClicked"},
43                 {name: "feedButton", kind: "onyx.Button", classes: "feed-button", content: "Stalk Your Victims", onclick: "feedClicked"},
44                 {name: "spewButton", kind: "onyx.Button", classes: "spew-button", content: "Spew It!", onclick: "spewToggle"}
45             ]},
46         ]},
47         {name: "api", kind: "blerg.API",
48          onFeedInfo: "gotFeedInfo"}
49     ],
50     showRSS: function(url) {
51         this.$.rssButton.show();
52         if (url)
53             this.$.rssButton.setAttribute('href', url);
54     },
55     hideRSS: function() {
56         this.$.rssButton.hide();
57     },
58     loggedInChanged: function() {
59         if (this.loggedIn) {
60             this.$.loggedOutControls.hide();
61             this.$.loggedInControls.show();
62         } else {
63             this.$.loggedOutControls.show();
64             this.$.loggedInControls.hide();
65         }
66     },
67     loginClicked: function(inSender, inEvent) {
68         this.bubble('onTryLogin', {
69             username: this.$.username.getValue(),
70             password: this.$.password.getValue()
71         });
72         inEvent.preventDefault();
73         return true;
74     },
75     loginKeyUp: function(inSender, inEvent) {
76         if (inEvent.keyCode == 13) {
77             if (this.$.username.hasFocus()) {
78                 this.$.password.focus();
79             } else {
80                 this.loginClicked(this, inEvent);
81             }
82             return true;
83         }
84     },
85     logoutClicked: function() {
86         this.bubble('onTryLogout');
87         return true;
88     },
89     login: function(inSender, inEvent) {
90         this.$.password.setValue('');
91         this.setLoggedIn(true);
92         this.$.userlink.setAttribute('href', '/#' + inEvent.username);
93         this.$.userlink.setContent('@' + inEvent.username);
94         this.username = inEvent.username;
95
96         this.updateFeedInfo();
97         this.feedStatusUpdateInterval = setInterval(function() {
98             this.updateFeedInfo();
99         }.bind(this), 900000);
100     },
101     logout: function(inSender, inEvent) {
102         this.setLoggedIn(false);
103         clearInterval(this.feedStatusUpdateInterval);
104     },
105     changePasswordClicked: function() {
106         this.bubble('onShowChangePassword');
107     },
108     spewToggle: function(inSender, inEvent) {
109         this.postShowing = !this.postShowing;
110         this.bubble('onPostVisibility', {showing: this.postShowing});
111     },
112     postVisibilityUpdate: function(inSender, inEvent) {
113         this.postShowing = inEvent.showing;
114         this.$.spewButton.addRemoveClass('active', inEvent.showing);
115     },
116     updateFeedInfo: function() {
117         this.$.api.getFeedInfo();
118     },
119     gotFeedInfo: function(inSender, inEvent) {
120         this.$.feedButton.addRemoveClass('new', inEvent.new > 0);
121         if (inEvent.new > 0) {
122             this.$.feedButton.setContent('Stalk Your Victims (' + inEvent.new + ')');
123         } else {
124             this.$.feedButton.setContent('Stalk Your Victims');
125         }
126     },
127     chatterClicked: function() {
128         window.location.href = '/#/ref/' + this.username;
129         this.bubble('onNavigate');
130     },
131     feedClicked: function() {
132         window.location.href = '/#/feed';
133         this.bubble('onNavigate');
134     },
135     clearFeedStatus: function() {
136         this.gotFeedInfo(this, {new: 0});
137     }
138 });