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';
|
|
|
|
|
2017-04-10 12:09:26 +03:00
|
|
|
import React from 'react';
|
|
|
|
import * as Roles from '../../../Roles';
|
2017-06-02 12:18:31 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2016-01-18 02:58:38 +03:00
|
|
|
|
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'PowerSelector',
|
|
|
|
|
|
|
|
propTypes: {
|
|
|
|
value: React.PropTypes.number.isRequired,
|
2017-11-13 15:08:53 +03:00
|
|
|
// The maximum value that can be set with the power selector
|
|
|
|
maxValue: React.PropTypes.number.isRequired,
|
|
|
|
|
|
|
|
// Default user power level for the room
|
|
|
|
usersDefault: React.PropTypes.number.isRequired,
|
2016-06-23 13:15:55 +03:00
|
|
|
|
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.
|
2016-06-23 13:15:55 +03:00
|
|
|
//
|
|
|
|
// 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 {
|
2017-11-13 14:57:34 +03:00
|
|
|
levelRoleMap: {},
|
2017-11-13 15:08:53 +03:00
|
|
|
// List of power levels to show in the drop-down
|
|
|
|
options: [],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
|
|
|
maxValue: Infinity,
|
|
|
|
usersDefault: 0,
|
2016-01-18 02:58:38 +03:00
|
|
|
};
|
|
|
|
},
|
2017-10-11 19:56:17 +03:00
|
|
|
|
2017-05-23 17:16:31 +03:00
|
|
|
componentWillMount: function() {
|
2017-11-14 16:06:54 +03:00
|
|
|
this._initStateFromProps(this.props);
|
2017-11-13 15:08:53 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
componentWillReceiveProps: function(newProps) {
|
|
|
|
this._initStateFromProps(newProps);
|
|
|
|
},
|
|
|
|
|
2017-11-14 16:06:54 +03:00
|
|
|
_initStateFromProps: function(newProps) {
|
2017-11-13 14:57:34 +03:00
|
|
|
// This needs to be done now because levelRoleMap has translated strings
|
2017-11-13 15:08:53 +03:00
|
|
|
const levelRoleMap = Roles.levelRoleMap(newProps.usersDefault);
|
|
|
|
const options = Object.keys(levelRoleMap).filter((l) => {
|
|
|
|
return l === undefined || l <= newProps.maxValue;
|
|
|
|
});
|
|
|
|
|
2017-11-13 14:57:34 +03:00
|
|
|
this.setState({
|
|
|
|
levelRoleMap,
|
2017-11-13 15:08:53 +03:00
|
|
|
options,
|
2017-11-14 16:06:54 +03:00
|
|
|
custom: levelRoleMap[newProps.value] === undefined,
|
2017-11-13 14:57:34 +03:00
|
|
|
});
|
2017-05-23 17:16:31 +03:00
|
|
|
},
|
2016-01-18 02:58:38 +03:00
|
|
|
|
|
|
|
onSelectChange: function(event) {
|
2017-11-14 15:02:37 +03:00
|
|
|
this.setState({ custom: event.target.value === "SELECT_VALUE_CUSTOM" });
|
|
|
|
if (event.target.value !== "SELECT_VALUE_CUSTOM") {
|
2017-11-14 16:06:54 +03:00
|
|
|
this.props.onChange(event.target.value);
|
2016-03-15 04:34:49 +03:00
|
|
|
}
|
2016-01-18 02:58:38 +03:00
|
|
|
},
|
|
|
|
|
2016-01-18 04:18:02 +03:00
|
|
|
onCustomBlur: function(event) {
|
2017-11-14 16:06:54 +03:00
|
|
|
this.props.onChange(parseInt(this.refs.custom.value));
|
2016-01-18 02:58:38 +03:00
|
|
|
},
|
|
|
|
|
2016-01-18 04:18:02 +03:00
|
|
|
onCustomKeyDown: function(event) {
|
|
|
|
if (event.key == "Enter") {
|
2017-11-14 16:06:54 +03:00
|
|
|
this.props.onChange(parseInt(this.refs.custom.value));
|
2016-01-18 02:58:38 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
2017-10-11 19:56:17 +03:00
|
|
|
let customPicker;
|
2016-01-18 02:58:38 +03:00
|
|
|
if (this.state.custom) {
|
|
|
|
if (this.props.disabled) {
|
2017-11-14 16:06:54 +03:00
|
|
|
customPicker = <span>{ _t(
|
2017-11-14 15:41:36 +03:00
|
|
|
"Custom of %(powerLevel)s",
|
|
|
|
{ powerLevel: this.props.value },
|
|
|
|
) }</span>;
|
2017-10-11 19:56:17 +03:00
|
|
|
} else {
|
2017-11-14 16:06:54 +03:00
|
|
|
customPicker = <span> = <input
|
2017-11-13 15:08:53 +03:00
|
|
|
ref="custom"
|
|
|
|
type="text"
|
|
|
|
size="3"
|
|
|
|
defaultValue={this.props.value}
|
|
|
|
onBlur={this.onCustomBlur}
|
|
|
|
onKeyDown={this.onCustomKeyDown}
|
2017-11-14 16:06:54 +03:00
|
|
|
/>
|
|
|
|
</span>;
|
2016-01-18 02:58:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-11 19:56:17 +03:00
|
|
|
let selectValue;
|
2016-03-21 03:49:18 +03:00
|
|
|
if (this.state.custom) {
|
2017-11-14 15:02:37 +03:00
|
|
|
selectValue = "SELECT_VALUE_CUSTOM";
|
2017-10-11 19:56:17 +03:00
|
|
|
} else {
|
2017-11-14 16:06:54 +03:00
|
|
|
selectValue = this.state.levelRoleMap[this.props.value] ?
|
2017-11-14 15:02:37 +03:00
|
|
|
this.props.value : "SELECT_VALUE_CUSTOM";
|
2016-03-21 03:49:18 +03:00
|
|
|
}
|
2017-10-11 19:56:17 +03:00
|
|
|
let select;
|
2016-01-18 02:58:38 +03:00
|
|
|
if (this.props.disabled) {
|
2017-11-14 15:02:37 +03:00
|
|
|
select = <span>{ this.state.levelRoleMap[selectValue] }</span>;
|
2017-10-11 19:56:17 +03:00
|
|
|
} else {
|
2017-11-13 14:57:34 +03:00
|
|
|
// Each level must have a definition in this.state.levelRoleMap
|
2017-11-13 15:08:53 +03:00
|
|
|
let options = this.state.options.map((level) => {
|
2017-04-10 12:09:26 +03:00
|
|
|
return {
|
2017-11-14 15:02:37 +03:00
|
|
|
value: level,
|
2017-11-13 15:08:53 +03:00
|
|
|
text: Roles.textualPowerLevel(level, this.props.usersDefault),
|
2017-10-11 19:56:17 +03:00
|
|
|
};
|
2017-04-10 12:09:26 +03:00
|
|
|
});
|
2017-11-14 15:02:37 +03:00
|
|
|
options.push({ value: "SELECT_VALUE_CUSTOM", text: _t("Custom level") });
|
2017-04-10 12:09:26 +03:00
|
|
|
options = options.map((op) => {
|
2017-10-11 19:56:17 +03:00
|
|
|
return <option value={op.value} key={op.value}>{ op.text }</option>;
|
2017-04-10 12:09:26 +03:00
|
|
|
});
|
|
|
|
|
2016-01-18 02:58:38 +03:00
|
|
|
select =
|
2016-03-22 20:17:40 +03:00
|
|
|
<select ref="select"
|
2017-10-11 19:56:17 +03:00
|
|
|
value={this.props.controlled ? selectValue : undefined}
|
|
|
|
defaultValue={!this.props.controlled ? selectValue : undefined}
|
|
|
|
onChange={this.onSelectChange}>
|
2017-04-10 12:09:26 +03:00
|
|
|
{ options }
|
2017-01-20 17:22:27 +03:00
|
|
|
</select>;
|
2016-01-18 02:58:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<span className="mx_PowerSelector">
|
|
|
|
{ select }
|
|
|
|
{ customPicker }
|
|
|
|
</span>
|
|
|
|
);
|
2017-10-11 19:56:17 +03:00
|
|
|
},
|
2016-01-18 02:58:38 +03:00
|
|
|
});
|