{"version":3,"file":"find.min.cjs","sources":["../../../../../../src/in/packages/find/src/Finder.ts","../../../../../../src/in/packages/find/src/find.ts","../../../../../../src/in/packages/find/src/index.ts"],"sourcesContent":["import { ILeaf, ILeafMap, IEventListenerId, IFindMethod, IAnswer, IFindCondition, IBooleanMap, IFinder, ISelectorConfig, ILeafer } from '@leafer-ui/interface'\nimport { ChildEvent, DataHelper, Answer, PropertyEvent, LeafHelper, isArray, isUndefined } from '@leafer-ui/draw'\n\n\nconst { Yes, NoAndSkip, YesAndSkip } = Answer\nconst idCondition = {} as IFindCondition, classNameCondition = {} as IFindCondition, tagCondition = {} as IFindCondition\n\nexport class Finder implements IFinder {\n\n    public target?: ILeaf // target 不存在时，为临时选择器（不能缓存数据）\n\n    protected innerIdMap: ILeafMap\n    protected idMap: ILeafMap\n\n    protected findLeaf: ILeaf\n\n    protected methods = {\n        id: (leaf: ILeaf, name: string) => leaf.id === name ? (this.target && (this.idMap[name] = leaf), 1) : 0,\n        innerId: (leaf: ILeaf, innerId: number) => leaf.innerId === innerId ? (this.target && (this.innerIdMap[innerId] = leaf), 1) : 0,\n        className: (leaf: ILeaf, name: string) => leaf.className === name ? 1 : 0,\n        tag: (leaf: ILeaf, name: string) => leaf.__tag === name ? 1 : 0,\n        tags: (leaf: ILeaf, nameMap: IBooleanMap) => nameMap[leaf.__tag] ? 1 : 0\n    }\n\n    protected __eventIds: IEventListenerId[]\n\n\n    constructor(target: ILeaf, _config: ISelectorConfig) {\n        this.idMap = {}\n        this.innerIdMap = {}\n\n        const app = (target && target.app) as ILeafer\n        if (app) {\n            app.idMap ? this.idMap = app.idMap : app.idMap = this.idMap\n            app.innerIdMap ? this.innerIdMap = app.innerIdMap : app.innerIdMap = this.innerIdMap\n        }\n\n        if (this.target = target) this.__listenEvents()\n    }\n\n    public getBy(condition: number | string | IFindCondition | IFindMethod, branch?: ILeaf, one?: boolean, options?: any): ILeaf | ILeaf[] {\n        switch (typeof condition) {\n            case 'number':\n                const leaf = this.getByInnerId(condition, branch)\n                return one ? leaf : (leaf ? [leaf] : [])\n            case 'string':\n                switch (condition[0]) {\n                    case '#':\n                        idCondition.id = condition.substring(1), condition = idCondition; break\n                    case '.':\n                        classNameCondition.className = condition.substring(1), condition = classNameCondition; break\n                    default:\n                        tagCondition.tag = condition, condition = tagCondition\n                }\n            case 'object':\n                if (!isUndefined(condition.id)) {\n                    const leaf = this.getById(condition.id as string, branch)\n                    return one ? leaf : (leaf ? [leaf] : [])\n                } else if (condition.tag) {\n                    const { tag } = condition, isArr = isArray(tag)\n                    return this.getByMethod(isArr ? this.methods.tags : this.methods.tag, branch, one, isArr ? DataHelper.toMap(tag) : tag)\n                } else {\n                    return this.getByMethod(this.methods.className, branch, one, condition.className)\n                }\n            case 'function':\n                return this.getByMethod(condition as IFindMethod, branch, one, options)\n        }\n    }\n\n\n    public getByInnerId(innerId: number, branch?: ILeaf): ILeaf {\n        const cache = this.innerIdMap[innerId]\n        if (cache) return cache\n        this.eachFind(this.toChildren(branch), this.methods.innerId, null, innerId)\n        return this.findLeaf\n    }\n\n    public getById(id: string, branch?: ILeaf): ILeaf {\n        const cache = this.idMap[id]\n        if (cache && LeafHelper.hasParent(cache, branch || this.target)) return cache\n        this.eachFind(this.toChildren(branch), this.methods.id, null, id)\n        return this.findLeaf\n    }\n\n    public getByClassName(className: string, branch?: ILeaf): ILeaf[] {\n        return this.getByMethod(this.methods.className, branch, false, className) as ILeaf[]\n    }\n\n    public getByTag(tag: string, branch?: ILeaf): ILeaf[] {\n        return this.getByMethod(this.methods.tag, branch, false, tag) as ILeaf[]\n    }\n\n    public getByMethod(method: IFindMethod, branch?: ILeaf, one?: boolean, options?: any): ILeaf[] | ILeaf {\n        const list: ILeaf[] = one ? null : []\n        this.eachFind(this.toChildren(branch), method, list, options)\n        return list || this.findLeaf\n    }\n\n\n    protected eachFind(children: ILeaf[], method: IFindMethod, list?: ILeaf[], options?: any): void {\n        let child: ILeaf, result: IAnswer\n        for (let i = 0, len = children.length; i < len; i++) {\n            child = children[i]\n            result = method(child, options)\n            if (typeof result === 'boolean') result = result ? 1 : 0\n            if (result === Yes || result === YesAndSkip) {\n                if (list) {\n                    list.push(child)\n                } else {\n                    this.findLeaf = child\n                    return\n                }\n            }\n            if (child.isBranch && result < NoAndSkip) this.eachFind(child.children, method, list, options)\n        }\n    }\n\n    protected toChildren(branch: ILeaf): ILeaf[] {\n        this.findLeaf = null\n        return [branch || this.target]\n    }\n\n\n    protected __onRemoveChild(event: ChildEvent): void {\n        const { id, innerId } = event.child\n        if (this.idMap[id]) delete this.idMap[id]\n        if (this.innerIdMap[innerId]) delete this.innerIdMap[innerId]\n    }\n\n    protected __checkIdChange(event: PropertyEvent): void {\n        if (event.attrName === 'id') {\n            const id = event.oldValue as string\n            if (this.idMap[id]) delete this.idMap[id]\n        }\n    }\n\n\n    protected __listenEvents(): void {\n        this.__eventIds = [\n            this.target.on_(ChildEvent.REMOVE, this.__onRemoveChild, this),\n            this.target.on_(PropertyEvent.CHANGE, this.__checkIdChange, this)\n        ]\n    }\n\n    protected __removeListenEvents(): void {\n        this.target.off_(this.__eventIds)\n        this.__eventIds.length = 0\n    }\n\n    public destroy(): void {\n        const { __eventIds } = this\n        if (__eventIds && __eventIds.length) {\n            this.__removeListenEvents()\n            this.innerIdMap = {}\n            this.idMap = {}\n        }\n        this.findLeaf = null\n    }\n\n}","\nimport { IFindMethod, ISelector, IFindUIMethod, IUI } from '@leafer-ui/interface'\nimport { UI, Creator, Platform } from '@leafer-ui/draw'\n\n\nconst ui = UI.prototype\n\nfunction getSelector(ui: IUI): ISelector {\n    return ui.leafer ? ui.leafer.selector : (Platform.selector || (Platform.selector = Creator.selector()))\n}\n\nui.find = function (condition: number | string | IFindUIMethod, options?: any): IUI[] {\n    return getSelector(this).getBy(condition as IFindMethod, this, false, options) as IUI[]\n}\n\nui.findOne = function (condition: number | string | IFindUIMethod, options?: any): IUI | undefined {\n    return getSelector(this).getBy(condition as IFindMethod, this, true, options) as IUI\n}","export { Finder } from './Finder'\n\nimport { ILeaf } from '@leafer-ui/interface'\nimport { Creator, LeafHelper, Plugin } from '@leafer-ui/draw'\nimport { Finder } from './Finder'\nimport './find'\n\n\nPlugin.add('find')\n\n\nCreator.finder = function (target, config) {\n    return new Finder(target, config)\n}\n\nLeafHelper.cacheId = function (t: ILeaf): void {  // 创建时缓存id元素\n    const { leafer, id } = t\n    if (id) leafer.app.idMap[id] = t\n    if (leafer.cacheInnerId) leafer.app.innerIdMap[t.innerId] = t\n\n}"],"names":["Yes","NoAndSkip","YesAndSkip","Answer","idCondition","classNameCondition","tagCondition","Finder","constructor","target","_config","this","methods","id","leaf","name","idMap","innerId","innerIdMap","className","tag","__tag","tags","nameMap","app","__listenEvents","getBy","condition","branch","one","options","getByInnerId","substring","isUndefined","isArr","isArray","getByMethod","DataHelper","toMap","getById","cache","eachFind","toChildren","findLeaf","LeafHelper","hasParent","getByClassName","getByTag","method","list","children","child","result","i","len","length","push","isBranch","__onRemoveChild","event","__checkIdChange","attrName","oldValue","__eventIds","on_","ChildEvent","REMOVE","PropertyEvent","CHANGE","__removeListenEvents","off_","destroy","ui","UI","prototype","getSelector","leafer","selector","Platform","Creator","find","findOne","Plugin","add","finder","config","cacheId","t","cacheInnerId"],"mappings":"8CAIA,MAAMA,IAAEA,EAAGC,UAAEA,EAASC,WAAEA,GAAeC,EAAAA,OACjCC,EAAc,CAAA,EAAsBC,EAAqB,CAAA,EAAsBC,EAAe,CAAA,QAEvFC,EAoBT,WAAAC,CAAYC,EAAeC,GAXjBC,KAAAC,QAAU,CAChBC,GAAI,CAACC,EAAaC,IAAiBD,EAAKD,KAAOE,GAAQJ,KAAKF,SAAWE,KAAKK,MAAMD,GAAQD,GAAO,GAAK,EACtGG,QAAS,CAACH,EAAaG,IAAoBH,EAAKG,UAAYA,GAAWN,KAAKF,SAAWE,KAAKO,WAAWD,GAAWH,GAAO,GAAK,EAC9HK,UAAW,CAACL,EAAaC,IAAiBD,EAAKK,YAAcJ,EAAO,EAA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