c1b889c1ab198edd8a78d93f727b0cff92776a86
[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             location.hash = '#' + blerg.API.username
50             this.bubble('onNavigate');
51         } else {
52             this.bubble('onReload');
53         }
54     },
55     postFailed: function() {
56         alert('Could not post!');
57     },
58     postVisibilityUpdate: function(inSender, inEvent) {
59         if (inEvent.showing) {
60             this.show();
61             this.$.postContent.focus();
62             if (inEvent.data && this.getData() == "") {
63                 this.setData(inEvent.data);
64                 this.$.postContent.node.setSelectionRange(inEvent.data.length, inEvent.data.length);
65             }
66         } else {
67             this.hide();
68         }
69     },
70     resizePostContent: function(inSender, inEvent) {
71         if (this.resizePostContentTimeout)
72             clearTimeout(this.resizePostContentTimeout);
73         this.resizePostContentTimeout = setTimeout(function() {
74             var n = this.$.postContent.hasNode();
75             if (!n)
76                 return;
77             var c = this.getData();
78             var lines = Math.floor(c.length / (100 * (n.clientWidth / 1000))) + 1;
79             var m = c.match(/\r?\n/g);
80             if (m)
81                 lines += m.length;
82             if (lines <= 3) {
83                 this.$.postContent.setStyle("");
84             } else {
85                 this.$.postContent.setStyle("height: " + (lines * 17) + "pt");
86             }
87             this.resizePostContentTimeout = null;
88         }.bind(this), 150);
89     },
90     loggedIn: function() {
91         this.$.postButton.setDisabled(false);
92         this.$.loginReminder.hide();
93     },
94     loggedOut: function() {
95         this.$.postButton.setDisabled(true);
96         this.$.loginReminder.show();
97     },
98     showHelp: function() {
99         this.$.helpContent.show();
100         this.$.showHelpLink.hide();
101         this.$.hideHelpLink.show();
102         if (this.$.helpContent.getContent() == '') {
103             var req = new enyo.Ajax({
104                 url: baseURL + '/doc/post_help.html',
105                 handleAs: 'text'
106             });
107             req.response(function(inSender, inResponse) {
108                 this.$.helpContent.setContent(inResponse);
109             }.bind(this));
110             req.go();
111         }
112     },
113     hideHelp: function() {
114         this.$.helpContent.hide();
115         this.$.showHelpLink.show();
116         this.$.hideHelpLink.hide();
117     }
118 });