GrantSelector.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. }
  30. componentDidMount() {
  31. this.init();
  32. }
  33. init() {
  34. this.grantSelectorInputEl.value = this.state.pageGrant.grant;
  35. }
  36. onChangeGrant(grant) {
  37. const newValue = this.grantSelectorInputEl.value;
  38. const newGrant = Object.assign(this.state.pageGrant, {grant: newValue});
  39. this.setState({ pageGrant: newGrant });
  40. // dispatch event
  41. this.dispatchOnChange();
  42. }
  43. onClickGrantGroup() {
  44. const newValue = this.groupSelectorInputEl.value;
  45. const newGrant = Object.assign(this.state.pageGrant, { grantGroup: newValue });
  46. this.setState({ pageGrant: newGrant });
  47. // dispatch event
  48. this.dispatchOnChange();
  49. // close group select modal
  50. if (this.state.isModalShown) {
  51. this.setState({ isGroupModalShown: false });
  52. }
  53. }
  54. /**
  55. * dispatch onChange event
  56. */
  57. dispatchOnChange() {
  58. if (this.props.onChange != null) {
  59. this.props.onChange(this.state.pageGrant);
  60. }
  61. }
  62. renderGrantSelector() {
  63. const grantElems = this.availableGrants.map((grant) => {
  64. return <option key={grant} value={grant}>{this.availableGrantLabels[grant]}</option>;
  65. });
  66. const bsClassName = 'form-control-dummy'; // set form-control* to shrink width
  67. return (
  68. <FormGroup controlId="formControlsSelect">
  69. <ControlLabel>Grant:</ControlLabel>
  70. <FormControl componentClass="select" placeholder="select" defaultValue={this.state.pageGrant.grant} bsClass={bsClassName} className="btn-group-sm selectpicker"
  71. onChange={this.onChangeGrant}
  72. inputRef={ el => this.grantSelectorInputEl=el }>
  73. {grantElems}
  74. </FormControl>
  75. </FormGroup>
  76. )
  77. }
  78. renderConfigurationDropdown() {
  79. return (
  80. <FormGroup controlId="formControlsSelect">
  81. <Dropdown dropup id="configurationDropdown" className="configuration-dropdown"
  82. open={this.state.isCddMenuOpened} onToggle={this.onToggleConfigurationDropdown}>
  83. <Dropdown.Toggle bsSize="sm">
  84. <i className="icon-settings"></i>
  85. </Dropdown.Toggle>
  86. <Dropdown.Menu>
  87. {this.renderActiveLineMenuItem()}
  88. {this.renderRealtimeMathJaxMenuItem()}
  89. {/* <MenuItem divider /> */}
  90. </Dropdown.Menu>
  91. </Dropdown>
  92. </FormGroup>
  93. )
  94. }
  95. renderActiveLineMenuItem() {
  96. const isActive = this.state.editorOptions.styleActiveLine;
  97. const iconClasses = ['text-info']
  98. if (isActive) {
  99. iconClasses.push('ti-check')
  100. }
  101. const iconClassName = iconClasses.join(' ');
  102. return (
  103. <MenuItem onClick={this.onClickStyleActiveLine}>
  104. <span className="icon-container"></span>
  105. <span className="menuitem-label">Show active line</span>
  106. <span className="icon-container"><i className={iconClassName}></i></span>
  107. </MenuItem>
  108. )
  109. }
  110. renderRealtimeMathJaxMenuItem() {
  111. if (!this.state.isMathJaxEnabled) {
  112. return;
  113. }
  114. const isEnabled = this.state.isMathJaxEnabled;
  115. const isActive = isEnabled && this.state.previewOptions.renderMathJaxInRealtime;
  116. const iconClasses = ['text-info']
  117. if (isActive) {
  118. iconClasses.push('ti-check')
  119. }
  120. const iconClassName = iconClasses.join(' ');
  121. return (
  122. <MenuItem onClick={this.onClickRenderMathJaxInRealtime}>
  123. <span className="icon-container"><img src="/images/icons/fx.svg" width="14px"></img></span>
  124. <span className="menuitem-label">MathJax Rendering</span>
  125. <i className={iconClassName}></i>
  126. </MenuItem>
  127. )
  128. }
  129. renderSelectGroupModal() {
  130. const userRelatedGroups = this.props.userRelatedGroups;
  131. const groupList = this.userRelatedGroups.map((group) => {
  132. return
  133. <li>
  134. <Button onClick={this.onClickGrantGroup(group)} bsClass="btn btn-sm btn-primary">{group.name}</Button>
  135. </li>
  136. });
  137. return (
  138. <Modal show={this.props.isGroupModalShown} onHide={this.props.cancel} className="select-grant-group">
  139. <Modal.Header closeButton>
  140. <Modal.Title>
  141. Select a Group
  142. </Modal.Title>
  143. </Modal.Header>
  144. <Modal.Body>
  145. <ul class="list-inline">
  146. {groupList}
  147. </ul>
  148. </Modal.Body>
  149. </Modal>
  150. );
  151. }
  152. render() {
  153. return <span>
  154. <span className="m-l-5">{this.renderGrantSelector()}</span>
  155. </span>
  156. }
  157. }
  158. export class PageGrant {
  159. constructor(props) {
  160. this.grant = '';
  161. this.grantGroup = null;
  162. Object.assign(this, props);
  163. }
  164. }
  165. export class UserGroup {
  166. constructor(props) {
  167. this.userGroupId = '';
  168. this.userGroup
  169. Object.assign(this, props);
  170. }
  171. }
  172. GrantSelector.propTypes = {
  173. crowi: PropTypes.object.isRequired,
  174. userRelatedGroups: PropTypes.object,
  175. pageGrant: PropTypes.instanceOf(PageGrant),
  176. onChange: PropTypes.func,
  177. };