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