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

Open in your IDE?
  1. class GalleryCarousel {
  2.     options = {
  3.         elementClickTriggerSelector: null,
  4.         slideIdAttrName: null,
  5.         dataTableInstance: null,
  6.     };
  7.     constructor(modalId, options = null) {
  8.         this.modalId = modalId;
  9.         let elementClickTriggerSelector = this.$modal.data('element-click-trigger-selector');
  10.         this.options.elementClickTriggerSelector = elementClickTriggerSelector;
  11.         if (options) {
  12.             this.options = { ...this.options, ...options };
  13.         }
  14.         this.currentGalleryIndex = 0;
  15.         this.initEvents();
  16.         if (this.options.elementClickTriggerSelector) {
  17.             $(this.options.elementClickTriggerSelector).on('click', (e) => {
  18.                 e.preventDefault();
  19.                 const index = $(e.target).data('gallery-index');
  20.                 this.open(index);
  21.             });
  22.         }
  23.         if (this.options.slideIdAttrName && this.options.dataTableInstance) {
  24.             this.setGalleryIndexRetriever((increment, slideElement, currentGalleryIndex) => {
  25.                 let tableData = this.options.dataTableInstance.getAllFilteredTableData();
  26.                 let itemIdColIndex = this.options.dataTableInstance.getColumnIndexByColumnId('id');
  27.                 if (itemIdColIndex === -1) {
  28.                     throw new Error("Item ID column not found in data table");
  29.                 }
  30.                 let curTableRowIndex = null;
  31.                 let curItemId = parseInt($(slideElement).attr(this.options.slideIdAttrName));
  32.                 for (let i = 0; i < tableData.length; i++) {
  33.                     let tableRowItemId = parseInt(tableData[i][itemIdColIndex]);
  34.                     if (tableRowItemId === curItemId) {
  35.                         curTableRowIndex = i;
  36.                         break;
  37.                     }
  38.                 }
  39.                 if (curTableRowIndex === null) {
  40.                     throw new Error("Current slide id not found in filtered table data");
  41.                 }
  42.                 //find next or prev slide index in filtered table data
  43.                 curTableRowIndex += increment;
  44.                 if (curTableRowIndex < 0) {
  45.                     curTableRowIndex = tableData.length - 1;
  46.                 } else if (curTableRowIndex >= tableData.length) {
  47.                     curTableRowIndex = 0;
  48.                 }
  49.                 let nextItemId = parseInt(tableData[curTableRowIndex][itemIdColIndex]);
  50.                 //find gallery index by item id
  51.                 for (let j = 0; j < this.$slides.length; j++) {
  52.                     let itemId = parseInt($(this.$slides[j]).attr(this.options.slideIdAttrName));
  53.                     if (itemId === nextItemId) {
  54.                         return j;
  55.                     }
  56.                 }
  57.                 return 0
  58.             });
  59.         }
  60.     }
  61.     get $modal() {
  62.         return $('#' + this.modalId);
  63.     }
  64.     get $slides() {
  65.         return this.$modal.find('.gallery-slide');
  66.     }
  67.     get $title() {
  68.         return this.$modal.find('.gallery-title');
  69.     }
  70.     get $slide() {
  71.         return this.$slides.eq(this.currentGalleryIndex);
  72.     }
  73.     initEvents() {
  74.         $(document).on('click', `#${this.modalId} .gallery-next-btn`, () => this.next());
  75.         $(document).on('click', `#${this.modalId} .gallery-prev-btn`, () => this.prev());
  76.         // Support keyboard navigation
  77.         $(document).on('keydown', `#${this.modalId}`, (e) => {
  78.             if (e.key === "ArrowRight") {
  79.                 this.next();
  80.             } else if (e.key === "ArrowLeft") {
  81.                 this.prev();
  82.             }
  83.         });
  84.         // Focus modal on show to enable keyboard events
  85.         $(document).on('shown.bs.modal', `#${this.modalId}`, () => {
  86.             this.$modal.trigger('focus');
  87.         });
  88.     }
  89.     updateGallery() {
  90.         const $slides = this.$slides;
  91.         $slides.addClass('d-none').removeClass('d-flex');
  92.         if (this.currentGalleryIndex < 0) {
  93.             this.currentGalleryIndex = $slides.length - 1;
  94.         } else if (this.currentGalleryIndex >= $slides.length) {
  95.             this.currentGalleryIndex = 0;
  96.         }
  97.         const $currentSlide = $slides.eq(this.currentGalleryIndex);
  98.         if ($currentSlide.length) {
  99.             $currentSlide.removeClass('d-none').addClass('d-flex');
  100.             // Update title
  101.             let isPermanentTitle = this.$title.hasClass('permanent');
  102.             if (!isPermanentTitle) {
  103.                 let slideText = $currentSlide.data('slide-text');
  104.                 const name = slideText || ('Изображение ' + (this.currentGalleryIndex + 1));
  105.                 this.$title.text(name);}
  106.         }
  107.     }
  108.     setGalleryIndexRetriever(callback) {
  109.         this.galleryIndexRetriever = callback;
  110.     }
  111.     open(index) {
  112.         this.currentGalleryIndex = index;
  113.         this.updateGallery();
  114.         this.$modal.modal('show');
  115.     }
  116.     next() {
  117.         if (this.$slides.length > 0) {
  118.             if (this.galleryIndexRetriever) {
  119.                 this.currentGalleryIndex = this.galleryIndexRetriever(1, this.$slide, this.currentGalleryIndex);
  120.             } else {
  121.                 this.currentGalleryIndex++;
  122.             }
  123.             this.updateGallery();
  124.         }
  125.     }
  126.     prev() {
  127.         if (this.$slides.length > 0) {
  128.             if (this.galleryIndexRetriever) {
  129.                 this.currentGalleryIndex = this.galleryIndexRetriever(-1, this.$slide, this.currentGalleryIndex);
  130.             } else {
  131.                 this.currentGalleryIndex--;
  132.             }
  133.             this.updateGallery();
  134.         }
  135.     }
  136. }