// PermissionsEditor: Component for editing permissions // import React from 'react'; import CheckBox from './checkbox.jsx'; export default class PermissionsEditor extends React.Component { constructor(props) { super(props); this.state = { mode: (props.mode || '').replace('N', '') }; this.handleChange = this.handleChange.bind(this); this.handleSubmit = this.handleSubmit.bind(this); this.handleCancel = this.handleCancel.bind(this); } handleChange(val) { let mode = this.state.mode; let idx = mode.indexOf(val); if (idx == -1) { mode += val; } else { mode = mode.replace(val, ''); } this.setState({mode: mode}); } handleSubmit() { // Normalize string, otherwise cannot check if mode has changed. var mode = (this.state.mode || 'N').split('').sort().join(''); var before = (this.props.mode || 'N').split('').sort().join('') if (mode !== before) { this.props.onSubmit(mode); } else { this.props.onCancel(); } } handleCancel() { this.props.onCancel(); } render() { const all = 'JRWPASDO'; const names = { 'J': 'Join (J)', 'R': 'Read (R)', 'W': 'Write (W)', 'P': 'Get notified (P)', 'A': 'Approve (A)', 'S': 'Share (S)', 'D': 'Delete (D)', 'O': 'Owner (O)' }; let skip = this.props.skip || ''; let mode = this.state.mode; let compare = (this.props.compare || '').replace('N', ''); let items = []; for (let i=0; i= 0 && mode.indexOf(c) < 0) { // Permission is marked as inactive: hide unchecked permissions, disable checked permissions continue; } items.push( {names[c]} {skip.indexOf(c) < 0 ? = 0)} onChange={this.handleChange}/> : = 0)} /> }{this.props.compare ? = 0)}/> : null} ); } return ( {this.props.userTitle ? : null} Permissions {this.props.compare ? {this.props.modeTitle} {this.props.compareTitle} : null} {items} Ok Cancel ); } };