2015-06-14 23:53:32 -08:00
|
|
|
// 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();
|
|
|
|
|
|
2015-08-10 14:01:11 -04:00
|
|
|
if (this.state.uploadsInProgress.length > 0 || this.state.submitting) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
|
|
|
|
var post = {};
|
|
|
|
|
post.filenames = [];
|
|
|
|
|
post.message = this.state.messageText;
|
|
|
|
|
|
2015-06-18 17:39:12 -04:00
|
|
|
if (post.message.trim().length === 0 && this.state.previews.length === 0) {
|
2015-06-14 23:53:32 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (post.message.length > Constants.CHARACTER_LIMIT) {
|
2015-08-10 14:01:11 -04:00
|
|
|
this.setState({postError: 'Post length must be less than ' + Constants.CHARACTER_LIMIT + ' characters.'});
|
2015-06-14 23:53:32 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-10 14:01:11 -04:00
|
|
|
this.setState({submitting: true, serverError: null});
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-08-10 14:01:11 -04:00
|
|
|
if (post.message.indexOf('/') === 0) {
|
2015-06-14 23:53:32 -08:00
|
|
|
client.executeCommand(
|
2015-08-10 14:01:11 -04:00
|
|
|
this.state.channelId,
|
2015-06-14 23:53:32 -08:00
|
|
|
post.message,
|
|
|
|
|
false,
|
|
|
|
|
function(data) {
|
2015-07-20 12:54:13 -07:00
|
|
|
PostStore.storeDraft(data.channel_id, null);
|
2015-08-10 14:01:11 -04:00
|
|
|
this.setState({messageText: '', submitting: false, postError: null, previews: [], serverError: null});
|
2015-06-14 23:53:32 -08:00
|
|
|
|
|
|
|
|
if (data.goto_location.length > 0) {
|
|
|
|
|
window.location.href = data.goto_location;
|
|
|
|
|
}
|
|
|
|
|
}.bind(this),
|
2015-08-10 14:01:11 -04:00
|
|
|
function(err) {
|
|
|
|
|
var state = {};
|
|
|
|
|
state.serverError = err.message;
|
2015-06-14 23:53:32 -08:00
|
|
|
state.submitting = false;
|
|
|
|
|
this.setState(state);
|
|
|
|
|
}.bind(this)
|
|
|
|
|
);
|
|
|
|
|
} else {
|
2015-08-10 14:01:11 -04:00
|
|
|
post.channel_id = this.state.channelId;
|
2015-06-14 23:53:32 -08:00
|
|
|
post.filenames = this.state.previews;
|
|
|
|
|
|
|
|
|
|
client.createPost(post, ChannelStore.getCurrent(),
|
|
|
|
|
function(data) {
|
2015-07-20 12:54:13 -07:00
|
|
|
PostStore.storeDraft(data.channel_id, null);
|
2015-08-10 14:01:11 -04:00
|
|
|
this.setState({messageText: '', submitting: false, postError: null, previews: [], serverError: null});
|
2015-06-14 23:53:32 -08:00
|
|
|
this.resizePostHolder();
|
|
|
|
|
AsyncClient.getPosts(true);
|
|
|
|
|
|
2015-08-10 14:01:11 -04:00
|
|
|
var channel = ChannelStore.get(this.state.channelId);
|
|
|
|
|
var member = ChannelStore.getMember(this.state.channelId);
|
2015-06-14 23:53:32 -08:00
|
|
|
member.msg_count = channel.total_msg_count;
|
2015-08-10 14:01:11 -04:00
|
|
|
member.last_viewed_at = Date.now();
|
2015-06-14 23:53:32 -08:00
|
|
|
ChannelStore.setChannelMember(member);
|
|
|
|
|
}.bind(this),
|
|
|
|
|
function(err) {
|
2015-08-10 14:01:11 -04:00
|
|
|
var state = {};
|
|
|
|
|
state.serverError = err.message;
|
2015-07-21 15:55:34 -04:00
|
|
|
|
2015-06-14 23:53:32 -08:00
|
|
|
state.submitting = false;
|
|
|
|
|
this.setState(state);
|
|
|
|
|
}.bind(this)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-10 14:01:11 -04:00
|
|
|
$('.post-list-holder-by-time').perfectScrollbar('update');
|
2015-06-14 23:53:32 -08:00
|
|
|
},
|
|
|
|
|
componentDidUpdate: function() {
|
|
|
|
|
this.resizePostHolder();
|
|
|
|
|
},
|
|
|
|
|
postMsgKeyPress: function(e) {
|
2015-08-10 14:01:11 -04:00
|
|
|
if (e.which === 13 && !e.shiftKey && !e.altKey) {
|
2015-06-14 23:53:32 -08:00
|
|
|
e.preventDefault();
|
|
|
|
|
this.refs.textbox.getDOMNode().blur();
|
|
|
|
|
this.handleSubmit(e);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-10 14:01:11 -04:00
|
|
|
var t = Date.now();
|
2015-06-14 23:53:32 -08:00
|
|
|
if ((t - this.lastTime) > 5000) {
|
2015-08-10 14:01:11 -04:00
|
|
|
SocketStore.sendMessage({channelId: this.state.channelId, action: 'typing', props: {'parent_id': ''}, state: {}});
|
2015-06-14 23:53:32 -08:00
|
|
|
this.lastTime = t;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
handleUserInput: function(messageText) {
|
|
|
|
|
this.resizePostHolder();
|
|
|
|
|
this.setState({messageText: messageText});
|
2015-07-16 15:25:28 -04:00
|
|
|
|
2015-06-14 23:53:32 -08:00
|
|
|
var draft = PostStore.getCurrentDraft();
|
|
|
|
|
draft['message'] = messageText;
|
|
|
|
|
PostStore.storeCurrentDraft(draft);
|
|
|
|
|
},
|
|
|
|
|
resizePostHolder: function() {
|
|
|
|
|
var height = $(window).height() - $(this.refs.topDiv.getDOMNode()).height() - $('#error_bar').outerHeight() - 50;
|
2015-08-10 14:01:11 -04:00
|
|
|
$('.post-list-holder-by-time').css('height', height + 'px');
|
2015-06-14 23:53:32 -08:00
|
|
|
$(window).trigger('resize');
|
|
|
|
|
},
|
2015-08-10 14:01:11 -04:00
|
|
|
handleUploadStart: function(clientIds, channelId) {
|
|
|
|
|
var draft = PostStore.getDraft(channelId);
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-08-10 12:05:45 -04:00
|
|
|
draft['uploadsInProgress'] = draft['uploadsInProgress'].concat(clientIds);
|
2015-08-10 14:01:11 -04:00
|
|
|
PostStore.storeDraft(channelId, draft);
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-08-07 16:24:13 -04:00
|
|
|
this.setState({uploadsInProgress: draft['uploadsInProgress']});
|
|
|
|
|
},
|
2015-08-10 14:01:11 -04:00
|
|
|
handleFileUploadComplete: function(filenames, clientIds, channelId) {
|
|
|
|
|
var draft = PostStore.getDraft(channelId);
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-08-07 16:24:13 -04:00
|
|
|
// remove each finished file from uploads
|
2015-08-10 12:05:45 -04:00
|
|
|
for (var i = 0; i < clientIds.length; i++) {
|
|
|
|
|
var index = draft['uploadsInProgress'].indexOf(clientIds[i]);
|
2015-08-07 16:24:13 -04:00
|
|
|
|
2015-08-10 14:01:11 -04:00
|
|
|
if (index !== -1) {
|
2015-08-07 16:24:13 -04:00
|
|
|
draft['uploadsInProgress'].splice(index, 1);
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
2015-08-07 16:24:13 -04:00
|
|
|
|
|
|
|
|
draft['previews'] = draft['previews'].concat(filenames);
|
2015-08-10 14:01:11 -04:00
|
|
|
PostStore.storeDraft(channelId, draft);
|
2015-08-07 16:24:13 -04:00
|
|
|
|
|
|
|
|
this.setState({uploadsInProgress: draft['uploadsInProgress'], previews: draft['previews']});
|
2015-06-14 23:53:32 -08:00
|
|
|
},
|
2015-08-10 13:15:01 -04:00
|
|
|
handleUploadError: function(err, clientId) {
|
2015-08-10 14:01:11 -04:00
|
|
|
var draft = PostStore.getDraft(this.state.channelId);
|
2015-08-10 13:15:01 -04:00
|
|
|
|
|
|
|
|
var index = draft['uploadsInProgress'].indexOf(clientId);
|
2015-08-10 14:01:11 -04:00
|
|
|
if (index !== -1) {
|
2015-08-10 13:15:01 -04:00
|
|
|
draft['uploadsInProgress'].splice(index, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-10 14:01:11 -04:00
|
|
|
PostStore.storeDraft(this.state.channelId, draft);
|
2015-08-10 13:15:01 -04:00
|
|
|
|
2015-08-10 14:01:11 -04:00
|
|
|
this.setState({uploadsInProgress: draft['uploadsInProgress'], serverError: err});
|
2015-06-14 23:53:32 -08:00
|
|
|
},
|
2015-08-10 12:05:45 -04:00
|
|
|
removePreview: function(id) {
|
2015-06-14 23:53:32 -08:00
|
|
|
var previews = this.state.previews;
|
2015-08-10 08:41:18 -04:00
|
|
|
var uploadsInProgress = this.state.uploadsInProgress;
|
|
|
|
|
|
2015-08-10 12:05:45 -04:00
|
|
|
// id can either be the path of an uploaded file or the client id of an in progress upload
|
|
|
|
|
var index = previews.indexOf(id);
|
2015-08-10 08:41:18 -04:00
|
|
|
if (index !== -1) {
|
|
|
|
|
previews.splice(index, 1);
|
|
|
|
|
} else {
|
2015-08-10 12:05:45 -04:00
|
|
|
index = uploadsInProgress.indexOf(id);
|
2015-08-10 08:41:18 -04:00
|
|
|
|
|
|
|
|
if (index !== -1) {
|
|
|
|
|
uploadsInProgress.splice(index, 1);
|
2015-08-10 12:05:45 -04:00
|
|
|
this.refs.fileUpload.cancelUpload(id);
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
}
|
2015-08-10 08:41:18 -04:00
|
|
|
|
2015-06-14 23:53:32 -08:00
|
|
|
var draft = PostStore.getCurrentDraft();
|
|
|
|
|
draft['previews'] = previews;
|
2015-08-10 08:41:18 -04:00
|
|
|
draft['uploadsInProgress'] = uploadsInProgress;
|
2015-06-14 23:53:32 -08:00
|
|
|
PostStore.storeCurrentDraft(draft);
|
2015-08-10 08:41:18 -04:00
|
|
|
|
|
|
|
|
this.setState({previews: previews, uploadsInProgress: uploadsInProgress});
|
2015-06-14 23:53:32 -08:00
|
|
|
},
|
|
|
|
|
componentDidMount: function() {
|
|
|
|
|
ChannelStore.addChangeListener(this._onChange);
|
|
|
|
|
this.resizePostHolder();
|
|
|
|
|
},
|
|
|
|
|
componentWillUnmount: function() {
|
|
|
|
|
ChannelStore.removeChangeListener(this._onChange);
|
|
|
|
|
},
|
|
|
|
|
_onChange: function() {
|
2015-08-10 14:01:11 -04:00
|
|
|
var channelId = ChannelStore.getCurrentId();
|
|
|
|
|
if (this.state.channelId !== channelId) {
|
2015-06-14 23:53:32 -08:00
|
|
|
var draft = PostStore.getCurrentDraft();
|
2015-08-10 14:01:11 -04:00
|
|
|
this.setState({
|
2015-08-11 09:41:18 -04:00
|
|
|
channelId: channelId, messageText: draft['message'], initialText: draft['message'], submitting: false,
|
|
|
|
|
serverError: null, postError: null, previews: draft['previews'], uploadsInProgress: draft['uploadsInProgress']
|
2015-08-10 14:01:11 -04:00
|
|
|
});
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
getInitialState: function() {
|
|
|
|
|
PostStore.clearDraftUploads();
|
|
|
|
|
|
|
|
|
|
var draft = PostStore.getCurrentDraft();
|
2015-08-11 09:41:18 -04:00
|
|
|
return {
|
|
|
|
|
channelId: ChannelStore.getCurrentId(), messageText: draft['message'], uploadsInProgress: draft['uploadsInProgress'],
|
|
|
|
|
previews: draft['previews'], submitting: false, initialText: draft['message']
|
|
|
|
|
};
|
2015-06-14 23:53:32 -08:00
|
|
|
},
|
2015-08-10 14:01:11 -04:00
|
|
|
getFileCount: function(channelId) {
|
|
|
|
|
if (channelId === this.state.channelId) {
|
2015-08-07 16:24:13 -04:00
|
|
|
return this.state.previews.length + this.state.uploadsInProgress.length;
|
2015-06-22 08:35:24 -04:00
|
|
|
} else {
|
2015-08-10 14:01:11 -04:00
|
|
|
var draft = PostStore.getDraft(channelId);
|
2015-06-22 08:35:24 -04:00
|
|
|
|
2015-08-11 09:41:18 -04:00
|
|
|
return draft['previews'].length + draft['uploadsInProgress'].length;
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
render: function() {
|
2015-08-10 14:01:11 -04:00
|
|
|
var serverError = null;
|
|
|
|
|
if (this.state.serverError) {
|
|
|
|
|
serverError = (
|
|
|
|
|
<div className='has-error'>
|
|
|
|
|
<label className='control-label'>{this.state.serverError}</label>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-08-10 14:01:11 -04:00
|
|
|
var postError = null;
|
|
|
|
|
if (this.state.postError) {
|
|
|
|
|
postError = <label className='control-label'>{this.state.postError}</label>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var preview = null;
|
2015-08-07 16:24:13 -04:00
|
|
|
if (this.state.previews.length > 0 || this.state.uploadsInProgress.length > 0) {
|
2015-06-14 23:53:32 -08:00
|
|
|
preview = (
|
|
|
|
|
<FilePreview
|
|
|
|
|
files={this.state.previews}
|
|
|
|
|
onRemove={this.removePreview}
|
|
|
|
|
uploadsInProgress={this.state.uploadsInProgress} />
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-10 14:01:11 -04:00
|
|
|
var postFooterClassName = 'post-create-footer';
|
|
|
|
|
if (postError) {
|
|
|
|
|
postFooterClassName += ' has-error';
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-14 23:53:32 -08:00
|
|
|
return (
|
2015-08-10 14:01:11 -04:00
|
|
|
<form id='create_post' ref='topDiv' role='form' onSubmit={this.handleSubmit}>
|
|
|
|
|
<div className='post-create'>
|
|
|
|
|
<div className='post-create-body'>
|
2015-06-14 23:53:32 -08:00
|
|
|
<Textbox
|
|
|
|
|
onUserInput={this.handleUserInput}
|
|
|
|
|
onKeyPress={this.postMsgKeyPress}
|
|
|
|
|
messageText={this.state.messageText}
|
2015-08-10 14:01:11 -04:00
|
|
|
createMessage='Write a message...'
|
|
|
|
|
channelId={this.state.channelId}
|
|
|
|
|
id='post_textbox'
|
|
|
|
|
ref='textbox' />
|
2015-06-14 23:53:32 -08:00
|
|
|
<FileUpload
|
2015-08-10 08:41:18 -04:00
|
|
|
ref='fileUpload'
|
2015-08-07 16:24:13 -04:00
|
|
|
getFileCount={this.getFileCount}
|
|
|
|
|
onUploadStart={this.handleUploadStart}
|
|
|
|
|
onFileUpload={this.handleFileUploadComplete}
|
2015-08-07 14:24:09 -07:00
|
|
|
onUploadError={this.handleUploadError}
|
|
|
|
|
postType='post'
|
|
|
|
|
channelId='' />
|
2015-06-14 23:53:32 -08:00
|
|
|
</div>
|
2015-08-10 14:01:11 -04:00
|
|
|
<div className={postFooterClassName}>
|
|
|
|
|
{postError}
|
|
|
|
|
{serverError}
|
|
|
|
|
{preview}
|
|
|
|
|
<MsgTyping channelId={this.state.channelId} parentId=''/>
|
2015-06-14 23:53:32 -08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|