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

Open in your IDE?
  1. class Dropdown {
  2.     EVENTS = {
  3.         ITEM_CLICK: 'itemClick',
  4.     };
  5.     options = {
  6.         preventDropdownClose: false,
  7.     };
  8.     constructor(dropdownElement, options = null) {
  9.         this.options = { ...this.options, ...options };
  10.         this.dropdownElement = dropdownElement;
  11.         this.dropdownButton = $(dropdownElement).find('.dropdown-toggle')[0];
  12.         this.eventDispatcher = new EventDispatcher();
  13.         this._addEventListeners();
  14.         if (this.options.preventDropdownClose) {
  15.                 this._preventDropdownClose();
  16.         }
  17.     }
  18.     _preventDropdownClose() {
  19.         const dropdownMenu = $(this.dropdownElement).find('.dropdown-menu');
  20.         dropdownMenu.on('click', function(e) {
  21.             e.stopPropagation();
  22.         });
  23.     }
  24.     setDropdownButtonText(text) {
  25.         if (this.dropdownButton) {
  26.             this.dropdownButton.textContent = text;
  27.         }
  28.     }
  29.     getDropdownButtonText() {
  30.         return this.dropdownButton ? this.dropdownButton.textContent : '';
  31.     }
  32.     onDropdownItemClick(dropdownItem) {
  33.         const selectedText = $(dropdownItem)[0].innerText;
  34.         const selectedId = $(dropdownItem).data('list-item-id');
  35.         let result = this.eventDispatcher.emit(this.EVENTS.ITEM_CLICK, {
  36.             dropdownItem,
  37.             selectedText,
  38.             selectedId,
  39.             dropdownElement: this.dropdownElement,
  40.         });
  41.         if (result.defaultPrevented) {
  42.             return;
  43.         }
  44.         const dropdownButton = $(this.dropdownElement).find('.dropdown-toggle');
  45.         const selectElement = $(this.dropdownElement).find('select');
  46.         dropdownButton.attr('data-selected-id', selectedId);
  47.         dropdownButton.html(dropdownButton.is('.form-select') ? selectedText
  48.             : selectedText.replace(/\n/g, '<br>'));
  49.         selectElement.val(selectedId).trigger('change');
  50.     }
  51.     _addEventListeners() {
  52.         const dropdownItems = $(this.dropdownElement).find('.dropdown-item');
  53.         const me = this;
  54.         dropdownItems.click(function() {
  55.             me.onDropdownItemClick($(this)[0]);
  56.         });
  57.     }
  58.     createNewDropDownItemsByData(itemsData) {
  59.         const dropdownMenu = $(this.dropdownElement).find('.dropdown-menu');
  60.         dropdownMenu.empty();
  61.         itemsData.forEach(itemData => {
  62.             const itemElement = $('<li></li>');
  63.             const linkElement = $('<a></a>')
  64.                 .addClass('dropdown-item')
  65.                 .attr('href', 'javascript:void(0)')
  66.                 .attr('data-list-item-id', itemData.listItemId)
  67.                 .attr('data-option-value', itemData.optionValue)
  68.                 .html(itemData.optionValue.toString().replace(/\n/g, '<br>'));
  69.             itemElement.append(linkElement);
  70.             dropdownMenu.append(itemElement);
  71.         });
  72.         // Re-add event listeners to new items
  73.         this._addEventListeners();
  74.     }
  75.     getDropDownItemValue(item) {
  76.         let optionValue = $(item).data('option-value');
  77.         if (!optionValue) {
  78.             optionValue = $(item)[0].innerText;
  79.             if (optionValue) {
  80.                 optionValue = optionValue.trim();
  81.             }
  82.         }
  83.         return optionValue;
  84.     }
  85.     getDropDownItemsData() {
  86.         const itemsData = [];
  87.         const dropdownItems = $(this.dropdownElement).find('.dropdown-item');
  88.         dropdownItems.each((index, item) => {
  89.             const itemId = $(item).data('list-item-id');
  90.             let optionValue = this.getDropDownItemValue(item);
  91.             itemsData.push({
  92.                 listItemId: itemId,
  93.                 optionValue: optionValue
  94.             });
  95.         });
  96.         return itemsData;
  97.     }
  98.     getSelectedItem() {
  99.         const selectedId = $(this.dropdownElement).find('.dropdown-toggle').attr('data-selected-id');
  100.         const dropdownItems = $(this.dropdownElement).find('.dropdown-item');
  101.         let selectedItem = null;
  102.         dropdownItems.each((index, item) => {
  103.             const itemId = $(item).data('list-item-id');
  104.             if (itemId.toString() === selectedId.toString()) {
  105.                 selectedItem = item;
  106.                 return false; // Break the loop
  107.             }
  108.         });
  109.         return selectedItem;
  110.     }
  111.     getSelectedItemValue() {
  112.         let item = this.getSelectedItem();
  113.         return this.getDropDownItemValue(item);
  114.     }
  115.     getSelectedItemId() {
  116.         return $(this.dropdownElement).find('.dropdown-toggle').attr('data-selected-id');
  117.     }
  118. }