120 lines
2.3 KiB
TypeScript
Raw Normal View History

2017-06-19 14:49:24 +00:00
import { Application } from "./Application"
2017-06-20 10:41:26 +00:00
import { findAll } from "./utils"
import * as actions from "./actions"
2017-06-19 14:49:24 +00:00
export class AnimeNotifier {
app: Application
constructor(app: Application) {
this.app = app
}
2017-06-19 15:45:27 +00:00
onReadyStateChange() {
if(document.readyState !== "interactive") {
return
}
this.run()
}
2017-06-19 14:49:24 +00:00
run() {
this.app.content = this.app.find("content")
this.app.loading = this.app.find("loading")
this.app.run()
}
2017-06-19 15:45:27 +00:00
2017-06-20 10:41:26 +00:00
loading(isLoading: boolean) {
if(isLoading) {
this.app.loading.classList.remove(this.app.fadeOutClass)
} else {
this.app.loading.classList.add(this.app.fadeOutClass)
}
}
2017-06-19 15:45:27 +00:00
onContentLoaded() {
2017-06-20 18:13:04 +00:00
this.updateMountables()
2017-06-19 15:45:27 +00:00
this.updateAvatars()
2017-06-20 10:41:26 +00:00
for(let element of findAll(".action")) {
let actionName = element.dataset.action
2017-06-20 20:54:45 +00:00
element.addEventListener(element.dataset.trigger, e => {
actions[actionName](this, element, e)
})
element.classList.remove("action")
2017-06-20 10:41:26 +00:00
}
2017-06-19 15:45:27 +00:00
}
2017-06-20 12:16:23 +00:00
onPopState(e: PopStateEvent) {
if(e.state) {
2017-06-20 13:46:49 +00:00
this.app.load(e.state, {
addToHistory: false
})
2017-06-20 12:16:23 +00:00
} else if(this.app.currentPath !== this.app.originalPath) {
2017-06-20 13:46:49 +00:00
this.app.load(this.app.originalPath, {
addToHistory: false
})
2017-06-20 12:16:23 +00:00
}
}
2017-06-20 19:37:52 +00:00
onKeyDown(e: KeyboardEvent) {
// Ctrl + Q = Search
if(e.ctrlKey && e.keyCode == 81) {
let search = this.app.find("search") as HTMLInputElement
search.focus()
search.select()
e.preventDefault()
e.stopPropagation()
}
}
2017-06-19 15:45:27 +00:00
updateAvatars() {
2017-06-20 10:41:26 +00:00
for(let element of findAll(".user-image")) {
let img = element as HTMLImageElement
2017-06-19 15:45:27 +00:00
if(img.naturalWidth === 0) {
img.onload = function() {
this.classList.add("user-image-found")
}
img.onerror = function() {
this.classList.add("user-image-not-found")
}
} else {
img.classList.add("user-image-found")
}
}
}
2017-06-20 12:16:23 +00:00
2017-06-20 18:13:04 +00:00
updateMountables() {
const delay = 20
const maxDelay = 1000
let time = 0
for(let element of findAll(".mountable")) {
setTimeout(() => {
window.requestAnimationFrame(() => element.classList.add("mounted"))
}, time)
time += delay
if(time > maxDelay) {
time = maxDelay
}
}
}
2017-06-20 12:16:23 +00:00
// onResize(e: UIEvent) {
// let hasScrollbar = this.app.content.clientHeight === this.app.content.scrollHeight
// if(hasScrollbar) {
// this.app.content.classList.add("has-scrollbar")
// } else {
// this.app.content.classList.remove("has-scrollbar")
// }
// }
2017-06-19 14:49:24 +00:00
}