GrantSelector.js 4.5 KB

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