GrantSelector.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import FormGroup from 'react-bootstrap/es/FormGroup';
  4. import FormControl from 'react-bootstrap/es/FormControl';
  5. import ControlLabel from 'react-bootstrap/es/ControlLabel';
  6. import Button from 'react-bootstrap/es/Button';
  7. import Dropdown from 'react-bootstrap/es/Dropdown';
  8. import MenuItem from 'react-bootstrap/es/MenuItem';
  9. import Modal from 'react-bootstrap/es/Modal';
  10. import OverlayTrigger from 'react-bootstrap/es/OverlayTrigger';
  11. import Tooltip from 'react-bootstrap/es/Tooltip';
  12. export default class GrantSelector extends React.Component {
  13. constructor(props) {
  14. super(props);
  15. const config = this.props.crowi.getConfig();
  16. this.state = {
  17. pageGrant: this.props.pageGrant,
  18. isGroupModalShown: false,
  19. }
  20. this.availableGrants = [1, 2,/* 3,*/ 4, 5]
  21. this.availableGrantLabels = {
  22. 1: 'Public',
  23. 2: 'Anyone with the linc',
  24. // 3:'Specified users only',
  25. 4: 'Just me',
  26. 5: 'Only inside the group',
  27. }
  28. this.onChangeGrant = this.onChangeGrant.bind(this);
  29. // this.updateElementValue = this.updateElementValue.bind(this);
  30. }
  31. componentDidMount() {
  32. this.init();
  33. }
  34. init() {
  35. this.grantSelectorInputEl.value = this.state.pageGrant.grant;
  36. }
  37. updateElementValue() {
  38. console.log(this.state.pageGrant);
  39. if (this.state.pageGrant != null) {
  40. document.getElementById("page-grant").value = this.state.pageGrant.grant;
  41. if (this.state.pageGrant.grantGroup != null) {
  42. document.getElementById("grant-group").value = this.state.pageGrant.grantGroup.grantGroupId;
  43. }
  44. }
  45. }
  46. onChangeGrant(grant) {
  47. const newValue = this.grantSelectorInputEl.value;
  48. const newGrant = Object.assign(this.state.pageGrant, {grant: newValue});
  49. this.setState({ pageGrant: newGrant });
  50. this.updateElementValue();
  51. // dispatch event
  52. this.dispatchOnChange();
  53. }
  54. onClickGrantGroup() {
  55. const newValue = this.groupSelectorInputEl.value;
  56. const newGrant = Object.assign(this.state.pageGrant, { grantGroup: newValue });
  57. this.setState({ pageGrant: newGrant });
  58. // dispatch event
  59. this.dispatchOnChange();
  60. // close group select modal
  61. if (this.state.isModalShown) {
  62. this.setState({ isGroupModalShown: false });
  63. }
  64. }
  65. /**
  66. * dispatch onChange event
  67. */
  68. dispatchOnChange() {
  69. if (this.props.onChange != null) {
  70. this.props.onChange(this.state.pageGrant);
  71. }
  72. }
  73. renderGrantSelector() {
  74. const grantElems = this.availableGrants.map((grant) => {
  75. return <option key={grant} value={grant}>{this.availableGrantLabels[grant]}</option>;
  76. });
  77. const bsClassName = 'form-control-dummy'; // set form-control* to shrink width
  78. return (
  79. <FormGroup controlId="formControlsSelect">
  80. <ControlLabel>Grant:</ControlLabel>
  81. <FormControl componentClass="select" placeholder="select" defaultValue={this.state.pageGrant.grant} bsClass={bsClassName} className="btn-group-sm selectpicker"
  82. onChange={this.onChangeGrant}
  83. inputRef={ el => this.grantSelectorInputEl=el }>
  84. {grantElems}
  85. </FormControl>
  86. </FormGroup>
  87. )
  88. }
  89. renderSelectGroupModal() {
  90. const userRelatedGroups = this.props.userRelatedGroups;
  91. const groupList = this.userRelatedGroups.map((group) => {
  92. return
  93. <li>
  94. <Button onClick={this.onClickGrantGroup(group)} bsClass="btn btn-sm btn-primary">{group.name}</Button>
  95. </li>
  96. });
  97. return (
  98. <Modal show={this.props.isGroupModalShown} onHide={this.props.cancel} className="select-grant-group">
  99. <Modal.Header closeButton>
  100. <Modal.Title>
  101. Select a Group
  102. </Modal.Title>
  103. </Modal.Header>
  104. <Modal.Body>
  105. <ul class="list-inline">
  106. {groupList}
  107. </ul>
  108. </Modal.Body>
  109. </Modal>
  110. );
  111. }
  112. render() {
  113. return <span>
  114. <span className="m-l-5">{this.renderGrantSelector()}</span>
  115. </span>
  116. }
  117. }
  118. export class PageGrant {
  119. constructor(props) {
  120. this.grant = '';
  121. this.grantGroup = null;
  122. Object.assign(this, props);
  123. }
  124. }
  125. export class UserGroup {
  126. constructor(props) {
  127. this.userGroupId = '';
  128. this.userGroup
  129. Object.assign(this, props);
  130. }
  131. }
  132. GrantSelector.propTypes = {
  133. crowi: PropTypes.object.isRequired,
  134. userRelatedGroups: PropTypes.object,
  135. pageGrant: PropTypes.instanceOf(PageGrant),
  136. onChange: PropTypes.func,
  137. };