New edit dialog
[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                     {content: "Replying to"},
19                     {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             {content: "In reply to:"},
31             {kind: "Scroller", components: [
32                 {name: "preview", classes: "record", allowHtml: true},
33             ]},
34             {kind: "Scroller", components: [
35                 {name: "helpContent", allowHtml: true},
36             ]}
37         ]},
38         {name: "api", kind: "blerg.API",
39          onPostSuccessful: "postSuccessful",
40          onPostFailed: "postFailed"}
41     ],
42     create: function() {
43         this.inherited(arguments);
44     },
45     getData: function() {
46         return this.$.postContent.getValue();
47     },
48     setData: function(inVal) {
49         this.$.postContent.setValue(inVal);
50     },
51     closePost: function() {
52         this.bubble('onPostVisibility', {showing: false});
53     },
54     doPost: function() {
55         this.$.api.post(this.getData());
56     },
57     postSuccessful: function() {
58         this.setData('');
59         this.closePost();
60         if (location.hash != '#' + blerg.API.username) {
61             location.hash = '#' + blerg.API.username
62             this.bubble('onNavigate');
63         } else {
64             this.bubble('onReload');
65         }
66     },
67     postFailed: function() {
68         alert('Could not post!');
69     },
70     postVisibilityUpdate: function(inSender, inEvent) {
71         if (inEvent.showing) {
72             this.show();
73             // Need to reflow manually because this is hidden by default
74             this.$.toolbar.reflow();
75             this.reflow();
76             this.$.postContent.focus();
77             if (inEvent.data && this.getData() == "") {
78                 this.setData(inEvent.data);
79                 this.$.postContent.node.setSelectionRange(inEvent.data.length, inEvent.data.length);
80             }
81         } else {
82             this.hide();
83         }
84     },
85     updatePreview: function(inSender, inEvent) {
86         this.$.preview.setContent(blerg.Util.blergFormat(this.getData()));
87     },
88     loggedIn: function() {
89         this.$.postButton.setDisabled(false);
90         this.$.loginReminder.hide();
91     },
92     loggedOut: function() {
93         this.$.postButton.setDisabled(true);
94         this.$.loginReminder.show();
95     },
96     loadHelp: function() {
97         if (this.$.helpContent.getContent() == '') {
98             var req = new enyo.Ajax({
99                 url: baseURL + '/doc/post_help.html',
100                 handleAs: 'text'
101             });
102             req.response(function(inSender, inResponse) {
103                 this.$.helpContent.setContent(inResponse);
104             }.bind(this));
105             req.go();
106         }
107     },
108     switchBottomPanel: function(inSender, inEvent) {
109         var active = inSender.getActive();
110         var index = inSender.children.indexOf(active);
111         this.$.bottomPanel.setIndex(index);
112         if (index == 2) {
113             this.loadHelp();
114         }
115     },
116 });