UNPKG

swup

Version:

Complete, flexible, extensible and easy to use page transition library for your web.

83 lines (65 loc) 2.58 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _utils = require('../utils'); var _helpers = require('../helpers'); var renderPage = function renderPage(page, popstate) { var _this = this; document.documentElement.classList.remove('is-leaving'); // replace state in case the url was redirected var link = new _helpers.Link(page.responseURL); if (window.location.pathname !== link.getPath()) { window.history.replaceState({ url: link.getPath(), random: Math.random(), source: 'swup' }, document.title, link.getPath()); // save new record for redirected url this.cache.cacheUrl(_extends({}, page, { url: link.getPath() })); } // only add for non-popstate transitions if (!popstate || this.options.animateHistoryBrowsing) { document.documentElement.classList.add('is-rendering'); } this.triggerEvent('willReplaceContent', popstate); // replace blocks for (var i = 0; i < page.blocks.length; i++) { document.body.querySelector('[data-swup="' + i + '"]').outerHTML = page.blocks[i]; } // set title document.title = page.title; this.triggerEvent('contentReplaced', popstate); this.triggerEvent('pageView', popstate); // empty cache if it's disabled (because pages could be preloaded and stuff) if (!this.options.cache) { this.cache.empty(); } // start animation IN setTimeout(function () { if (!popstate || _this.options.animateHistoryBrowsing) { _this.triggerEvent('animationInStart'); document.documentElement.classList.remove('is-animating'); } }, 10); // handle end of animation var animationPromises = this.getAnimationPromises('in'); if (!popstate || this.options.animateHistoryBrowsing) { Promise.all(animationPromises).then(function () { _this.triggerEvent('animationInDone'); _this.triggerEvent('transitionEnd', popstate); // remove "to-{page}" classes document.documentElement.className.split(' ').forEach(function (classItem) { if (new RegExp('^to-').test(classItem) || classItem === 'is-changing' || classItem === 'is-rendering' || classItem === 'is-popstate') { document.documentElement.classList.remove(classItem); } }); }); } else { this.triggerEvent('transitionEnd', popstate); } // reset scroll-to element this.scrollToElement = null; }; exports.default = renderPage;