class Dropdown {
EVENTS = {
ITEM_CLICK: 'itemClick',
};
options = {
preventDropdownClose: false,
};
constructor(dropdownElement, options = null) {
this.options = { ...this.options, ...options };
this.dropdownElement = dropdownElement;
this.dropdownButton = $(dropdownElement).find('.dropdown-toggle')[0];
this.eventDispatcher = new EventDispatcher();
this._addEventListeners();
if (this.options.preventDropdownClose) {
this._preventDropdownClose();
}
}
_preventDropdownClose() {
const dropdownMenu = $(this.dropdownElement).find('.dropdown-menu');
dropdownMenu.on('click', function(e) {
e.stopPropagation();
});
}
setDropdownButtonText(text) {
if (this.dropdownButton) {
this.dropdownButton.textContent = text;
}
}
getDropdownButtonText() {
return this.dropdownButton ? this.dropdownButton.textContent : '';
}
onDropdownItemClick(dropdownItem) {
const selectedText = $(dropdownItem)[0].innerText;
const selectedId = $(dropdownItem).data('list-item-id');
let result = this.eventDispatcher.emit(this.EVENTS.ITEM_CLICK, {
dropdownItem,
selectedText,
selectedId,
dropdownElement: this.dropdownElement,
});
if (result.defaultPrevented) {
return;
}
const dropdownButton = $(this.dropdownElement).find('.dropdown-toggle');
const selectElement = $(this.dropdownElement).find('select');
dropdownButton.attr('data-selected-id', selectedId);
dropdownButton.html(dropdownButton.is('.form-select') ? selectedText
: selectedText.replace(/\n/g, '<br>'));
selectElement.val(selectedId).trigger('change');
}
_addEventListeners() {
const dropdownItems = $(this.dropdownElement).find('.dropdown-item');
const me = this;
dropdownItems.click(function() {
me.onDropdownItemClick($(this)[0]);
});
}
createNewDropDownItemsByData(itemsData) {
const dropdownMenu = $(this.dropdownElement).find('.dropdown-menu');
dropdownMenu.empty();
itemsData.forEach(itemData => {
const itemElement = $('<li></li>');
const linkElement = $('<a></a>')
.addClass('dropdown-item')
.attr('href', 'javascript:void(0)')
.attr('data-list-item-id', itemData.listItemId)
.attr('data-option-value', itemData.optionValue)
.html(itemData.optionValue.toString().replace(/\n/g, '<br>'));
itemElement.append(linkElement);
dropdownMenu.append(itemElement);
});
// Re-add event listeners to new items
this._addEventListeners();
}
getDropDownItemValue(item) {
let optionValue = $(item).data('option-value');
if (!optionValue) {
optionValue = $(item)[0].innerText;
if (optionValue) {
optionValue = optionValue.trim();
}
}
return optionValue;
}
getDropDownItemsData() {
const itemsData = [];
const dropdownItems = $(this.dropdownElement).find('.dropdown-item');
dropdownItems.each((index, item) => {
const itemId = $(item).data('list-item-id');
let optionValue = this.getDropDownItemValue(item);
itemsData.push({
listItemId: itemId,
optionValue: optionValue
});
});
return itemsData;
}
getSelectedItem() {
const selectedId = $(this.dropdownElement).find('.dropdown-toggle').attr('data-selected-id');
const dropdownItems = $(this.dropdownElement).find('.dropdown-item');
let selectedItem = null;
dropdownItems.each((index, item) => {
const itemId = $(item).data('list-item-id');
if (itemId.toString() === selectedId.toString()) {
selectedItem = item;
return false; // Break the loop
}
});
return selectedItem;
}
getSelectedItemValue() {
let item = this.getSelectedItem();
return this.getDropDownItemValue(item);
}
getSelectedItemId() {
return $(this.dropdownElement).find('.dropdown-toggle').attr('data-selected-id');
}
}