1 | import { getWindow, getDocument } from 'ssr-window';
|
2 | import $ from '../../shared/dom.js';
|
3 | export default function HashNavigation(_ref) {
|
4 | let {
|
5 | swiper,
|
6 | extendParams,
|
7 | emit,
|
8 | on
|
9 | } = _ref;
|
10 | let initialized = false;
|
11 | const document = getDocument();
|
12 | const window = getWindow();
|
13 | extendParams({
|
14 | hashNavigation: {
|
15 | enabled: false,
|
16 | replaceState: false,
|
17 | watchState: false
|
18 | }
|
19 | });
|
20 |
|
21 | const onHashChange = () => {
|
22 | emit('hashChange');
|
23 | const newHash = document.location.hash.replace('#', '');
|
24 | const activeSlideHash = swiper.slides.eq(swiper.activeIndex).attr('data-hash');
|
25 |
|
26 | if (newHash !== activeSlideHash) {
|
27 | const newIndex = swiper.$wrapperEl.children(`.${swiper.params.slideClass}[data-hash="${newHash}"]`).index();
|
28 | if (typeof newIndex === 'undefined') return;
|
29 | swiper.slideTo(newIndex);
|
30 | }
|
31 | };
|
32 |
|
33 | const setHash = () => {
|
34 | if (!initialized || !swiper.params.hashNavigation.enabled) return;
|
35 |
|
36 | if (swiper.params.hashNavigation.replaceState && window.history && window.history.replaceState) {
|
37 | window.history.replaceState(null, null, `#${swiper.slides.eq(swiper.activeIndex).attr('data-hash')}` || '');
|
38 | emit('hashSet');
|
39 | } else {
|
40 | const slide = swiper.slides.eq(swiper.activeIndex);
|
41 | const hash = slide.attr('data-hash') || slide.attr('data-history');
|
42 | document.location.hash = hash || '';
|
43 | emit('hashSet');
|
44 | }
|
45 | };
|
46 |
|
47 | const init = () => {
|
48 | if (!swiper.params.hashNavigation.enabled || swiper.params.history && swiper.params.history.enabled) return;
|
49 | initialized = true;
|
50 | const hash = document.location.hash.replace('#', '');
|
51 |
|
52 | if (hash) {
|
53 | const speed = 0;
|
54 |
|
55 | for (let i = 0, length = swiper.slides.length; i < length; i += 1) {
|
56 | const slide = swiper.slides.eq(i);
|
57 | const slideHash = slide.attr('data-hash') || slide.attr('data-history');
|
58 |
|
59 | if (slideHash === hash && !slide.hasClass(swiper.params.slideDuplicateClass)) {
|
60 | const index = slide.index();
|
61 | swiper.slideTo(index, speed, swiper.params.runCallbacksOnInit, true);
|
62 | }
|
63 | }
|
64 | }
|
65 |
|
66 | if (swiper.params.hashNavigation.watchState) {
|
67 | $(window).on('hashchange', onHashChange);
|
68 | }
|
69 | };
|
70 |
|
71 | const destroy = () => {
|
72 | if (swiper.params.hashNavigation.watchState) {
|
73 | $(window).off('hashchange', onHashChange);
|
74 | }
|
75 | };
|
76 |
|
77 | on('init', () => {
|
78 | if (swiper.params.hashNavigation.enabled) {
|
79 | init();
|
80 | }
|
81 | });
|
82 | on('destroy', () => {
|
83 | if (swiper.params.hashNavigation.enabled) {
|
84 | destroy();
|
85 | }
|
86 | });
|
87 | on('transitionEnd _freeModeNoMomentumRelease', () => {
|
88 | if (initialized) {
|
89 | setHash();
|
90 | }
|
91 | });
|
92 | on('slideChange', () => {
|
93 | if (initialized && swiper.params.cssMode) {
|
94 | setHash();
|
95 | }
|
96 | });
|
97 | } |
\ | No newline at end of file |