JS part of Aero is working now

This commit is contained in:
Eduard Urbach 2017-06-19 16:43:20 +02:00
parent f7732295a2
commit be11e469b1

View File

@ -1,6 +1,7 @@
class Aero { class Aero {
ajaxClass: string ajaxClass: string
fadeOutClass: string fadeOutClass: string
activeLinkClass: string
content: HTMLElement content: HTMLElement
loading: HTMLElement loading: HTMLElement
currentURL: string currentURL: string
@ -11,6 +12,7 @@ class Aero {
this.currentURL = window.location.pathname this.currentURL = window.location.pathname
this.originalURL = window.location.pathname this.originalURL = window.location.pathname
this.ajaxClass = "ajax" this.ajaxClass = "ajax"
this.activeLinkClass = "active"
this.fadeOutClass = "fade-out" this.fadeOutClass = "fade-out"
} }
@ -46,8 +48,6 @@ class Aero {
this.currentURL = url this.currentURL = url
console.log(url)
// function sleep(ms) { // function sleep(ms) {
// return new Promise(resolve => setTimeout(resolve, ms)) // return new Promise(resolve => setTimeout(resolve, ms))
// } // }
@ -58,26 +58,60 @@ class Aero {
let request = this.get("/_" + url) let request = this.get("/_" + url)
this.content.addEventListener("transitionend", e => { let onTransitionEnd = e => {
// Ignore transitions of child elements.
// We only care about the transition event on the content element.
if(e.target !== this.content) {
return
}
// Remove listener after we finally got the correct event.
this.content.removeEventListener("transitionend", onTransitionEnd)
// Wait for the network request to end.
request.then(html => { request.then(html => {
// Add to browser history
if(addToHistory) if(addToHistory)
history.pushState(url, null, url) history.pushState(url, null, url)
// Set content
this.setContent(html) this.setContent(html)
this.scrollToTop() this.scrollToTop()
// Fade animations
this.content.classList.remove(this.fadeOutClass) this.content.classList.remove(this.fadeOutClass)
this.loading.classList.add(this.fadeOutClass) this.loading.classList.add(this.fadeOutClass)
}) })
}, { once: true }) }
this.content.addEventListener("transitionend", onTransitionEnd)
this.content.classList.add(this.fadeOutClass) this.content.classList.add(this.fadeOutClass)
this.loading.classList.remove(this.fadeOutClass) this.loading.classList.remove(this.fadeOutClass)
this.markActiveLinks()
} }
setContent(html: string) { setContent(html: string) {
this.content.innerHTML = html this.content.innerHTML = html
this.ajaxify(this.content) this.ajaxify(this.content)
this.markActiveLinks(this.content)
}
markActiveLinks(element?: HTMLElement) {
if(element === undefined)
element = document.body
let links = element.querySelectorAll("a")
for(let i = 0; i < links.length; i++) {
let link = links[i]
let href = link.getAttribute("href")
if(href === this.currentURL)
link.classList.add(this.activeLinkClass)
else
link.classList.remove(this.activeLinkClass)
}
} }
ajaxify(element?: HTMLElement) { ajaxify(element?: HTMLElement) {
@ -111,6 +145,7 @@ class Aero {
run() { run() {
this.ajaxify() this.ajaxify()
this.markActiveLinks()
this.loading.classList.add(this.fadeOutClass) this.loading.classList.add(this.fadeOutClass)
} }