GrantSelector.js 8.7 KB

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