From d6f373dfb86e9cc60b0e970972e99986fb4ec851 Mon Sep 17 00:00:00 2001 From: Peter Zingg Date: Tue, 23 Jun 2020 08:12:08 -0700 Subject: [PATCH] phoenix_live_view 0.13 (#45654) --- types/phoenix_live_view/index.d.ts | 271 ++++++++++++++++++++++++++ types/phoenix_live_view/tsconfig.json | 23 +++ types/phoenix_live_view/tslint.json | 1 + 3 files changed, 295 insertions(+) create mode 100644 types/phoenix_live_view/index.d.ts create mode 100644 types/phoenix_live_view/tsconfig.json create mode 100644 types/phoenix_live_view/tslint.json diff --git a/types/phoenix_live_view/index.d.ts b/types/phoenix_live_view/index.d.ts new file mode 100644 index 0000000000..b36fae6e46 --- /dev/null +++ b/types/phoenix_live_view/index.d.ts @@ -0,0 +1,271 @@ +// Type definitions for phoenix_live_view 0.13 +// Project: https://github.com/phoenixframework/phoenix_live_view +// Definitions by: Peter Zingg +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import { Socket, SocketConnectOption } from 'phoenix'; + +export interface Defaults { + debounce?: number; + throttle?: number; +} + +// From morphdom +export interface DomOptions { + getNodeKey?: (node: Node) => any; + onBeforeNodeAdded?: (node: Node) => Node; + onNodeAdded?: (node: Node) => Node; + onBeforeElUpdated?: (fromEl: HTMLElement, toEl: HTMLElement) => boolean; + onElUpdated?: (el: HTMLElement) => void; + onBeforeNodeDiscarded?: (node: Node) => boolean; + onNodeDiscarded?: (node: Node) => void; + onBeforeElChildrenUpdated?: (fromEl: HTMLElement, toEl: HTMLElement) => boolean; + childrenOnly?: boolean; +} + +export type ViewLogger = (view: View, kind: string, msg: any, obj: any) => void; + +export interface SocketOptions { + bindingPrefix?: string; + defaults?: Defaults; + dom?: DomOptions; + hooks?: object; + loaderTimeout?: number; + params?: object; + viewLogger?: ViewLogger; +} + +export type BindCallback = (e: Event, event: string, view: View, el: HTMLElement, targetCtx: object, phxEvent: string, windowOwner?: string) => void; + +export class LiveSocket { + // phxSocket should be the Socket class (LiveSocket will use the constructor) + constructor(url: string, phxSocket: any, opts: SocketOptions); + + // public + + connect(): void; + disableDebug(): void; + disableLatencySim(): void; + disableProfiling(): void; + disconnect(callback: any): void; + enableDebug(): void; + enableLatencySim(upperBoundMs: number): void; + enableProfiling(): void; + getLatencySim(): number | null; + getSocket(): Socket; + isDebugEnabled(): boolean; + isPhxView(el: HTMLElement): boolean; + isProfileEnabled(): boolean; + + // private + + // channel(topic: string, params: any): Channel; + // binding(kind: any): string; + // bind(events: any[], callback: BindCallback): void; + // bindClicks(): void; + // bindForms(): void; + // bindNav(): void; + // bindTopLevelEvents(): void; + // blurActiveElement(): void; + // commitPendingLink(linkRef: number): boolean; + // debounce(el: HTMLElement, event: string, callback: any): void; + // destroyAllViews(): void; + // destroyViewByEl(el: HTMLElement): void; + // dropActiveElement(view: View): void; + // eventMeta(eventName: string, e: any, targetEl: HTMLElement): object; + // getActiveElement(): HTMLElement; + // getBindingPrefix(): string; + // getHookCallbacks(hookName: string): any; + // getHref(): string; + // getRootById(id: string): any; + // getViewByEl(el: HTMLElement): any; + // hasPendingLink(): boolean; + // historyPatch(href: string, linkState: any): void; + // historyRedirect(href: string, linkState: any, flash: any): void; + // isConnected(): boolean; + // isUnloaded(): boolean; + // joinRootView(el: HTMLElement, href: string, flash: any, callback: any): View; + // joinRootViews(): boolean; + // log(view: View, kind: string, msgCallback: () => [any, any]): void; + // on(event: string, callback: (e: Event) => void): void; + // onChannel(channel: any, event: any, cb: (data: any) => void): void; + // onViewError(view: View): void; + // owner(childEl: HTMLElement, callback: (view: View) => void): void; + // pushHistoryPatch(href: string, linkState: any, targetEl: HTMLElement): void; + // redirect(to: string, flash: any): void; + // registerNewLocation(newLocation: Location): boolean; + // reloadWithJitter(view: any): void; + // replaceMain(href: string, flash: any, callback?: any, linkRef?: any): void; + // replaceRootHistory(): void; + // restorePreviouslyActiveFocus(): void; + // setActiveElement(target: HTMLElement): void; + // setPendingLink(href: string): number; + // silenceEvents(callback: () => void): void; + // time(name: string, func: () => any): any; + // triggerDOM(kind: string, args: any): void; + // withinOwners(childEl: HTMLElement, callback: (view: View, el: HTMLElement) => void): void; + // withinTargets(el: HTMLElement, phxTarget: string, callback: (view: View, el: HTMLElement) => void): void; + // withPageLoading(info: Event, callback: any): any; + // wrapPush(push: any): any; +} + +export class Rendered { + constructor(viewId: string, rendered: any); + + // public + + componentCIDs(diff: any): number[]; + componentToString(cid: number): string; + expandStatics(diff: any): void; + isComponentOnlyDiff(diff: any): boolean; + mergeDiff(diff: any): void; + parentViewId(): string; + pruneCIDs(cids: number[]): any; + recursiveMerge(target: object, source: object): void; + recursiveToString(rendered: any, components: any, onlyCids?: number[]): string; + toString(onlyCids?: number[]): string; + + // private + + // comprehensionToBuffer(rendered: any, output: any): void; + // dynamicToBuffer(rendered: any, output: any): void; + // get(): any; + // isNewFingerprint(diff: object): boolean; + // recursiveCIDToString(components: any, cid: string, onlyCids?: number[]): any; + // replaceRendered(rendered: any): void; + // toOutputBuffer(rendered: any, output: object): any; +} + +export interface ViewHookInterface { + el: HTMLElement; + viewName: string; + pushEvent(event: string, payload: object): void; + pushEventTo(phxTarget: any, event: string, payload: object): void; + + // callbacks + mounted?: () => void; + disconnected?: () => void; + connected?: () => void; + beforeUpdate?: () => void; + updated?: () => void; + beforeDestroy?: () => void; + destroyed?: () => void; +} + +export class View { + constructor(el: HTMLElement, liveSocket: LiveSocket, parentView: View, href: string, o: any); + + ackJoin(child: any): void; + addHook(el: HTMLElement): void; + applyJoinPatch(live_patch: any, html: any): void; + applyPendingUpdates(): void; + attachTrueDocEl(): void; + bindChannel(): void; + binding(kind: string): any; + closestComponentID(targetCtx: object | null): number | null; + componentID(el: HTMLElement): number | null; + componentPatch(diff: any, cid: number, ref: any): boolean; + connectParams(): object; + destroy(callback?: () => void): void; + destroyAllChildren(): void; + destroyDescendent(id: string): any; + destroyHook(hook: ViewHookInterface): void; + displayError(): void; + dropPendingRefs(): void; + expandURL(to: string): string; + extractMeta(el: HTMLElement, meta: object): object; + formsForRecovery(html: string): HTMLElement[]; + getChildById(id: string): any; + getDescendentByEl(el: HTMLElement): any; + getHook(el: HTMLElement): ViewHookInterface; + getSession(): string; + getStatic(): string | null; + hideLoader(): void; + isConnected(): boolean; + isDestroyed(): boolean; + isJoinPending(): boolean; + isLoading(): boolean; + isMain(): boolean; + join(callback?: (view: View, joinCount: number) => void): any; + joinChild(el: HTMLElement): any; + joinNewChildren(): void; + log(kind: string, msgCallback: any): void; + maybePushComponentsDestroyed(destroyedCIDs: number[]): any; + name(): string; + onAllChildJoinsComplete(): void; + onChannel(event: string, cb: (resp: any) => void): void; + onError(reason: any): void; + onJoin(resp: object): void; + onJoinComplete(resp: object, html: any): void; + onJoinError(resp: object): void; + onLivePatch(redir: object): void; + onLiveRedirect(redir: object): void; + onRedirect(redir: object): void; + ownsElement(el: HTMLElement): boolean; + performPatch(patch: any): boolean; + pushEvent(type: string, el: HTMLElement, targetCtx: object | null, phxEvent: string, meta: object): void; + pushFormRecovery(form: HTMLElement, callback: any): void; + pushFormSubmit(inputEl: HTMLElement, targetCtx: object | null, kind: string, phxEvent: string, onReply: any): void; + pushHookEvent(targetCtx: object | null, event: string, payload: object): void; + pushInput(inputEl: HTMLElement, targetCtx: object | null, kind: string, phxEvent: string, callback: any): void; + pushKey(keyElement: HTMLElement, targetCtx: object | null, kind: string, phxEvent: string, meta: object): void; + pushLinkPatch(href: string, targetEl: HTMLElement, callback: any): void; + pushWithReply(refGenerator: any, event: string, payload: object, onReply: any): any; + putRef(elements: HTMLElement[], event: string): [number, HTMLElement[]]; + renderContainer(diff: any, kind: string): string; + setContainerClasses(...classes: any[]): void; + showLoader(timeout?: number): void; + submitForm(form: HTMLElement, targetCtx: object | null, phxEvent: string): void; + targetComponentID(target: HTMLElement, targetCtx?: object): number | null; + triggerReconnected(): void; + update(diff: any, cidAck: any, ref: any): void; +} + +export function debug(view: View, kind: string, msg: object, obj: object): void; + +export namespace Browser { + function canPushState(): boolean; + function dropLocal(namespace: string, subkey: string): any; + function fetchPage(href: string, callback: (status: number, resp?: string) => any): any; + function getCookie(name: string): string; + function getHashTargetEl(hash: any): HTMLElement | null; + function getLocal(namespace: string, subkey: string): any; + function localKey(namespace: string, subkey: string): string; + function pushState(kind: any, meta: any, to: string): void; + function redirect(toURL: string, flash: any): void; + function setCookie(name: string, value: string): void; + function updateLocal(namespace: string, subkey: string, initial: any, func: (current: any) => any): any; +} + +export namespace DOM { + function all(node: Node, query: string, callback: (el: HTMLElement) => HTMLElement): HTMLElement[]; + function byId(id: string): HTMLElement | void; + function cloneNode(node: Node, html: any): Node; + function copyPrivates(target: HTMLElement, source: HTMLElement): void; + function debounce(el: HTMLElement, event: Event, phxDebounce: string, defaultDebounce: string | null, phxThrottle: string, defaultThrottle: string | null, callback: () => any): any; + function deletePrivate(el: HTMLElement, key: string): void; + function discardError(container: HTMLElement, el: HTMLElement, phxFeedbackFor: string): void; + function dispatchEvent(target: Node, eventString: string, detail?: object): void; + function findComponentNodeList(node: Node, cid: number): HTMLElement[]; + function findFirstComponentNode(node: Node, cid: number): HTMLElement | null; + function findParentCIDs(node: Node, cids: number[]): Set; + function findPhxChildren(el: HTMLElement, parentId: string): HTMLElement[]; + function findPhxChildrenInFragment(html: string, parentId: string): HTMLElement[]; + function incCycle(el: HTMLElement, key: string, trigger?: any): number; + function isFormInput(el: HTMLElement): boolean; + function isNowTriggerFormExternal(el: HTMLElement, phxTriggerExternal: string): boolean; + function isPhxChild(e: any): boolean; + function isPhxUpdate(e: any, t: any, n: any): boolean; + function isTextualInput(el: HTMLElement): boolean; + function mergeAttrs(target: HTMLElement, source: HTMLElement, exclude?: string[]): void; + function mergeFocusedInput(target: HTMLElement, source: HTMLElement): void; + function once(el: HTMLElement, key: string): boolean; + function putPrivate(el: HTMLElement, key: string, value: any): void; + function putTitle(str: string): void; + function removeClass(el: HTMLElement, className: string): void; + function restoreFocus(focused: HTMLElement, selectionStart: number, selectionEnd: number): void; + function syncAttrsToProps(el: HTMLElement): void; + function syncPendingRef(ref: number | null, fromEl: HTMLElement, toEl: HTMLElement): boolean; + function triggerCycle(el: HTMLElement, key: string, currentCycle?: number): void; + function undoRefs(ref: number | null, container: Node): void; +} diff --git a/types/phoenix_live_view/tsconfig.json b/types/phoenix_live_view/tsconfig.json new file mode 100644 index 0000000000..53fc585c7c --- /dev/null +++ b/types/phoenix_live_view/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "dom", + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts" + ] +} \ No newline at end of file diff --git a/types/phoenix_live_view/tslint.json b/types/phoenix_live_view/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/phoenix_live_view/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }