UNPKG

18.7 kBSource Map (JSON)View Raw
1{"version":3,"file":"lg-hash.es5.js","sources":["../../../src/lg-events.ts","../../../src/plugins/hash/lg-hash-settings.ts","../../../src/plugins/hash/lg-hash.ts"],"sourcesContent":["import { LightGallery } from './lightgallery';\nimport { VideoSource } from './plugins/video/types';\n\n/**\n * List of lightGallery events\n * All events should be documented here\n * Below interfaces are used to build the website documentations\n * */\nexport const lGEvents: {\n [key: string]: string;\n} = {\n afterAppendSlide: 'lgAfterAppendSlide',\n init: 'lgInit',\n hasVideo: 'lgHasVideo',\n containerResize: 'lgContainerResize',\n updateSlides: 'lgUpdateSlides',\n afterAppendSubHtml: 'lgAfterAppendSubHtml',\n beforeOpen: 'lgBeforeOpen',\n afterOpen: 'lgAfterOpen',\n slideItemLoad: 'lgSlideItemLoad',\n beforeSlide: 'lgBeforeSlide',\n afterSlide: 'lgAfterSlide',\n posterClick: 'lgPosterClick',\n dragStart: 'lgDragStart',\n dragMove: 'lgDragMove',\n dragEnd: 'lgDragEnd',\n beforeNextSlide: 'lgBeforeNextSlide',\n beforePrevSlide: 'lgBeforePrevSlide',\n beforeClose: 'lgBeforeClose',\n afterClose: 'lgAfterClose',\n rotateLeft: 'lgRotateLeft',\n rotateRight: 'lgRotateRight',\n flipHorizontal: 'lgFlipHorizontal',\n flipVertical: 'lgFlipVertical',\n autoplay: 'lgAutoplay',\n autoplayStart: 'lgAutoplayStart',\n autoplayStop: 'lgAutoplayStop',\n};\n\n// Follow the below format for the event documentation\n// @method is the method name when event is used with Angular/React components\n\n/**\n * Fired only once when lightGallery is initialized\n * @name lgInit\n * @method onInit\n * @example\n * const lg = document.getElementById('custom-events-demo');\n * // Perform any action on lightGallery initialization.\n * // Init event returns the plugin instance that can be used to call any lightGalley public method\n * let pluginInstance = null;\n * lg.addEventListener('lgInit', (event) => {\n * pluginInstance = event.detail.instance;\n * });\n * lightGallery(lg);\n * @see <a href=\"/docs/methods\">Methods<a>\n */\nexport interface InitDetail {\n /**\n * lightGallery plugin instance\n */\n instance: LightGallery;\n}\n\n/**\n * Fired when the slide content has been inserted into it's slide container.\n * @name lgAfterAppendSlide\n * @method onAfterAppendSlide\n */\nexport interface AfterAppendSlideEventDetail {\n /**\n * Index of the slide\n */\n index: number;\n}\n\n/**\n * Fired immediately before opening the gallery\n * @name lgBeforeOpen\n * @method onBeforeOpen\n */\nexport interface BeforeOpenDetail {}\n\n/**\n * Fired immediately after opening the gallery\n * @name lgAfterOpen\n * @method onAfterOpen\n */\nexport interface AfterOpenDetail {}\n\n/**\n * Fired once the media inside the slide has been completely loaded .\n * @name lgSlideItemLoad\n * @method onSlideItemLoad\n */\nexport interface SlideItemLoadDetail {\n /**\n * Index of the slide\n */\n index: number;\n /**\n * For the first slide, lightGallery adds some delay for displaying the loaded slide item.\n * This delay is required for the transition effect when the slide item is displayed\n * Respect the delay when you use this event\n */\n delay: number;\n\n // Will be true for the first slide\n isFirstSlide: boolean;\n}\n\n/**\n * Fired immediately before each slide transition.\n * @name lgBeforeSlide\n * @method onBeforeSlide\n * @example\n * const lg = document.getElementById('custom-events-demo');\n * // Perform any action before each slide transition\n * lg.addEventListener('lgBeforeSlide', (event) => {\n * const { index, prevIndex } = event.detail;\n * alert(index, prevIndex);\n * });\n * lightGallery(lg);\n */\nexport interface BeforeSlideDetail {\n /**\n * Index of the previous slide\n */\n prevIndex: number;\n /**\n * Index of the slide\n */\n index: number;\n /**\n * true if slide function called via touch event or mouse drag\n */\n fromTouch: boolean;\n /**\n * true if slide function called via thumbnail click\n */\n fromThumb: boolean;\n}\n\n/**\n * Fired immediately after each slide transition.\n * @name lgAfterSlide\n * @method onAfterSlide\n */\nexport interface AfterSlideDetail {\n /**\n * Index of the previous slide\n */\n prevIndex: number;\n /**\n * Index of the slide\n */\n index: number;\n /**\n * true if slide function called via touch event or mouse drag\n */\n fromTouch: boolean;\n /**\n * true if slide function called via thumbnail click\n */\n fromThumb: boolean;\n}\n\n/**\n * Fired when the video poster is clicked.\n * @name lgPosterClick\n * @method onPosterClick\n */\nexport interface PosterClickDetail {}\n\n/**\n * Fired when the drag event to move to different slide starts.\n * @name lgDragStart\n * @method onDragStart\n */\nexport interface DragStartDetail {}\n\n/**\n * Fired periodically during the drag operation.\n * @name lgDragMove\n * @method onDragMove\n */\nexport interface DragMoveDetail {}\n\n/**\n * Fired when the user has finished the drag operation\n * @name lgDragEnd\n * @method onDragEnd\n */\nexport interface DragEndDetail {}\n\n/**\n * Fired immediately before the start of the close process.\n * @name lgBeforeClose\n * @method onBeforeClose\n */\nexport interface BeforeCloseDetail {}\n\n/**\n * Fired immediately once lightGallery is closed.\n * @name lgAfterClose\n * @method onAfterClose\n */\nexport interface AfterCloseDetail {\n /**\n * lightGallery plugin instance\n */\n instance: LightGallery;\n}\n\n/**\n * Fired immediately before each \"next\" slide transition\n * @name lgBeforeNextSlide\n * @method onBeforeNextSlide\n */\nexport interface BeforeNextSlideDetail {\n /**\n * Index of the slide\n */\n index: number;\n /**\n * true if slide function called via touch event or mouse drag\n */\n fromTouch: boolean;\n}\n\n/**\n * Fired immediately before each \"prev\" slide transition\n * @name lgBeforePrevSlide\n * @method onBeforePrevSlide\n */\nexport interface BeforePrevSlideDetail {\n /**\n * Index of the slide\n */\n index: number;\n /**\n * true if slide function called via touch event or mouse drag\n */\n fromTouch: boolean;\n}\n\n/**\n * Fired when the sub-html content (ex : title/ description) has been appended into the slide.\n * @name lgAfterAppendSubHtml\n * @method onAfterAppendSubHtml\n */\nexport interface AfterAppendSubHtmlDetail {\n /**\n * Index of the slide\n */\n index: number;\n}\n\n/**\n * Fired when the lightGallery container has been resized.\n * @name lgContainerResize\n * @method onContainerResize\n */\nexport interface ContainerResizeDetail {\n /**\n * Index of the slide\n */\n index: number;\n}\n\n/**\n * Fired when lightGallery detects video slide\n * @name lgHasVideo\n * @method onHasVideo\n */\nexport interface HasVideoDetail {\n /**\n * Index of the slide,\n */\n index: number;\n /**\n * Video source\n */\n src: string;\n /**\n * HTML5 video source if available\n * <p>\n HTML5 video source = source: {\n src: string;\n type: string;\n }[];\n attributes: HTMLVideoElement;\n * </p>\n */\n html5Video: VideoSource;\n /**\n * True if video has poster\n */\n hasPoster: boolean;\n}\n\n/**\n * Fired when the image is rotated in anticlockwise direction\n * @name lgRotateLeft\n * @method onRotateLeft\n */\nexport interface RotateLeftDetail {\n /**\n * Index of the slide\n */\n index: number;\n}\n\n/**\n * Fired when the image is rotated in clockwise direction\n * @name lgRotateRight\n * @method onRotateRight\n */\nexport interface RotateRightDetail {\n /**\n * Index of the slide\n */\n index: number;\n}\n\n/**\n * Fired when the image is flipped horizontally\n * @name lgFlipHorizontal\n * @method onFlipHorizontal\n */\nexport interface FlipHorizontalDetail {\n /**\n * Index of the slide\n */\n index: number;\n}\n\n/**\n * Fired when the image is flipped vertically\n * @name lgFlipVertical\n * @method onFlipVertical\n */\nexport interface FlipVerticalDetail {\n /**\n * Index of the slide\n */\n index: number;\n}\n","export interface HashSettings {\n /**\n * Enable/Disable hash option\n */\n hash: boolean;\n\n /**\n * Unique id for each gallery.\n * @description It is mandatory when you use hash plugin for multiple galleries on the same page.\n */\n galleryId: string;\n\n /**\n * Custom slide name to use in the url when hash plugin is enabled\n */\n customSlideName: boolean;\n}\n\nexport const hashSettings: HashSettings = {\n hash: true,\n galleryId: '1',\n customSlideName: false,\n};\n","import { lGEvents } from '../../lg-events';\nimport { LgQuery } from '../../lgQuery';\nimport { LightGallery } from '../../lightgallery';\nimport { hashSettings, HashSettings } from './lg-hash-settings';\n\nexport default class Hash {\n core: LightGallery;\n settings: HashSettings;\n oldHash!: string;\n private $LG!: LgQuery;\n constructor(instance: LightGallery, $LG: LgQuery) {\n // get lightGallery core plugin instance\n this.core = instance;\n this.$LG = $LG;\n // extend module default settings with lightGallery core settings\n this.settings = { ...hashSettings, ...this.core.settings };\n return this;\n }\n\n public init(): void {\n if (!this.settings.hash) {\n return;\n }\n this.oldHash = window.location.hash;\n setTimeout(() => {\n this.buildFromHash();\n }, 100);\n // Change hash value on after each slide transition\n this.core.LGel.on(\n `${lGEvents.afterSlide}.hash`,\n this.onAfterSlide.bind(this),\n );\n this.core.LGel.on(\n `${lGEvents.afterClose}.hash`,\n this.onCloseAfter.bind(this),\n );\n\n // Listen hash change and change the slide according to slide value\n this.$LG(window).on(\n `hashchange.lg.hash.global${this.core.lgId}`,\n this.onHashchange.bind(this),\n );\n }\n\n private onAfterSlide(event: CustomEvent) {\n let slideName = this.core.galleryItems[event.detail.index].slideName;\n slideName = this.settings.customSlideName\n ? slideName || event.detail.index\n : event.detail.index;\n if (history.replaceState) {\n history.replaceState(\n null,\n '',\n window.location.pathname +\n window.location.search +\n '#lg=' +\n this.settings.galleryId +\n '&slide=' +\n slideName,\n );\n } else {\n window.location.hash =\n 'lg=' + this.settings.galleryId + '&slide=' + slideName;\n }\n }\n\n /**\n * Get index of the slide from custom slideName. Has to be a public method. Used in hash plugin\n * @param {String} hash\n * @returns {Number} Index of the slide.\n */\n getIndexFromUrl(hash = window.location.hash): number {\n const slideName = hash.split('&slide=')[1];\n let _idx = 0;\n\n if (this.settings.customSlideName) {\n for (\n let index = 0;\n index < this.core.galleryItems.length;\n index++\n ) {\n const dynamicEl = this.core.galleryItems[index];\n if (dynamicEl.slideName === slideName) {\n _idx = index;\n break;\n }\n }\n } else {\n _idx = parseInt(slideName, 10);\n }\n\n return isNaN(_idx) ? 0 : _idx;\n }\n\n // Build Gallery if gallery id exist in the URL\n buildFromHash(): boolean | undefined {\n // if dynamic option is enabled execute immediately\n const _hash = window.location.hash;\n if (_hash.indexOf('lg=' + this.settings.galleryId) > 0) {\n // This class is used to remove the initial animation if galleryId present in the URL\n this.$LG(document.body).addClass('lg-from-hash');\n\n const index = this.getIndexFromUrl(_hash);\n\n this.core.openGallery(index);\n return true;\n }\n }\n\n private onCloseAfter() {\n // Reset to old hash value\n if (\n this.oldHash &&\n this.oldHash.indexOf('lg=' + this.settings.galleryId) < 0\n ) {\n if (history.replaceState) {\n history.replaceState(null, '', this.oldHash);\n } else {\n window.location.hash = this.oldHash;\n }\n } else {\n if (history.replaceState) {\n history.replaceState(\n null,\n document.title,\n window.location.pathname + window.location.search,\n );\n } else {\n window.location.hash = '';\n }\n }\n }\n\n private onHashchange() {\n if (!this.core.lgOpened) return;\n const _hash = window.location.hash;\n const index = this.getIndexFromUrl(_hash);\n\n // it galleryId doesn't exist in the url close the gallery\n if (_hash.indexOf('lg=' + this.settings.galleryId) > -1) {\n this.core.slide(index, false, false);\n } else if (this.core.lGalleryOn) {\n this.core.closeGallery();\n }\n }\n\n closeGallery(): void {\n if (this.settings.hash) {\n this.$LG(document.body).removeClass('lg-from-hash');\n }\n }\n\n destroy(): void {\n this.core.LGel.off('.lg.hash');\n this.core.LGel.off('.hash');\n this.$LG(window).off(`hashchange.lg.hash.global${this.core.lgId}`);\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA;;;;;AAKO,IAAM,QAAQ,GAEjB;IACA,gBAAgB,EAAE,oBAAoB;IACtC,IAAI,EAAE,QAAQ;IACd,QAAQ,EAAE,YAAY;IACtB,eAAe,EAAE,mBAAmB;IACpC,YAAY,EAAE,gBAAgB;IAC9B,kBAAkB,EAAE,sBAAsB;IAC1C,UAAU,EAAE,cAAc;IAC1B,SAAS,EAAE,aAAa;IACxB,aAAa,EAAE,iBAAiB;IAChC,WAAW,EAAE,eAAe;IAC5B,UAAU,EAAE,cAAc;IAC1B,WAAW,EAAE,eAAe;IAC5B,SAAS,EAAE,aAAa;IACxB,QAAQ,EAAE,YAAY;IACtB,OAAO,EAAE,WAAW;IACpB,eAAe,EAAE,mBAAmB;IACpC,eAAe,EAAE,mBAAmB;IACpC,WAAW,EAAE,eAAe;IAC5B,UAAU,EAAE,cAAc;IAC1B,UAAU,EAAE,cAAc;IAC1B,WAAW,EAAE,eAAe;IAC5B,cAAc,EAAE,kBAAkB;IAClC,YAAY,EAAE,gBAAgB;IAC9B,QAAQ,EAAE,YAAY;IACtB,aAAa,EAAE,iBAAiB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