1f7ea7da4bb10e448031721dbfa9a2d2f2b83934
[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     },
8     resizePostContentTimeout: null,
9     components: [
10         {tag: "h2", content: "What's on your mind?"},
11         {kind: "onyx.InputDecorator", components: [
12             {name: "postContent", classes: "content", kind: "onyx.TextArea", onkeydown: "resizePostContent"}
13         ]},
14         {classes: "buttons", components: [
15             {kind: "onyx.Button", content: "Close", onclick: "closePost", classes: "onyx-negative"},
16             {kind: "onyx.Button", content: "Post", onclick: "doPost", classes: "onyx-affirmative"}
17         ]}
18     ],
19     getData: function() {
20         return this.$.postContent.getValue();
21     },
22     setData: function(inVal) {
23         this.$.postContent.setValue(inVal);
24     },
25     closePost: function() {
26         this.bubble('onPostVisibility', {showing: false});
27     },
28     doPost: function() {
29         this.bubble('onPost', {data: this.getData()});
30     },
31     postVisibilityUpdate: function(inSender, inEvent) {
32         if (inEvent.showing) {
33             this.show();
34             if (inEvent.data && this.getData() == "")
35                 this.setData(inEvent.data);
36         } else {
37             this.hide();
38         }
39     },
40     resizePostContent: function(inSender, inEvent) {
41         if (this.resizePostContentTimeout)
42             clearTimeout(this.resizePostContentTimeout);
43         this.resizePostContentTimeout = setTimeout(function() {
44             var n = this.$.postContent.hasNode();
45             if (!n)
46                 return;
47             var c = this.getData();
48             var lines = Math.floor(c.length / (100 * (n.clientWidth / 1000))) + 1;
49             var m = c.match(/\r?\n/g);
50             if (m)
51                 lines += m.length;
52             if (lines <= 3) {
53                 this.$.postContent.setStyle("");
54             } else {
55                 this.$.postContent.setStyle("height: " + (lines * 17) + "pt");
56             }
57             this.resizePostContentTimeout = null;
58         }.bind(this), 150);
59     }
60 });