2017-08-03 20:31:53 +00:00
|
|
|
import _ from 'underscore';
|
|
|
|
|
2018-01-12 07:32:41 +00:00
|
|
|
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) {
|
2018-01-12 07:32:41 +00:00
|
|
|
this.defaultToggleLabel = options.defaultToggleLabel;
|
|
|
|
this.fieldName = options.fieldName;
|
|
|
|
this.onSelect = options.onSelect || (() => {});
|
|
|
|
this.getDataOption = options.getData;
|
2019-06-03 22:51:02 +00:00
|
|
|
this.getDataRemote = Boolean(options.filterRemote);
|
2018-01-25 06:55:49 +00:00
|
|
|
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');
|
2018-01-12 07:32:41 +00:00
|
|
|
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() {
|
|
|
|
this.$dropdown.glDropdown({
|
2018-01-12 07:32:41 +00:00
|
|
|
data: this.getData.bind(this),
|
2017-03-21 15:51:56 +00:00
|
|
|
filterable: true,
|
2018-08-06 22:21:39 +00:00
|
|
|
filterRemote: this.getDataRemote,
|
2017-03-21 15:51:56 +00:00
|
|
|
search: {
|
2018-01-25 06:55:49 +00:00
|
|
|
fields: ['text'],
|
2017-03-21 15:51:56 +00:00
|
|
|
},
|
|
|
|
selectable: true,
|
|
|
|
toggleLabel(selected) {
|
2018-10-24 19:17:03 +00:00
|
|
|
return selected && 'id' in selected ? _.escape(selected.title) : this.defaultToggleLabel;
|
2017-03-21 15:51:56 +00:00
|
|
|
},
|
2018-01-12 07:32:41 +00:00
|
|
|
fieldName: this.fieldName,
|
|
|
|
text(item) {
|
2018-01-25 06:55:49 +00:00
|
|
|
return _.escape(item.text);
|
2017-03-21 15:51:56 +00:00
|
|
|
},
|
2018-01-12 07:32:41 +00:00
|
|
|
id(item) {
|
|
|
|
return _.escape(item.id);
|
2017-03-21 15:51:56 +00:00
|
|
|
},
|
|
|
|
onFilter: this.toggleCreateNewButton.bind(this),
|
2018-10-24 19:17:03 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-25 06:55:49 +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() {
|
2018-01-12 07:32:41 +00:00
|
|
|
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) {
|
2018-01-12 07:32:41 +00:00
|
|
|
e.preventDefault();
|
|
|
|
|
2018-01-25 06:55:49 +00:00
|
|
|
this.refreshData();
|
|
|
|
this.$dropdown.data('glDropdown').selectRowAtIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshData() {
|
2018-01-12 07:32:41 +00:00
|
|
|
// Refresh the dropdown's data, which ends up calling `getData`
|
2017-03-21 15:51:56 +00:00
|
|
|
this.$dropdown.data('glDropdown').remote.execute();
|
|
|
|
}
|
|
|
|
|
2018-01-12 07:32:41 +00:00
|
|
|
getData(term, callback) {
|
|
|
|
this.getDataOption(term, (data = []) => {
|
2018-01-18 08:02:21 +00:00
|
|
|
// 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);
|
2018-01-18 08:02:21 +00:00
|
|
|
|
|
|
|
let uniqueData = data;
|
|
|
|
if (!alreadyHasSelectedItem) {
|
|
|
|
uniqueData = data.concat(this.selectedItem || []);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(uniqueData);
|
2018-01-12 07:32:41 +00:00
|
|
|
});
|
2017-03-21 15:51:56 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 06:55:49 +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
|
|
|
}
|
|
|
|
|
2018-01-25 06:55:49 +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
|
|
|
}
|
|
|
|
}
|