gitlab-org--gitlab-foss/app/assets/javascripts/create_item_dropdown.js

119 lines
3.3 KiB
JavaScript
Raw Normal View History

import { escape } from 'lodash';
import initDeprecatedJQueryDropdown from '~/deprecated_jquery_dropdown';
export default class CreateItemDropdown {
2017-04-06 09:17:50 +00:00
/**
* @param {Object} options containing
* `$dropdown` target element
* `onSelect` event callback
* $dropdown must be an element created using `dropdown_tag()` rails helper
*/
2017-03-21 15:51:56 +00:00
constructor(options) {
this.defaultToggleLabel = options.defaultToggleLabel;
this.fieldName = options.fieldName;
this.onSelect = options.onSelect || (() => {});
this.getDataOption = options.getData;
this.getDataRemote = Boolean(options.filterRemote);
this.createNewItemFromValueOption = options.createNewItemFromValue;
2017-03-21 15:51:56 +00:00
this.$dropdown = options.$dropdown;
this.$dropdownContainer = this.$dropdown.parent();
this.$dropdownFooter = this.$dropdownContainer.find('.dropdown-footer');
this.$createButton = this.$dropdownContainer.find('.js-dropdown-create-new-item');
2017-03-21 15:51:56 +00:00
this.buildDropdown();
this.bindEvents();
// Hide footer
2017-04-06 09:17:50 +00:00
this.toggleFooter(true);
2017-03-21 15:51:56 +00:00
}
buildDropdown() {
initDeprecatedJQueryDropdown(this.$dropdown, {
data: this.getData.bind(this),
2017-03-21 15:51:56 +00:00
filterable: true,
filterRemote: this.getDataRemote,
2017-03-21 15:51:56 +00:00
search: {
fields: ['text'],
2017-03-21 15:51:56 +00:00
},
selectable: true,
toggleLabel(selected) {
return selected && 'id' in selected ? escape(selected.title) : this.defaultToggleLabel;
2017-03-21 15:51:56 +00:00
},
fieldName: this.fieldName,
text(item) {
return escape(item.text);
2017-03-21 15:51:56 +00:00
},
id(item) {
return escape(item.id);
2017-03-21 15:51:56 +00:00
},
onFilter: this.toggleCreateNewButton.bind(this),
clicked: (options) => {
2017-05-04 20:55:36 +00:00
options.e.preventDefault();
2017-03-21 15:51:56 +00:00
this.onSelect();
2017-04-06 08:47:19 +00:00
},
2017-03-21 15:51:56 +00:00
});
}
clearDropdown() {
this.$dropdownContainer.find('.dropdown-content').html('');
this.$dropdownContainer.find('.dropdown-input-field').val('');
}
2017-03-21 15:51:56 +00:00
bindEvents() {
this.$createButton.on('click', this.onClickCreateWildcard.bind(this));
2017-03-21 15:51:56 +00:00
}
2017-04-06 13:39:48 +00:00
onClickCreateWildcard(e) {
e.preventDefault();
this.refreshData();
this.$dropdown.data('deprecatedJQueryDropdown').selectRowAtIndex();
}
refreshData() {
// Refresh the dropdown's data, which ends up calling `getData`
this.$dropdown.data('deprecatedJQueryDropdown').remote.execute();
2017-03-21 15:51:56 +00:00
}
getData(term, callback) {
this.getDataOption(term, (data = []) => {
// Ensure the selected item isn't already in the data to avoid duplicates
2018-10-24 19:17:03 +00:00
const alreadyHasSelectedItem =
this.selectedItem && data.some((item) => item.id === this.selectedItem.id);
let uniqueData = data;
if (!alreadyHasSelectedItem) {
uniqueData = data.concat(this.selectedItem || []);
}
callback(uniqueData);
});
2017-03-21 15:51:56 +00:00
}
createNewItemFromValue(newValue) {
if (this.createNewItemFromValueOption) {
return this.createNewItemFromValueOption(newValue);
}
return {
title: newValue,
id: newValue,
text: newValue,
};
}
toggleCreateNewButton(newValue) {
if (newValue) {
this.selectedItem = this.createNewItemFromValue(newValue);
2017-04-06 09:17:50 +00:00
2018-10-24 19:17:03 +00:00
this.$dropdownContainer.find('.js-dropdown-create-new-item code').text(newValue);
2017-03-21 15:51:56 +00:00
}
this.toggleFooter(!newValue);
2017-04-06 09:17:50 +00:00
}
toggleFooter(toggleState) {
this.$dropdownFooter.toggleClass('hidden', toggleState);
2017-03-21 15:51:56 +00:00
}
}