debian-mirror-gitlab/app/assets/javascripts/mirrors/mirror_repos.js

137 lines
4 KiB
JavaScript
Raw Normal View History

2018-11-20 20:47:30 +05:30
import $ from 'jquery';
2020-03-13 15:44:24 +05:30
import { debounce } from 'lodash';
2021-09-30 23:02:18 +05:30
import createFlash from '~/flash';
2018-11-20 20:47:30 +05:30
import axios from '~/lib/utils/axios_utils';
2021-03-11 19:13:27 +05:30
import { __ } from '~/locale';
2021-01-03 14:25:43 +05:30
import { hide } from '~/tooltips';
2021-03-11 19:13:27 +05:30
import SSHMirror from './ssh_mirror';
2018-11-20 20:47:30 +05:30
2022-02-27 12:50:16 +05:30
const PASSWORD_FIELD_SELECTOR = '.js-mirror-password-field';
2018-11-20 20:47:30 +05:30
export default class MirrorRepos {
constructor(container) {
this.$container = $(container);
2019-02-15 15:39:39 +05:30
this.$password = null;
2018-11-20 20:47:30 +05:30
this.$form = $('.js-mirror-form', this.$container);
this.$urlInput = $('.js-mirror-url', this.$form);
this.$protectedBranchesInput = $('.js-mirror-protected', this.$form);
this.$table = $('.js-mirrors-table-body', this.$container);
this.mirrorEndpoint = this.$form.data('projectMirrorEndpoint');
}
init() {
this.initMirrorPush();
this.registerUpdateListeners();
}
initMirrorPush() {
2020-04-22 19:07:51 +05:30
this.$keepDivergentRefsInput = $('.js-mirror-keep-divergent-refs', this.$form);
2018-11-20 20:47:30 +05:30
this.$passwordGroup = $('.js-password-group', this.$container);
this.$password = $('.js-password', this.$passwordGroup);
this.$authMethod = $('.js-auth-method', this.$form);
2020-04-22 19:07:51 +05:30
this.$keepDivergentRefsInput.on('change', () => this.updateKeepDivergentRefs());
2018-11-20 20:47:30 +05:30
this.$authMethod.on('change', () => this.togglePassword());
this.$password.on('input.updateUrl', () => this.debouncedUpdateUrl());
2019-02-15 15:39:39 +05:30
this.initMirrorSSH();
this.updateProtectedBranches();
2020-04-22 19:07:51 +05:30
this.updateKeepDivergentRefs();
2022-02-27 12:50:16 +05:30
MirrorRepos.resetPasswordField();
}
static resetPasswordField() {
if (document.querySelector(PASSWORD_FIELD_SELECTOR)) {
document.querySelector(PASSWORD_FIELD_SELECTOR).value = '';
}
2019-02-15 15:39:39 +05:30
}
initMirrorSSH() {
if (this.$password) {
2021-02-22 17:27:13 +05:30
// eslint-disable-next-line @gitlab/no-global-event-off
2019-02-15 15:39:39 +05:30
this.$password.off('input.updateUrl');
}
this.$password = undefined;
this.sshMirror = new SSHMirror('.js-mirror-form');
this.sshMirror.init();
2018-11-20 20:47:30 +05:30
}
updateUrl() {
let val = this.$urlInput.val();
if (this.$password) {
const password = this.$password.val();
if (password) val = val.replace('@', `:${password}@`);
}
$('.js-mirror-url-hidden', this.$form).val(val);
}
updateProtectedBranches() {
const val = this.$protectedBranchesInput.get(0).checked
? this.$protectedBranchesInput.val()
: '0';
$('.js-mirror-protected-hidden', this.$form).val(val);
}
2020-04-22 19:07:51 +05:30
updateKeepDivergentRefs() {
const field = this.$keepDivergentRefsInput.get(0);
// This field only exists after the form is switched to 'Push' mode
if (field) {
const val = field.checked ? this.$keepDivergentRefsInput.val() : '0';
$('.js-mirror-keep-divergent-refs-hidden', this.$form).val(val);
}
}
2018-11-20 20:47:30 +05:30
registerUpdateListeners() {
2020-03-13 15:44:24 +05:30
this.debouncedUpdateUrl = debounce(() => this.updateUrl(), 200);
2018-11-20 20:47:30 +05:30
this.$urlInput.on('input', () => this.debouncedUpdateUrl());
this.$protectedBranchesInput.on('change', () => this.updateProtectedBranches());
2021-03-08 18:12:59 +05:30
this.$table.on('click', '.js-delete-mirror', (event) => this.deleteMirror(event));
2018-11-20 20:47:30 +05:30
}
togglePassword() {
const isPassword = this.$authMethod.val() === 'password';
if (!isPassword) {
this.$password.val('');
this.updateUrl();
}
this.$passwordGroup.collapse(isPassword ? 'show' : 'hide');
}
deleteMirror(event, existingPayload) {
const $target = $(event.currentTarget);
let payload = existingPayload;
if (!payload) {
payload = {
project: {
remote_mirrors_attributes: {
id: $target.data('mirrorId'),
2019-07-31 22:56:46 +05:30
_destroy: 1,
2018-11-20 20:47:30 +05:30
},
},
};
}
return axios
.put(this.mirrorEndpoint, payload)
.then(() => this.removeRow($target))
2021-09-30 23:02:18 +05:30
.catch(() =>
createFlash({
message: __('Failed to remove mirror.'),
}),
);
2018-11-20 20:47:30 +05:30
}
/* eslint-disable class-methods-use-this */
removeRow($target) {
const row = $target.closest('tr');
2021-01-03 14:25:43 +05:30
hide($('.js-delete-mirror', row));
2018-11-20 20:47:30 +05:30
row.remove();
}
/* eslint-enable class-methods-use-this */
}