summaryrefslogtreecommitdiffstats
path: root/web/react/components/create_post.jsx
blob: 3275202101b9a0fb68392c25ba2e2441962c3109 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.

var AppDispatcher = require('../dispatcher/app_dispatcher.jsx');
var client = require('../utils/client.jsx');
var AsyncClient = require('../utils/async_client.jsx');
var ChannelStore = require('../stores/channel_store.jsx');
var PostStore = require('../stores/post_store.jsx');
var UserStore = require('../stores/user_store.jsx');
var SocketStore = require('../stores/socket_store.jsx');
var MsgTyping = require('./msg_typing.jsx');
var Textbox = require('./textbox.jsx');
var FileUpload = require('./file_upload.jsx');
var FilePreview = require('./file_preview.jsx');
var utils = require('../utils/utils.jsx');

var Constants = require('../utils/constants.jsx');
var ActionTypes = Constants.ActionTypes;

module.exports = React.createClass({
    lastTime: 0,
    handleSubmit: function(e) {
        e.preventDefault();

        if (this.state.uploadsInProgress > 0) return;

        if (this.state.submitting) return;

        var post = {};
        post.filenames = [];

        post.message = this.state.messageText;

        // if this is a reply, trim off any carets from the beginning of a message
        if (this.state.rootId && post.message[0] === "^") {
            post.message = post.message.replace(/^\^+\s*/g, "");
        }

        if (post.message.trim().length === 0 && this.state.previews.length === 0) {
            return;
        }

        if (post.message.length > Constants.CHARACTER_LIMIT) {
            this.setState({ post_error: 'Post length must be less than '+Constants.CHARACTER_LIMIT+' characters.' });
            return;
        }

        this.setState({ submitting: true, limit_error: null });

        var user_id = UserStore.getCurrentId();

        if (post.message.indexOf("/") == 0) {
            client.executeCommand(
                this.state.channel_id,
                post.message,
                false,
                function(data) {
                    PostStore.storeDraft(data.channel_id, null);
                    this.setState({ messageText: '', submitting: false, post_error: null, previews: [], server_error: null, limit_error: null });

                    if (data.goto_location.length > 0) {
                        window.location.href = data.goto_location;
                    }
                }.bind(this),
                function(err){
                    var state = {}
                    state.server_error = err.message;
                    state.submitting = false;
                    this.setState(state);
                }.bind(this)
            );
        } else {
            post.channel_id = this.state.channel_id;
            post.filenames = this.state.previews;

            post.root_id = this.state.rootId;
            post.parent_id = this.state.parentId;

            client.createPost(post, ChannelStore.getCurrent(),
                function(data) {
                    PostStore.storeDraft(data.channel_id, null);
                    this.setState({ messageText: '', submitting: false, post_error: null, previews: [], server_error: null, limit_error: null });
                    this.resizePostHolder();
                    AsyncClient.getPosts(true);

                    var channel = ChannelStore.get(this.state.channel_id);
                    var member = ChannelStore.getMember(this.state.channel_id);
                    member.msg_count = channel.total_msg_count;
                    member.last_viewed_at = (new Date).getTime();
                    ChannelStore.setChannelMember(member);

                }.bind(this),
                function(err) {
                    var state = {}

                    if (err.message === "Invalid RootId parameter") {
                        if ($('#post_deleted').length > 0) $('#post_deleted').modal('show');
                    } else {
                        state.server_error = err.message;
                    }

                    state.submitting = false;
                    this.setState(state);
                }.bind(this)
            );
        }

        $(".post-list-holder-by-time").perfectScrollbar('update');

        if (this.state.rootId || this.state.parentId) {
            this.setState({rootId: "", parentId: "", caretCount: 0});

            // clear the active thread since we've now sent our message
            AppDispatcher.handleViewAction({
                type: ActionTypes.RECEIVED_ACTIVE_THREAD_CHANGED,
                root_id: "",
                parent_id: ""
            });
        }
    },
    componentDidUpdate: function() {
        this.resizePostHolder();
    },
    postMsgKeyPress: function(e) {
        if (e.which == 13 && !e.shiftKey && !e.altKey) {
            e.preventDefault();
            this.refs.textbox.getDOMNode().blur();
            this.handleSubmit(e);
        }

        var t = new Date().getTime();
        if ((t - this.lastTime) > 5000) {
            SocketStore.sendMessage({channel_id: this.state.channel_id, action: "typing", props: {"parent_id": ""}, state: {} });
            this.lastTime = t;
        }
    },
    handleUserInput: function(messageText) {
        this.resizePostHolder();
        this.setState({messageText: messageText});

        // look to see if the message begins with any carets to indicate that it's a reply
        var replyMatch = messageText.match(/^\^+/g);
        if (replyMatch) {
            // the number of carets indicates how many message threads back we're replying to
            var caretCount = replyMatch[0].length;

            // note that if someone else replies to this thread while a user is typing a reply, the message to which they're replying
            // won't change unless they change the number of carets. this is probably the desired behaviour since we don't want the
            // active message thread to change without the user noticing
            if (caretCount != this.state.caretCount) {
                this.setState({caretCount: caretCount});

                var posts = PostStore.getCurrentPosts();

                var rootId = "";

                // find the nth most recent post that isn't a comment on another (ie it has no parent) where n is caretCount
                for (var i = 0; i < posts.order.length; i++) {
                    var postId = posts.order[i];

                    if (posts.posts[postId].parent_id === "") {
                        caretCount -= 1;

                        if (caretCount < 1) {
                            rootId = postId;
                            break;
                        }
                    }
                }

                // only dispatch an event if something changed
                if (rootId != this.state.rootId) {
                    // set the parent id to match the root id so that we're replying to the first post in the thread
                    var parentId = rootId;

                    // alert the post list so that it can display the active thread
                    AppDispatcher.handleViewAction({
                        type: ActionTypes.RECEIVED_ACTIVE_THREAD_CHANGED,
                        root_id: rootId,
                        parent_id: parentId
                    });
                }
            }
        } else {
            if (this.state.caretCount > 0) {
                this.setState({caretCount: 0});

                // clear the active thread since there no longer is one
                AppDispatcher.handleViewAction({
                    type: ActionTypes.RECEIVED_ACTIVE_THREAD_CHANGED,
                    root_id: "",
                    parent_id: ""
                });
            }
        }

        var draft = PostStore.getCurrentDraft();
        if (!draft) {
            draft = {}
            draft['previews'] = [];
            draft['uploadsInProgress'] = 0;
        }
        draft['message'] = messageText;
        PostStore.storeCurrentDraft(draft);
    },
    resizePostHolder: function() {
        var height = $(window).height() - $(this.refs.topDiv.getDOMNode()).height() - $('#error_bar').outerHeight() - 50;
        $(".post-list-holder-by-time").css("height", height + "px");
        $(window).trigger('resize');
    },
    handleFileUpload: function(newPreviews, channel_id) {
        var draft = PostStore.getDraft(channel_id);
        if (!draft) {
            draft = {}
            draft['message'] = '';
            draft['uploadsInProgress'] = 0;
            draft['previews'] = [];
        }

        if (channel_id === this.state.channel_id) {
            var num = this.state.uploadsInProgress;
            var oldPreviews = this.state.previews;
            var previews = oldPreviews.concat(newPreviews);

            draft['previews'] = previews;
            draft['uploadsInProgress'] = num-1;
            PostStore.storeCurrentDraft(draft);

            this.setState({previews: previews, uploadsInProgress:num-1});
        } else {
            draft['previews'] = draft['previews'].concat(newPreviews);
            draft['uploadsInProgress'] = draft['uploadsInProgress'] > 0 ? draft['uploadsInProgress'] - 1 : 0;
            PostStore.storeDraft(channel_id, draft);
        }
    },
    handleUploadError: function(err) {
        this.setState({ server_error: err });
    },
    removePreview: function(filename) {
        var previews = this.state.previews;
        for (var i = 0; i < previews.length; i++) {
            if (previews[i] === filename) {
                previews.splice(i, 1);
                break;
            }
        }
        var draft = PostStore.getCurrentDraft();
        if (!draft) {
            draft = {}
            draft['message'] = '';
            draft['uploadsInProgress'] = 0;
        }
        draft['previews'] = previews;
        PostStore.storeCurrentDraft(draft);
        this.setState({previews: previews});
    },
    componentDidMount: function() {
        ChannelStore.addChangeListener(this._onChange);
        PostStore.addActiveThreadChangedListener(this._onActiveThreadChanged);
        this.resizePostHolder();
    },
    componentWillUnmount: function() {
        ChannelStore.removeChangeListener(this._onChange);
        PostStore.removeActiveThreadChangedListener(this._onActiveThreadChanged);
    },
    _onChange: function() {
        var channel_id = ChannelStore.getCurrentId();
        if (this.state.channel_id != channel_id) {
            var draft = PostStore.getCurrentDraft();
            var previews = [];
            var messageText = '';
            var uploadsInProgress = 0;
            if (draft) {
                previews = draft['previews'];
                messageText = draft['message'];
                uploadsInProgress = draft['uploadsInProgress'];
            }
            this.setState({ channel_id: channel_id, messageText: messageText, initialText: messageText, submitting: false, limit_error: null, server_error: null, post_error: null, previews: previews, uploadsInProgress: uploadsInProgress });
        }
    },
    _onActiveThreadChanged: function(rootId, parentId) {
        // note that we register for our own events and set the state from there so we don't need to manually set
        // our state and dispatch an event each time the active thread changes
        this.setState({"rootId": rootId, "parentId": parentId});
    },
    getInitialState: function() {
        PostStore.clearDraftUploads();

        var draft = PostStore.getCurrentDraft();
        var previews = [];
        var messageText = '';
        if (draft) {
            previews = draft['previews'];
            messageText = draft['message'];
        }
        return { channel_id: ChannelStore.getCurrentId(), messageText: messageText, uploadsInProgress: 0, previews: previews, submitting: false, initialText: messageText, caretCount: 0 };
    },
    setUploads: function(val) {
        var oldInProgress = this.state.uploadsInProgress
        var newInProgress = oldInProgress + val;

        if (newInProgress + this.state.previews.length > Constants.MAX_UPLOAD_FILES) {
            newInProgress = Constants.MAX_UPLOAD_FILES - this.state.previews.length;
            this.setState({limit_error: "Uploads limited to " + Constants.MAX_UPLOAD_FILES + " files maximum. Please use additional posts for more files."});
        } else {
            this.setState({limit_error: null});
        }

        var numToUpload = newInProgress - oldInProgress;
        if (numToUpload <= 0) return 0;

        var draft = PostStore.getCurrentDraft();
        if (!draft) {
            draft = {}
            draft['message'] = '';
            draft['previews'] = [];
        }
        draft['uploadsInProgress'] = newInProgress;
        PostStore.storeCurrentDraft(draft);
        this.setState({uploadsInProgress: newInProgress});

        return numToUpload;
    },
    render: function() {

        var server_error = this.state.server_error ? <div className='has-error'><label className='control-label'>{ this.state.server_error }</label></div> : null;
        var post_error = this.state.post_error ? <label className='control-label'>{this.state.post_error}</label> : null;
        var limit_error = this.state.limit_error ? <div className='has-error'><label className='control-label'>{this.state.limit_error}</label></div> : null;

        var preview = <div/>;
        if (this.state.previews.length > 0 || this.state.uploadsInProgress > 0) {
            preview = (
                <FilePreview
                    files={this.state.previews}
                    onRemove={this.removePreview}
                    uploadsInProgress={this.state.uploadsInProgress} />
            );
        }

        return (
            <form id="create_post" ref="topDiv" role="form" onSubmit={this.handleSubmit}>
                <div className="post-create">
                    <div className="post-create-body">
                        <Textbox
                            onUserInput={this.handleUserInput}
                            onKeyPress={this.postMsgKeyPress}
                            messageText={this.state.messageText}
                            createMessage="Write a message..."
                            channelId={this.state.channel_id}
                            id="post_textbox"
                            ref="textbox" />
                        <FileUpload
                            setUploads={this.setUploads}
                            onFileUpload={this.handleFileUpload}
                            onUploadError={this.handleUploadError} />
                    </div>
                    <div className={post_error ? 'post-create-footer has-error' : 'post-create-footer'}>
                        { post_error }
                        { server_error }
                        { limit_error }
                        { preview }
                        <MsgTyping channelId={this.state.channel_id} parentId=""/>
                    </div>
                </div>
            </form>
        );
    }
});