2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2019-07-03 04:20:57 -04:00
|
|
|
import { slugify } from './lib/utils/text_utility';
|
2019-11-05 19:06:13 -05:00
|
|
|
import fetchGroupPathAvailability from '~/pages/groups/new/fetch_group_path_availability';
|
|
|
|
import flash from '~/flash';
|
|
|
|
import { __ } from '~/locale';
|
2018-03-09 15:18:59 -05:00
|
|
|
|
2017-03-12 11:22:00 -04:00
|
|
|
export default class Group {
|
|
|
|
constructor() {
|
|
|
|
this.groupPath = $('#group_path');
|
|
|
|
this.groupName = $('#group_name');
|
2019-11-05 19:06:13 -05:00
|
|
|
this.parentId = $('#group_parent_id');
|
2017-03-12 11:22:00 -04:00
|
|
|
this.updateHandler = this.update.bind(this);
|
|
|
|
this.resetHandler = this.reset.bind(this);
|
2019-11-05 19:06:13 -05:00
|
|
|
this.updateGroupPathSlugHandler = this.updateGroupPathSlug.bind(this);
|
2017-03-12 11:22:00 -04:00
|
|
|
if (this.groupName.val() === '') {
|
2018-10-30 12:23:47 -04:00
|
|
|
this.groupName.on('keyup', this.updateHandler);
|
|
|
|
this.groupPath.on('keydown', this.resetHandler);
|
2019-11-05 19:06:13 -05:00
|
|
|
if (!this.parentId.val()) {
|
|
|
|
this.groupName.on('blur', this.updateGroupPathSlugHandler);
|
|
|
|
}
|
2017-03-12 11:22:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
update() {
|
2019-07-03 04:20:57 -04:00
|
|
|
const slug = slugify(this.groupName.val());
|
2018-10-30 12:23:47 -04:00
|
|
|
this.groupPath.val(slug);
|
2017-03-12 11:22:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
2018-10-30 12:23:47 -04:00
|
|
|
this.groupName.off('keyup', this.updateHandler);
|
|
|
|
this.groupPath.off('keydown', this.resetHandler);
|
2019-11-05 19:06:13 -05:00
|
|
|
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-03-12 11:22:00 -04:00
|
|
|
}
|
|
|
|
}
|