@ideal-postcodes/postcode-lookup
Version:
UK Postcode Lookup plugin from Ideal Postcodes
104 lines (103 loc) • 2.94 kB
TypeScript
/**
* @module Watch
*/
import { Controller, ControllerConfig } from "./controller";
import { Config } from "./index";
import { ObserverConfig } from "@ideal-postcodes/jsutil";
interface OnBindOptions extends ControllerConfig {
}
interface OnBindAttempt {
(options: OnBindOptions): void;
}
interface OnAnchorFoundOptions {
anchor: HTMLElement;
scope: HTMLElement | Document | null;
config: ControllerConfig;
}
interface OnAnchorFound {
(options: OnAnchorFoundOptions): void;
}
export interface WatchOptions {
/**
* A function executed on every interval to determine whether binding should take place
*
* @default `() => true`
*/
pageTest?: () => boolean;
/**
* Callback executed when an AddressFinder instance successfully binds
*
* @default `NOOP`
*/
onBind?: (c: Controller) => void;
/**
* Error callback. Executes when general error will be thrown.
*/
onError?: (error: unknown) => void;
/**
* Callback executed each time bind is attempted
*
* @default `NOOP`
*/
onBindAttempt?: OnBindAttempt;
/**
* Callback executed when anchor and its scope is detected
*
* @default `NOOP`
*/
onAnchorFound?: OnAnchorFound;
/**
* Custom anchor. Set this if you do not wish to anchor the `watch` method using inputField or outputFields.line_1
*
* @default `undefined`
*/
anchor?: string;
/**
* Sets the scope in which the AddressFinder can operate. Set to `null` to mark the whole page as in scope
*
* A function which provides the anchor as the first argument. The operable scope must be returned. By default the parent `<form>` element is returned
*
* @default Immediate <form> parent of anchor
*/
getScope?: (anchor: HTMLElement | null) => HTMLElement | Document;
/**
* Set the periodicity of page checks in milliseconds
*
* @default `1000`
*/
interval?: number;
/**
* Switch to mutationObserver
*/
mutationObserver?: boolean;
/**
* Allow to set DOM element to observe changes in it when mutationObserver is set to true
*/
target?: HTMLElement;
/**
* Setting mutationObserver option how observing should be observed
*/
observerConfig?: ObserverConfig;
/**
* Configure marker for solution is attached to specific context/anchor
* in DOM
*/
marker?: string;
}
export interface BindResult {
controller: Controller | undefined;
start: any;
stop: any;
}
export interface Watch {
(config: Config, options?: WatchOptions): BindResult;
}
/**
* Dynamically apply PostcodeLookup
* when relevant html configuration appear
* - Exits if page test is fails
* - Not binding when context is null or already have controller bound
* - Use controller bind to build solution
*/
export declare const watch: Watch;
export {};