|
553 | 553 | var Bookshelf = { |
554 | 554 | DEFAULT_BOOKS_PER_SHELF: 5, |
555 | 555 |
|
556 | | - init: function (gallery) { |
557 | | - var carousel = gallery.querySelector('.ep-pdf-gallery__carousel'); |
558 | | - if (!carousel) return; |
559 | | - |
560 | | - var track = carousel.querySelector('.ep-pdf-gallery__carousel-track'); |
561 | | - if (!track) return; |
562 | | - |
563 | | - var items = Array.prototype.slice.call(track.querySelectorAll('.ep-pdf-gallery__item')); |
564 | | - if (!items.length) return; |
| 556 | + _getPerShelf: function (gallery) { |
| 557 | + var w = window.innerWidth; |
| 558 | + if (w <= 767) { |
| 559 | + return parseInt(gallery.dataset.columnsMobile, 10) || 1; |
| 560 | + } else if (w <= 1024) { |
| 561 | + return parseInt(gallery.dataset.columnsTablet, 10) || 2; |
| 562 | + } |
| 563 | + return parseInt(gallery.dataset.columns, 10) || this.DEFAULT_BOOKS_PER_SHELF; |
| 564 | + }, |
565 | 565 |
|
566 | | - // Get shelf style from gallery data attribute |
567 | | - var shelfStyle = gallery.dataset.shelfStyle || 'dark-wood'; |
| 566 | + _buildRows: function (gallery, container, items) { |
| 567 | + var perShelf = this._getPerShelf(gallery); |
568 | 568 |
|
569 | | - // Get books per shelf from columns setting |
570 | | - var perShelf = parseInt(gallery.dataset.columns, 10) || this.DEFAULT_BOOKS_PER_SHELF; |
| 569 | + // Store current perShelf to avoid unnecessary rebuilds |
| 570 | + if (container._lastPerShelf === perShelf) return; |
| 571 | + container._lastPerShelf = perShelf; |
571 | 572 |
|
572 | | - // Create bookshelf container replacing carousel |
573 | | - var container = document.createElement('div'); |
574 | | - container.className = 'ep-pdf-gallery__bookshelf-container'; |
| 573 | + // Clear existing rows |
| 574 | + container.innerHTML = ''; |
575 | 575 |
|
576 | 576 | // Chunk items into rows |
577 | 577 | for (var i = 0; i < items.length; i += perShelf) { |
|
585 | 585 |
|
586 | 586 | container.appendChild(row); |
587 | 587 | } |
| 588 | + }, |
588 | 589 |
|
589 | | - // Replace carousel with bookshelf container |
590 | | - carousel.parentNode.replaceChild(container, carousel); |
| 590 | + init: function (gallery) { |
| 591 | + var carousel = gallery.querySelector('.ep-pdf-gallery__carousel'); |
| 592 | + var container = gallery.querySelector('.ep-pdf-gallery__bookshelf-container'); |
| 593 | + var items; |
| 594 | + |
| 595 | + if (carousel) { |
| 596 | + var track = carousel.querySelector('.ep-pdf-gallery__carousel-track'); |
| 597 | + if (!track) return; |
| 598 | + |
| 599 | + items = Array.prototype.slice.call(track.querySelectorAll('.ep-pdf-gallery__item')); |
| 600 | + if (!items.length) return; |
| 601 | + |
| 602 | + // Create bookshelf container replacing carousel |
| 603 | + container = document.createElement('div'); |
| 604 | + container.className = 'ep-pdf-gallery__bookshelf-container'; |
| 605 | + carousel.parentNode.replaceChild(container, carousel); |
| 606 | + } else if (container) { |
| 607 | + items = Array.prototype.slice.call(container.querySelectorAll('.ep-pdf-gallery__item')); |
| 608 | + if (!items.length) return; |
| 609 | + } else { |
| 610 | + return; |
| 611 | + } |
| 612 | + |
| 613 | + // Store items reference for resize rebuilds |
| 614 | + gallery._bookshelfItems = items; |
| 615 | + gallery._bookshelfContainer = container; |
| 616 | + |
| 617 | + var self = this; |
| 618 | + self._buildRows(gallery, container, items); |
| 619 | + |
| 620 | + // Rebuild rows on resize for responsive columns |
| 621 | + if (!gallery._bookshelfResizeListener) { |
| 622 | + var resizeTimer; |
| 623 | + gallery._bookshelfResizeListener = function () { |
| 624 | + clearTimeout(resizeTimer); |
| 625 | + resizeTimer = setTimeout(function () { |
| 626 | + self._buildRows(gallery, gallery._bookshelfContainer, gallery._bookshelfItems); |
| 627 | + }, 150); |
| 628 | + }; |
| 629 | + window.addEventListener('resize', gallery._bookshelfResizeListener); |
| 630 | + } |
591 | 631 | } |
592 | 632 | }; |
593 | 633 |
|
|
0 commit comments