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

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