|
| 1 | +/* |
| 2 | + * Copyright 2021 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +__webpack_public_path__ = window.webStoriesCarouselSettings.publicPath; |
| 18 | + |
| 19 | +document.addEventListener('DOMContentLoaded', () => { |
| 20 | + void (async () => { |
| 21 | + // Do not initialize Glider.js if browser supports CSS carousels. |
| 22 | + if (CSS.supports('scroll-marker-group: after')) { |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + const carouselWrappers: NodeListOf<HTMLElement> = document.querySelectorAll( |
| 27 | + '.web-stories-list__carousel' |
| 28 | + ); |
| 29 | + |
| 30 | + const isRTL = |
| 31 | + window.webStoriesCarouselSettings.config.isRTL || |
| 32 | + 'rtl' === document.documentElement.getAttribute('dir'); |
| 33 | + |
| 34 | + if (!carouselWrappers.length) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + const { default: Glider } = await import( |
| 39 | + /* webpackChunkName: "chunk-web-stories-glider" */ '@web-stories-wp/glider' // @ts-expexct-error |
| 40 | + ); |
| 41 | + |
| 42 | + carouselWrappers.forEach((carouselWrapper: HTMLElement) => { |
| 43 | + // For multiple instance of the glider we need to link nav arrows appropriately. |
| 44 | + const carouselId = carouselWrapper.dataset.id; |
| 45 | + |
| 46 | + const navArrows = !isRTL |
| 47 | + ? { |
| 48 | + prev: `.${carouselId} .glider-prev`, |
| 49 | + next: `.${carouselId} .glider-next`, |
| 50 | + } |
| 51 | + : { |
| 52 | + prev: `.${carouselId} .glider-next`, |
| 53 | + next: `.${carouselId} .glider-prev`, |
| 54 | + }; |
| 55 | + |
| 56 | + const isCircles = carouselWrapper.classList.contains('circles'); |
| 57 | + const itemStyle = window.getComputedStyle( |
| 58 | + carouselWrapper.querySelector( |
| 59 | + '.web-stories-list__story' |
| 60 | + ) as unknown as HTMLElement |
| 61 | + ); |
| 62 | + |
| 63 | + const itemWidth = |
| 64 | + Number.parseFloat(itemStyle.width) + |
| 65 | + (Number.parseFloat(itemStyle.marginLeft) + |
| 66 | + Number.parseFloat(itemStyle.marginRight)); |
| 67 | + |
| 68 | + // For circles view we would want to keep it auto. |
| 69 | + if (isCircles) { |
| 70 | + /* eslint-disable-next-line no-new -- we do not store the object as no further computation required with the built object. */ |
| 71 | + new Glider(carouselWrapper, { |
| 72 | + // Set to `auto` and provide item width to adjust to viewport |
| 73 | + slidesToShow: 'auto', |
| 74 | + slidesToScroll: 'auto', |
| 75 | + itemWidth, |
| 76 | + duration: 0.25, |
| 77 | + scrollLock: true, |
| 78 | + arrows: navArrows, |
| 79 | + }); |
| 80 | + } else { |
| 81 | + // For Box Carousel we are showing single slide below tablets viewport. |
| 82 | + /* eslint-disable-next-line no-new -- we do not store the object as no further computation required with the built object. */ |
| 83 | + new Glider(carouselWrapper, { |
| 84 | + slidesToShow: 'auto', |
| 85 | + slidesToScroll: 'auto', |
| 86 | + itemWidth, |
| 87 | + duration: 0.25, |
| 88 | + scrollLock: true, |
| 89 | + arrows: navArrows, |
| 90 | + }); |
| 91 | + } |
| 92 | + }); |
| 93 | + })(); |
| 94 | +}); |
0 commit comments