2016-01-18 02:58:38 +03:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var React = require('react');
|
|
|
|
|
|
|
|
var roles = {
|
|
|
|
0: 'User',
|
|
|
|
50: 'Moderator',
|
|
|
|
100: 'Admin',
|
|
|
|
};
|
|
|
|
|
|
|
|
var reverseRoles = {};
|
|
|
|
Object.keys(roles).forEach(function(key) {
|
|
|
|
reverseRoles[roles[key]] = key;
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'PowerSelector',
|
|
|
|
|
|
|
|
propTypes: {
|
|
|
|
value: React.PropTypes.number.isRequired,
|
2016-03-22 20:17:40 +03:00
|
|
|
// if true, the <select/> should be a 'controlled' form element and updated by React
|
|
|
|
// to reflect the current value, rather than left freeform.
|
|
|
|
// MemberInfo uses controlled; RoomSettings uses non-controlled.
|
|
|
|
controlled: React.PropTypes.bool.isRequired,
|
2016-01-18 02:58:38 +03:00
|
|
|
disabled: React.PropTypes.bool,
|
|
|
|
onChange: React.PropTypes.func,
|
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
custom: (roles[this.props.value] === undefined),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
onSelectChange: function(event) {
|
2016-03-15 04:34:49 +03:00
|
|
|
this.setState({ custom: event.target.value === "Custom" });
|
|
|
|
if (event.target.value !== "Custom") {
|
|
|
|
this.props.onChange(this.getValue());
|
|
|
|
}
|
2016-01-18 02:58:38 +03:00
|
|
|
},
|
|
|
|
|
2016-01-18 04:18:02 +03:00
|
|
|
onCustomBlur: function(event) {
|
2016-01-18 02:58:38 +03:00
|
|
|
this.props.onChange(this.getValue());
|
|
|
|
},
|
|
|
|
|
2016-01-18 04:18:02 +03:00
|
|
|
onCustomKeyDown: function(event) {
|
|
|
|
if (event.key == "Enter") {
|
|
|
|
this.props.onChange(this.getValue());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-01-18 02:58:38 +03:00
|
|
|
getValue: function() {
|
|
|
|
var value;
|
|
|
|
if (this.refs.select) {
|
|
|
|
value = reverseRoles[ this.refs.select.value ];
|
|
|
|
if (this.refs.custom) {
|
|
|
|
if (value === undefined) value = parseInt( this.refs.custom.value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
var customPicker;
|
|
|
|
if (this.state.custom) {
|
|
|
|
var input;
|
|
|
|
if (this.props.disabled) {
|
|
|
|
input = <span>{ this.props.value }</span>
|
|
|
|
}
|
|
|
|
else {
|
2016-01-18 04:18:02 +03:00
|
|
|
input = <input ref="custom" type="text" size="3" defaultValue={ this.props.value } onBlur={ this.onCustomBlur } onKeyDown={ this.onCustomKeyDown }/>
|
2016-01-18 02:58:38 +03:00
|
|
|
}
|
|
|
|
customPicker = <span> of { input }</span>;
|
|
|
|
}
|
|
|
|
|
2016-03-21 03:49:18 +03:00
|
|
|
var selectValue;
|
|
|
|
if (this.state.custom) {
|
|
|
|
selectValue = "Custom";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
selectValue = roles[this.props.value] || "Custom";
|
|
|
|
}
|
2016-01-18 02:58:38 +03:00
|
|
|
var select;
|
|
|
|
if (this.props.disabled) {
|
|
|
|
select = <span>{ selectValue }</span>;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
select =
|
2016-03-22 20:17:40 +03:00
|
|
|
<select ref="select"
|
|
|
|
value={ this.props.controlled ? selectValue : undefined }
|
|
|
|
defaultValue={ !this.props.controlled ? selectValue : undefined }
|
|
|
|
onChange={ this.onSelectChange }>
|
2016-01-18 02:58:38 +03:00
|
|
|
<option value="User">User (0)</option>
|
|
|
|
<option value="Moderator">Moderator (50)</option>
|
|
|
|
<option value="Admin">Admin (100)</option>
|
|
|
|
<option value="Custom">Custom level</option>
|
|
|
|
</select>
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<span className="mx_PowerSelector">
|
|
|
|
{ select }
|
|
|
|
{ customPicker }
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|