debian-mirror-gitlab/app/assets/javascripts/blob/file_template_selector.js

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

106 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2022-07-16 23:28:13 +05:30
import { loadingIconForLegacyJS } from '~/loading_icon_for_legacy_js';
2018-05-09 12:01:36 +05:30
2017-08-17 22:00:37 +05:30
export default class FileTemplateSelector {
constructor(mediator) {
this.mediator = mediator;
this.$dropdown = null;
this.$wrapper = null;
2022-07-16 23:28:13 +05:30
this.dropdown = null;
this.wrapper = null;
2017-08-17 22:00:37 +05:30
}
init() {
const cfg = this.config;
this.$dropdown = $(cfg.dropdown);
this.$wrapper = $(cfg.wrapper);
2022-07-16 23:28:13 +05:30
this.dropdown = document.querySelector(cfg.dropdown);
this.wrapper = document.querySelector(cfg.wrapper);
this.dropdownIcon = this.wrapper.querySelector('.dropdown-menu-toggle-icon');
this.loadingIcon = loadingIconForLegacyJS({ classes: ['gl-display-none'] });
this.dropdown.appendChild(this.loadingIcon);
this.dropdownToggleText = this.wrapper.querySelector('.dropdown-toggle-text');
2017-08-17 22:00:37 +05:30
this.initDropdown();
2021-04-29 21:17:54 +05:30
this.selectInitialTemplate();
}
selectInitialTemplate() {
2022-07-16 23:28:13 +05:30
const template = this.dropdown.dataset.selected;
2021-04-29 21:17:54 +05:30
if (!template) {
return;
}
this.mediator.selectTemplateFile(this, template);
2017-08-17 22:00:37 +05:30
}
show() {
2022-07-16 23:28:13 +05:30
if (this.dropdown === null) {
2017-08-17 22:00:37 +05:30
this.init();
}
2022-07-16 23:28:13 +05:30
this.wrapper.classList.remove('hidden');
2021-04-29 21:17:54 +05:30
/**
* We set the focus on the dropdown that was just shown. This is done so that, after selecting
* a template type, the template selector immediately receives the focus.
* This improves the UX of the tour as the suggest_gitlab_ci_yml popover requires its target to
* be have the focus to appear. This way, users don't have to interact with the template
* selector to actually see the first hint: it is shown as soon as the selector becomes visible.
* We also need a timeout here, otherwise the template type selector gets stuck and can not be
* closed anymore.
*/
setTimeout(() => {
2022-07-16 23:28:13 +05:30
this.dropdown.focus();
2021-04-29 21:17:54 +05:30
}, 0);
2017-08-17 22:00:37 +05:30
}
hide() {
2022-07-16 23:28:13 +05:30
if (this.dropdown !== null) {
this.wrapper.classList.add('hidden');
2017-08-17 22:00:37 +05:30
}
}
2018-10-15 14:42:47 +05:30
isHidden() {
2022-07-16 23:28:13 +05:30
return !this.wrapper || this.wrapper.classList.contains('hidden');
2018-10-15 14:42:47 +05:30
}
2017-08-17 22:00:37 +05:30
getToggleText() {
2022-07-16 23:28:13 +05:30
return this.dropdownToggleText.textContent;
2017-08-17 22:00:37 +05:30
}
setToggleText(text) {
2022-07-16 23:28:13 +05:30
this.dropdownToggleText.textContent = text;
2017-08-17 22:00:37 +05:30
}
renderLoading() {
2022-07-16 23:28:13 +05:30
this.loadingIcon.classList.remove('gl-display-none');
this.dropdownIcon.classList.add('gl-display-none');
2017-08-17 22:00:37 +05:30
}
renderLoaded() {
2022-07-16 23:28:13 +05:30
this.loadingIcon.classList.add('gl-display-none');
this.dropdownIcon.classList.remove('gl-display-none');
2017-08-17 22:00:37 +05:30
}
reportSelection(options) {
const { query, e, data } = options;
e.preventDefault();
return this.mediator.selectTemplateFile(this, query, data);
}
reportSelectionName(options) {
const opts = options;
opts.query = options.selectedObj.name;
2021-03-11 19:13:27 +05:30
opts.data = options.selectedObj;
opts.data.source_template_project_id = options.selectedObj.project_id;
2017-08-17 22:00:37 +05:30
this.reportSelection(opts);
}
}