import Agile from "../agile"; import Item from "./item"; import Group, { GroupConfigInterface, GroupKey } from "./group"; import Selector, { SelectorKey } from "./selector"; import State, { StateKey } from "../state"; import { StorageKey } from "../storage"; export declare type DefaultDataItem = { [key: string]: any; }; export declare type CollectionKey = string | number; export declare type ItemKey = string | number; export interface CollectionConfigInterface { groups?: { [key: string]: Group; } | string[]; selectors?: { [key: string]: Selector; } | string[]; key?: CollectionKey; primaryKey?: string; defaultGroupKey?: ItemKey; } export interface CollectOptionsInterface { patch?: boolean; method?: 'push' | 'unshift'; forEachItem?: (item: DataType, key: ItemKey, index: number) => void; background?: boolean; } export declare type CollectionConfig = CollectionConfigInterface | ((collection: Collection) => CollectionConfigInterface); export default class Collection { agileInstance: () => Agile; config: CollectionConfigInterface; size: number; data: { [key: string]: Item; }; _key?: CollectionKey; isPersistCollection: boolean; groups: { [key: string]: Group; }; selectors: { [key: string]: Selector; }; constructor(agileInstance: Agile, config?: CollectionConfig); set key(value: StateKey | undefined); get key(): StateKey | undefined; /** * @internal * Init SubInstances like groups or selectors */ private initSubInstances; /** * Collect iterable data into this collection. * Note: Data items must include a primary key (id) */ collect(items: DataType | Array, groups?: GroupKey | Array, options?: CollectOptionsInterface): void; /** * * Update data by updateKey(id) in a Agile Collection */ update(updateKey: ItemKey, changes: DefaultDataItem | DataType, options?: { addNewProperties?: boolean; background?: boolean; }): State | undefined; /** * Create a group instance on this collection */ createGroup(groupName: GroupKey, initialItems?: Array): Group; /** * Create a selector instance on this collection */ createSelector(selectorName: SelectorKey, id: ItemKey): Selector; /** * Return an group from this collection as Group instance (extends State) */ getGroup(groupName: GroupKey): Group; /** * Return an selector from this collection as Selector instance (extends State) */ getSelector(selectorName: SelectorKey): Selector | undefined; /** * Remove fromGroups or everywhere */ remove(primaryKeys: ItemKey | Array): { fromGroups: (groups: Array | ItemKey) => void; everywhere: () => void; }; /** * Return an item from this collection by primaryKey as Item instance (extends State) */ findById(id: ItemKey): Item | undefined; /** * Return a value from this collection by primaryKey */ getValueById(id: ItemKey): DataType | undefined; /** * Saves the collection in the local storage or in a own configured storage * @param key - the storage key (if no key passed it will take the collection key) */ persist(key?: StorageKey): this; /** * Create a group instance under this collection (can be used in function based config) */ Group(initialItems?: Array, config?: GroupConfigInterface): Group; /** * Create a selector instance under this collection (can be used in function based config) */ Selector(initialSelection: ItemKey, options?: { key?: SelectorKey; }): Selector; /** * @internal * This will properly change the key of a collection item */ private updateItemPrimaryKeys; /** * @internal * Deletes Data from Groups */ removeFromGroups(primaryKeys: ItemKey | Array, groups: GroupKey | Array): void; /** * @internal * Deletes data directly form the collection */ removeData(primaryKeys: ItemKey | Array): void; /** * @internal * Save data directly into the collection */ saveData(data: DataType, options?: { patch?: boolean; background?: boolean; }): ItemKey | null; /** * @internal * Rebuild the Groups which contains the primaryKey */ rebuildGroupsThatIncludePrimaryKey(primaryKey: ItemKey, options?: { background?: boolean; forceRerender?: boolean; }): void; }