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

102 lines
2.5 KiB
JavaScript
Raw Normal View History

2019-12-04 20:38:33 +05:30
/* eslint-disable class-methods-use-this */
2017-08-17 22:00:37 +05:30
2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2019-12-21 20:55:43 +05:30
import '~/gl_dropdown';
2018-05-09 12:01:36 +05:30
2017-08-17 22:00:37 +05:30
export default class TemplateSelector {
constructor({ dropdown, data, pattern, wrapper, editor, $input } = {}) {
this.pattern = pattern;
this.editor = editor;
this.dropdown = dropdown;
this.$dropdownContainer = wrapper;
this.$filenameInput = $input || $('#file_name');
this.$dropdownIcon = $('.fa-chevron-down', dropdown);
this.initDropdown(dropdown, data);
this.listenForFilenameInput();
this.renderMatchedDropdown();
this.initAutosizeUpdateEvent();
}
initDropdown(dropdown, data) {
return $(dropdown).glDropdown({
data,
filterable: true,
selectable: true,
toggleLabel: item => item.name,
search: {
fields: ['name'],
},
2019-12-21 20:55:43 +05:30
clicked: options => this.onDropdownClicked(options),
2017-08-17 22:00:37 +05:30
text: item => item.name,
});
}
2019-12-21 20:55:43 +05:30
// Subclasses can override this method to conditionally prevent fetching file templates
onDropdownClicked(options) {
this.fetchFileTemplate(options);
}
2017-08-17 22:00:37 +05:30
initAutosizeUpdateEvent() {
this.autosizeUpdateEvent = document.createEvent('Event');
this.autosizeUpdateEvent.initEvent('autosize:update', true, false);
}
listenForFilenameInput() {
return this.$filenameInput.on('keyup blur', e => this.renderMatchedDropdown(e));
}
renderMatchedDropdown() {
if (!this.$filenameInput.length) {
return null;
}
const filenameMatches = this.pattern.test(this.$filenameInput.val().trim());
if (!filenameMatches) {
return this.$dropdownContainer.addClass('hidden');
}
return this.$dropdownContainer.removeClass('hidden');
}
fetchFileTemplate(options) {
const { e } = options;
const item = options.selectedObj;
e.preventDefault();
return this.requestFile(item);
}
2019-12-04 20:38:33 +05:30
requestFile() {
2017-08-17 22:00:37 +05:30
// This `requestFile` method is an abstract method that should
// be added by all subclasses.
}
setEditorContent(file, { skipFocus } = {}) {
if (!file) return;
const newValue = file.content;
this.editor.setValue(newValue, 1);
if (!skipFocus) this.editor.focus();
2018-05-09 12:01:36 +05:30
if (this.editor instanceof $) {
2017-08-17 22:00:37 +05:30
this.editor.get(0).dispatchEvent(this.autosizeUpdateEvent);
2019-12-21 20:55:43 +05:30
this.editor.trigger('input');
2017-08-17 22:00:37 +05:30
}
}
2019-12-21 20:55:43 +05:30
getEditorContent() {
return this.editor.getValue();
}
2017-08-17 22:00:37 +05:30
startLoadingSpinner() {
2020-04-08 14:13:33 +05:30
this.$dropdownIcon.addClass('spinner').removeClass('fa-chevron-down');
2017-08-17 22:00:37 +05:30
}
stopLoadingSpinner() {
2020-04-08 14:13:33 +05:30
this.$dropdownIcon.addClass('fa-chevron-down').removeClass('spinner');
2017-08-17 22:00:37 +05:30
}
}