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";
|
|
|
|
import SettingsStore from "../../../settings/SettingsStore";
|
|
|
|
import { _t } from '../../../languageHandler';
|
|
|
|
|
|
|
|
module.exports = React.createClass({
|
2017-10-31 05:08:27 +03:00
|
|
|
displayName: 'SettingsFlag',
|
2017-10-30 04:46:48 +03:00
|
|
|
propTypes: {
|
|
|
|
name: React.PropTypes.string.isRequired,
|
|
|
|
level: React.PropTypes.string.isRequired,
|
|
|
|
roomId: React.PropTypes.string, // for per-room settings
|
|
|
|
label: React.PropTypes.string, // untranslated
|
|
|
|
onChange: React.PropTypes.func,
|
2017-10-31 04:49:44 +03:00
|
|
|
isExplicit: React.PropTypes.bool,
|
2017-11-05 01:32:13 +03:00
|
|
|
manualSave: React.PropTypes.bool,
|
2017-10-30 04:46:48 +03:00
|
|
|
|
|
|
|
// If group is supplied, then this will create a radio button instead.
|
|
|
|
group: React.PropTypes.string,
|
|
|
|
value: React.PropTypes.any, // the value for the radio button
|
|
|
|
},
|
|
|
|
|
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,
|
|
|
|
),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2017-10-30 04:46:48 +03:00
|
|
|
onChange: function(e) {
|
|
|
|
if (this.props.group && !e.target.checked) return;
|
|
|
|
|
|
|
|
const newState = this.props.group ? this.props.value : e.target.checked;
|
2017-11-05 01:32:13 +03:00
|
|
|
if (!this.props.manualSave) this.save(newState);
|
|
|
|
else this.setState({ value: newState });
|
2017-10-30 04:46:48 +03:00
|
|
|
if (this.props.onChange) this.props.onChange(newState);
|
|
|
|
},
|
|
|
|
|
2017-11-05 01:32:13 +03:00
|
|
|
save: function(val = null) {
|
2017-11-05 05:15:55 +03:00
|
|
|
return SettingsStore.setValue(
|
|
|
|
this.props.name,
|
|
|
|
this.props.roomId,
|
|
|
|
this.props.level,
|
|
|
|
val ? val : this.state.value
|
|
|
|
);
|
2017-11-05 01:32:13 +03:00
|
|
|
},
|
|
|
|
|
2017-10-30 04:46:48 +03:00
|
|
|
render: function() {
|
2017-11-05 01:32:13 +03:00
|
|
|
const value = this.props.manualSave ? this.state.value : SettingsStore.getValueAt(
|
|
|
|
this.props.level,
|
|
|
|
this.props.name,
|
|
|
|
this.props.roomId,
|
|
|
|
this.props.isExplicit,
|
|
|
|
);
|
|
|
|
|
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);
|
|
|
|
|
2017-10-30 07:16:46 +03:00
|
|
|
// We generate a relatively complex ID to avoid conflicts
|
|
|
|
const id = this.props.name + "_" + this.props.group + "_" + this.props.value + "_" + this.props.level;
|
2017-10-30 04:46:48 +03:00
|
|
|
let checkbox = (
|
2017-10-30 07:16:46 +03:00
|
|
|
<input id={id}
|
2017-10-30 04:46:48 +03:00
|
|
|
type="checkbox"
|
2017-11-05 01:32:13 +03:00
|
|
|
defaultChecked={value}
|
2017-10-30 04:46:48 +03:00
|
|
|
onChange={this.onChange}
|
2017-10-31 04:49:44 +03:00
|
|
|
disabled={!canChange}
|
2017-10-30 04:46:48 +03:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
if (this.props.group) {
|
|
|
|
checkbox = (
|
|
|
|
<input id={id}
|
|
|
|
type="radio"
|
|
|
|
name={this.props.group}
|
|
|
|
value={this.props.value}
|
2017-11-05 01:32:13 +03:00
|
|
|
checked={value === this.props.value}
|
2017-10-30 04:46:48 +03:00
|
|
|
onChange={this.onChange}
|
2017-10-31 04:49:44 +03:00
|
|
|
disabled={!canChange}
|
2017-10-30 07:53:59 +03:00
|
|
|
/>
|
2017-10-30 04:46:48 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2017-10-30 07:17:15 +03:00
|
|
|
<label>
|
2017-10-30 04:46:48 +03:00
|
|
|
{ checkbox }
|
2017-10-30 07:17:15 +03:00
|
|
|
{ label }
|
|
|
|
</label>
|
2017-10-30 04:46:48 +03:00
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|