TypeScript cleanup

This commit is contained in:
Eduard Urbach 2018-04-02 07:34:16 +02:00
parent 3f86a970f0
commit 88296da8be
44 changed files with 135 additions and 133 deletions

View File

@ -1,20 +0,0 @@
export * from "./Actions/Audio"
export * from "./Actions/AnimeList"
export * from "./Actions/Diff"
export * from "./Actions/Editor"
export * from "./Actions/Explore"
export * from "./Actions/FollowUser"
export * from "./Actions/Forum"
export * from "./Actions/InfiniteScroller"
export * from "./Actions/Install"
export * from "./Actions/Like"
export * from "./Actions/Notifications"
export * from "./Actions/Object"
export * from "./Actions/Publish"
export * from "./Actions/Search"
export * from "./Actions/Serialization"
export * from "./Actions/Shop"
export * from "./Actions/SideBar"
export * from "./Actions/StatusMessage"
export * from "./Actions/Theme"
export * from "./Actions/Upload"

View File

@ -1,4 +1,4 @@
import { AnimeNotifier } from "../AnimeNotifier"
import AnimeNotifier from "../AnimeNotifier"
// Add anime to collection
export async function addAnimeToCollection(arn: AnimeNotifier, button: HTMLButtonElement) {

View File

@ -1,4 +1,4 @@
import { AnimeNotifier } from "../AnimeNotifier"
import AnimeNotifier from "../AnimeNotifier"
// Play audio
export function playAudio(arn: AnimeNotifier, element: HTMLElement) {

View File

@ -1,4 +1,5 @@
import { AnimeNotifier } from "../AnimeNotifier"
import AnimeNotifier from "../AnimeNotifier"
import { requestIdleCallback } from "../Utils"
// Load
export function load(arn: AnimeNotifier, element: HTMLElement) {
@ -13,7 +14,7 @@ export function diff(arn: AnimeNotifier, element: HTMLElement) {
arn.diff(url)
.then(() => {
// Avoid instant layout thrashing
arn.requestIdleCallback(() => arn.scrollTo(element))
requestIdleCallback(() => arn.scrollTo(element))
})
.catch(console.error)
}

View File

@ -1,4 +1,4 @@
import { AnimeNotifier } from "../AnimeNotifier"
import AnimeNotifier from "../AnimeNotifier"
// newAnimeDiffIgnore
export function newAnimeDiffIgnore(arn: AnimeNotifier, button: HTMLButtonElement) {

View File

@ -1,4 +1,4 @@
import { AnimeNotifier } from "../AnimeNotifier"
import AnimeNotifier from "../AnimeNotifier"
import { findAll } from "scripts/Utils";
// Filter anime on explore page

View File

@ -1,4 +1,4 @@
import { AnimeNotifier } from "../AnimeNotifier"
import AnimeNotifier from "../AnimeNotifier"
// Follow user
export function followUser(arn: AnimeNotifier, elem: HTMLElement) {

View File

@ -1,4 +1,4 @@
import { AnimeNotifier } from "../AnimeNotifier"
import AnimeNotifier from "../AnimeNotifier"
// Edit post
export function editPost(arn: AnimeNotifier, element: HTMLElement) {

View File

@ -1,5 +1,5 @@
import { AnimeNotifier } from "../AnimeNotifier"
import { Diff } from "../Diff"
import AnimeNotifier from "../AnimeNotifier"
import Diff from "../Diff"
// Load more
export async function loadMore(arn: AnimeNotifier, button: HTMLButtonElement) {

View File

@ -1,4 +1,4 @@
import { AnimeNotifier } from "../AnimeNotifier"
import AnimeNotifier from "../AnimeNotifier"
// Chrome extension installation
export function installExtension(arn: AnimeNotifier, button: HTMLElement) {

View File

@ -1,4 +1,4 @@
import { AnimeNotifier } from "../AnimeNotifier"
import AnimeNotifier from "../AnimeNotifier"
// like
export async function like(arn: AnimeNotifier, element: HTMLElement) {

View File

@ -1,4 +1,4 @@
import { AnimeNotifier } from "../AnimeNotifier"
import AnimeNotifier from "../AnimeNotifier"
// Enable notifications
export async function enableNotifications(arn: AnimeNotifier, button: HTMLElement) {

View File

@ -1,4 +1,4 @@
import { AnimeNotifier } from "../AnimeNotifier"
import AnimeNotifier from "../AnimeNotifier"
// New
export function newObject(arn: AnimeNotifier, button: HTMLButtonElement) {

View File

@ -1,4 +1,4 @@
import { AnimeNotifier } from "../AnimeNotifier"
import AnimeNotifier from "../AnimeNotifier"
// Publish
export function publish(arn: AnimeNotifier, button: HTMLButtonElement) {

View File

@ -1,5 +1,5 @@
import { AnimeNotifier } from "../AnimeNotifier"
import { delay } from "../Utils"
import AnimeNotifier from "../AnimeNotifier"
import { delay, requestIdleCallback } from "../Utils"
// Search page reference
var emptySearchHTML = ""
@ -122,7 +122,7 @@ export async function search(arn: AnimeNotifier, search: HTMLInputElement, e: Ke
.then(showResponseInElement(arn, url, "anime", animeSearchResults))
.catch(console.error)
arn.requestIdleCallback(() => {
requestIdleCallback(() => {
fetch("/_/character-search/" + term, fetchOptions)
.then(showResponseInElement(arn, url, "character", characterSearchResults))
.catch(console.error)

View File

@ -1,4 +1,4 @@
import { AnimeNotifier } from "../AnimeNotifier"
import AnimeNotifier from "../AnimeNotifier"
// Save new data from an input field
export function save(arn: AnimeNotifier, input: HTMLElement) {

View File

@ -1,4 +1,4 @@
import { AnimeNotifier } from "../AnimeNotifier"
import AnimeNotifier from "../AnimeNotifier"
// Charge up
export function chargeUp(arn: AnimeNotifier, button: HTMLElement) {
@ -55,7 +55,7 @@ export function buyItem(arn: AnimeNotifier, button: HTMLElement) {
if(body !== "ok") {
throw body
}
return arn.reloadContent()
})
.then(() => arn.statusMessage.showInfo(`You bought ${itemName} for ${price} gems. Check out your inventory to confirm the purchase.`, 4000))

View File

@ -1,4 +1,4 @@
import { AnimeNotifier } from "../AnimeNotifier"
import AnimeNotifier from "../AnimeNotifier"
// Toggle sidebar
export function toggleSidebar(arn: AnimeNotifier) {

View File

@ -1,4 +1,4 @@
import { AnimeNotifier } from "../AnimeNotifier"
import AnimeNotifier from "../AnimeNotifier"
// Close status message
export function closeStatusMessage(arn: AnimeNotifier) {

View File

@ -1,4 +1,4 @@
import { AnimeNotifier } from "../AnimeNotifier"
import AnimeNotifier from "../AnimeNotifier"
let currentTheme = "light"

View File

@ -1,5 +1,5 @@
import { AnimeNotifier } from "../AnimeNotifier"
import { StatusMessage } from "../StatusMessage"
import AnimeNotifier from "../AnimeNotifier"
import StatusMessage from "../StatusMessage"
// Select file
export function selectFile(arn: AnimeNotifier, button: HTMLButtonElement) {

20
scripts/Actions/index.ts Normal file
View File

@ -0,0 +1,20 @@
export * from "./Audio"
export * from "./AnimeList"
export * from "./Diff"
export * from "./Editor"
export * from "./Explore"
export * from "./FollowUser"
export * from "./Forum"
export * from "./InfiniteScroller"
export * from "./Install"
export * from "./Like"
export * from "./Notifications"
export * from "./Object"
export * from "./Publish"
export * from "./Search"
export * from "./Serialization"
export * from "./Shop"
export * from "./SideBar"
export * from "./StatusMessage"
export * from "./Theme"
export * from "./Upload"

View File

@ -1,4 +1,4 @@
export class Analytics {
export default class Analytics {
push() {
let analytics = {
general: {

View File

@ -1,19 +1,19 @@
import { Application } from "./Application"
import { Diff } from "./Diff"
import { StatusMessage } from "./StatusMessage"
import { PushManager } from "./PushManager"
import { TouchController } from "./TouchController"
import { NotificationManager } from "./NotificationManager"
import { AudioPlayer } from "./AudioPlayer"
import { Analytics } from "./Analytics"
import { SideBar } from "./SideBar"
import { InfiniteScroller } from "./InfiniteScroller"
import { ServiceWorkerManager } from "./ServiceWorkerManager"
import Application from "./Application"
import Diff from "./Diff"
import StatusMessage from "./StatusMessage"
import PushManager from "./PushManager"
import TouchController from "./TouchController"
import NotificationManager from "./NotificationManager"
import AudioPlayer from "./AudioPlayer"
import Analytics from "./Analytics"
import SideBar from "./SideBar"
import InfiniteScroller from "./InfiniteScroller"
import ServiceWorkerManager from "./ServiceWorkerManager"
import { displayAiringDate, displayDate, displayTime } from "./DateView"
import { findAll, delay, canUseWebP, swapElements } from "./Utils"
import { findAll, canUseWebP, requestIdleCallback, swapElements, delay } from "./Utils"
import * as actions from "./Actions"
export class AnimeNotifier {
export default class AnimeNotifier {
app: Application
analytics: Analytics
user: HTMLElement
@ -86,15 +86,7 @@ export class AnimeNotifier {
window.addEventListener("popstate", this.onPopState.bind(this))
// Idle
this.requestIdleCallback(this.onIdle.bind(this))
}
requestIdleCallback(func: Function) {
if("requestIdleCallback" in window) {
window["requestIdleCallback"](func)
} else {
func()
}
requestIdleCallback(this.onIdle.bind(this))
}
onReadyStateChange() {

View File

@ -1,11 +1,11 @@
import { Diff } from "./Diff"
import Diff from "./Diff"
class LoadOptions {
addToHistory?: boolean
forceReload?: boolean
}
export class Application {
export default class Application {
fadeOutClass: string
activeLinkClass: string
content: HTMLElement

View File

@ -1,7 +1,7 @@
import { AnimeNotifier } from "./AnimeNotifier"
import AnimeNotifier from "./AnimeNotifier"
import { Anime } from "./Types/Anime"
export class AudioPlayer {
export default class AudioPlayer {
arn: AnimeNotifier
// Web audio

View File

@ -1,6 +1,6 @@
import { MutationQueue } from "./MutationQueue"
export class Diff {
export default class Diff {
static persistentClasses = new Set<string>()
static persistentAttributes = new Set<string>()

View File

@ -1,4 +1,4 @@
export class InfiniteScroller {
export default class InfiniteScroller {
container: HTMLElement
threshold: number

View File

@ -1,6 +1,6 @@
import { Diff } from "./Diff"
import Diff from "./Diff"
export class NotificationManager {
export default class NotificationManager {
unseen: number
icon: HTMLElement
counter: HTMLElement

View File

@ -1,6 +1,6 @@
export class PushManager {
export default class PushManager {
pushSupported: boolean
constructor() {
this.pushSupported = ("serviceWorker" in navigator) && ("PushManager" in window)
}
@ -52,7 +52,7 @@ export class PushManager {
console.error("Subscription does not exist")
return
}
await subscription.unsubscribe()
this.unsubscribeOnServer(subscription, userId)

View File

@ -1,6 +1,6 @@
import { AnimeNotifier } from "./AnimeNotifier"
import AnimeNotifier from "./AnimeNotifier"
export class ServiceWorkerManager {
export default class ServiceWorkerManager {
arn: AnimeNotifier
uri: string

View File

@ -1,7 +1,7 @@
import { TouchController } from "./TouchController"
import { Diff } from "./Diff"
import TouchController from "./TouchController"
import Diff from "./Diff"
export class SideBar {
export default class SideBar {
element: HTMLElement
touchController: TouchController

View File

@ -1,6 +1,6 @@
import { delay } from "./Utils"
export class StatusMessage {
export default class StatusMessage {
container: HTMLElement
text: HTMLElement

View File

@ -1,4 +1,4 @@
export class TouchController {
export default class TouchController {
x: number
y: number
@ -46,7 +46,7 @@ export class TouchController {
this.downSwipe()
}
}
this.x = undefined
this.y = undefined
}

View File

@ -1,42 +0,0 @@
export function* findAll(className: string): IterableIterator<HTMLElement> {
let elements = document.getElementsByClassName(className)
for(let i = 0; i < elements.length; ++i) {
yield elements[i] as HTMLElement
}
}
export function delay<T>(millis: number, value?: T): Promise<T> {
return new Promise(resolve => setTimeout(() => resolve(value), millis))
}
export function plural(count: number, singular: string): string {
return (count === 1 || count === -1) ? (count + " " + singular) : (count + " " + singular + "s")
}
export function canUseWebP(): boolean {
return document.createElement("canvas").toDataURL("image/webp").indexOf("data:image/webp") === 0
}
export function swapElements(a: Node, b: Node) {
let parent = b.parentNode
let bNext = b.nextSibling
// Special case for when a is the next sibling of b
if(bNext === a) {
// Just put a before b
parent.insertBefore(a, b)
} else {
// Insert b right before a
a.parentNode.insertBefore(b, a)
// Now insert a where b was
if(bNext) {
// If there was an element after b, then insert a right before that
parent.insertBefore(a, bNext)
} else {
// Otherwise just append it as the last child
parent.appendChild(a)
}
}
}

View File

@ -0,0 +1,3 @@
export function canUseWebP(): boolean {
return document.createElement("canvas").toDataURL("image/webp").indexOf("data:image/webp") === 0
}

3
scripts/Utils/delay.ts Normal file
View File

@ -0,0 +1,3 @@
export function delay<T>(millis: number, value?: T): Promise<T> {
return new Promise(resolve => setTimeout(() => resolve(value), millis))
}

7
scripts/Utils/findAll.ts Normal file
View File

@ -0,0 +1,7 @@
export function* findAll(className: string): IterableIterator<HTMLElement> {
let elements = document.getElementsByClassName(className)
for(let i = 0; i < elements.length; ++i) {
yield elements[i] as HTMLElement
}
}

6
scripts/Utils/index.ts Normal file
View File

@ -0,0 +1,6 @@
export * from "./canUseWebP"
export * from "./delay"
export * from "./findAll"
export * from "./plural"
export * from "./requestIdleCallback"
export * from "./swapElements"

3
scripts/Utils/plural.ts Normal file
View File

@ -0,0 +1,3 @@
export function plural(count: number, singular: string): string {
return (count === 1 || count === -1) ? (count + " " + singular) : (count + " " + singular + "s")
}

View File

@ -0,0 +1,7 @@
export function requestIdleCallback(func: Function) {
if("requestIdleCallback" in window) {
window["requestIdleCallback"](func)
} else {
func()
}
}

View File

@ -0,0 +1,22 @@
export function swapElements(a: Node, b: Node) {
let parent = b.parentNode
let bNext = b.nextSibling
// Special case for when a is the next sibling of b
if(bNext === a) {
// Just put a before b
parent.insertBefore(a, b)
} else {
// Insert b right before a
a.parentNode.insertBefore(b, a)
// Now insert a where b was
if(bNext) {
// If there was an element after b, then insert a right before that
parent.insertBefore(a, bNext)
} else {
// Otherwise just append it as the last child
parent.appendChild(a)
}
}
}

View File

@ -1,5 +1,5 @@
import { Application } from "./Application"
import { AnimeNotifier } from "./AnimeNotifier"
import Application from "./Application"
import AnimeNotifier from "./AnimeNotifier"
let app = new Application()
let arn = new AnimeNotifier(app)

2
sw/index.d.ts vendored
View File

@ -654,7 +654,7 @@ interface ServiceWorkerGlobalScope extends EventTarget {
interface NotificationOptions {
image?: string;
data?: string;
data?: any;
badge?: string;
}