Skip to content

Commit

Permalink
move setting the custom transition into a loadPage method, add commen…
Browse files Browse the repository at this point in the history
…ts, remove unnecessary variable "mobile"
  • Loading branch information
gmrchk committed Oct 28, 2018
1 parent 6ffeb75 commit 7626731
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 36 deletions.
38 changes: 25 additions & 13 deletions dist/swup.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,11 +500,15 @@ var Swup = function () {
var link = new _Link2.default();
event.preventDefault();
link.setPath(event.delegateTarget.href);

if (link.getAddress() == this.currentUrl || link.getAddress() == '') {
// link to the same URL
if (link.getHash() != '') {
// link to the same URL with hash
this.triggerEvent('samePageWithHash');
var element = document.querySelector(link.getHash());
if (element != null) {
// referenced element found
if (this.options.scroll) {
var top = element.getBoundingClientRect().top + window.pageYOffset;
this.scrollTo(document.body, top);
Expand All @@ -515,29 +519,30 @@ var Swup = function () {
source: "swup"
}, document.title, link.getAddress() + link.getHash());
} else {
// referenced element not found
console.warn('Element for offset not found (' + link.getHash() + ')');
}
} else {
// link to the same URL without hash
this.triggerEvent('samePage');
if (this.options.scroll) {
this.scrollTo(document.body, 0, 1);
}
}
} else {
// link to different url
if (link.getHash() != '') {
this.scrollToElement = link.getHash();
}
// custom class fro dynamic pages
var swupClass = event.delegateTarget.dataset.swupClass;
if (swupClass != null) {
this.updateTransition(window.location.pathname, link.getAddress(), event.delegateTarget.dataset.swupClass);
document.documentElement.classList.add('to-' + swupClass);
} else {
this.updateTransition(window.location.pathname, link.getAddress());
}
this.loadPage({ url: link.getAddress() }, false);

// get custom transition from data
var customTransition = event.delegateTarget.dataset.swupTransition;

// load page
this.loadPage({ url: link.getAddress(), customTransition: customTransition }, false);
}
} else {
// open in new tab (do nothing)
this.triggerEvent('openPageInNewTab');
}
}
Expand Down Expand Up @@ -976,15 +981,22 @@ var forEach = Array.prototype.forEach;
module.exports = function (data, popstate) {
var _this = this;

var finalPage = null;

// scrolling
if (this.options.doScrollingRightAway && !this.scrollToElement) {
this.doScrolling(popstate);
}

// create array for storing animation promises
var animationPromises = [];

// set transition object
if (data.customTransition != null) {
this.updateTransition(window.location.pathname, data.url, data.customTransition);
document.documentElement.classList.add('to-' + this.classify(data.customTransition));
} else {
this.updateTransition(window.location.pathname, data.url);
}

if (!popstate || this.options.animateHistoryBrowsing) {
// start animation
this.triggerEvent('animationOutStart');
Expand Down Expand Up @@ -1060,8 +1072,8 @@ module.exports = function (data, popstate) {
}

Promise.all(animationPromises.concat([xhrPromise])).then(function () {
finalPage = _this.cache.getPage(data.url);
_this.renderPage(finalPage, popstate);
// render page
_this.renderPage(_this.cache.getPage(data.url), popstate);
_this.preloadPromise = null;
}).catch(function (errorUrl) {
// rewrite the skipPopStateHandling function to redirect manually when the history.go is processed
Expand Down
Loading

0 comments on commit 7626731

Please sign in to comment.