debian-mirror-gitlab/app/assets/javascripts/group.js

74 lines
2.4 KiB
JavaScript
Raw Normal View History

2019-09-30 21:07:59 +05:30
import { slugify } from './lib/utils/text_utility';
2019-12-26 22:10:19 +05:30
import fetchGroupPathAvailability from '~/pages/groups/new/fetch_group_path_availability';
2020-10-24 23:57:45 +05:30
import { deprecatedCreateFlash as flash } from '~/flash';
2019-12-26 22:10:19 +05:30
import { __ } from '~/locale';
2018-05-09 12:01:36 +05:30
2017-08-17 22:00:37 +05:30
export default class Group {
constructor() {
2020-06-23 00:09:42 +05:30
this.groupPaths = Array.from(document.querySelectorAll('.js-autofill-group-path'));
this.groupNames = Array.from(document.querySelectorAll('.js-autofill-group-name'));
this.parentId = document.getElementById('group_parent_id');
2017-08-17 22:00:37 +05:30
this.updateHandler = this.update.bind(this);
this.resetHandler = this.reset.bind(this);
2019-12-26 22:10:19 +05:30
this.updateGroupPathSlugHandler = this.updateGroupPathSlug.bind(this);
2020-06-23 00:09:42 +05:30
this.groupNames.forEach(groupName => {
if (groupName.value === '') {
groupName.addEventListener('keyup', this.updateHandler);
if (!this.parentId.value) {
groupName.addEventListener('blur', this.updateGroupPathSlugHandler);
}
2019-12-26 22:10:19 +05:30
}
2020-06-23 00:09:42 +05:30
});
this.groupPaths.forEach(groupPath => {
groupPath.addEventListener('keydown', this.resetHandler);
});
2017-08-17 22:00:37 +05:30
}
2020-06-23 00:09:42 +05:30
update({ currentTarget: { value: updatedValue } }) {
const slug = slugify(updatedValue);
this.groupNames.forEach(element => {
element.value = updatedValue;
});
this.groupPaths.forEach(element => {
element.value = slug;
});
2017-08-17 22:00:37 +05:30
}
reset() {
2020-06-23 00:09:42 +05:30
this.groupNames.forEach(groupName => {
groupName.removeEventListener('keyup', this.updateHandler);
groupName.removeEventListener('blur', this.checkPathHandler);
});
this.groupPaths.forEach(groupPath => {
groupPath.removeEventListener('keydown', this.resetHandler);
});
2019-12-26 22:10:19 +05:30
}
2020-06-23 00:09:42 +05:30
updateGroupPathSlug({ currentTarget: { value } = '' } = {}) {
const slug = this.groupPaths[0]?.value || slugify(value);
2019-12-26 22:10:19 +05:30
if (!slug) return;
fetchGroupPathAvailability(slug)
.then(({ data }) => data)
2020-06-23 00:09:42 +05:30
.then(({ exists, suggests }) => {
if (exists && suggests.length) {
const [suggestedSlug] = suggests;
this.groupPaths.forEach(element => {
element.value = suggestedSlug;
});
} else if (exists && !suggests.length) {
flash(__('Unable to suggest a path. Please refresh and try again.'));
2019-12-26 22:10:19 +05:30
}
})
2020-06-23 00:09:42 +05:30
.catch(() =>
flash(__('An error occurred while checking group path. Please refresh and try again.')),
);
2017-08-17 22:00:37 +05:30
}
}