gitlab-org--gitlab-foss/app/assets/javascripts/mirrors/mirror_repos.js

109 lines
3 KiB
JavaScript
Raw Normal View History

2018-07-25 07:05:40 -04:00
import $ from 'jquery';
import _ from 'underscore';
import { __ } from '~/locale';
import Flash from '~/flash';
import axios from '~/lib/utils/axios_utils';
import SSHMirror from './ssh_mirror';
2018-07-25 07:05:40 -04:00
2018-08-01 19:08:22 -04:00
export default class MirrorRepos {
2018-07-25 07:05:40 -04:00
constructor(container) {
this.$container = $(container);
this.$password = null;
2018-07-25 07:05:40 -04:00
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);
2018-08-01 19:08:22 -04:00
this.mirrorEndpoint = this.$form.data('projectMirrorEndpoint');
2018-07-25 07:05:40 -04:00
}
init() {
2018-08-01 19:08:22 -04:00
this.initMirrorPush();
2018-08-03 15:34:14 -04:00
this.registerUpdateListeners();
}
2018-07-25 07:05:40 -04:00
2018-08-03 15:34:14 -04:00
initMirrorPush() {
this.$passwordGroup = $('.js-password-group', this.$container);
this.$password = $('.js-password', this.$passwordGroup);
this.$authMethod = $('.js-auth-method', this.$form);
this.$authMethod.on('change', () => this.togglePassword());
this.$password.on('input.updateUrl', () => this.debouncedUpdateUrl());
this.initMirrorSSH();
}
initMirrorSSH() {
if (this.$password) {
this.$password.off('input.updateUrl');
}
this.$password = undefined;
this.sshMirror = new SSHMirror('.js-mirror-form');
this.sshMirror.init();
2018-07-25 07:05:40 -04:00
}
updateUrl() {
2018-08-01 19:08:22 -04:00
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);
2018-07-25 07:05:40 -04:00
}
updateProtectedBranches() {
const val = this.$protectedBranchesInput.get(0).checked
? this.$protectedBranchesInput.val()
: '0';
$('.js-mirror-protected-hidden', this.$form).val(val);
}
registerUpdateListeners() {
2018-08-01 19:08:22 -04:00
this.debouncedUpdateUrl = _.debounce(() => this.updateUrl(), 200);
this.$urlInput.on('input', () => this.debouncedUpdateUrl());
2018-07-25 07:05:40 -04:00
this.$protectedBranchesInput.on('change', () => this.updateProtectedBranches());
2018-08-07 10:10:25 -04:00
this.$table.on('click', '.js-delete-mirror', event => this.deleteMirror(event));
2018-07-25 07:05:40 -04:00
}
2018-08-03 15:34:14 -04:00
togglePassword() {
const isPassword = this.$authMethod.val() === 'password';
if (!isPassword) {
this.$password.val('');
this.updateUrl();
}
this.$passwordGroup.collapse(isPassword ? 'show' : 'hide');
2018-08-01 19:08:22 -04:00
}
2018-07-25 07:05:40 -04:00
deleteMirror(event, existingPayload) {
const $target = $(event.currentTarget);
let payload = existingPayload;
if (!payload) {
payload = {
project: {
remote_mirrors_attributes: {
id: $target.data('mirrorId'),
enabled: 0,
},
},
};
}
return axios
.put(this.mirrorEndpoint, payload)
.then(() => this.removeRow($target))
.catch(() => Flash(__('Failed to remove mirror.')));
}
/* eslint-disable class-methods-use-this */
removeRow($target) {
const row = $target.closest('tr');
$('.js-delete-mirror', row).tooltip('hide');
row.remove();
}
/* eslint-enable class-methods-use-this */
}