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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

122 lines
3.5 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2022-03-02 08:16:31 +05:30
import { sprintf, __ } from '~/locale';
2017-09-10 17:25:29 +05:30
import AccessorUtilities from './lib/utils/accessor';
2021-02-22 17:27:13 +05:30
import { loadCSSFile } from './lib/utils/css_utils';
2017-09-10 17:25:29 +05:30
export default class ProjectSelectComboButton {
constructor(select) {
this.projectSelectInput = $(select);
2022-03-02 08:16:31 +05:30
this.newItemBtn = $('.js-new-project-item-link');
2018-03-17 18:26:18 +05:30
this.resourceType = this.newItemBtn.data('type');
this.resourceLabel = this.newItemBtn.data('label');
this.formattedText = this.deriveTextVariants();
2017-09-10 17:25:29 +05:30
this.groupId = this.projectSelectInput.data('groupId');
this.bindEvents();
this.initLocalStorage();
}
bindEvents() {
2018-12-13 13:39:08 +05:30
this.projectSelectInput
.siblings('.new-project-item-select-button')
2021-03-08 18:12:59 +05:30
.on('click', (e) => this.openDropdown(e));
2018-03-17 18:26:18 +05:30
2021-03-08 18:12:59 +05:30
this.newItemBtn.on('click', (e) => {
2018-03-17 18:26:18 +05:30
if (!this.getProjectFromLocalStorage()) {
e.preventDefault();
this.openDropdown(e);
}
});
2017-09-10 17:25:29 +05:30
this.projectSelectInput.on('change', () => this.selectProject());
}
initLocalStorage() {
2021-11-11 11:23:49 +05:30
const localStorageIsSafe = AccessorUtilities.canUseLocalStorage();
2017-09-10 17:25:29 +05:30
if (localStorageIsSafe) {
2018-12-13 13:39:08 +05:30
this.localStorageKey = [
'group',
this.groupId,
this.formattedText.localStorageItemType,
'recent-project',
].join('-');
2017-09-10 17:25:29 +05:30
this.setBtnTextFromLocalStorage();
}
}
2018-03-17 18:26:18 +05:30
// eslint-disable-next-line class-methods-use-this
openDropdown(event) {
2019-03-02 22:35:43 +05:30
import(/* webpackChunkName: 'select2' */ 'select2/select2')
.then(() => {
2021-02-22 17:27:13 +05:30
// eslint-disable-next-line promise/no-nesting
loadCSSFile(gon.select2_css_path)
.then(() => {
2021-03-08 18:12:59 +05:30
$(event.currentTarget).siblings('.project-item-select').select2('open');
2021-02-22 17:27:13 +05:30
})
.catch(() => {});
2019-03-02 22:35:43 +05:30
})
.catch(() => {});
2017-09-10 17:25:29 +05:30
}
selectProject() {
const selectedProjectData = JSON.parse(this.projectSelectInput.val());
const projectUrl = `${selectedProjectData.url}/${this.projectSelectInput.data('relativePath')}`;
const projectName = selectedProjectData.name;
const projectMeta = {
url: projectUrl,
name: projectName,
};
this.setNewItemBtnAttributes(projectMeta);
this.setProjectInLocalStorage(projectMeta);
}
setBtnTextFromLocalStorage() {
const cachedProjectData = this.getProjectFromLocalStorage();
this.setNewItemBtnAttributes(cachedProjectData);
}
setNewItemBtnAttributes(project) {
if (project) {
this.newItemBtn.attr('href', project.url);
2022-03-02 08:16:31 +05:30
this.newItemBtn.text(
sprintf(__('New %{type} in %{project}'), {
type: this.resourceLabel,
project: project.name,
}),
);
2017-09-10 17:25:29 +05:30
} else {
2022-03-02 08:16:31 +05:30
this.newItemBtn.text(
sprintf(__('Select project to create %{type}'), {
type: this.formattedText.presetTextSuffix,
}),
);
2017-09-10 17:25:29 +05:30
}
}
getProjectFromLocalStorage() {
const projectString = localStorage.getItem(this.localStorageKey);
return JSON.parse(projectString);
}
setProjectInLocalStorage(projectMeta) {
const projectString = JSON.stringify(projectMeta);
localStorage.setItem(this.localStorageKey, projectString);
}
2018-03-17 18:26:18 +05:30
deriveTextVariants() {
// the trailing slice call depluralizes each of these strings (e.g. new-issues -> new-issue)
2021-03-08 18:12:59 +05:30
const localStorageItemType = `new-${this.resourceType.split('_').join('-').slice(0, -1)}`;
const presetTextSuffix = this.resourceType.split('_').join(' ').slice(0, -1);
2018-03-17 18:26:18 +05:30
return {
localStorageItemType, // new-issue / new-merge-request
presetTextSuffix, // issue / merge request
};
}
2017-09-10 17:25:29 +05:30
}