2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2020-09-21 11:09:44 -04:00
|
|
|
import { Rails } from '~/lib/utils/rails_ujs';
|
2020-07-15 23:09:12 -04:00
|
|
|
import { disableButtonIfEmptyField } from '~/lib/utils/common_utils';
|
2020-08-24 14:10:19 -04:00
|
|
|
import initDeprecatedJQueryDropdown from '~/deprecated_jquery_dropdown';
|
2020-09-30 08:09:53 -04:00
|
|
|
import { __, sprintf } from '~/locale';
|
2018-03-09 15:18:59 -05:00
|
|
|
|
2017-10-12 05:42:13 -04:00
|
|
|
export default class Members {
|
|
|
|
constructor() {
|
|
|
|
this.addListeners();
|
|
|
|
this.initGLDropdown();
|
|
|
|
}
|
2016-08-31 10:28:58 -04:00
|
|
|
|
2017-10-12 05:42:13 -04:00
|
|
|
addListeners() {
|
2018-10-30 16:28:31 -04:00
|
|
|
$('.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));
|
2020-07-15 23:09:12 -04:00
|
|
|
disableButtonIfEmptyField('#user_ids', 'input[name=commit]', 'change');
|
2017-10-12 05:42:13 -04:00
|
|
|
}
|
2016-08-31 10:28:58 -04:00
|
|
|
|
2019-02-27 05:09:52 -05:00
|
|
|
dropdownClicked(options) {
|
2019-08-15 02:27:55 -04:00
|
|
|
options.e.preventDefault();
|
|
|
|
|
2019-02-27 05:09:52 -05:00
|
|
|
this.formSubmit(null, options.$el);
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
dropdownToggleLabel(selected, $el) {
|
|
|
|
return $el.text();
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
dropdownIsSelectable(selected, $el) {
|
|
|
|
return !$el.hasClass('is-active');
|
|
|
|
}
|
|
|
|
|
2017-10-12 05:42:13 -04:00
|
|
|
initGLDropdown() {
|
|
|
|
$('.js-member-permissions-dropdown').each((i, btn) => {
|
|
|
|
const $btn = $(btn);
|
2016-12-07 10:27:14 -05:00
|
|
|
|
2020-08-24 14:10:19 -04:00
|
|
|
initDeprecatedJQueryDropdown($btn, {
|
2017-10-12 05:42:13 -04:00
|
|
|
selectable: true,
|
2019-02-27 05:09:52 -05:00
|
|
|
isSelectable: (selected, $el) => this.dropdownIsSelectable(selected, $el),
|
2018-02-20 17:20:48 -05:00
|
|
|
fieldName: $btn.data('fieldName'),
|
2017-10-12 05:42:13 -04:00
|
|
|
id(selected, $el) {
|
|
|
|
return $el.data('id');
|
|
|
|
},
|
2019-02-27 05:09:52 -05:00
|
|
|
toggleLabel: (selected, $el) => this.dropdownToggleLabel(selected, $el, $btn),
|
|
|
|
clicked: options => this.dropdownClicked(options),
|
2016-12-07 10:27:14 -05:00
|
|
|
});
|
2017-10-12 05:42:13 -04:00
|
|
|
});
|
|
|
|
}
|
2016-08-31 10:28:58 -04:00
|
|
|
|
2017-10-12 05:42:13 -04:00
|
|
|
formSubmit(e, $el = null) {
|
|
|
|
const $this = e ? $(e.currentTarget) : $el;
|
|
|
|
const { $toggle, $dateInput } = this.getMemberListItems($this);
|
2020-09-21 11:09:44 -04:00
|
|
|
const formEl = $this.closest('form').get(0);
|
2016-09-05 10:11:45 -04:00
|
|
|
|
2020-09-21 11:09:44 -04:00
|
|
|
Rails.fire(formEl, 'submit');
|
2016-12-07 10:27:14 -05:00
|
|
|
|
2017-10-12 05:42:13 -04:00
|
|
|
$toggle.disable();
|
|
|
|
$dateInput.disable();
|
|
|
|
}
|
2016-12-07 10:27:14 -05:00
|
|
|
|
2017-10-12 05:42:13 -04:00
|
|
|
formSuccess(e) {
|
2020-09-30 08:09:53 -04:00
|
|
|
const { $toggle, $dateInput, $expiresIn, $expiresInText } = this.getMemberListItems(
|
|
|
|
$(e.currentTarget).closest('.js-member'),
|
|
|
|
);
|
|
|
|
|
|
|
|
const [data] = e.detail;
|
|
|
|
const expiresIn = data?.expires_in;
|
|
|
|
|
|
|
|
if (expiresIn) {
|
|
|
|
$expiresIn.removeClass('gl-display-none');
|
|
|
|
|
|
|
|
$expiresInText.text(sprintf(__('Expires in %{expires_at}'), { expires_at: expiresIn }));
|
|
|
|
|
2020-10-06 14:08:49 -04:00
|
|
|
const { expires_soon: expiresSoon, expires_at_formatted: expiresAtFormatted } = data;
|
2020-09-30 08:09:53 -04:00
|
|
|
|
|
|
|
if (expiresSoon) {
|
|
|
|
$expiresInText.addClass('text-warning');
|
|
|
|
} else {
|
|
|
|
$expiresInText.removeClass('text-warning');
|
|
|
|
}
|
2020-10-06 14:08:49 -04:00
|
|
|
|
|
|
|
// Update tooltip
|
|
|
|
if (expiresAtFormatted) {
|
|
|
|
$expiresInText.attr('title', expiresAtFormatted);
|
|
|
|
$expiresInText.attr('data-original-title', expiresAtFormatted);
|
|
|
|
}
|
2020-09-30 08:09:53 -04:00
|
|
|
} else {
|
|
|
|
$expiresIn.addClass('gl-display-none');
|
|
|
|
}
|
2016-12-07 10:27:14 -05:00
|
|
|
|
2017-10-12 05:42:13 -04:00
|
|
|
$toggle.enable();
|
|
|
|
$dateInput.enable();
|
2016-08-31 10:28:58 -04:00
|
|
|
}
|
2019-02-27 05:09:52 -05:00
|
|
|
|
2017-10-12 05:42:13 -04:00
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
getMemberListItems($el) {
|
2020-09-30 08:09:53 -04:00
|
|
|
const $memberListItem = $el.is('.js-member') ? $el : $(`#${$el.data('elId')}`);
|
2016-08-31 10:28:58 -04:00
|
|
|
|
2017-10-12 05:42:13 -04:00
|
|
|
return {
|
|
|
|
$memberListItem,
|
2020-09-30 08:09:53 -04:00
|
|
|
$expiresIn: $memberListItem.find('.js-expires-in'),
|
|
|
|
$expiresInText: $memberListItem.find('.js-expires-in-text'),
|
2017-10-12 05:42:13 -04:00
|
|
|
$toggle: $memberListItem.find('.dropdown-menu-toggle'),
|
|
|
|
$dateInput: $memberListItem.find('.js-access-expiration-date'),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|