79ab1263ab3baa1f8e0e2db8ee66c5a43a7fde9d
[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         {name: "helpContent", allowHtml: true, showing: false},
13         {style: "position: relative", components: [
14             {tag: "h2", content: "What's on your mind?", style: "width: 75%",},
15             {name: "showHelpLink", kind: "blerg.Link", onNavigate: "showHelp", content: "Help!", style: "position: absolute; right: 1pt; bottom: 1pt; font-weight: bold; font-size: large"},
16             {name: "hideHelpLink", kind: "blerg.Link", onNavigate: "hideHelp", content: "Hide Help", style: "position: absolute; right: 1pt; bottom: 1pt; font-weight: bold; font-size: large", showing: false}
17         ]},
18         {kind: "onyx.InputDecorator", components: [
19             {name: "postContent", classes: "content", kind: "onyx.TextArea", onkeydown: "resizePostContent", attributes: {tabindex: 4}}
20         ]},
21         {classes: "buttons", components: [
22             {name: "loginReminder", tag: "span", classes: "blerg-error", style: "margin-right: 8px; vertical-align: 60%", content: "You must log in before posting."},
23             {kind: "onyx.Button", content: "Close", onclick: "closePost", classes: "onyx-negative", attributes: {tabindex: 6}},
24             {name: "postButton", kind: "onyx.Button", content: "Post", onclick: "doPost", classes: "onyx-affirmative", disabled: true, attributes: {tabindex: 5}}
25         ]},
26         {name: "api", kind: "blerg.API",
27          onPostSuccessful: "postSuccessful",
28          onPostFailed: "postFailed"}
29     ],
30     create: function() {
31         this.inherited(arguments);
32     },
33     getData: function() {
34         return this.$.postContent.getValue();
35     },
36     setData: function(inVal) {
37         this.$.postContent.setValue(inVal);
38     },
39     closePost: function() {
40         this.bubble('onPostVisibility', {showing: false});
41     },
42     doPost: function() {
43         this.$.api.post(this.getData());
44     },
45     postSuccessful: function() {
46         this.setData('');
47         this.closePost();
48         if (location.hash != '#' + blerg.API.username) {
49             qlink(blerg.API.username);
50         } else {
51             this.bubble('onReload');
52         }
53     },
54     postFailed: function() {
55         alert('Could not post!');
56     },
57     postVisibilityUpdate: function(inSender, inEvent) {
58         if (inEvent.showing) {
59             this.show();
60             this.$.postContent.focus();
61             if (inEvent.data && this.getData() == "") {
62                 this.setData(inEvent.data);
63                 this.$.postContent.node.setSelectionRange(inEvent.data.length, inEvent.data.length);
64             }
65         } else {
66             this.hide();
67         }
68     },
69     resizePostContent: function(inSender, inEvent) {
70         if (this.resizePostContentTimeout)
71             clearTimeout(this.resizePostContentTimeout);
72         this.resizePostContentTimeout = setTimeout(function() {
73             var n = this.$.postContent.hasNode();
74             if (!n)
75                 return;
76             var c = this.getData();
77             var lines = Math.floor(c.length / (100 * (n.clientWidth / 1000))) + 1;
78             var m = c.match(/\r?\n/g);
79             if (m)
80                 lines += m.length;
81             if (lines <= 3) {
82                 this.$.postContent.setStyle("");
83             } else {
84                 this.$.postContent.setStyle("height: " + (lines * 17) + "pt");
85             }
86             this.resizePostContentTimeout = null;
87         }.bind(this), 150);
88     },
89     loggedIn: function() {
90         this.$.postButton.setDisabled(false);
91         this.$.loginReminder.hide();
92     },
93     loggedOut: function() {
94         this.$.postButton.setDisabled(true);
95         this.$.loginReminder.show();
96     },
97     showHelp: function() {
98         this.$.helpContent.show();
99         this.$.showHelpLink.hide();
100         this.$.hideHelpLink.show();
101         if (this.$.helpContent.getContent() == '') {
102             var req = new enyo.Ajax({
103                 url: baseURL + '/doc/post_help.html',
104                 handleAs: 'text'
105             });
106             req.response(function(inSender, inResponse) {
107                 this.$.helpContent.setContent(inResponse);
108             }.bind(this));
109             req.go();
110         }
111     },
112     hideHelp: function() {
113         this.$.helpContent.hide();
114         this.$.showHelpLink.show();
115         this.$.hideHelpLink.hide();
116     }
117 });