80037c4e3c502916968ff700f3d5c092f1817fc1
[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 ],
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: "passwdDialog", kind: "blerg.PasswdDialog"},
30         {name: "api", kind: "blerg.API",
31          onLoginSuccessful: "loginSuccessful",
32          onLoginFailed: "loginFailed",
33          onLogoutSuccessful: "logout"}
34     ],
35     rendered: function() {
36         this.inherited(arguments);
37
38         this.lastHash = location.hash;
39         this.urlSwitch();
40
41         setInterval(this.hashCheck.bind(this), 250);
42
43         document.body.addEventListener('keyup', function(event) {
44             if (event.shiftKey && event.keyCode == 32) {
45                 this.waterfall('onPostVisibility', {showing: true});
46                 event.stopPropagation();
47             }
48         }.bind(this), false);
49     },
50     hashCheck: function() {
51         if (location.hash != this.lastHash) {
52             this.lastHash = location.hash;
53             this.urlSwitch();
54         }
55     },
56     urlSwitch: function() {
57         var m;
58         var objdef = null;
59
60         for (var i = 0; i < this.pathHandlers.length; i++) {
61             var handler = this.pathHandlers[i];
62             objdef = handler.locationDetect(window.location);
63             if (objdef)
64                 break;
65         }
66         if (!objdef)
67             objdef = {classes: "blerg-error", content: "No handler found"}
68
69         this.$.main.updateView(objdef);
70     },
71     showSignupDialog: function() {
72         this.$.signupDialog.show();
73     },
74     setTitle: function(inSender, inEvent) {
75         this.$.title.waterfall('onSetTitle', inEvent);
76     },
77     tryLogin: function(inSender, inEvent) {
78         this.$.api.login(inEvent.username, inEvent.password);
79     },
80     tryLogout: function(inSender, inEvent) {
81         this.$.api.logout();
82     },
83     loginSuccessful: function(inSender, inEvent) {
84         this.waterfall('onLogin', inEvent);
85     },
86     loginFailed: function(inSender, inEvent) {
87         alert('Login failed');
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     showChangePassword: function() {
102         this.$.passwdDialog.show();
103     },
104     authFailure: function(inSender, inEvent) {
105         this.logout();
106     }
107 });