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

50 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
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';
import flash from '~/flash';
import { __ } from '~/locale';
2018-05-09 12:01:36 +05:30
2017-08-17 22:00:37 +05:30
export default class Group {
constructor() {
this.groupPath = $('#group_path');
this.groupName = $('#group_name');
2019-12-26 22:10:19 +05:30
this.parentId = $('#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);
2017-08-17 22:00:37 +05:30
if (this.groupName.val() === '') {
2018-12-13 13:39:08 +05:30
this.groupName.on('keyup', this.updateHandler);
this.groupPath.on('keydown', this.resetHandler);
2019-12-26 22:10:19 +05:30
if (!this.parentId.val()) {
this.groupName.on('blur', this.updateGroupPathSlugHandler);
}
2017-08-17 22:00:37 +05:30
}
}
update() {
2019-09-30 21:07:59 +05:30
const slug = slugify(this.groupName.val());
2018-12-13 13:39:08 +05:30
this.groupPath.val(slug);
2017-08-17 22:00:37 +05:30
}
reset() {
2018-12-13 13:39:08 +05:30
this.groupName.off('keyup', this.updateHandler);
this.groupPath.off('keydown', this.resetHandler);
2019-12-26 22:10:19 +05:30
this.groupName.off('blur', this.checkPathHandler);
}
updateGroupPathSlug() {
const slug = this.groupPath.val() || slugify(this.groupName.val());
if (!slug) return;
fetchGroupPathAvailability(slug)
.then(({ data }) => data)
.then(data => {
if (data.exists && data.suggests.length > 0) {
const suggestedSlug = data.suggests[0];
this.groupPath.val(suggestedSlug);
}
})
.catch(() => flash(__('An error occurred while checking group path')));
2017-08-17 22:00:37 +05:30
}
}