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 // Add anime to collection
export async function addAnimeToCollection(arn: AnimeNotifier, button: HTMLButtonElement) { export async function addAnimeToCollection(arn: AnimeNotifier, button: HTMLButtonElement) {

View File

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

View File

@ -1,4 +1,4 @@
import { AnimeNotifier } from "../AnimeNotifier" import AnimeNotifier from "../AnimeNotifier"
// newAnimeDiffIgnore // newAnimeDiffIgnore
export function newAnimeDiffIgnore(arn: AnimeNotifier, button: HTMLButtonElement) { 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"; import { findAll } from "scripts/Utils";
// Filter anime on explore page // Filter anime on explore page

View File

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

View File

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

View File

@ -1,5 +1,5 @@
import { AnimeNotifier } from "../AnimeNotifier" import AnimeNotifier from "../AnimeNotifier"
import { Diff } from "../Diff" import Diff from "../Diff"
// Load more // Load more
export async function loadMore(arn: AnimeNotifier, button: HTMLButtonElement) { 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 // Chrome extension installation
export function installExtension(arn: AnimeNotifier, button: HTMLElement) { export function installExtension(arn: AnimeNotifier, button: HTMLElement) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +1,5 @@
import { AnimeNotifier } from "../AnimeNotifier" import AnimeNotifier from "../AnimeNotifier"
import { StatusMessage } from "../StatusMessage" import StatusMessage from "../StatusMessage"
// Select file // Select file
export function selectFile(arn: AnimeNotifier, button: HTMLButtonElement) { 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() { push() {
let analytics = { let analytics = {
general: { general: {

View File

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

View File

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

View File

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

View File

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

View File

@ -1,4 +1,4 @@
export class InfiniteScroller { export default class InfiniteScroller {
container: HTMLElement container: HTMLElement
threshold: number 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 unseen: number
icon: HTMLElement icon: HTMLElement
counter: HTMLElement counter: HTMLElement

View File

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

View File

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

View File

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

View File

@ -1,4 +1,4 @@
export class TouchController { export default class TouchController {
x: number x: number
y: number y: number
@ -46,7 +46,7 @@ export class TouchController {
this.downSwipe() this.downSwipe()
} }
} }
this.x = undefined this.x = undefined
this.y = 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 Application from "./Application"
import { AnimeNotifier } from "./AnimeNotifier" import AnimeNotifier from "./AnimeNotifier"
let app = new Application() let app = new Application()
let arn = new AnimeNotifier(app) let arn = new AnimeNotifier(app)

2
sw/index.d.ts vendored
View File

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