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

120 lines
3.2 KiB
JavaScript
Raw Normal View History

import _ from 'underscore';
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) {
this.defaultToggleLabel = options.defaultToggleLabel;
this.fieldName = options.fieldName;
this.onSelect = options.onSelect || (() => {});
this.getDataOption = options.getData;
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');
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({
data: this.getData.bind(this),
2017-03-21 11:51:56 -04:00
filterable: true,
remote: false,
search: {
fields: ['text'],
2017-03-21 11:51:56 -04:00
},
selectable: true,
toggleLabel(selected) {
return (selected && 'id' in selected) ? _.escape(selected.title) : this.defaultToggleLabel;
2017-03-21 11:51:56 -04:00
},
fieldName: this.fieldName,
text(item) {
return _.escape(item.text);
2017-03-21 11:51:56 -04:00
},
id(item) {
return _.escape(item.id);
2017-03-21 11:51:56 -04:00
},
onFilter: this.toggleCreateNewButton.bind(this),
2017-05-04 16:55:36 -04:00
clicked: (options) => {
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
});
}
clearDropdown() {
this.$dropdownContainer.find('.dropdown-content').html('');
this.$dropdownContainer.find('.dropdown-input-field').val('');
}
2017-03-21 11:51:56 -04:00
bindEvents() {
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) {
e.preventDefault();
this.refreshData();
this.$dropdown.data('glDropdown').selectRowAtIndex();
}
refreshData() {
// Refresh the dropdown's data, which ends up calling `getData`
2017-03-21 11:51:56 -04:00
this.$dropdown.data('glDropdown').remote.execute();
}
getData(term, callback) {
this.getDataOption(term, (data = []) => {
// Ensure the selected item isn't already in the data to avoid duplicates
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 11:51:56 -04: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
2017-03-21 11:51:56 -04:00
this.$dropdownContainer
.find('.js-dropdown-create-new-item code')
.text(newValue);
2017-03-21 11:51:56 -04: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
}
}