2020-04-21 14:09:31 -04:00
|
|
|
import { escape } from 'lodash';
|
2019-09-19 11:06:08 -04:00
|
|
|
import '~/gl_dropdown';
|
2017-08-03 16:31:53 -04:00
|
|
|
|
2018-01-12 02:32:41 -05:00
|
|
|
export default class CreateItemDropdown {
|
2017-04-06 05:17:50 -04: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 11:51:56 -04:00
|
|
|
constructor(options) {
|
2018-01-12 02:32:41 -05:00
|
|
|
this.defaultToggleLabel = options.defaultToggleLabel;
|
|
|
|
this.fieldName = options.fieldName;
|
|
|
|
this.onSelect = options.onSelect || (() => {});
|
|
|
|
this.getDataOption = options.getData;
|
2019-06-03 18:51:02 -04:00
|
|
|
this.getDataRemote = Boolean(options.filterRemote);
|
2018-01-25 01:55:49 -05:00
|
|
|
this.createNewItemFromValueOption = options.createNewItemFromValue;
|
2017-03-21 11:51:56 -04:00
|
|
|
this.$dropdown = options.$dropdown;
|
|
|
|
this.$dropdownContainer = this.$dropdown.parent();
|
|
|
|
this.$dropdownFooter = this.$dropdownContainer.find('.dropdown-footer');
|
2018-01-12 02:32:41 -05:00
|
|
|
this.$createButton = this.$dropdownContainer.find('.js-dropdown-create-new-item');
|
2017-03-21 11:51:56 -04:00
|
|
|
|
|
|
|
this.buildDropdown();
|
|
|
|
this.bindEvents();
|
|
|
|
|
|
|
|
// Hide footer
|
2017-04-06 05:17:50 -04:00
|
|
|
this.toggleFooter(true);
|
2017-03-21 11:51:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
buildDropdown() {
|
|
|
|
this.$dropdown.glDropdown({
|
2018-01-12 02:32:41 -05:00
|
|
|
data: this.getData.bind(this),
|
2017-03-21 11:51:56 -04:00
|
|
|
filterable: true,
|
2018-08-06 18:21:39 -04:00
|
|
|
filterRemote: this.getDataRemote,
|
2017-03-21 11:51:56 -04:00
|
|
|
search: {
|
2018-01-25 01:55:49 -05:00
|
|
|
fields: ['text'],
|
2017-03-21 11:51:56 -04:00
|
|
|
},
|
|
|
|
selectable: true,
|
|
|
|
toggleLabel(selected) {
|
2020-04-21 14:09:31 -04:00
|
|
|
return selected && 'id' in selected ? escape(selected.title) : this.defaultToggleLabel;
|
2017-03-21 11:51:56 -04:00
|
|
|
},
|
2018-01-12 02:32:41 -05:00
|
|
|
fieldName: this.fieldName,
|
|
|
|
text(item) {
|
2020-04-21 14:09:31 -04:00
|
|
|
return escape(item.text);
|
2017-03-21 11:51:56 -04:00
|
|
|
},
|
2018-01-12 02:32:41 -05:00
|
|
|
id(item) {
|
2020-04-21 14:09:31 -04:00
|
|
|
return escape(item.id);
|
2017-03-21 11:51:56 -04:00
|
|
|
},
|
|
|
|
onFilter: this.toggleCreateNewButton.bind(this),
|
2018-10-24 15:17:03 -04:00
|
|
|
clicked: options => {
|
2017-05-04 16:55:36 -04:00
|
|
|
options.e.preventDefault();
|
2017-03-21 11:51:56 -04:00
|
|
|
this.onSelect();
|
2017-04-06 04:47:19 -04:00
|
|
|
},
|
2017-03-21 11:51:56 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:55:49 -05:00
|
|
|
clearDropdown() {
|
|
|
|
this.$dropdownContainer.find('.dropdown-content').html('');
|
|
|
|
this.$dropdownContainer.find('.dropdown-input-field').val('');
|
|
|
|
}
|
|
|
|
|
2017-03-21 11:51:56 -04:00
|
|
|
bindEvents() {
|
2018-01-12 02:32:41 -05:00
|
|
|
this.$createButton.on('click', this.onClickCreateWildcard.bind(this));
|
2017-03-21 11:51:56 -04:00
|
|
|
}
|
|
|
|
|
2017-04-06 09:39:48 -04:00
|
|
|
onClickCreateWildcard(e) {
|
2018-01-12 02:32:41 -05:00
|
|
|
e.preventDefault();
|
|
|
|
|
2018-01-25 01:55:49 -05:00
|
|
|
this.refreshData();
|
|
|
|
this.$dropdown.data('glDropdown').selectRowAtIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshData() {
|
2018-01-12 02:32:41 -05:00
|
|
|
// Refresh the dropdown's data, which ends up calling `getData`
|
2017-03-21 11:51:56 -04:00
|
|
|
this.$dropdown.data('glDropdown').remote.execute();
|
|
|
|
}
|
|
|
|
|
2018-01-12 02:32:41 -05:00
|
|
|
getData(term, callback) {
|
|
|
|
this.getDataOption(term, (data = []) => {
|
2018-01-18 03:02:21 -05:00
|
|
|
// Ensure the selected item isn't already in the data to avoid duplicates
|
2018-10-24 15:17:03 -04:00
|
|
|
const alreadyHasSelectedItem =
|
|
|
|
this.selectedItem && data.some(item => item.id === this.selectedItem.id);
|
2018-01-18 03:02:21 -05:00
|
|
|
|
|
|
|
let uniqueData = data;
|
|
|
|
if (!alreadyHasSelectedItem) {
|
|
|
|
uniqueData = data.concat(this.selectedItem || []);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(uniqueData);
|
2018-01-12 02:32:41 -05:00
|
|
|
});
|
2017-03-21 11:51:56 -04:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:55:49 -05: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 05:17:50 -04:00
|
|
|
|
2018-10-24 15:17:03 -04:00
|
|
|
this.$dropdownContainer.find('.js-dropdown-create-new-item code').text(newValue);
|
2017-03-21 11:51:56 -04:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:55:49 -05:00
|
|
|
this.toggleFooter(!newValue);
|
2017-04-06 05:17:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
toggleFooter(toggleState) {
|
|
|
|
this.$dropdownFooter.toggleClass('hidden', toggleState);
|
2017-03-21 11:51:56 -04:00
|
|
|
}
|
|
|
|
}
|