Add external URL Post method
[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     },
13     components: [
14         {classes: "blerg-header", components: [
15             {name: "title", kind: "blerg.Title"},
16             {name: "controls", kind: "blerg.Controls"},
17             {style: "clear: both"},
18             {name: "post", kind: "blerg.Post", showing: false},
19             {name: "help", kind: "blerg.Help"}
20         ]},
21         {name: "main", kind: "blerg.Main"},
22         {name: "signupDialog", kind: "blerg.SignupDialog"},
23         {name: "passwdDialog", kind: "blerg.PasswdDialog"},
24         {name: "api", kind: "blerg.API",
25          onLoginSuccessful: "loginSuccessful",
26          onLoginFailed: "loginFailed",
27          onLogoutSuccessful: "logout"}
28     ],
29     rendered: function() {
30         this.inherited(arguments);
31
32         this.lastHash = location.hash;
33         this.urlSwitch();
34
35         setInterval(this.hashCheck.bind(this), 250);
36
37         document.body.addEventListener('keyup', function(event) {
38             if (event.shiftKey && event.keyCode == 32) {
39                 this.$.post.show();
40                 event.stopPropagation();
41             }
42         }, false);
43     },
44     hashCheck: function() {
45         if (location.hash != this.lastHash) {
46             this.lastHash = location.hash;
47             this.urlSwitch();
48         }
49     },
50     urlSwitch: function() {
51         var m;
52         var objdef = null;
53
54         for (var i = 0; i < this.pathHandlers.length; i++) {
55             var handler = this.pathHandlers[i];
56             objdef = handler.locationDetect(window.location);
57             if (objdef)
58                 break;
59         }
60         if (!objdef)
61             objdef = {classes: "blerg-error", content: "No handler found"}
62
63         this.$.main.updateView(objdef);
64     },
65     showSignupDialog: function() {
66         this.$.signupDialog.show();
67     },
68     setTitle: function(inSender, inEvent) {
69         this.$.title.waterfall('onSetTitle', inEvent);
70     },
71     tryLogin: function(inSender, inEvent) {
72         this.$.api.login(inEvent.username, inEvent.password);
73     },
74     tryLogout: function(inSender, inEvent) {
75         this.$.api.logout();
76     },
77     loginSuccessful: function(inSender, inEvent) {
78         this.waterfall('onLogin', inEvent);
79     },
80     loginFailed: function(inSender, inEvent) {
81         alert('Login failed');
82         this.logout();
83     },
84     logout: function(inSender, inEvent) {
85         clearInterval(this.feedStatusUpdateInterval);
86         this.waterfall('onLogout');
87     },
88     postVisibilityUpdate: function(inSender, inEvent) {
89         this.$.post.waterfall('onPostVisibility', inEvent);
90         this.$.controls.waterfall('onPostVisibility', inEvent);
91     }
92 });