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

72 lines
2.3 KiB
JavaScript
Raw Normal View History

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';
2021-03-11 19:13:27 +05:30
import fetchGroupPathAvailability from '~/pages/groups/new/fetch_group_path_availability';
import { slugify } from './lib/utils/text_utility';
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
2021-03-08 18:12:59 +05:30
this.groupNames.forEach((groupName) => {
2020-06-23 00:09:42 +05:30
if (groupName.value === '') {
groupName.addEventListener('keyup', this.updateHandler);
2021-04-29 21:17:54 +05:30
groupName.addEventListener('blur', this.updateGroupPathSlugHandler);
2019-12-26 22:10:19 +05:30
}
2020-06-23 00:09:42 +05:30
});
2021-03-08 18:12:59 +05:30
this.groupPaths.forEach((groupPath) => {
2020-06-23 00:09:42 +05:30
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);
2021-03-08 18:12:59 +05:30
this.groupNames.forEach((element) => {
2020-06-23 00:09:42 +05:30
element.value = updatedValue;
});
2021-03-08 18:12:59 +05:30
this.groupPaths.forEach((element) => {
2020-06-23 00:09:42 +05:30
element.value = slug;
});
2017-08-17 22:00:37 +05:30
}
reset() {
2021-03-08 18:12:59 +05:30
this.groupNames.forEach((groupName) => {
2020-06-23 00:09:42 +05:30
groupName.removeEventListener('keyup', this.updateHandler);
groupName.removeEventListener('blur', this.checkPathHandler);
});
2021-03-08 18:12:59 +05:30
this.groupPaths.forEach((groupPath) => {
2020-06-23 00:09:42 +05:30
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;
2021-04-29 21:17:54 +05:30
fetchGroupPathAvailability(slug, this.parentId?.value)
2019-12-26 22:10:19 +05:30
.then(({ data }) => data)
2020-06-23 00:09:42 +05:30
.then(({ exists, suggests }) => {
if (exists && suggests.length) {
const [suggestedSlug] = suggests;
2021-03-08 18:12:59 +05:30
this.groupPaths.forEach((element) => {
2020-06-23 00:09:42 +05:30
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
}
}