import Utils from '../../utils/utils';

const HashNavigation = {
  onHashCange() {
    const swiper = this;
    const newHash = document.location.hash.replace('#', '');
    const activeSlideHash = swiper.slides.eq(swiper.activeIndex).attr('data-hash');
    if (newHash !== activeSlideHash) {
      swiper.slideTo(swiper.$wrapperEl.children(`.${swiper.params.slideClass}[data-hash="${newHash}"]`).index());
    }
  },
  setHash() {
    const swiper = this;
    if (!swiper.hashNavigation.initialized || !swiper.params.hashNavigation.enabled) return;
    if (swiper.params.hashNavigation.replaceState && window.history && window.history.replaceState) {
      window.history.replaceState(null, null, (`#${swiper.slides.eq(swiper.activeIndex).attr('data-hash')}` || ''));
    } else {
      const slide = swiper.slides.eq(swiper.activeIndex);
      const hash = slide.attr('data-hash') || slide.attr('data-history');
      document.location.hash = hash || '';
    }
  },
  init() {
    const swiper = this;
    if (!swiper.params.hashNavigation.enabled || (swiper.params.history && swiper.params.history.enabled)) return;
    swiper.hashNavigation.initialized = true;
    const hash = document.location.hash.replace('#', '');
    if (hash) {
      const speed = 0;
      for (let i = 0, length = swiper.slides.length; i < length; i += 1) {
        const slide = swiper.slides.eq(i);
Exemple #2
0
   return { key, value };
 },
 setHistory(key, index) {
   const swiper = this;
   if (!swiper.history.initialized || !swiper.params.history.enabled) return;
   const slide = swiper.slides.eq(index);
   let value = History.slugify(slide.attr('data-history'));
   if (!window.location.pathname.includes(key)) {
     value = `${key}/${value}`;
   }
   const currentState = window.history.state;
   if (currentState && currentState.value === value) {
     return;
   }
   if (swiper.params.history.replaceState) {
     window.history.replaceState({ value }, null, value);
   } else {
     window.history.pushState({ value }, null, value);
   }
 },
 slugify(text) {
   return text.toString().toLowerCase()
     .replace(/\s+/g, '-')
     .replace(/[^\w-]+/g, '')
     .replace(/--+/g, '-')
     .replace(/^-+/, '')
     .replace(/-+$/, '');
 },
 scrollToSlide(speed, value, runCallbacks) {
   const swiper = this;
   if (value) {
     }
   });
 },
 push(viewId, viewState, url) {
   if (!History.allowChange) {
     History.queue.push(() => {
       History.push(viewId, viewState, url);
     });
     return;
   }
   History.previousState = History.state;
   const newState = Utils.extend({}, (History.previousState || {}), {
     [viewId]: viewState,
   });
   History.state = newState;
   window.history.pushState(newState, '', url);
 },
 replace(viewId, viewState, url) {
   if (!History.allowChange) {
     History.queue.push(() => {
       History.replace(viewId, viewState, url);
     });
     return;
   }
   History.previousState = History.state;
   const newState = Utils.extend({}, (History.previousState || {}), {
     [viewId]: viewState,
   });
   History.state = newState;
   window.history.replaceState(newState, '', url);
 },