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