import $ from 'jquery'; import Slider from "../../sections/slider/slider"; import Masonry from "../../_global/scripts/masonry"; class IHKGallery { constructor(section) { this.section = section.addClass('initiated'); this.wrapper = section.find('.gallery-wrapper'); this.items = this.wrapper.children('.image'); this.gridInitialized = false; this.currentPage = -1; this.visibleItems = []; this.type = section.attr('data-type'); this.initPopup(); if (this.type === 'first-image' || this.type === 'single-image') { this.initSingleThumb(); } else if (this.type === 'grid') { this.initThumbs(); this.initLoading(); } else if (this.type === 'masonry') { this.initMasonry(); } this.initScrollCheck(); $('body').on('lazyload-gallery-image', () => { this.lazyLoadSingleImages.call(this); }) } initScrollCheck() { window.addEventListener("scroll", () => { window.requestAnimationFrame(() => { this.scrollCheck(); }) }, {passive: true}); this.scrollCheck(); } scrollCheck() { const w = $(window); if (this.type === 'first-image' || this.type === 'single-image') { if (!this.isLoaded && w.scrollTop() + w.height() + 100 > this.section.offset().top) { const thumb = this.items.first(); thumb.find('img').attr('src', thumb.data(this.type === 'first-image' ? 'thumb' : 'full')); this.isLoaded = true; } } else if (this.type === 'grid') { if (!this.gridInitialized && this.items.length > this.visibleItems.length && w.scrollTop() + w.height() + 300 > ($(this.section).offset().top + $(this.section).height())) { this.currentPage++; this.loadThumbs(); this.gridInitialized = true; } } } lazyLoadSingleImages() { if (this.type === 'first-image' || this.type === 'single-image') { if (!this.isLoaded) { const thumb = this.items.first(); thumb.find('img').attr('src', thumb.data(this.type === 'first-image' ? 'thumb' : 'full')); this.isLoaded = true; } } } initSingleThumb() { const first = this.items.first().addClass('loading'); const a = $('').attr('data-index', 0).appendTo(first); const img = $('' + first.attr(').appendTo(a); if (first.data('copyright')) { $('').appendTo(a); $('' + first.data('copyright') + '').appendTo(a); } first.find('.image-description').appendTo(this.wrapper); this.isLoaded = false; img.one('load', () => { this.section.addClass('loaded'); first.removeClass('loading'); }); if (this.items.length > 1) { a.append($('').text(this.items.length + ' Bilder')); first.find('.image-description').hide(); } if (this.section.data('type') === 'single-image') { const ratio = (Math.round(first.data('height') / first.data('width') * 1000) / 10) + '%'; a.css('padding-top', ratio); first.find('.copyright').appendTo(a).css('max-width', ratio); } this.section.closest('.main-col').addClass('clearfix'); } initThumbs() { this.items.each(function (i) { const item = $(this); $('').attr('tabindex', '-1').attr('data-index', i).appendTo(item); }) } initMasonry() { this.items.each(function (i) { const item = $(this); const a = $('').attr('data-index', i).appendTo(item); a.css('padding-top', Math.round(item.data('height') / item.data('width') * 10000) / 100 + '%'); // Copyright für Masonry-Element vorbereiten if (item.data('copyright')) { a.append('' + item.data('copyright') + ''); } }) const btnText = window.ihk.translations.loadMoreImages; new Masonry( this.wrapper, btnText, [ {minWidth: 0, batchSize: 6}, {minWidth: 0, batchSize: 6}, {minWidth: 567, batchSize: 9}, {minWidth: 1000, batchSize: 12} ], 'primary-light'); } initLoading() { const imagesPerPage = this.section.data('per-page'); const buttonWrapper = $('
').appendTo(this.section); const buttonText = window.ihk.translations.loadMoreImages; if (this.items.length > imagesPerPage) { this.moreButton = $('