/** * # Observable aRRAY * * this is module have a collection of class and functions to help with list * with tasks like events, binds, subgroups and tags * * @module */ import type { EventObject, EventTargetCallback, Options } from "./event.js"; import type { G } from "./galho.js"; import type { Dic, Key, bool, int, str } from "./util.js"; type CalcOptions = { vars: Dic; }; type Exp = { calc(opts: CalcOptions): any; }; export interface Tag { /**value */ v: T; /**index */ i: number; replace: bool; } export interface ListPlaceEvent { old: int; new: int; item: T; } export type UpdateEvent = { tp: "push"; items: T[]; start: int; } | { tp: "pop"; items: T[]; start: int; } | { tp: "place"; } | { tp: "edit"; } | { tp: "set"; items: T[]; removed: T[]; } | { tp: "tag"; tag: str; newI: number; oldI: number; } | { tp: "reload"; start: int; items: T[]; }; export interface EventMap extends Dic { update: [UpdateEvent]; push: [T[]]; edit: [EditEvent[]]; pop: [T[]]; place: [ListPlaceEvent]; } export interface ListEditItem { item: Key | T; props: Partial; } export interface EditEvent { item: T; props: Partial; } type Parse = (this: L, value: T | A, index: number) => void | T; export type IList = { key?: keyof T; child?: str; parse?: Parse; /**@deprecated */ converter?: Parse; g?: str[]; sorts?: Exp[]; clear?: bool; } | Parse; /** * An Array with suport for events, binds, subgroups and tags * @template T type of element this array store * @template A type of element this array accept */ export declare class L extends Array implements EventObject> { put(start: number, ...values: Array): this; removeAt(start: number, length?: number): T[]; push(...values: Array): number; pop(): T; shift(): T; unshift(...values: Array): number; /** * clear array and insert new elements * @param values */ set(values?: Array): this; has(id: T[K], fromIndex?: number): boolean; includes(searchElement: T[K], fromIndex?: number): boolean; includes(searchElement: T, fromIndex?: number): boolean; addGroup(key: str): Group; /** * get a group from id if it not exist create new */ group(key: str): Group; on(callback: EventTargetCallback, [UpdateEvent]>): L; on>(event: K, callback: EventTargetCallback[K]>, options?: Options): this; find(predicate: (value: T, index: number, obj: T[]) => value is S, thisArg?: any): S | undefined; find(predicate: (value: T, index: number, obj: T[]) => unknown, thisArg?: any): T | undefined; find(key: T | T[K]): T | undefined; findIndex(predicate: (value: T, index: number, obj: T[]) => unknown, thisArg?: any): number; findIndex(key: T | K): number; sort(compareFn?: (a: T, b: T) => number): this; remove(...items: (K | T)[]): this; place(item: K | T, newIndex: number): this; /**get tag */ tag(key: str): T; /**set tag */ tag(key: str, value: T | K, replace?: bool): this; /**remove tag */ tag(key: str, value: null): this; /**reload tag */ retag(k: str, o?: int): void; ontag(key: str, callback: (this: L, e: T | undefined) => any): this; unbind(s: G): this; bind(s: TS, opts?: LBond): TS; bind(s: TS, opts?: LBondInsert): TS; reload(item: T | K): this; /** volta a chamar o bind do elemento */ reloadAt(index: int): this; reloadAll(): this; /**events handlers*/ eh: { [P in keyof EventMap]?: EventTargetCallback[P]>[]; }; /**when true this List do not raise events */ slip?: bool; tags?: Dic>; sorts?: Exp[]; /**groups */ g: Dic>; /**no update */ nu?: bool; key?: K; childKey?: str; parse?: Parse; binds?: [s: G, fn: Function][]; } export type Alias = Array | L; export declare function copy(src: L, dest: L, fill?: bool): void; export declare function copy(src: L, dest: L, fill: bool, parse: (value: S, index: number) => D | A): void; export declare function tryPush(l: L, item: T): void; /** * edit properties of an element of collection * @param item * @param prop * @param value */ export declare function edit(l: L, item: ListEditItem): L; export declare function editItems(l: L, ...items: T[]): L; export type LBondInsert = (this: L, value: T, index?: number, container?: TS) => any; export interface LBond { /** * metodo que sera chamado no clear, caso n�ot tenha removera um item de cada vez*/ clear?: false | ((container: G) => void); /**inset an element in arbitrary position se retornar um valor inserira este elemento n�o posi��o do item adicionado*/ insert?: LBondInsert; reload?: (this: L, value: T, index?: number, container?: TS) => any; /** * remove an arbitrary element * se retornar true remove o item naquela posi��o * se não definido remove o item automaticamente * @param this * @param pos */ remove?: (this: L, item: T, index: number, container?: TS) => true | void; /** * * @param this * @param value * @param props * @param container */ edit?: (this: L, item: T, index: number, props: Partial, container: G) => G | void; /**chamado quando tenta se reposicionar um elemento */ place?: (this: L, oldPlace: number, newPlace: number, container: TS) => bool | void; /** * * @param this * @param empty * @param container */ empty?: (this: L, empty: bool, container?: TS) => any; /** * */ groups?: Dic> | GroupBind; /** */ tag?: (this: L, active: bool, index: number, parent: TS, tag: str, value: T) => void; } export declare function extend(l?: Alias, opts?: IList): L; export declare function orray(options: IList): L; export declare function orray(array?: L, options?: Parse): L; export declare function orray(array?: Alias, options?: IList): L; export default orray; interface GroupEvents extends Dic { push: [T[]]; remove: [T[]]; set: GroupSetEvent; } export type GroupSetEvent = [add?: T[], addId?: int[], remove?: T[], removeId?: int[]]; export declare class Group extends Array implements EventObject> { eh: { [P in keyof GroupEvents]?: EventTargetCallback[P]>[]; }; slip?: boolean; /**no update */ nu?: boolean; l: L; push(...items: (T | K)[]): number; toggle(item: T): void; pushRange(start: int, end: int): number[]; pushAll(): number[]; remove(...items: T[]): int[]; removeAt(index: number, count?: number): void; clear(): number[]; removeRange(from: number, to?: number): number[]; set(add: (T | K)[]): this; invert(): this; setRange(start: number, end: number): void; indexes(): number[]; keyField(): K[]; /**on update */ on(callback: EventTargetCallback, GroupSetEvent>): this; reload(v: T, i?: number): this; static get [Symbol.species](): ArrayConstructor; } export type GroupBind = (this: L, state: boolean, index: number, parent: TS, groupKey: string, item: T) => void; export declare function bind(l: L, s: TS, groupKey: string, bond: GroupBind): TS; export declare namespace range { type Tp = "set" | "add" | "range" | "addR"; function pivot(l: L, tag: str): number; function add(l: L, key: str, value: T | int | str, tp: Tp): L; /**select all elements */ function addAll(l: L, tag: str): L; /** remove focus */ function clear(l: L, tag: str): L; function onchange(l: L, tag: str, listener?: (this: L, active: T, selected?: Group) => void): L; /**select type */ const tp: (control: boolean, shift: boolean) => Tp; function move(l: L, tag: str, distance: number, tp: Tp): L; function movePivot(l: L, tag: str, distance: number, revert?: boolean): L; function list(l: L, key: str): T[]; }