Add post functionality
[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         this.href = '#' + blerg.API.username;
44     },
45     postFailed: function() {
46         alert('Could not post!');
47     },
48     postVisibilityUpdate: function(inSender, inEvent) {
49         if (inEvent.showing) {
50             this.show();
51             this.$.postContent.focus();
52             if (inEvent.data && this.getData() == "") {
53                 this.setData(inEvent.data);
54                 this.$.postContent.node.setSelectionRange(inEvent.data.length, inEvent.data.length);
55             }
56         } else {
57             this.hide();
58         }
59     },
60     resizePostContent: function(inSender, inEvent) {
61         if (this.resizePostContentTimeout)
62             clearTimeout(this.resizePostContentTimeout);
63         this.resizePostContentTimeout = setTimeout(function() {
64             var n = this.$.postContent.hasNode();
65             if (!n)
66                 return;
67             var c = this.getData();
68             var lines = Math.floor(c.length / (100 * (n.clientWidth / 1000))) + 1;
69             var m = c.match(/\r?\n/g);
70             if (m)
71                 lines += m.length;
72             if (lines <= 3) {
73                 this.$.postContent.setStyle("");
74             } else {
75                 this.$.postContent.setStyle("height: " + (lines * 17) + "pt");
76             }
77             this.resizePostContentTimeout = null;
78         }.bind(this), 150);
79     },
80     loggedIn: function() {
81         this.$.postButton.setDisabled(false);
82         this.$.loginReminder.hide();
83     },
84     loggedOut: function() {
85         this.$.postButton.setDisabled(true);
86         this.$.loginReminder.show();
87     }
88 });