2a571a82a23c252e1d3d6c702595f060d00304b8
[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         },
11         components: [
12                 {classes: "blerg-header", components: [
13                         {name: "title", kind: "blerg.Title"},
14                         {name: "controls", kind: "blerg.Controls"},
15                         {style: "clear: both"},
16                         {name: "post", kind: "blerg.Post", showing: false},
17                         {name: "help", kind: "blerg.Help"}
18                 ]},
19                 {name: "main", kind: "blerg.Main"},
20                 {name: "signupDialog", kind: "blerg.SignupDialog"},
21                 {name: "passwdDialog", kind: "blerg.PasswdDialog"},
22                 {name: "api", kind: "blerg.API",
23                  onLoginSuccessful: "loginSuccessful",
24                  onLoginFailed: "loginFailed",
25                  onLogoutSuccessful: "logout"}
26         ],
27         urlmap: [
28                 ['search', /^\?post\/([^/]+)\/(.+)/, "blerg.ExternalURLPost"],
29                 ['hash',   /^#\/(ref|tag)\/([A-Za-z0-9_-]+)(?:\/p(\d+))?$/, "blerg.Tag"],
30                 ['hash',   /^#\/feed(?:\/p(\d+))?$/, "blerg.Feed"],
31                 ['hash',   /^#([A-Za-z0-9_-]+)(?:\/(p)?(\d+))?$/, "blerg.User"]
32         ],
33         pathHandlers: [ blerg.Welcome ],
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.$.post.show();
45                                 event.stopPropagation();
46                         }
47                 }, 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                 if (inEvent.section != undefined)
75                         this.$.title.setSection(inEvent.section);
76
77                 if (inEvent.subscribed != undefined)
78                         this.$.title.setSubscribed(inEvent.subscribed);
79
80                 if (inEvent.showControls)
81                         this.$.title.showControls()
82                 else
83                         this.$.title.hideControls();
84         },
85         tryLogin: function(inSender, inEvent) {
86                 this.$.api.login(inEvent.username, inEvent.password);
87         },
88         tryLogout: function(inSender, inEvent) {
89                 this.$.api.logout();
90         },
91         loginSuccessful: function(inSender, inEvent) {
92                 this.$.api.requestFeedStatus();
93                 this.feedStatusUpdateInterval = setInterval(function() {
94                         this.$.api.requestFeedStatus();
95                 }.bind(this), 900000);
96                 this.waterfall('onLogin', inEvent);
97         },
98         loginFailed: function(inSender, inEvent) {
99                 alert('Login failed');
100                 this.logout();
101         },
102         logout: function(inSender, inEvent) {
103                 clearInterval(this.feedStatusUpdateInterval);
104                 this.waterfall('onLogout');
105         }
106 });