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
|
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.
var client = require('../utils/client.jsx');
var AsyncClient =require('../utils/async_client.jsx');
var SocketStore = require('../stores/socket_store.jsx');
var ChannelStore = require('../stores/channel_store.jsx');
var Textbox = require('./textbox.jsx');
var MsgTyping = require('./msg_typing.jsx');
var FileUpload = require('./file_upload.jsx');
var FilePreview = require('./file_preview.jsx');
var Constants = require('../utils/constants.jsx');
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 (post.message.trim().length === 0 && this.state.previews.length === 0) {
return;
}
if (post.message.length > Constants.CHARACTER_LIMIT) {
this.setState({ post_error: 'Comment length must be less than '+Constants.CHARACTER_LIMIT+' characters.' });
return;
}
post.channel_id = this.props.channelId;
post.root_id = this.props.rootId;
post.parent_id = this.props.parentId;
post.filenames = this.state.previews;
this.setState({ submitting: true });
client.createPost(post, ChannelStore.getCurrent(),
function(data) {
this.setState({ messageText: '', submitting: false, post_error: null });
this.clearPreviews();
AsyncClient.getPosts(true, this.props.channelId);
var channel = ChannelStore.get(this.props.channelId);
var member = ChannelStore.getMember(this.props.channelId);
member.msg_count = channel.total_msg_count;
member.last_viewed_at = (new Date).getTime();
ChannelStore.setChannelMember(member);
}.bind(this),
function(err) {
var state = {}
state.server_error = err.message;
this.setState(state);
if (err.message === "Invalid RootId parameter") {
if ($('#post_deleted').length > 0) $('#post_deleted').modal('show');
}
}.bind(this)
);
},
commentMsgKeyPress: 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.props.channelId, action: "typing", props: {"parent_id": this.props.rootId} });
this.lastTime = t;
}
},
handleUserInput: function(messageText) {
$(".post-right__scroll").scrollTop($(".post-right__scroll")[0].scrollHeight);
$(".post-right__scroll").perfectScrollbar('update');
this.setState({messageText: messageText});
},
handleFileUpload: function(newPreviews) {
$(".post-right__scroll").scrollTop($(".post-right__scroll")[0].scrollHeight);
$(".post-right__scroll").perfectScrollbar('update');
var oldPreviews = this.state.previews;
var num = this.state.uploadsInProgress;
this.setState({previews: oldPreviews.concat(newPreviews), uploadsInProgress:num-1});
},
handleUploadError: function(err) {
this.setState({ server_error: err });
},
clearPreviews: function() {
this.setState({previews: []});
},
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;
}
}
this.setState({previews: previews});
},
getInitialState: function() {
return { messageText: '', uploadsInProgress: 0, previews: [], submitting: false };
},
setUploads: function(val) {
var num = this.state.uploadsInProgress + val;
this.setState({uploadsInProgress: num});
},
render: function() {
var server_error = this.state.server_error ? <div className='form-group 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 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} />
);
}
var limit_previews = ""
if (this.state.previews.length > 5) {
limit_previews = <div className='has-error'><label className='control-label'>{ "Note: While all files will be available, only first five will show thumbnails." }</label></div>
}
if (this.state.previews.length > 20) {
limit_previews = <div className='has-error'><label className='control-label'>{ "Note: Uploads limited to 20 files maximum. Please use additional posts for more files." }</label></div>
}
return (
<form onSubmit={this.handleSubmit}>
<div className="post-create">
<div id={this.props.rootId} className="post-create-body comment-create-body">
<Textbox
onUserInput={this.handleUserInput}
onKeyPress={this.commentMsgKeyPress}
messageText={this.state.messageText}
createMessage="Create a comment..."
initialText=""
id="reply_textbox"
ref="textbox" />
<FileUpload
setUploads={this.setUploads}
onFileUpload={this.handleFileUpload}
onUploadError={this.handleUploadError} />
</div>
<MsgTyping channelId={this.props.channelId} parentId={this.props.rootId} />
<div className={post_error ? 'has-error' : 'post-create-footer'}>
<input type="button" className="btn btn-primary comment-btn pull-right" value="Add Comment" onClick={this.handleSubmit} />
{ post_error }
{ server_error }
{ limit_previews }
</div>
</div>
{ preview }
</form>
);
}
});
|