element-web/src/components/views/elements/PowerSelector.js

141 lines
4.4 KiB
JavaScript
Raw Normal View History

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';
import React from 'react';
import * as Roles from '../../../Roles';
2016-01-18 02:58:38 +03:00
var LEVEL_ROLE_MAP = {};
2016-01-18 02:58:38 +03:00
var reverseRoles = {};
module.exports = React.createClass({
displayName: 'PowerSelector',
propTypes: {
value: React.PropTypes.number.isRequired,
// 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.
//
// ignored if disabled is truthy. false by default.
controlled: React.PropTypes.bool,
// should the user be able to change the value? false by default.
2016-01-18 02:58:38 +03:00
disabled: React.PropTypes.bool,
onChange: React.PropTypes.func,
},
getInitialState: function() {
return {
custom: (LEVEL_ROLE_MAP[this.props.value] === undefined),
2016-01-18 02:58:38 +03:00
};
},
componentWillMount: function() {
LEVEL_ROLE_MAP = Roles.levelRoleMap();
Object.keys(LEVEL_ROLE_MAP).forEach(function(key) {
reverseRoles[LEVEL_ROLE_MAP[key]] = key;
});
},
2016-01-18 02:58:38 +03:00
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];
2016-01-18 02:58:38 +03:00
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>;
2016-01-18 02:58:38 +03:00
}
else {
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 = LEVEL_ROLE_MAP[this.props.value] || "Custom";
2016-03-21 03:49:18 +03:00
}
2016-01-18 02:58:38 +03:00
var select;
if (this.props.disabled) {
select = <span>{ selectValue }</span>;
}
else {
// Each level must have a definition in LEVEL_ROLE_MAP
const levels = [0, 50, 100];
let options = levels.map((level) => {
return {
value: LEVEL_ROLE_MAP[level],
// Give a userDefault (users_default in the power event) of 0 but
// because level !== undefined, this should never be used.
text: Roles.textualPowerLevel(level, 0),
}
});
options.push({ value: "Custom", text: "Custom level" });
options = options.map((op) => {
2017-06-01 15:31:24 +03:00
return <option value={op.value} key={op.value}>{op.text}</option>;
});
2016-01-18 02:58:38 +03:00
select =
<select ref="select"
value={ this.props.controlled ? selectValue : undefined }
defaultValue={ !this.props.controlled ? selectValue : undefined }
onChange={ this.onSelectChange }>
{ options }
</select>;
2016-01-18 02:58:38 +03:00
}
return (
<span className="mx_PowerSelector">
{ select }
{ customPicker }
</span>
);
}
});