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
|
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.
var utils = require('../utils/utils.jsx');
var Client = require('../utils/client.jsx');
var AsyncClient = require('../utils/async_client.jsx');
var ChannelStore = require('../stores/channel_store.jsx');
var TeamStore = require('../stores/team_store.jsx');
var Constants = require('../utils/constants.jsx');
module.exports = React.createClass({
handleSubmit: function(e) {
e.preventDefault();
if (this.state.channel_id.length !== 26) return;
var channel = ChannelStore.get(this.state.channel_id);
var oldName = channel.name
var oldDisplayName = channel.display_name
var state = { server_error: "" };
channel.display_name = this.state.display_name.trim();
if (!channel.display_name) {
state.display_name_error = "This field is required";
state.inValid = true;
}
else if (channel.display_name.length > 22) {
state.display_name_error = "This field must be less than 22 characters";
state.inValid = true;
}
else {
state.display_name_error = "";
}
channel.name = this.state.channel_name.trim();
if (!channel.name) {
state.name_error = "This field is required";
state.inValid = true;
}
else if(channel.name.length > 22){
state.name_error = "This field must be less than 22 characters";
state.inValid = true;
}
else {
var cleaned_name = utils.cleanUpUrlable(channel.name);
if (cleaned_name != channel.name) {
state.name_error = "Must be lowercase alphanumeric characters";
state.inValid = true;
}
else {
state.name_error = "";
}
}
this.setState(state);
if (state.inValid)
return;
if (oldName == channel.name && oldDisplayName == channel.display_name)
return;
Client.updateChannel(channel,
function(data, text, req) {
this.refs.display_name.getDOMNode().value = "";
this.refs.channel_name.getDOMNode().value = "";
$('#' + this.props.modalId).modal('hide');
window.location.href = TeamStore.getCurrentTeamUrl() + '/channels/' + this.state.channel_name;
AsyncClient.getChannels(true);
}.bind(this),
function(err) {
state.server_error = err.message;
state.inValid = true;
this.setState(state);
}.bind(this)
);
},
onNameChange: function() {
this.setState({ channel_name: this.refs.channel_name.getDOMNode().value })
},
onDisplayNameChange: function() {
this.setState({ display_name: this.refs.display_name.getDOMNode().value })
},
displayNameKeyUp: function(e) {
var display_name = this.refs.display_name.getDOMNode().value.trim();
var channel_name = utils.cleanUpUrlable(display_name);
this.refs.channel_name.getDOMNode().value = channel_name;
this.setState({ channel_name: channel_name })
},
handleClose: function() {
this.setState({display_name: "", channel_name: "", display_name_error: "", server_error: "", name_error: ""});
},
componentDidMount: function() {
var self = this;
$(this.refs.modal.getDOMNode()).on('show.bs.modal', function(e) {
var button = $(e.relatedTarget);
self.setState({ display_name: button.attr('data-display'), title: button.attr('data-name'), channel_id: button.attr('data-channelid') });
});
$(this.refs.modal.getDOMNode()).on('hidden.bs.modal', this.handleClose);
},
componentWillUnmount: function() {
$(this.refs.modal.getDOMNode()).off('hidden.bs.modal', this.handleClose);
},
getInitialState: function() {
return { display_name: "", channel_name: "", channel_id: "" };
},
render: function() {
var display_name_error = this.state.display_name_error ? <label className='control-label'>{ this.state.display_name_error }</label> : null;
var name_error = this.state.name_error ? <label className='control-label'>{ this.state.name_error }</label> : null;
var server_error = this.state.server_error ? <div className='form-group has-error'><label className='control-label'>{ this.state.server_error }</label></div> : null;
return (
<div className="modal fade" ref="modal" id="rename_channel" tabIndex="-1" role="dialog" aria-hidden="true">
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<button type="button" className="close" data-dismiss="modal">
<span aria-hidden="true">×</span>
<span className="sr-only">Close</span>
</button>
<h4 className="modal-title">Rename Channel</h4>
</div>
<div className="modal-body">
<form role="form">
<div className={ this.state.display_name_error ? "form-group has-error" : "form-group" }>
<label className='control-label'>Display Name</label>
<input onKeyUp={this.displayNameKeyUp} onChange={this.onDisplayNameChange} type="text" ref="display_name" className="form-control" placeholder="Enter display name" value={this.state.display_name} maxLength="64" />
{ display_name_error }
</div>
<div className={ this.state.name_error ? "form-group has-error" : "form-group" }>
<label className='control-label'>Handle</label>
<input onChange={this.onNameChange} type="text" className="form-control" ref="channel_name" placeholder="lowercase alphanumeric's only" value={this.state.channel_name} maxLength="64" />
{ name_error }
</div>
{ server_error }
</form>
</div>
<div className="modal-footer">
<button type="button" className="btn btn-default" data-dismiss="modal">Close</button>
<button onClick={this.handleSubmit} type="button" className="btn btn-primary">Save</button>
</div>
</div>
</div>
</div>
);
}
});
|