Reload user when posting
[blerg.git] / www / jssrc / blerg / Post.js
1 enyo.kind({
2     name: "blerg.Post",
3     kind: "Control",
4     classes: "blerg-post",
5     handlers: {
6         onPostVisibility: "postVisibilityUpdate",
7         onLogin: "loggedIn",
8         onLogout: "loggedOut"
9     },
10     resizePostContentTimeout: null,
11     components: [
12         {tag: "h2", content: "What's on your mind?"},
13         {kind: "onyx.InputDecorator", components: [
14             {name: "postContent", classes: "content", kind: "onyx.TextArea", onkeydown: "resizePostContent", attributes: {tabindex: 4}}
15         ]},
16         {classes: "buttons", components: [
17             {name: "loginReminder", tag: "span", classes: "blerg-error", style: "margin-right: 8px; vertical-align: 60%", content: "You must log in before posting."},
18             {kind: "onyx.Button", content: "Close", onclick: "closePost", classes: "onyx-negative", attributes: {tabindex: 6}},
19             {name: "postButton", kind: "onyx.Button", content: "Post", onclick: "doPost", classes: "onyx-affirmative", disabled: true, attributes: {tabindex: 5}}
20         ]},
21         {name: "api", kind: "blerg.API",
22          onPostSuccessful: "postSuccessful",
23          onPostFailed: "postFailed"}
24     ],
25     create: function() {
26         this.inherited(arguments);
27     },
28     getData: function() {
29         return this.$.postContent.getValue();
30     },
31     setData: function(inVal) {
32         this.$.postContent.setValue(inVal);
33     },
34     closePost: function() {
35         this.bubble('onPostVisibility', {showing: false});
36     },
37     doPost: function() {
38         this.$.api.post(this.getData());
39     },
40     postSuccessful: function() {
41         this.setData('');
42         this.closePost();
43         if (location.hash != '#' + blerg.API.username) {
44             qlink(blerg.API.username);
45         } else {
46             this.bubble('onReload');
47         }
48     },
49     postFailed: function() {
50         alert('Could not post!');
51     },
52     postVisibilityUpdate: function(inSender, inEvent) {
53         if (inEvent.showing) {
54             this.show();
55             this.$.postContent.focus();
56             if (inEvent.data && this.getData() == "") {
57                 this.setData(inEvent.data);
58                 this.$.postContent.node.setSelectionRange(inEvent.data.length, inEvent.data.length);
59             }
60         } else {
61             this.hide();
62         }
63     },
64     resizePostContent: function(inSender, inEvent) {
65         if (this.resizePostContentTimeout)
66             clearTimeout(this.resizePostContentTimeout);
67         this.resizePostContentTimeout = setTimeout(function() {
68             var n = this.$.postContent.hasNode();
69             if (!n)
70                 return;
71             var c = this.getData();
72             var lines = Math.floor(c.length / (100 * (n.clientWidth / 1000))) + 1;
73             var m = c.match(/\r?\n/g);
74             if (m)
75                 lines += m.length;
76             if (lines <= 3) {
77                 this.$.postContent.setStyle("");
78             } else {
79                 this.$.postContent.setStyle("height: " + (lines * 17) + "pt");
80             }
81             this.resizePostContentTimeout = null;
82         }.bind(this), 150);
83     },
84     loggedIn: function() {
85         this.$.postButton.setDisabled(false);
86         this.$.loginReminder.hide();
87     },
88     loggedOut: function() {
89         this.$.postButton.setDisabled(true);
90         this.$.loginReminder.show();
91     }
92 });