064db0057febeae8e9aa5f1e80b91af2f53ace5b
[blerg.git] / www / jssrc / blerg / Post.js
1 enyo.kind({
2     name: "blerg.Post",
3     kind: "FittableRows",
4     classes: "blerg-post",
5     handlers: {
6         onPostVisibility: "postVisibilityUpdate",
7         onLogin: "loggedIn",
8         onLogout: "loggedOut"
9     },
10     components: [
11         {tag: "h2", content: "What's on your mind?"},
12         {kind: "onyx.InputDecorator", alwaysLooksFocused: true, classes: "content-decorator", fit: true, components: [
13             {name: "postContent", kind: "onyx.TextArea", classes: "content", onkeyup: "updatePreview", attributes: {tabindex: 4}}
14         ]},
15         {name: "toolbar", kind: "FittableColumns", classes: "toolbar", components: [
16             {classes: "switcher", components: [
17                 {kind: "onyx.RadioGroup", onActivate: "switchBottomPanel", components: [
18                     {name: "replyButton", content: "Replying to", showing: false},
19                     {name: "previewButton", content: "Preview"},
20                     {content: "Help"}
21                 ]}
22             ]},
23             {classes: "buttons", fit: true, components: [
24                 {name: "loginReminder", tag: "span", classes: "blerg-error", style: "margin-right: 8px", content: "You must log in before posting."},
25                 {kind: "onyx.Button", content: "Close", onclick: "closePost", classes: "onyx-negative", attributes: {tabindex: 6}},
26                 {name: "postButton", kind: "onyx.Button", content: "Post", onclick: "doPost", classes: "onyx-affirmative", disabled: true, attributes: {tabindex: 5}}
27             ]},
28         ]},
29         {name: "bottomPanel", kind: "Panels", classes: "bottom-panel", draggable: false, fit: true, components: [
30             {kind: "Scroller", components: [
31                 {name: "replyView", classes: "record", allowHtml: true},
32             ]},
33             {kind: "Scroller", components: [
34                 {name: "preview", classes: "record", allowHtml: true},
35             ]},
36             {kind: "Scroller", components: [
37                 {name: "helpContent", allowHtml: true},
38             ]}
39         ]},
40         {name: "api", kind: "blerg.API",
41          onPostSuccessful: "postSuccessful",
42          onPostFailed: "postFailed"}
43     ],
44     create: function() {
45         this.inherited(arguments);
46     },
47     getData: function() {
48         return this.$.postContent.getValue();
49     },
50     setData: function(inVal) {
51         this.$.postContent.setValue(inVal);
52     },
53     closePost: function() {
54         this.bubble('onPostVisibility', {showing: false});
55     },
56     doPost: function() {
57         this.$.api.post(this.getData());
58     },
59     postSuccessful: function() {
60         this.setData('');
61         this.closePost();
62         if (location.hash != '#' + blerg.API.username) {
63             location.hash = '#' + blerg.API.username
64             this.bubble('onNavigate');
65         } else {
66             this.bubble('onReload');
67         }
68     },
69     postFailed: function() {
70         alert('Could not post!');
71     },
72     postVisibilityUpdate: function(inSender, inEvent) {
73         if (inEvent.showing) {
74             this.show();
75             this.$.postContent.focus();
76
77             if (inEvent.data && this.getData() == "") {
78                 this.setData(inEvent.data);
79                 this.$.postContent.node.setSelectionRange(inEvent.data.length, inEvent.data.length);
80                 this.updatePreview();
81             }
82
83             if (inEvent.replyto) {
84                 this.$.replyButton.show();
85                 this.$.replyButton.setActive(true);
86                 this.$.replyView.setContent(inEvent.replyto);
87             } else {
88                 this.$.replyButton.hide();
89                 this.$.previewButton.setActive(true);
90             }
91
92             // Need to reflow manually because this is hidden by default
93             this.$.toolbar.reflow();
94             this.reflow();
95         } else {
96             this.hide();
97         }
98     },
99     updatePreview: function(inSender, inEvent) {
100         this.$.preview.setContent(blerg.Util.blergFormat(this.getData()));
101     },
102     loggedIn: function() {
103         this.$.postButton.setDisabled(false);
104         this.$.loginReminder.hide();
105     },
106     loggedOut: function() {
107         this.$.postButton.setDisabled(true);
108         this.$.loginReminder.show();
109     },
110     loadHelp: function() {
111         if (this.$.helpContent.getContent() == '') {
112             var req = new enyo.Ajax({
113                 url: baseURL + '/doc/post_help.html',
114                 handleAs: 'text'
115             });
116             req.response(function(inSender, inResponse) {
117                 this.$.helpContent.setContent(inResponse);
118             }.bind(this));
119             req.go();
120         }
121     },
122     switchBottomPanel: function(inSender, inEvent) {
123         var active = inSender.getActive();
124         var index = inSender.children.indexOf(active);
125         this.$.bottomPanel.setIndex(index);
126         if (index == 2) {
127             this.loadHelp();
128         }
129     },
130 });