class DropdownWithTextarea extends Dropdown {
EVENTS = {
TEXTAREA_VALUE_CHANGED: 'textareaValueChanged'
};
constructor(containerElement, options = null) {
const dropdownElement = $(containerElement).find('.dropdown')[0];
super(dropdownElement, options);
this.containerElement = containerElement;
this.textareaElement = $(containerElement).find('textarea')[0];
this.showTextareaForItemId = $(containerElement).data('show-textarea-for-item-id');
this.emptyTextareaForItemId = $(containerElement).data('empty-textarea-for-item-id');
// Слушатель изменений textarea (реагирует на ввод и изменения)
if (this.textareaElement) {
$(this.textareaElement).on('input change', (e) => {
const value = $(this.textareaElement).val();
this.eventDispatcher.emit(this.EVENTS.TEXTAREA_VALUE_CHANGED, {
value: value,
containerElement: this.containerElement,
textareaElement: this.textareaElement,
originalEvent: e
});
});
}
}
onDropdownItemClick(dropdownItem) {
const itemId = $(dropdownItem).data('list-item-id');
let optionValue = null;
// Показываем/скрываем textarea в зависимости от выбранного элемента
if (itemId === this.showTextareaForItemId) {
$(this.textareaElement).show();
} else if (itemId === this.emptyTextareaForItemId) {
$(this.textareaElement).hide();
optionValue = '';
} else {
$(this.textareaElement).hide();
optionValue = $(dropdownItem).data('option-value');
if (!optionValue) {
optionValue = $(dropdownItem)[0].innerText;
if (optionValue) {
optionValue = optionValue.trim();
}
}
}
// Заполняем textarea если есть значение
if (optionValue !== undefined && optionValue !== null) {
$(this.textareaElement).val(optionValue);
// Уведомляем слушателей о программном изменении значения textarea
this.eventDispatcher.emit(this.EVENTS.TEXTAREA_VALUE_CHANGED, {
value: optionValue,
containerElement: this.containerElement,
textareaElement: this.textareaElement,
triggeredBy: 'dropdownItemClick'
});
}
super.onDropdownItemClick(dropdownItem);
}
getDropDownItemsData() {
let itemsData = super.getDropDownItemsData();
itemsData = itemsData.filter(item => {
return item.listItemId !== this.showTextareaForItemId && item.listItemId !== this.emptyTextareaForItemId;
});
return itemsData;
}
getAllDropDownItemsData() {
return super.getDropDownItemsData();
}
isItemSelected() {
const selectedId = $(this.dropdownElement).find('.dropdown-toggle').attr('data-selected-id');
return !(selectedId === this.showTextareaForItemId || selectedId === this.emptyTextareaForItemId);
}
isManualInput() {
const selectedId = $(this.dropdownElement).find('.dropdown-toggle').attr('data-selected-id');
return selectedId === this.showTextareaForItemId;
}
getTextareaValue() {
return $(this.textareaElement).val();
}
}