Make Account Center only usable when logged in
[blerg.git] / www / jssrc / blerg / Blerg.js
1 // Listen for onsubmit events
2 enyo.dispatcher.listen(document, "submit");
3
4 enyo.kind({
5     name: "blerg.Blerg",
6     kind: "Control",
7     lastHash: null,
8     pathHandlers: [ blerg.User, blerg.Tag, blerg.Feed, blerg.ExternalURLPost, blerg.Welcome, blerg.AccountCenter, blerg.Recovery, blerg.EmailVerify ],
9     handlers: {
10         onStartSignup: "showSignupDialog",
11         onTryLogin: "tryLogin",
12         onTryLogout: "tryLogout",
13         onSetTitle: "setTitle",
14         onPostVisibility: "postVisibilityUpdate",
15         onReload: "sendReload",
16         onShowChangePassword: "showChangePassword",
17         onAuthFailure: "authFailure"
18     },
19     components: [
20         {classes: "blerg-header", components: [
21             {name: "title", kind: "blerg.Title"},
22             {name: "controls", kind: "blerg.Controls"},
23             {style: "clear: both;"},
24             {name: "post", kind: "blerg.Post", showing: false},
25             {name: "help", kind: "blerg.Help"}
26         ]},
27         {name: "main", kind: "blerg.Main"},
28         {name: "signupDialog", kind: "blerg.SignupDialog"},
29         {name: "api", kind: "blerg.API",
30          onLoginSuccessful: "loginSuccessful",
31          onLoginFailed: "loginFailed",
32          onLogoutSuccessful: "logout"}
33     ],
34     rendered: function() {
35         this.inherited(arguments);
36
37         this.lastHash = location.hash;
38         this.urlSwitch();
39
40         setInterval(this.hashCheck.bind(this), 250);
41
42         document.body.addEventListener('keyup', function(event) {
43             if (event.shiftKey && event.keyCode == 32) {
44                 this.waterfall('onPostVisibility', {showing: true});
45                 event.stopPropagation();
46             }
47         }.bind(this), false);
48     },
49     hashCheck: function() {
50         if (location.hash != this.lastHash) {
51             this.lastHash = location.hash;
52             this.urlSwitch();
53         }
54     },
55     urlSwitch: function() {
56         var m;
57         var objdef = null;
58
59         for (var i = 0; i < this.pathHandlers.length; i++) {
60             var handler = this.pathHandlers[i];
61             objdef = handler.locationDetect(window.location);
62             if (objdef)
63                 break;
64         }
65         if (!objdef)
66             objdef = {classes: "blerg-error", content: "No handler found"}
67
68         this.$.main.updateView(objdef);
69     },
70     showSignupDialog: function() {
71         this.$.signupDialog.show();
72     },
73     setTitle: function(inSender, inEvent) {
74         this.$.title.waterfall('onSetTitle', inEvent);
75     },
76     tryLogin: function(inSender, inEvent) {
77         this.$.api.login(inEvent.username, inEvent.password);
78     },
79     tryLogout: function(inSender, inEvent) {
80         this.$.api.logout();
81     },
82     loginSuccessful: function(inSender, inEvent) {
83         this.waterfall('onLogin', inEvent);
84     },
85     loginFailed: function(inSender, inEvent) {
86         alert('Login failed');
87         this.waterfall('onShowForgotPasswordLink');
88         this.logout();
89     },
90     logout: function(inSender, inEvent) {
91         clearInterval(this.feedStatusUpdateInterval);
92         this.waterfall('onLogout');
93     },
94     postVisibilityUpdate: function(inSender, inEvent) {
95         this.$.post.waterfall('onPostVisibility', inEvent);
96         this.$.controls.waterfall('onPostVisibility', inEvent);
97     },
98     sendReload: function() {
99         this.$.main.waterfall('onReload');
100     },
101     authFailure: function(inSender, inEvent) {
102         this.logout();
103     }
104 });