GrantSelector.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { translate } from 'react-i18next';
  4. import * as entities from 'entities';
  5. import FormGroup from 'react-bootstrap/es/FormGroup';
  6. import FormControl from 'react-bootstrap/es/FormControl';
  7. import ListGroup from 'react-bootstrap/es/ListGroup';
  8. import ListGroupItem from 'react-bootstrap/es/ListGroupItem';
  9. import Modal from 'react-bootstrap/es/Modal';
  10. const SPECIFIED_GROUP_VALUE = 'specifiedGroup';
  11. /**
  12. * Page grant select component
  13. *
  14. * @export
  15. * @class GrantSelector
  16. * @extends {React.Component}
  17. */
  18. class GrantSelector extends React.Component {
  19. constructor(props) {
  20. super(props);
  21. this.availableGrants = [
  22. { pageGrant: 1, iconClass: 'icon-people', styleClass: '', label: 'Public' },
  23. { pageGrant: 2, iconClass: 'icon-link', styleClass: 'text-info', label: 'Anyone with the link' },
  24. // { pageGrant: 3, iconClass: '', label: 'Specified users only' },
  25. { pageGrant: 4, iconClass: 'icon-lock', styleClass: 'text-danger', label: 'Just me' },
  26. { pageGrant: 5, iconClass: 'icon-options', styleClass: '', label: 'Only inside the group' }, // appeared only one of these 'pageGrant: 5'
  27. { pageGrant: 5, iconClass: 'icon-options', styleClass: '', label: 'Reselect the group' }, // appeared only one of these 'pageGrant: 5'
  28. ];
  29. this.state = {
  30. pageGrant: this.props.pageGrant || 1, // default: 1
  31. userRelatedGroups: [],
  32. isSelectGroupModalShown: false,
  33. };
  34. if (this.props.pageGrantGroupId !== '') {
  35. this.state.pageGrantGroup = {
  36. _id: this.props.pageGrantGroupId,
  37. name: this.props.pageGrantGroupName
  38. };
  39. }
  40. this.showSelectGroupModal = this.showSelectGroupModal.bind(this);
  41. this.hideSelectGroupModal = this.hideSelectGroupModal.bind(this);
  42. this.getGroupName = this.getGroupName.bind(this);
  43. this.changeGrantHandler = this.changeGrantHandler.bind(this);
  44. this.groupListItemClickHandler = this.groupListItemClickHandler.bind(this);
  45. }
  46. componentDidUpdate(prevProps, prevState) {
  47. /*
  48. * set SPECIFIED_GROUP_VALUE to grant selector
  49. * cz: bootstrap-select input element has the defferent state to React component
  50. */
  51. if (this.state.pageGrantGroup != null) {
  52. this.grantSelectorInputEl.value = SPECIFIED_GROUP_VALUE;
  53. }
  54. // refresh bootstrap-select
  55. // see https://silviomoreto.github.io/bootstrap-select/methods/#selectpickerrefresh
  56. $('.page-grant-selector.selectpicker').selectpicker('refresh');
  57. //// DIRTY HACK -- 2018.05.25 Yuki Takei
  58. // set group name to the bootstrap-select options
  59. // cz: .selectpicker('refresh') doesn't replace data-content
  60. $('.page-grant-selector .group-name').text(this.getGroupName());
  61. }
  62. showSelectGroupModal() {
  63. this.retrieveUserGroupRelations();
  64. this.setState({ isSelectGroupModalShown: true });
  65. }
  66. hideSelectGroupModal() {
  67. this.setState({ isSelectGroupModalShown: false });
  68. }
  69. getGroupName() {
  70. const pageGrantGroup = this.state.pageGrantGroup;
  71. return pageGrantGroup ? entities.encodeHTML(pageGrantGroup.name) : '';
  72. }
  73. /**
  74. * Retrieve user-group-relations data from backend
  75. */
  76. retrieveUserGroupRelations() {
  77. this.props.crowi.apiGet('/me/user-group-relations')
  78. .then(res => {
  79. return res.userGroupRelations;
  80. })
  81. .then(userGroupRelations => {
  82. const userRelatedGroups = userGroupRelations.map(relation => {
  83. return relation.relatedGroup;
  84. });
  85. this.setState({userRelatedGroups});
  86. });
  87. }
  88. /**
  89. * change event handler for pageGrant selector
  90. */
  91. changeGrantHandler() {
  92. const pageGrant = +this.grantSelectorInputEl.value;
  93. // select group
  94. if (pageGrant === 5) {
  95. this.showSelectGroupModal();
  96. /*
  97. * reset grant selector to state
  98. */
  99. this.grantSelectorInputEl.value = this.state.pageGrant;
  100. return;
  101. }
  102. this.setState({ pageGrant, pageGrantGroup: null });
  103. // dispatch event
  104. this.dispatchOnChangePageGrant(pageGrant);
  105. this.dispatchOnDeterminePageGrantGroup(null);
  106. }
  107. groupListItemClickHandler(pageGrantGroup) {
  108. this.setState({ pageGrant: 5, pageGrantGroup });
  109. // dispatch event
  110. this.dispatchOnChangePageGrant(5);
  111. this.dispatchOnDeterminePageGrantGroup(pageGrantGroup);
  112. // hide modal
  113. this.hideSelectGroupModal();
  114. }
  115. dispatchOnChangePageGrant(pageGrant) {
  116. if (this.props.onChangePageGrant != null) {
  117. this.props.onChangePageGrant(pageGrant);
  118. }
  119. }
  120. dispatchOnDeterminePageGrantGroup(pageGrantGroup) {
  121. if (this.props.onDeterminePageGrantGroupId != null) {
  122. this.props.onDeterminePageGrantGroupId(pageGrantGroup ? pageGrantGroup._id : '');
  123. }
  124. if (this.props.onDeterminePageGrantGroupName != null) {
  125. this.props.onDeterminePageGrantGroupName(pageGrantGroup ? pageGrantGroup.name : '');
  126. }
  127. }
  128. /**
  129. * Render grant selector DOM.
  130. * @returns
  131. * @memberof GrantSelector
  132. */
  133. renderGrantSelector() {
  134. const { t } = this.props;
  135. let index = 0;
  136. let selectedValue = this.state.pageGrant;
  137. const grantElems = this.availableGrants.map((grant) => {
  138. const dataContent = `<i class="icon icon-fw ${grant.iconClass} ${grant.styleClass}"></i> <span class="${grant.styleClass}">${t(grant.label)}</span>`;
  139. return <option key={index++} value={grant.pageGrant} data-content={dataContent}>{t(grant.label)}</option>;
  140. });
  141. const pageGrantGroup = this.state.pageGrantGroup;
  142. if (pageGrantGroup != null) {
  143. selectedValue = SPECIFIED_GROUP_VALUE;
  144. // DIRTY HACK -- 2018.05.25 Yuki Takei
  145. // remove 'Only inside the group' item
  146. // cz: .selectpicker('refresh') doesn't replace data-content
  147. grantElems.splice(3, 1);
  148. }
  149. else {
  150. // DIRTY HACK -- 2018.05.25 Yuki Takei
  151. // remove 'Reselect the group' item
  152. // cz: .selectpicker('refresh') doesn't replace data-content
  153. grantElems.splice(4, 1);
  154. }
  155. /*
  156. * react-bootstrap couldn't be rendered only with React feature.
  157. * see also 'componentDidUpdate'
  158. */
  159. // add specified group option
  160. grantElems.push(
  161. <option ref="specifiedGroupOption" key="specifiedGroupKey" value={SPECIFIED_GROUP_VALUE} style={{ display: pageGrantGroup ? 'inherit' : 'none' }}
  162. data-content={`<i class="icon icon-fw icon-organization text-success"></i> <span class="group-name text-success">${this.getGroupName()}</span>`}>
  163. {this.getGroupName()}
  164. </option>
  165. );
  166. const bsClassName = 'form-control-dummy'; // set form-control* to shrink width
  167. return (
  168. <FormGroup className="m-b-0">
  169. <FormControl componentClass="select" placeholder="select" defaultValue={selectedValue} bsClass={bsClassName} className="btn-group-sm page-grant-selector selectpicker"
  170. onChange={this.changeGrantHandler}
  171. inputRef={ el => this.grantSelectorInputEl=el }>
  172. {grantElems}
  173. </FormControl>
  174. </FormGroup>
  175. );
  176. }
  177. /**
  178. * Render select grantgroup modal.
  179. *
  180. * @returns
  181. * @memberof GrantSelector
  182. */
  183. renderSelectGroupModal() {
  184. const generateGroupListItems = () => {
  185. return this.state.userRelatedGroups.map((group) => {
  186. return <ListGroupItem key={group._id} header={group.name} onClick={() => { this.groupListItemClickHandler(group) }}>
  187. (TBD) List group members
  188. </ListGroupItem>;
  189. });
  190. };
  191. let content = this.state.userRelatedGroups.length === 0
  192. ? <div>
  193. <h4>There is no group to which you belong.</h4>
  194. { this.props.crowi.isAdmin &&
  195. <p><a href="/admin/user-groups"><i className="icon icon-fw icon-login"></i> Manage Groups</a></p>
  196. }
  197. </div>
  198. : <ListGroup>
  199. {generateGroupListItems()}
  200. </ListGroup>;
  201. return (
  202. <Modal className="select-grant-group"
  203. container={this} show={this.state.isSelectGroupModalShown} onHide={this.hideSelectGroupModal}
  204. >
  205. <Modal.Header closeButton>
  206. <Modal.Title>
  207. Select a Group
  208. </Modal.Title>
  209. </Modal.Header>
  210. <Modal.Body>
  211. {content}
  212. </Modal.Body>
  213. </Modal>
  214. );
  215. }
  216. render() {
  217. return <React.Fragment>
  218. <div className="m-r-5">{this.renderGrantSelector()}</div>
  219. {this.renderSelectGroupModal()}
  220. </React.Fragment>;
  221. }
  222. }
  223. GrantSelector.propTypes = {
  224. t: PropTypes.func.isRequired, // i18next
  225. crowi: PropTypes.object.isRequired,
  226. isGroupModalShown: PropTypes.bool,
  227. pageGrant: PropTypes.number,
  228. pageGrantGroupId: PropTypes.string,
  229. pageGrantGroupName: PropTypes.string,
  230. onChangePageGrant: PropTypes.func,
  231. onDeterminePageGrantGroupId: PropTypes.func,
  232. onDeterminePageGrantGroupName: PropTypes.func,
  233. };
  234. export default translate()(GrantSelector);