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.
|
|
|
|
*/
|
|
|
|
|
2017-04-10 12:09:26 +03:00
|
|
|
import React from 'react';
|
2017-12-26 04:03:18 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2019-09-06 20:35:52 +03:00
|
|
|
import createReactClass from 'create-react-class';
|
2017-04-10 12:09:26 +03:00
|
|
|
import * as Roles from '../../../Roles';
|
2017-06-02 12:18:31 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2019-02-07 21:31:35 +03:00
|
|
|
import Field from "./Field";
|
2019-10-09 21:59:11 +03:00
|
|
|
import {Key} from "../../../Keyboard";
|
2016-01-18 02:58:38 +03:00
|
|
|
|
2019-09-06 20:35:52 +03:00
|
|
|
module.exports = createReactClass({
|
2016-01-18 02:58:38 +03:00
|
|
|
displayName: 'PowerSelector',
|
|
|
|
|
|
|
|
propTypes: {
|
2017-12-26 04:03:18 +03:00
|
|
|
value: PropTypes.number.isRequired,
|
2017-11-13 15:08:53 +03:00
|
|
|
// The maximum value that can be set with the power selector
|
2017-12-26 04:03:18 +03:00
|
|
|
maxValue: PropTypes.number.isRequired,
|
2017-11-13 15:08:53 +03:00
|
|
|
|
|
|
|
// Default user power level for the room
|
2017-12-26 04:03:18 +03:00
|
|
|
usersDefault: PropTypes.number.isRequired,
|
2016-06-23 13:15:55 +03:00
|
|
|
|
|
|
|
// should the user be able to change the value? false by default.
|
2017-12-26 04:03:18 +03:00
|
|
|
disabled: PropTypes.bool,
|
|
|
|
onChange: PropTypes.func,
|
2018-02-28 19:15:20 +03:00
|
|
|
|
|
|
|
// Optional key to pass as the second argument to `onChange`
|
|
|
|
powerLevelKey: PropTypes.string,
|
2019-02-07 21:31:35 +03:00
|
|
|
|
|
|
|
// The name to annotate the selector with
|
|
|
|
label: PropTypes.string,
|
2016-01-18 02:58:38 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
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: [],
|
2019-02-07 21:31:35 +03:00
|
|
|
|
|
|
|
customValue: this.props.value,
|
|
|
|
selectValue: 0,
|
2017-11-13 15:08:53 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
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);
|
2019-04-04 12:30:23 +03:00
|
|
|
const options = Object.keys(levelRoleMap).filter(level => {
|
|
|
|
return (
|
|
|
|
level === undefined ||
|
|
|
|
level <= newProps.maxValue ||
|
|
|
|
level == newProps.value
|
|
|
|
);
|
2017-11-13 15:08:53 +03:00
|
|
|
});
|
|
|
|
|
2019-02-07 21:31:35 +03:00
|
|
|
const isCustom = levelRoleMap[newProps.value] === undefined;
|
|
|
|
|
2017-11-13 14:57:34 +03:00
|
|
|
this.setState({
|
|
|
|
levelRoleMap,
|
2017-11-13 15:08:53 +03:00
|
|
|
options,
|
2019-02-07 21:31:35 +03:00
|
|
|
custom: isCustom,
|
|
|
|
customLevel: newProps.value,
|
|
|
|
selectValue: isCustom ? "SELECT_VALUE_CUSTOM" : newProps.value,
|
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) {
|
2019-02-07 21:31:35 +03:00
|
|
|
const isCustom = event.target.value === "SELECT_VALUE_CUSTOM";
|
|
|
|
if (isCustom) {
|
|
|
|
this.setState({custom: true});
|
|
|
|
} else {
|
2018-02-28 19:15:20 +03:00
|
|
|
this.props.onChange(event.target.value, this.props.powerLevelKey);
|
2019-02-07 21:31:35 +03:00
|
|
|
this.setState({selectValue: event.target.value});
|
2016-03-15 04:34:49 +03:00
|
|
|
}
|
2016-01-18 02:58:38 +03:00
|
|
|
},
|
|
|
|
|
2019-02-07 21:31:35 +03:00
|
|
|
onCustomChange: function(event) {
|
|
|
|
this.setState({customValue: event.target.value});
|
|
|
|
},
|
|
|
|
|
2016-01-18 04:18:02 +03:00
|
|
|
onCustomBlur: function(event) {
|
2019-03-04 23:08:54 +03:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
|
2019-02-07 21:31:35 +03:00
|
|
|
this.props.onChange(parseInt(this.state.customValue), this.props.powerLevelKey);
|
2016-01-18 02:58:38 +03:00
|
|
|
},
|
|
|
|
|
2019-07-17 18:23:19 +03:00
|
|
|
onCustomKeyDown: function(event) {
|
2019-10-09 21:59:11 +03:00
|
|
|
if (event.key === Key.ENTER) {
|
2019-03-04 23:08:54 +03:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
// Do not call the onChange handler directly here - it can cause an infinite loop.
|
|
|
|
// Long story short, a user hits Enter to submit the value which onChange handles as
|
|
|
|
// raising a dialog which causes a blur which causes a dialog which causes a blur and
|
|
|
|
// so on. By not causing the onChange to be called here, we avoid the loop because we
|
|
|
|
// handle the onBlur safely.
|
|
|
|
event.target.blur();
|
2016-01-18 02:58:38 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
2019-02-07 21:31:35 +03:00
|
|
|
let picker;
|
2016-03-21 03:49:18 +03:00
|
|
|
if (this.state.custom) {
|
2019-02-07 21:31:35 +03:00
|
|
|
picker = (
|
|
|
|
<Field id={`powerSelector_custom_${this.props.powerLevelKey}`} type="number"
|
|
|
|
label={this.props.label || _t("Power level")} max={this.props.maxValue}
|
2019-07-17 18:23:19 +03:00
|
|
|
onBlur={this.onCustomBlur} onKeyDown={this.onCustomKeyDown} onChange={this.onCustomChange}
|
2019-04-03 13:52:02 +03:00
|
|
|
value={String(this.state.customValue)} disabled={this.props.disabled} />
|
2019-02-07 21:31:35 +03:00
|
|
|
);
|
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
|
|
|
});
|
|
|
|
|
2019-02-07 21:31:35 +03:00
|
|
|
picker = (
|
|
|
|
<Field id={`powerSelector_notCustom_${this.props.powerLevelKey}`} element="select"
|
|
|
|
label={this.props.label || _t("Power level")} onChange={this.onSelectChange}
|
2019-04-03 13:52:02 +03:00
|
|
|
value={String(this.state.selectValue)} disabled={this.props.disabled}>
|
2019-02-07 21:31:35 +03:00
|
|
|
{options}
|
|
|
|
</Field>
|
|
|
|
);
|
2016-01-18 02:58:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2019-02-07 21:31:35 +03:00
|
|
|
<div className="mx_PowerSelector">
|
|
|
|
{ picker }
|
|
|
|
</div>
|
2016-01-18 02:58:38 +03:00
|
|
|
);
|
2017-10-11 19:56:17 +03:00
|
|
|
},
|
2016-01-18 02:58:38 +03:00
|
|
|
});
|