|
| 1 | +import { addEventListener, raf, removeEventListener } from '../../utils/helpers'; |
| 2 | + |
| 3 | +/** |
| 4 | + * This is a plugin for Swiper that allows it to work |
| 5 | + * with Ionic Framework and the routing integrations. |
| 6 | + * Without this plugin, Swiper would be incapable of correctly |
| 7 | + * determining the dimensions of the slides component as |
| 8 | + * each view is initially hidden before transitioning in. |
| 9 | + */ |
| 10 | +const setupSwiperInIonic = (swiper: any, watchForIonPageChanges = true) => { |
| 11 | + if (typeof (window as any) === 'undefined') { return; } |
| 12 | + |
| 13 | + const swiperEl = swiper.el; |
| 14 | + const ionPage = swiperEl.closest('.ion-page'); |
| 15 | + |
| 16 | + if (!ionPage) { |
| 17 | + if (watchForIonPageChanges) { |
| 18 | + |
| 19 | + /** |
| 20 | + * If no ion page found, it is possible |
| 21 | + * that we are in the overlay setup step |
| 22 | + * where the inner component has been |
| 23 | + * created but not attached to the DOM yet. |
| 24 | + * If so, wait for the .ion-page class to |
| 25 | + * appear on the root div and re-run setup. |
| 26 | + */ |
| 27 | + const rootNode = swiperEl.getRootNode(); |
| 28 | + if (rootNode.tagName === 'DIV') { |
| 29 | + const mo = new MutationObserver((m: MutationRecord[]) => { |
| 30 | + const mutation = m[0]; |
| 31 | + const wasEmpty = mutation.oldValue === null; |
| 32 | + const hasIonPage = rootNode.classList.contains('ion-page'); |
| 33 | + |
| 34 | + /** |
| 35 | + * Now that we have an .ion-page class |
| 36 | + * we can safely attempt setup again. |
| 37 | + */ |
| 38 | + if (wasEmpty && hasIonPage) { |
| 39 | + mo.disconnect(); |
| 40 | + |
| 41 | + /** |
| 42 | + * Set false here so we do not |
| 43 | + * get infinite loops |
| 44 | + */ |
| 45 | + setupSwiperInIonic(swiper, false); |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + mo.observe(rootNode, { |
| 50 | + attributeFilter: ['class'], |
| 51 | + attributeOldValue: true |
| 52 | + }); |
| 53 | + } |
| 54 | + } |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * If using slides in a modal or |
| 60 | + * popover we need to wait for the |
| 61 | + * overlay to be shown as these components |
| 62 | + * are hidden when they are initially created. |
| 63 | + */ |
| 64 | + const modalOrPopover = swiperEl.closest('ion-modal, ion-popover'); |
| 65 | + if (modalOrPopover) { |
| 66 | + const eventName = modalOrPopover.tagName === 'ION-MODAL' ? 'ionModalWillPresent' : 'ionPopoverWillPresent'; |
| 67 | + const overlayCallback = () => { |
| 68 | + /** |
| 69 | + * We need an raf here so the update |
| 70 | + * is fired one tick after the overlay is shown. |
| 71 | + */ |
| 72 | + raf(() => { |
| 73 | + swiperEl.swiper.update(); |
| 74 | + removeEventListener(modalOrPopover, eventName, overlayCallback); |
| 75 | + }); |
| 76 | + } |
| 77 | + addEventListener(modalOrPopover, eventName, overlayCallback); |
| 78 | + } else { |
| 79 | + /** |
| 80 | + * If using slides in a page |
| 81 | + * we need to wait for the ion-page-invisible |
| 82 | + * class to be removed so Swiper can correctly |
| 83 | + * compute the dimensions of the slides. |
| 84 | + */ |
| 85 | + const mo = new MutationObserver((m: MutationRecord[]) => { |
| 86 | + const mutation = m[0]; |
| 87 | + const wasPageHidden = mutation.oldValue?.includes('ion-page-invisible'); |
| 88 | + const isPageHidden = ionPage.classList.contains('ion-page-invisible'); |
| 89 | + |
| 90 | + /** |
| 91 | + * Only update Swiper if the page was |
| 92 | + * hidden but is no longer hidden. |
| 93 | + */ |
| 94 | + if (!isPageHidden && isPageHidden !== wasPageHidden) { |
| 95 | + swiperEl.swiper.update(); |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + mo.observe(ionPage, { |
| 100 | + attributeFilter: ['class'], |
| 101 | + attributeOldValue: true |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * We also need to listen for the appload event |
| 107 | + * which is emitted by Stencil in the |
| 108 | + * event that Swiper is being used on the |
| 109 | + * view that is rendered initially. |
| 110 | + */ |
| 111 | + const onAppLoad = () => { |
| 112 | + swiperEl.swiper.update(); |
| 113 | + removeEventListener(window, 'appload', onAppLoad); |
| 114 | + } |
| 115 | + |
| 116 | + addEventListener(window, 'appload', onAppLoad); |
| 117 | +} |
| 118 | + |
| 119 | +export const IonicSwiper = { |
| 120 | + name: 'ionic', |
| 121 | + on: { |
| 122 | + afterInit(swiper: any) { |
| 123 | + setupSwiperInIonic(swiper); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments