2018-05-09 12:01:36 +05:30
|
|
|
import $ from 'jquery';
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
export default class FileTemplateSelector {
|
|
|
|
constructor(mediator) {
|
|
|
|
this.mediator = mediator;
|
|
|
|
this.$dropdown = null;
|
|
|
|
this.$wrapper = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
|
|
|
const cfg = this.config;
|
|
|
|
|
|
|
|
this.$dropdown = $(cfg.dropdown);
|
|
|
|
this.$wrapper = $(cfg.wrapper);
|
2021-02-22 17:27:13 +05:30
|
|
|
this.$dropdownIcon = this.$wrapper.find('.dropdown-menu-toggle-icon');
|
|
|
|
this.$loadingIcon = $(
|
|
|
|
'<div class="gl-spinner gl-spinner-orange gl-spinner-sm gl-absolute gl-top-3 gl-right-3 gl-display-none"></div>',
|
|
|
|
).insertAfter(this.$dropdownIcon);
|
2017-08-17 22:00:37 +05:30
|
|
|
this.$dropdownToggleText = this.$wrapper.find('.dropdown-toggle-text');
|
|
|
|
|
|
|
|
this.initDropdown();
|
2021-04-29 21:17:54 +05:30
|
|
|
this.selectInitialTemplate();
|
|
|
|
}
|
|
|
|
|
|
|
|
selectInitialTemplate() {
|
|
|
|
const template = this.$dropdown.data('selected');
|
|
|
|
|
|
|
|
if (!template) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.mediator.selectTemplateFile(this, template);
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
show() {
|
|
|
|
if (this.$dropdown === null) {
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$wrapper.removeClass('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(() => {
|
|
|
|
this.$dropdown.focus();
|
|
|
|
}, 0);
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
hide() {
|
|
|
|
if (this.$dropdown !== null) {
|
|
|
|
this.$wrapper.addClass('hidden');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
isHidden() {
|
2021-04-29 21:17:54 +05:30
|
|
|
return !this.$wrapper || this.$wrapper.hasClass('hidden');
|
2018-10-15 14:42:47 +05:30
|
|
|
}
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
getToggleText() {
|
|
|
|
return this.$dropdownToggleText.text();
|
|
|
|
}
|
|
|
|
|
|
|
|
setToggleText(text) {
|
|
|
|
this.$dropdownToggleText.text(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderLoading() {
|
2021-02-22 17:27:13 +05:30
|
|
|
this.$loadingIcon.removeClass('gl-display-none');
|
|
|
|
this.$dropdownIcon.addClass('gl-display-none');
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
renderLoaded() {
|
2021-02-22 17:27:13 +05:30
|
|
|
this.$loadingIcon.addClass('gl-display-none');
|
|
|
|
this.$dropdownIcon.removeClass('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);
|
|
|
|
}
|
|
|
|
}
|