Răsfoiți Sursa

# Improve/346 Disable group select-box when the user does not Choose the grant "Only inside the group"
* Change group select UI to React Component.(not complete)

Tatsuya Ise 8 ani în urmă
părinte
comite
08afa22526

+ 6 - 2
lib/routes/page.js

@@ -9,6 +9,7 @@ module.exports = function(crowi, app) {
     , Revision = crowi.model('Revision')
     , Bookmark = crowi.model('Bookmark')
     , UserGroupRelation = crowi.model('UserGroupRelation')
+    , PageGroupRelation = crowi.model('PageGroupRelation')
     , ApiResponse = require('../util/apiResponse')
     , interceptorManager = crowi.getInterceptorManager()
     , pagePathUtil = require('../util/pagePathUtil')
@@ -236,6 +237,7 @@ module.exports = function(crowi, app) {
       pages: [],
       tree: [],
       userRelatedGroups: [],
+      pageRelatedGroup: null,
     };
 
     var pageTeamplate = 'customlayout-selector/page';
@@ -340,9 +342,11 @@ module.exports = function(crowi, app) {
     .then(function() {
       return UserGroupRelation.findAllRelationForUser(req.user);
     }).then(function (groupRelations) {
-      debug('findPage : relatedGroups ', groupRelations);
       renderVars.userRelatedGroups = groupRelations.map(relation => relation.relatedGroup);
-      debug('findPage : groups ', renderVars.userRelatedGroups);
+
+      return PageGroupRelation.findByPage(renderVars.page);
+    }).then((pageGroupRelation) => {
+      renderVars.pageRelatedGroup = pageGroupRelation.relatedGroup;
 
       return Promise.resolve();
     });

+ 12 - 3
lib/views/_form.html

@@ -46,14 +46,21 @@
       </span>
       {% endif %}
 
-      {% if forceGrant %}
+      <div id="page-grant-selector"></div>
+      <!-- {% if forceGrant %}
       <input type="hidden" name="pageForm[grant]" value="{{ forceGrant }}">
       {% else %}
       <select id="select-grant" name="pageForm[grant]" class="m-r-5 selectpicker btn-group-sm">
         {% for grantId, grantLabel in consts.pageGrants %}
         <option value="{{ grantId }}" {% if pageForm.grant|default(page.grant) == grantId %}selected{% endif %}>{{ t(grantLabel) }}</option>
         {% endfor %}
-        <option value="5" {% if pageForm.grant|default(page.grant) == "5" %}selected{% endif %}>{{ t('Only inside the group') }}</option>
+        {% if user and user.admin && userRelatedGroups %}
+        <option id="no-group" value="/admin/user-groups">{{ t('Only inside the group') }} you have no groups.</option>
+        {% endif %}
+        <option id="group-grant" value="5">{{ t('Only inside the group') }}</option>
+        {% if pageForm.grant|default(page.grant) == "5" && pageRelatedGroup != null %}
+        <option id="group-grant" value="5" selected>{{pageRelatedGroup}}</option>
+        {% endif %}
       </select>
       <input id="select-grant-pre" type="hidden" value="{{ page.grant }}">
       {% endif %}
@@ -66,7 +73,9 @@
           {% endfor %}
         </select>
       </div>
-      {% endif %}
+      {% endif %} -->
+      <input type="hidden" id="page-grant" value="{{ page.grant }}">
+      <input type="hidden" id="user-related-group-data" value="{{userRelatedGroups}}">
       <input type="hidden" id="edit-form-csrf" name="_csrf" value="{{ csrf() }}">
       <button type="submit" class="btn btn-primary btn-submit" id="edit-form-submit">{{ t('Update') }}</button>
     </div>

+ 1 - 3
lib/views/modal/select_grant_group.html

@@ -12,9 +12,7 @@
 
         <ul class="list-inline">
           {% for sGroup in userRelatedGroups %}
-          <li>
-            <button class="btn btn-xs btn-primary" onclick="$('#grant-group').val(sGroup.id)">{{sGroup.name}}</button>
-          </li>
+          <li><button class="btn btn-xs btn-primary" onclick="$('#grant-group').val('{{sGroup.id}}')" data-dismiss="modal">{{sGroup.name}}</button></li>
           {% endfor %}
         </ul>
 

+ 20 - 0
resource/js/app.js

@@ -10,6 +10,7 @@ import SearchPage       from './components/SearchPage';
 import PageEditor       from './components/PageEditor';
 import OptionsSelector  from './components/PageEditor/OptionsSelector';
 import { EditorOptions, PreviewOptions } from './components/PageEditor/OptionsSelector';
+import GrantSelector from './components/PageEditor/GrantSelector';
 import Page             from './components/Page';
 import PageListSearch   from './components/PageListSearch';
 import PageHistory      from './components/PageHistory';
@@ -180,6 +181,25 @@ if (pageEditorOptionsSelectorElem) {
     pageEditorOptionsSelectorElem
   );
 }
+// render GrantSelector
+const pageEditorGrantSelectorElem = document.getElementById('page-grant-selector');
+if (pageEditorGrantSelectorElem) {
+  const userRelatedGroups = window.getElementById("user-related-group-data").value;
+  const pageGrant = window.getElementById("page-grant").value;
+  ReactDOM.render(
+    <GrantSelector crowi={crowi}
+      userRelatedGroups={userRelatedGroups} pageGrant={pageGrant}
+      onChange={(newPageGrant, newPreviewOptions) => { // set onChange event handler
+        // set options
+        pageEditor.setEditorOptions(newEditorOptions);
+        pageEditor.setPreviewOptions(newPreviewOptions);
+        // save
+        crowi.saveEditorOptions(newEditorOptions);
+        crowi.savePreviewOptions(newPreviewOptions);
+      }} />,
+    pageEditorGrantSelectorElem
+  );
+}
 
 // render for admin
 const customCssEditorElem = document.getElementById('custom-css-editor');

+ 217 - 0
resource/js/components/PageEditor/GrantSelector.js

@@ -0,0 +1,217 @@
+import React from 'react';
+import PropTypes from 'prop-types';
+
+import FormGroup from 'react-bootstrap/es/FormGroup';
+import FormControl from 'react-bootstrap/es/FormControl';
+import ControlLabel from 'react-bootstrap/es/ControlLabel';
+import Button from 'react-bootstrap/es/Button';
+
+import Dropdown from 'react-bootstrap/es/Dropdown';
+import MenuItem from 'react-bootstrap/es/MenuItem';
+import Modal from 'react-bootstrap/es/Modal';
+
+import OverlayTrigger  from 'react-bootstrap/es/OverlayTrigger';
+import Tooltip from 'react-bootstrap/es/Tooltip';
+
+export default class GrantSelector extends React.Component {
+
+  constructor(props) {
+    super(props);
+
+    const config = this.props.crowi.getConfig();
+
+    this.state = {
+      pageGrant: this.props.pageGrant || new PageGrant(),
+      isGroupModalShown: false,
+    }
+
+    this.availableGrants = {
+      1:'Public',
+      2:'Anyone with the linc',
+      // 3:'Specified users only',
+      4:'Just me',
+      5:'Only inside the group',
+    }
+    this.onChangeGrant = this.onChangeGrant.bind(this);
+  }
+
+  componentDidMount() {
+    this.init();
+  }
+
+  init() {
+    this.grantSelectorInputEl.value = this.state.editorOptions.theme;
+  }
+
+  onChangeGrant(userGroup) {
+    const newValue = userGroup;
+    const newGrant = Object.assign(this.state.pageGrant, {grant: newValue});
+    this.setState({ pageGrant: newGrant });
+
+    // dispatch event
+    this.dispatchOnChange();
+  }
+
+  onClickGrantGroup() {
+    const newValue = this.groupSelectorInputEl.value;
+    const newGrant = Object.assign(this.state.pageGrant, { grantGroup: newValue });
+    this.setState({ pageGrant: newGrant });
+
+    // dispatch event
+    this.dispatchOnChange();
+    // close group select modal
+    if (this.state.isModalShown) {
+      this.setState({ isGroupModalShown: false });
+    }
+  }
+
+  /**
+   * dispatch onChange event
+   */
+  dispatchOnChange() {
+    if (this.props.onChange != null) {
+      this.props.onChange(this.state.pageGrant);
+    }
+  }
+
+  renderGrantSelector() {
+    const optionElems = this.availableGrants.map((entry) => {
+      return <option key={entry.key} value={entry.key}>{entry.value}</option>;
+    });
+
+    const bsClassName = 'form-control-dummy'; // set form-control* to shrink width
+
+    return (
+      <FormGroup controlId="formControlsSelect">
+        <ControlLabel>Theme:</ControlLabel>
+        <FormControl componentClass="select" placeholder="select" bsClass={bsClassName} className="btn-group-sm selectpicker"
+            onChange={this.onChangeGrant}
+            inputRef={ el => this.grantSelectorInputEl=el }>
+
+          {optionElems}
+
+        </FormControl>
+      </FormGroup>
+    )
+  }
+
+  renderConfigurationDropdown() {
+    return (
+      <FormGroup controlId="formControlsSelect">
+
+        <Dropdown dropup id="configurationDropdown" className="configuration-dropdown"
+            open={this.state.isCddMenuOpened} onToggle={this.onToggleConfigurationDropdown}>
+
+          <Dropdown.Toggle bsSize="sm">
+            <i className="icon-settings"></i>
+          </Dropdown.Toggle>
+
+          <Dropdown.Menu>
+            {this.renderActiveLineMenuItem()}
+            {this.renderRealtimeMathJaxMenuItem()}
+            {/* <MenuItem divider /> */}
+          </Dropdown.Menu>
+
+        </Dropdown>
+
+      </FormGroup>
+    )
+  }
+
+  renderActiveLineMenuItem() {
+    const isActive = this.state.editorOptions.styleActiveLine;
+
+    const iconClasses = ['text-info']
+    if (isActive) {
+      iconClasses.push('ti-check')
+    }
+    const iconClassName = iconClasses.join(' ');
+
+    return (
+      <MenuItem onClick={this.onClickStyleActiveLine}>
+        <span className="icon-container"></span>
+        <span className="menuitem-label">Show active line</span>
+        <span className="icon-container"><i className={iconClassName}></i></span>
+      </MenuItem>
+    )
+  }
+
+  renderRealtimeMathJaxMenuItem() {
+    if (!this.state.isMathJaxEnabled) {
+      return;
+    }
+
+    const isEnabled = this.state.isMathJaxEnabled;
+    const isActive = isEnabled && this.state.previewOptions.renderMathJaxInRealtime;
+
+    const iconClasses = ['text-info']
+    if (isActive) {
+      iconClasses.push('ti-check')
+    }
+    const iconClassName = iconClasses.join(' ');
+
+    return (
+      <MenuItem onClick={this.onClickRenderMathJaxInRealtime}>
+        <span className="icon-container"><img src="/images/icons/fx.svg" width="14px"></img></span>
+        <span className="menuitem-label">MathJax Rendering</span>
+        <i className={iconClassName}></i>
+      </MenuItem>
+    )
+  }
+
+  renderSelectGroupModal() {
+    const userRelatedGroups = this.props.userRelatedGroups;
+    const groupList = this.userRelatedGroups.map((group) => {
+      return
+      <li>
+        <Button onClick={this.onClickGrantGroup(group)} bsClass="btn btn-sm btn-primary">{group.name}</Button>
+      </li>
+    });
+    return (
+      <Modal show={this.props.isGroupModalShown} onHide={this.props.cancel} className="select-grant-group">
+        <Modal.Header closeButton>
+          <Modal.Title>
+            Select a Group
+          </Modal.Title>
+        </Modal.Header>
+        <Modal.Body>
+
+          <ul class="list-inline">
+            {groupList}
+          </ul>
+        </Modal.Body>
+      </Modal>
+    );
+  }
+
+  render() {
+    return <span>
+      <span className="m-l-5">{this.renderThemeSelector()}</span>
+    </span>
+  }
+}
+
+export class PageGrant {
+  constructor(props) {
+    this.grant = '';
+    this.grantGroup = null;
+
+    Object.assign(this, props);
+  }
+}
+
+export class UserGroup {
+  constructor(props) {
+    this.userGroupId = '';
+    this.userGroup
+
+    Object.assign(this, props);
+  }
+}
+
+GrantSelector.propTypes = {
+  crowi: PropTypes.object.isRequired,
+  userRelatedGroups: PropTypes.object,
+  pageGrant: PropTypes.instanceOf(PageGrant),
+  onChange: PropTypes.func,
+};

+ 19 - 3
resource/js/legacy/crowi-form.js

@@ -45,8 +45,8 @@
     $('body').removeClass('on-edit');
   });
 
-  $('#select-grant').on('change', function() {
-    var $selectGrant = $('#select-grant');
+  $('#select-grant > option').on('click', function() {
+    var $selectGrant = $('#select-grant > option');
     var selectGrant = $selectGrant.val();
     console.log(selectGrant);
     if (selectGrant === '5') {
@@ -55,8 +55,24 @@
     }
     else {
       $('#select-grant-pre').val(selectGrant);
+      $('#grant-group').val("");
     }
-    console.log('select-grant-pre : ', $('#select-grant-pre').val());
+  });
+
+  $('#grant-group').on('change', function($event) {
+    var $grantGroup = $('#grant-group');
+    var grantGroup = $grantGroup.val();
+    console.log(grantGroup);
+    if (grantGroup != null && grantGroup != "") {
+      var $groupGrant = $('#group-grant');
+      var selectedGroupElement =
+      $groupGrant.replaceWith
+
+    }
+  });
+
+  $('#no-group').on('click', function() {
+    location.href = "/admin/user-groups";
   });
 
 /**