templates/js/library/dropdown_with_textarea/dropdown_with_textarea.js.twig line 1

Open in your IDE?
  1. class DropdownWithTextarea extends Dropdown {
  2.     EVENTS = {
  3.         TEXTAREA_VALUE_CHANGED: 'textareaValueChanged'
  4.     };
  5.     constructor(containerElement, options = null) {
  6.         const dropdownElement = $(containerElement).find('.dropdown')[0];
  7.         super(dropdownElement, options);
  8.         this.containerElement = containerElement;
  9.         this.textareaElement = $(containerElement).find('textarea')[0];
  10.         this.showTextareaForItemId = $(containerElement).data('show-textarea-for-item-id');
  11.         this.emptyTextareaForItemId = $(containerElement).data('empty-textarea-for-item-id');
  12.         // Слушатель изменений textarea (реагирует на ввод и изменения)
  13.         if (this.textareaElement) {
  14.             $(this.textareaElement).on('input change', (e) => {
  15.                 const value = $(this.textareaElement).val();
  16.                 this.eventDispatcher.emit(this.EVENTS.TEXTAREA_VALUE_CHANGED, {
  17.                     value: value,
  18.                     containerElement: this.containerElement,
  19.                     textareaElement: this.textareaElement,
  20.                     originalEvent: e
  21.                 });
  22.             });
  23.         }
  24.     }
  25.     onDropdownItemClick(dropdownItem) {
  26.         const itemId = $(dropdownItem).data('list-item-id');
  27.         let optionValue = null;
  28.         // Показываем/скрываем textarea в зависимости от выбранного элемента
  29.         if (itemId === this.showTextareaForItemId) {
  30.             $(this.textareaElement).show();
  31.         } else if (itemId === this.emptyTextareaForItemId) {
  32.             $(this.textareaElement).hide();
  33.             optionValue = '';
  34.         } else {
  35.             $(this.textareaElement).hide();
  36.             optionValue = $(dropdownItem).data('option-value');
  37.             if (!optionValue) {
  38.                 optionValue = $(dropdownItem)[0].innerText;
  39.                 if (optionValue) {
  40.                     optionValue = optionValue.trim();
  41.                 }
  42.             }
  43.         }
  44.         // Заполняем textarea если есть значение
  45.         if (optionValue !== undefined && optionValue !== null) {
  46.             $(this.textareaElement).val(optionValue);
  47.             // Уведомляем слушателей о программном изменении значения textarea
  48.             this.eventDispatcher.emit(this.EVENTS.TEXTAREA_VALUE_CHANGED, {
  49.                 value: optionValue,
  50.                 containerElement: this.containerElement,
  51.                 textareaElement: this.textareaElement,
  52.                 triggeredBy: 'dropdownItemClick'
  53.             });
  54.         }
  55.         super.onDropdownItemClick(dropdownItem);
  56.     }
  57.     getDropDownItemsData() {
  58.         let itemsData = super.getDropDownItemsData();
  59.         itemsData = itemsData.filter(item => {
  60.             return item.listItemId !== this.showTextareaForItemId && item.listItemId !== this.emptyTextareaForItemId;
  61.         });
  62.         return itemsData;
  63.     }
  64.     getAllDropDownItemsData() {
  65.         return super.getDropDownItemsData();
  66.     }
  67.     isItemSelected() {
  68.         const selectedId = $(this.dropdownElement).find('.dropdown-toggle').attr('data-selected-id');
  69.         return !(selectedId === this.showTextareaForItemId || selectedId === this.emptyTextareaForItemId);
  70.     }
  71.     isManualInput() {
  72.         const selectedId = $(this.dropdownElement).find('.dropdown-toggle').attr('data-selected-id');
  73.         return selectedId === this.showTextareaForItemId;
  74.     }
  75.     getTextareaValue() {
  76.         return $(this.textareaElement).val();
  77.     }
  78. }