BigW Consortium Gitlab

members.js.es6 2.17 KB
Newer Older
1 2 3
/* eslint-disable class-methods-use-this */
(() => {
  window.gl = window.gl || {};
Phil Hughes committed
4

5
  class Members {
Phil Hughes committed
6 7
    constructor() {
      this.addListeners();
8
      this.initGLDropdown();
Phil Hughes committed
9 10 11
    }

    addListeners() {
12
      $('.project_member, .group_member').off('ajax:success').on('ajax:success', this.removeRow);
13 14
      $('.js-member-update-control').off('change').on('change', this.formSubmit.bind(this));
      $('.js-edit-member-form').off('ajax:success').on('ajax:success', this.formSuccess.bind(this));
15
      gl.utils.disableButtonIfEmptyField('#user_ids', 'input[name=commit]', 'change');
Phil Hughes committed
16 17
    }

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
    initGLDropdown() {
      $('.js-member-permissions-dropdown').each((i, btn) => {
        const $btn = $(btn);

        $btn.glDropdown({
          selectable: true,
          isSelectable(selected, $el) {
            return !$el.hasClass('is-active');
          },
          fieldName: $btn.data('field-name'),
          id(selected, $el) {
            return $el.data('id');
          },
          toggleLabel(selected, $el) {
            return $el.text();
          },
34 35
          clicked: (selected, $link) => {
            this.formSubmit(null, $link);
36 37 38 39 40
          },
        });
      });
    }

Phil Hughes committed
41 42 43 44
    removeRow(e) {
      const $target = $(e.target);

      if ($target.hasClass('btn-remove')) {
45
        $target.closest('.member')
46
          .fadeOut(function fadeOutMemberRow() {
47 48
            $(this).remove();
          });
Phil Hughes committed
49 50 51
      }
    }

52 53
    formSubmit(e, $el = null) {
      const $this = e ? $(e.currentTarget) : $el;
54 55 56 57
      const { $toggle, $dateInput } = this.getMemberListItems($this);

      $this.closest('form').trigger('submit.rails');

58 59
      $toggle.disable();
      $dateInput.disable();
Phil Hughes committed
60 61
    }

62 63 64
    formSuccess(e) {
      const { $toggle, $dateInput } = this.getMemberListItems($(e.currentTarget).closest('.member'));

65 66
      $toggle.enable();
      $dateInput.enable();
67 68 69 70 71 72 73 74 75 76
    }

    getMemberListItems($el) {
      const $memberListItem = $el.is('.member') ? $el : $(`#${$el.data('el-id')}`);

      return {
        $memberListItem,
        $toggle: $memberListItem.find('.dropdown-menu-toggle'),
        $dateInput: $memberListItem.find('.js-access-expiration-date'),
      };
Phil Hughes committed
77 78 79
    }
  }

80
  gl.Members = Members;
81
})();