52de4e3ce2b16fb6022c06c0c331af99570001d6
[blerg.git] / www / jssrc / blerg / Blerg.js
1 enyo.kind({
2         name: "blerg.Blerg",
3         kind: "Control",
4         lastHash: null,
5         handlers: {
6                 onStartSignup: "showSignupDialog",
7                 onTryLogin: "tryLogin",
8                 onTryLogout: "tryLogout",
9                 onSetTitle: "setTitle",
10                 onPostVisibility: "postVisibilityUpdate"
11         },
12         components: [
13                 {classes: "blerg-header", components: [
14                         {name: "title", kind: "blerg.Title"},
15                         {name: "controls", kind: "blerg.Controls"},
16                         {style: "clear: both"},
17                         {name: "post", kind: "blerg.Post", showing: false},
18                         {name: "help", kind: "blerg.Help"}
19                 ]},
20                 {name: "main", kind: "blerg.Main"},
21                 {name: "signupDialog", kind: "blerg.SignupDialog"},
22                 {name: "passwdDialog", kind: "blerg.PasswdDialog"},
23                 {name: "api", kind: "blerg.API",
24                  onLoginSuccessful: "loginSuccessful",
25                  onLoginFailed: "loginFailed",
26                  onLogoutSuccessful: "logout"}
27         ],
28         urlmap: [
29                 ['search', /^\?post\/([^/]+)\/(.+)/, "blerg.ExternalURLPost"],
30                 ['hash',   /^#\/(ref|tag)\/([A-Za-z0-9_-]+)(?:\/p(\d+))?$/, "blerg.Tag"],
31                 ['hash',   /^#\/feed(?:\/p(\d+))?$/, "blerg.Feed"],
32                 ['hash',   /^#([A-Za-z0-9_-]+)(?:\/(p)?(\d+))?$/, "blerg.User"]
33         ],
34         pathHandlers: [ blerg.User, blerg.Welcome ],
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.$.post.show();
46                                 event.stopPropagation();
47                         }
48                 }, 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.$.api.requestFeedStatus();
85                 this.feedStatusUpdateInterval = setInterval(function() {
86                         this.$.api.requestFeedStatus();
87                 }.bind(this), 900000);
88                 this.waterfall('onLogin', inEvent);
89         },
90         loginFailed: function(inSender, inEvent) {
91                 alert('Login failed');
92                 this.logout();
93         },
94         logout: function(inSender, inEvent) {
95                 clearInterval(this.feedStatusUpdateInterval);
96                 this.waterfall('onLogout');
97         },
98         postVisibilityUpdate: function(inSender, inEvent) {
99                 this.$.post.waterfall('onPostVisibility', inEvent);
100                 this.$.controls.waterfall('onPostVisibility', inEvent);
101         }
102 });