2017-10-30 04:46:48 +03:00
|
|
|
/*
|
|
|
|
Copyright 2017 Travis Ralston
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
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-10-30 04:46:48 +03:00
|
|
|
import SettingsStore from "../../../settings/SettingsStore";
|
|
|
|
import { _t } from '../../../languageHandler';
|
2019-01-24 01:50:41 +03:00
|
|
|
import ToggleSwitch from "./ToggleSwitch";
|
2017-10-30 04:46:48 +03:00
|
|
|
|
2019-09-06 20:35:52 +03:00
|
|
|
module.exports = createReactClass({
|
2017-10-31 05:08:27 +03:00
|
|
|
displayName: 'SettingsFlag',
|
2017-10-30 04:46:48 +03:00
|
|
|
propTypes: {
|
2017-12-26 04:03:18 +03:00
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
level: PropTypes.string.isRequired,
|
|
|
|
roomId: PropTypes.string, // for per-room settings
|
|
|
|
label: PropTypes.string, // untranslated
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
isExplicit: PropTypes.bool,
|
2017-10-30 04:46:48 +03:00
|
|
|
},
|
|
|
|
|
2017-11-05 01:32:13 +03:00
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
value: SettingsStore.getValueAt(
|
|
|
|
this.props.level,
|
|
|
|
this.props.name,
|
|
|
|
this.props.roomId,
|
|
|
|
this.props.isExplicit,
|
|
|
|
),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2019-01-24 01:50:41 +03:00
|
|
|
onChange: function(checked) {
|
|
|
|
if (this.props.group && !checked) return;
|
2017-10-30 04:46:48 +03:00
|
|
|
|
2019-10-10 01:20:59 +03:00
|
|
|
this.save(checked);
|
2019-10-10 00:33:14 +03:00
|
|
|
this.setState({ value: checked });
|
2019-01-24 01:50:41 +03:00
|
|
|
if (this.props.onChange) this.props.onChange(checked);
|
2017-10-30 04:46:48 +03:00
|
|
|
},
|
|
|
|
|
2017-11-05 05:52:42 +03:00
|
|
|
save: function(val = undefined) {
|
2017-11-05 05:15:55 +03:00
|
|
|
return SettingsStore.setValue(
|
|
|
|
this.props.name,
|
|
|
|
this.props.roomId,
|
|
|
|
this.props.level,
|
2017-11-06 01:37:06 +03:00
|
|
|
val !== undefined ? val : this.state.value,
|
2017-11-05 05:15:55 +03:00
|
|
|
);
|
2017-11-05 01:32:13 +03:00
|
|
|
},
|
|
|
|
|
2017-10-30 04:46:48 +03:00
|
|
|
render: function() {
|
2017-10-31 04:49:44 +03:00
|
|
|
const canChange = SettingsStore.canSetValue(this.props.name, this.props.roomId, this.props.level);
|
2017-10-30 04:46:48 +03:00
|
|
|
|
|
|
|
let label = this.props.label;
|
2017-10-30 06:48:29 +03:00
|
|
|
if (!label) label = SettingsStore.getDisplayName(this.props.name, this.props.level);
|
2017-10-30 04:46:48 +03:00
|
|
|
else label = _t(label);
|
|
|
|
|
|
|
|
return (
|
2019-01-24 01:50:41 +03:00
|
|
|
<div className="mx_SettingsFlag">
|
|
|
|
<span className="mx_SettingsFlag_label">{label}</span>
|
2019-10-10 00:27:49 +03:00
|
|
|
<ToggleSwitch checked={this.state.value} onChange={this.onChange} disabled={!canChange} aria-label={label} />
|
2019-01-24 01:50:41 +03:00
|
|
|
</div>
|
2017-10-30 04:46:48 +03:00
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|