2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2017-08-07 16:55:50 -04:00
|
|
|
import AccessorUtilities from './lib/utils/accessor';
|
|
|
|
|
|
|
|
export default class ProjectSelectComboButton {
|
|
|
|
constructor(select) {
|
|
|
|
this.projectSelectInput = $(select);
|
|
|
|
this.newItemBtn = $('.new-project-item-link');
|
2017-08-21 17:29:09 -04:00
|
|
|
this.resourceType = this.newItemBtn.data('type');
|
|
|
|
this.resourceLabel = this.newItemBtn.data('label');
|
|
|
|
this.formattedText = this.deriveTextVariants();
|
2017-08-07 16:55:50 -04:00
|
|
|
this.groupId = this.projectSelectInput.data('groupId');
|
|
|
|
this.bindEvents();
|
|
|
|
this.initLocalStorage();
|
|
|
|
}
|
|
|
|
|
|
|
|
bindEvents() {
|
2017-08-30 14:47:44 -04:00
|
|
|
this.projectSelectInput.siblings('.new-project-item-select-button')
|
|
|
|
.on('click', e => this.openDropdown(e));
|
2017-08-30 10:16:37 -04:00
|
|
|
|
|
|
|
this.newItemBtn.on('click', (e) => {
|
|
|
|
if (!this.getProjectFromLocalStorage()) {
|
|
|
|
e.preventDefault();
|
2017-08-30 14:47:44 -04:00
|
|
|
this.openDropdown(e);
|
2017-08-30 10:16:37 -04:00
|
|
|
}
|
|
|
|
});
|
2017-08-07 16:55:50 -04:00
|
|
|
|
|
|
|
this.projectSelectInput.on('change', () => this.selectProject());
|
|
|
|
}
|
|
|
|
|
|
|
|
initLocalStorage() {
|
|
|
|
const localStorageIsSafe = AccessorUtilities.isLocalStorageAccessSafe();
|
|
|
|
|
|
|
|
if (localStorageIsSafe) {
|
2017-08-21 17:29:09 -04:00
|
|
|
this.localStorageKey = ['group', this.groupId, this.formattedText.localStorageItemType, 'recent-project'].join('-');
|
2017-08-07 16:55:50 -04:00
|
|
|
this.setBtnTextFromLocalStorage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-30 14:47:44 -04:00
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
openDropdown(event) {
|
|
|
|
$(event.currentTarget).siblings('.project-item-select').select2('open');
|
2017-08-07 16:55:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2017-08-21 17:29:09 -04:00
|
|
|
this.newItemBtn.text(`${this.formattedText.defaultTextPrefix} in ${project.name}`);
|
2017-08-07 16:55:50 -04:00
|
|
|
} else {
|
2017-08-21 17:29:09 -04:00
|
|
|
this.newItemBtn.text(`Select project to create ${this.formattedText.presetTextSuffix}`);
|
2017-08-07 16:55:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getProjectFromLocalStorage() {
|
|
|
|
const projectString = localStorage.getItem(this.localStorageKey);
|
|
|
|
|
|
|
|
return JSON.parse(projectString);
|
|
|
|
}
|
|
|
|
|
|
|
|
setProjectInLocalStorage(projectMeta) {
|
|
|
|
const projectString = JSON.stringify(projectMeta);
|
|
|
|
|
|
|
|
localStorage.setItem(this.localStorageKey, projectString);
|
|
|
|
}
|
2017-08-21 17:29:09 -04:00
|
|
|
|
|
|
|
deriveTextVariants() {
|
|
|
|
const defaultTextPrefix = this.resourceLabel;
|
|
|
|
|
|
|
|
// the trailing slice call depluralizes each of these strings (e.g. new-issues -> new-issue)
|
|
|
|
const localStorageItemType = `new-${this.resourceType.split('_').join('-').slice(0, -1)}`;
|
|
|
|
const presetTextSuffix = this.resourceType.split('_').join(' ').slice(0, -1);
|
|
|
|
|
|
|
|
return {
|
|
|
|
localStorageItemType, // new-issue / new-merge-request
|
|
|
|
defaultTextPrefix, // New issue / New merge request
|
|
|
|
presetTextSuffix, // issue / merge request
|
|
|
|
};
|
|
|
|
}
|
2017-08-07 16:55:50 -04:00
|
|
|
}
|
|
|
|
|