2017-11-15 17:35:32 +01:00
|
|
|
/**
|
2019-06-19 11:51:44 +02:00
|
|
|
*
|
|
|
|
* Controller
|
|
|
|
*
|
|
|
|
*/
|
2017-11-15 17:35:32 +01:00
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-11-29 18:51:56 +01:00
|
|
|
import { get, map, some } from 'lodash';
|
|
|
|
import cn from 'classnames';
|
|
|
|
import { FormattedMessage } from 'react-intl';
|
2019-06-19 11:51:44 +02:00
|
|
|
import pluginId from '../../pluginId';
|
2017-11-15 17:35:32 +01:00
|
|
|
|
2019-02-22 11:16:42 +01:00
|
|
|
import InputCheckbox from '../InputCheckboxPlugin';
|
2017-11-15 17:35:32 +01:00
|
|
|
import styles from './styles.scss';
|
|
|
|
|
2017-11-16 15:13:46 +01:00
|
|
|
class Controller extends React.Component {
|
2019-06-19 11:51:44 +02:00
|
|
|
state = { inputSelected: '' };
|
2017-11-16 15:13:46 +01:00
|
|
|
|
2019-06-19 11:51:44 +02:00
|
|
|
setNewInputSelected = name => {
|
|
|
|
this.setState({ inputSelected: name });
|
|
|
|
};
|
2017-11-29 18:51:56 +01:00
|
|
|
|
|
|
|
handleChange = () => {
|
2019-06-19 11:51:44 +02:00
|
|
|
this.context.selectAllActions(
|
|
|
|
`${this.props.inputNamePath}.controllers.${this.props.name}`,
|
|
|
|
!this.areAllActionsSelected()
|
|
|
|
);
|
|
|
|
};
|
2017-11-29 18:51:56 +01:00
|
|
|
|
2019-06-19 11:51:44 +02:00
|
|
|
hasSomeActionsSelected = () => {
|
|
|
|
const { actions } = this.props;
|
|
|
|
|
|
|
|
return Object.keys(actions).some(
|
|
|
|
action => actions[action].enabled === true
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
areAllActionsSelected = () => {
|
|
|
|
const { actions } = this.props;
|
|
|
|
|
|
|
|
return Object.keys(actions).every(
|
|
|
|
action => actions[action].enabled === true
|
|
|
|
);
|
|
|
|
};
|
2017-11-16 15:13:46 +01:00
|
|
|
|
|
|
|
render() {
|
2019-06-19 11:51:44 +02:00
|
|
|
const labelId = this.areAllActionsSelected() ? 'unselectAll' : 'selectAll';
|
|
|
|
|
2017-11-16 15:13:46 +01:00
|
|
|
return (
|
|
|
|
<div className={styles.controller}>
|
|
|
|
<div className={styles.controllerHeader}>
|
2017-11-29 18:51:56 +01:00
|
|
|
<div>{this.props.name}</div>
|
2019-06-19 11:51:44 +02:00
|
|
|
<div className={styles.separator} />
|
2017-11-29 18:51:56 +01:00
|
|
|
<div>
|
|
|
|
<div className={cn(styles.inputCheckbox)}>
|
|
|
|
<div className="form-check">
|
2019-06-19 11:51:44 +02:00
|
|
|
<label
|
|
|
|
className={cn(
|
|
|
|
'form-check-label',
|
|
|
|
styles.label,
|
|
|
|
this.areAllActionsSelected() && styles.checked,
|
|
|
|
!this.areAllActionsSelected() &&
|
|
|
|
this.hasSomeActionsSelected() &&
|
|
|
|
styles.someChecked
|
|
|
|
)}
|
|
|
|
htmlFor={this.props.name}
|
|
|
|
>
|
2017-11-29 18:51:56 +01:00
|
|
|
<input
|
|
|
|
className="form-check-input"
|
2019-06-19 11:51:44 +02:00
|
|
|
checked={this.areAllActionsSelected()}
|
2017-11-29 18:51:56 +01:00
|
|
|
id={this.props.name}
|
|
|
|
name={this.props.name}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
type="checkbox"
|
|
|
|
/>
|
2019-06-19 11:51:44 +02:00
|
|
|
<FormattedMessage id={`${pluginId}.Controller.${labelId}`} />
|
2017-11-29 18:51:56 +01:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2017-11-16 15:13:46 +01:00
|
|
|
</div>
|
|
|
|
<div className="row">
|
2019-06-19 11:51:44 +02:00
|
|
|
{map(Object.keys(this.props.actions).sort(), actionKey => (
|
2017-11-16 15:13:46 +01:00
|
|
|
<InputCheckbox
|
|
|
|
inputSelected={this.state.inputSelected}
|
2017-11-30 15:01:19 +01:00
|
|
|
isOpen={this.props.isOpen}
|
2017-11-16 15:13:46 +01:00
|
|
|
key={actionKey}
|
|
|
|
label={actionKey}
|
2019-06-19 11:51:44 +02:00
|
|
|
name={`${this.props.inputNamePath}.controllers.${
|
|
|
|
this.props.name
|
|
|
|
}.${actionKey}.enabled`}
|
2017-11-16 15:13:46 +01:00
|
|
|
setNewInputSelected={this.setNewInputSelected}
|
|
|
|
value={get(this.props.actions[actionKey], 'enabled')}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</div>
|
2017-11-15 17:35:32 +01:00
|
|
|
</div>
|
2017-11-16 15:13:46 +01:00
|
|
|
);
|
|
|
|
}
|
2017-11-15 17:35:32 +01:00
|
|
|
}
|
|
|
|
|
2017-11-29 18:51:56 +01:00
|
|
|
Controller.contextTypes = {
|
|
|
|
selectAllActions: PropTypes.func.isRequired,
|
|
|
|
};
|
|
|
|
|
2017-11-15 17:35:32 +01:00
|
|
|
Controller.defaultProps = {
|
|
|
|
actions: {},
|
2017-11-16 13:43:55 +01:00
|
|
|
inputNamePath: 'permissions.application',
|
2017-11-15 17:35:32 +01:00
|
|
|
name: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
Controller.propTypes = {
|
|
|
|
actions: PropTypes.object,
|
2017-11-16 13:43:55 +01:00
|
|
|
inputNamePath: PropTypes.string,
|
2017-11-30 15:01:19 +01:00
|
|
|
isOpen: PropTypes.bool.isRequired,
|
2017-11-15 17:35:32 +01:00
|
|
|
name: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Controller;
|