Fix some of the niceties in Post
[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     ],
22     create: function() {
23         this.inherited(arguments);
24     },
25     getData: function() {
26         return this.$.postContent.getValue();
27     },
28     setData: function(inVal) {
29         this.$.postContent.setValue(inVal);
30     },
31     closePost: function() {
32         this.bubble('onPostVisibility', {showing: false});
33     },
34     doPost: function() {
35         this.bubble('onPost', {data: this.getData()});
36     },
37     postVisibilityUpdate: function(inSender, inEvent) {
38         if (inEvent.showing) {
39             this.show();
40             this.$.postContent.focus();
41             if (inEvent.data && this.getData() == "") {
42                 this.setData(inEvent.data);
43                 this.$.postContent.node.setSelectionRange(inEvent.data.length, inEvent.data.length);
44             }
45         } else {
46             this.hide();
47         }
48     },
49     resizePostContent: function(inSender, inEvent) {
50         if (this.resizePostContentTimeout)
51             clearTimeout(this.resizePostContentTimeout);
52         this.resizePostContentTimeout = setTimeout(function() {
53             var n = this.$.postContent.hasNode();
54             if (!n)
55                 return;
56             var c = this.getData();
57             var lines = Math.floor(c.length / (100 * (n.clientWidth / 1000))) + 1;
58             var m = c.match(/\r?\n/g);
59             if (m)
60                 lines += m.length;
61             if (lines <= 3) {
62                 this.$.postContent.setStyle("");
63             } else {
64                 this.$.postContent.setStyle("height: " + (lines * 17) + "pt");
65             }
66             this.resizePostContentTimeout = null;
67         }.bind(this), 150);
68     },
69     loggedIn: function() {
70         this.$.postButton.setDisabled(false);
71         this.$.loginReminder.hide();
72     },
73     loggedOut: function() {
74         this.$.postButton.setDisabled(true);
75         this.$.loginReminder.show();
76     }
77 });