@leafer/selector 1.2.2 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leafer/selector",
3
- "version": "1.2.2",
3
+ "version": "1.3.0",
4
4
  "description": "@leafer/selector",
5
5
  "author": "Chao (Leafer) Wan",
6
6
  "license": "MIT",
@@ -22,9 +22,9 @@
22
22
  "leaferjs"
23
23
  ],
24
24
  "dependencies": {
25
- "@leafer/core": "1.2.2"
25
+ "@leafer/core": "1.3.0"
26
26
  },
27
27
  "devDependencies": {
28
- "@leafer/interface": "1.2.2"
28
+ "@leafer/interface": "1.3.0"
29
29
  }
30
30
  }
package/src/Selector.ts CHANGED
@@ -1,164 +1,38 @@
1
- import { ILeaf, ILeafMap, ISelector, ISelectorProxy, IPickResult, IPickOptions, IPointData, IEventListenerId, ISelectorConfig, IFindMethod, IAnswer, IFindCondition, IBooleanMap } from '@leafer/interface'
2
- import { ChildEvent, LayoutEvent, DataHelper, Answer, Platform, PropertyEvent, LeafHelper } from '@leafer/core'
1
+ import { ILeaf, ISelector, ISelectorProxy, IPickResult, IPickOptions, IPointData, ISelectorConfig, IFinder, IFindMethod, IFindCondition, IPicker } from '@leafer/interface'
2
+ import { Creator, DataHelper, Plugin, Platform } from '@leafer/core'
3
3
 
4
4
  import { Picker } from './Picker'
5
5
 
6
6
 
7
- const { Yes, NoAndSkip, YesAndSkip } = Answer
8
- const idCondition = {} as IFindCondition, classNameCondition = {} as IFindCondition, tagCondition = {} as IFindCondition
9
7
  export class Selector implements ISelector {
10
8
 
11
9
  public target?: ILeaf // target 不存在时,为临时选择器(不能缓存数据)
12
-
13
10
  public proxy?: ISelectorProxy // editor
14
11
 
15
12
  public config: ISelectorConfig = {}
16
13
 
17
- protected picker: Picker
18
-
19
- protected innerIdMap: ILeafMap = {}
20
- protected idMap: ILeafMap = {}
21
-
22
- protected findLeaf: ILeaf
23
-
24
- protected methods = {
25
- id: (leaf: ILeaf, name: string) => leaf.id === name ? (this.target && (this.idMap[name] = leaf), 1) : 0,
26
- innerId: (leaf: ILeaf, innerId: number) => leaf.innerId === innerId ? (this.target && (this.innerIdMap[innerId] = leaf), 1) : 0,
27
- className: (leaf: ILeaf, name: string) => leaf.className === name ? 1 : 0,
28
- tag: (leaf: ILeaf, name: string) => leaf.__tag === name ? 1 : 0,
29
- tags: (leaf: ILeaf, nameMap: IBooleanMap) => nameMap[leaf.__tag] ? 1 : 0
30
- }
31
-
32
- protected __eventIds: IEventListenerId[]
33
-
14
+ public picker: IPicker
15
+ public finder?: IFinder
34
16
 
35
17
  constructor(target: ILeaf, userConfig?: ISelectorConfig) {
36
- this.target = target
37
18
  if (userConfig) this.config = DataHelper.default(userConfig, this.config)
38
- this.picker = new Picker(target, this)
39
- if (target) this.__listenEvents()
40
- }
41
-
42
- public getBy(condition: number | string | IFindCondition | IFindMethod, branch?: ILeaf, one?: boolean, options?: any): ILeaf | ILeaf[] {
43
- switch (typeof condition) {
44
- case 'number':
45
- const leaf = this.getByInnerId(condition, branch)
46
- return one ? leaf : (leaf ? [leaf] : [])
47
- case 'string':
48
- switch (condition[0]) {
49
- case '#':
50
- idCondition.id = condition.substring(1), condition = idCondition; break
51
- case '.':
52
- classNameCondition.className = condition.substring(1), condition = classNameCondition; break
53
- default:
54
- tagCondition.tag = condition, condition = tagCondition
55
- }
56
- case 'object':
57
- if (condition.id !== undefined) {
58
- const leaf = this.getById(condition.id as string, branch)
59
- return one ? leaf : (leaf ? [leaf] : [])
60
- } else if (condition.tag) {
61
- const { tag } = condition, isArray = tag instanceof Array
62
- return this.getByMethod(isArray ? this.methods.tags : this.methods.tag, branch, one, isArray ? DataHelper.toMap(tag) : tag)
63
- } else {
64
- return this.getByMethod(this.methods.className, branch, one, condition.className)
65
- }
66
- case 'function':
67
- return this.getByMethod(condition as IFindMethod, branch, one, options)
68
- }
19
+ this.picker = new Picker(this.target = target, this)
20
+ this.finder = Creator.finder && Creator.finder()
69
21
  }
70
22
 
71
23
  public getByPoint(hitPoint: IPointData, hitRadius: number, options?: IPickOptions): IPickResult {
72
- if (Platform.name === 'node' && this.target) this.target.emit(LayoutEvent.CHECK_UPDATE)
24
+ if (Platform.backgrounder && this.target) this.target.updateLayout()
73
25
  return this.picker.getByPoint(hitPoint, hitRadius, options)
74
26
  }
75
27
 
76
- public getByInnerId(innerId: number, branch?: ILeaf): ILeaf {
77
- const cache = this.innerIdMap[innerId]
78
- if (cache) return cache
79
- this.eachFind(this.toChildren(branch), this.methods.innerId, null, innerId)
80
- return this.findLeaf
81
- }
82
-
83
- public getById(id: string, branch?: ILeaf): ILeaf {
84
- const cache = this.idMap[id]
85
- if (cache && LeafHelper.hasParent(cache, branch || this.target)) return cache
86
- this.eachFind(this.toChildren(branch), this.methods.id, null, id)
87
- return this.findLeaf
88
- }
89
-
90
- public getByClassName(className: string, branch?: ILeaf): ILeaf[] {
91
- return this.getByMethod(this.methods.className, branch, false, className) as ILeaf[]
92
- }
93
-
94
- public getByTag(tag: string, branch?: ILeaf): ILeaf[] {
95
- return this.getByMethod(this.methods.tag, branch, false, tag) as ILeaf[]
96
- }
97
-
98
- public getByMethod(method: IFindMethod, branch?: ILeaf, one?: boolean, options?: any): ILeaf[] | ILeaf {
99
- const list: ILeaf[] = one ? null : []
100
- this.eachFind(this.toChildren(branch), method, list, options)
101
- return list || this.findLeaf
102
- }
103
-
104
-
105
- protected eachFind(children: ILeaf[], method: IFindMethod, list?: ILeaf[], options?: any): void {
106
- let child: ILeaf, result: IAnswer
107
- for (let i = 0, len = children.length; i < len; i++) {
108
- child = children[i]
109
- result = method(child, options)
110
- if (result === Yes || result === YesAndSkip) {
111
- if (list) {
112
- list.push(child)
113
- } else {
114
- this.findLeaf = child
115
- return
116
- }
117
- }
118
- if (child.isBranch && result < NoAndSkip) this.eachFind(child.children, method, list, options)
119
- }
120
- }
121
-
122
- protected toChildren(branch: ILeaf): ILeaf[] {
123
- this.findLeaf = null
124
- return [branch || this.target]
125
- }
126
-
127
-
128
- protected __onRemoveChild(event: ChildEvent): void {
129
- const { id, innerId } = event.child
130
- if (this.idMap[id]) delete this.idMap[id]
131
- if (this.innerIdMap[innerId]) delete this.innerIdMap[innerId]
132
- }
133
-
134
- protected __checkIdChange(event: PropertyEvent): void {
135
- if (event.attrName === 'id') {
136
- const id = event.oldValue as string
137
- if (this.idMap[id]) delete this.idMap[id]
138
- }
139
- }
140
-
141
-
142
- protected __listenEvents(): void {
143
- this.__eventIds = [
144
- this.target.on_(ChildEvent.REMOVE, this.__onRemoveChild, this),
145
- this.target.on_(PropertyEvent.CHANGE, this.__checkIdChange, this)
146
- ]
147
- }
148
-
149
- protected __removeListenEvents(): void {
150
- this.target.off_(this.__eventIds)
151
- this.__eventIds.length = 0
28
+ // @leafer-in/find will rewrite
29
+ public getBy(condition: number | string | IFindCondition | IFindMethod, branch?: ILeaf, one?: boolean, options?: any): ILeaf | ILeaf[] {
30
+ return this.finder ? this.finder.getBy(condition, branch, one, options) : Plugin.need('find')
152
31
  }
153
32
 
154
33
  public destroy(): void {
155
- if (this.__eventIds.length) {
156
- this.__removeListenEvents()
157
- this.picker.destroy()
158
- this.findLeaf = null
159
- this.innerIdMap = {}
160
- this.idMap = {}
161
- }
34
+ this.picker.destroy()
35
+ if (this.finder) this.finder.destroy()
162
36
  }
163
37
 
164
38
  }
package/src/index.ts CHANGED
@@ -1 +1,2 @@
1
- export { Selector } from './Selector'
1
+ export { Selector } from './Selector'
2
+ export { Picker } from './Picker'
package/types/index.d.ts CHANGED
@@ -1,5 +1,17 @@
1
- import { ILeaf, ISelector, ILeafList, IRadiusPointData, IPointData, IPickOptions, IPickResult, IPickBottom, ISelectorProxy, ISelectorConfig, ILeafMap, IBooleanMap, IEventListenerId, IFindCondition, IFindMethod } from '@leafer/interface';
2
- import { LeafList, ChildEvent, PropertyEvent } from '@leafer/core';
1
+ import { ISelector, ILeaf, ISelectorProxy, ISelectorConfig, IPicker, IFinder, IPointData, IPickOptions, IPickResult, IFindCondition, IFindMethod, ILeafList, IRadiusPointData, IPickBottom } from '@leafer/interface';
2
+ import { LeafList } from '@leafer/core';
3
+
4
+ declare class Selector implements ISelector {
5
+ target?: ILeaf;
6
+ proxy?: ISelectorProxy;
7
+ config: ISelectorConfig;
8
+ picker: IPicker;
9
+ finder?: IFinder;
10
+ constructor(target: ILeaf, userConfig?: ISelectorConfig);
11
+ getByPoint(hitPoint: IPointData, hitRadius: number, options?: IPickOptions): IPickResult;
12
+ getBy(condition: number | string | IFindCondition | IFindMethod, branch?: ILeaf, one?: boolean, options?: any): ILeaf | ILeaf[];
13
+ destroy(): void;
14
+ }
3
15
 
4
16
  declare class Picker {
5
17
  protected target?: ILeaf;
@@ -20,37 +32,4 @@ declare class Picker {
20
32
  destroy(): void;
21
33
  }
22
34
 
23
- declare class Selector implements ISelector {
24
- target?: ILeaf;
25
- proxy?: ISelectorProxy;
26
- config: ISelectorConfig;
27
- protected picker: Picker;
28
- protected innerIdMap: ILeafMap;
29
- protected idMap: ILeafMap;
30
- protected findLeaf: ILeaf;
31
- protected methods: {
32
- id: (leaf: ILeaf, name: string) => 1 | 0;
33
- innerId: (leaf: ILeaf, innerId: number) => 1 | 0;
34
- className: (leaf: ILeaf, name: string) => 1 | 0;
35
- tag: (leaf: ILeaf, name: string) => 1 | 0;
36
- tags: (leaf: ILeaf, nameMap: IBooleanMap) => 1 | 0;
37
- };
38
- protected __eventIds: IEventListenerId[];
39
- constructor(target: ILeaf, userConfig?: ISelectorConfig);
40
- getBy(condition: number | string | IFindCondition | IFindMethod, branch?: ILeaf, one?: boolean, options?: any): ILeaf | ILeaf[];
41
- getByPoint(hitPoint: IPointData, hitRadius: number, options?: IPickOptions): IPickResult;
42
- getByInnerId(innerId: number, branch?: ILeaf): ILeaf;
43
- getById(id: string, branch?: ILeaf): ILeaf;
44
- getByClassName(className: string, branch?: ILeaf): ILeaf[];
45
- getByTag(tag: string, branch?: ILeaf): ILeaf[];
46
- getByMethod(method: IFindMethod, branch?: ILeaf, one?: boolean, options?: any): ILeaf[] | ILeaf;
47
- protected eachFind(children: ILeaf[], method: IFindMethod, list?: ILeaf[], options?: any): void;
48
- protected toChildren(branch: ILeaf): ILeaf[];
49
- protected __onRemoveChild(event: ChildEvent): void;
50
- protected __checkIdChange(event: PropertyEvent): void;
51
- protected __listenEvents(): void;
52
- protected __removeListenEvents(): void;
53
- destroy(): void;
54
- }
55
-
56
- export { Selector };
35
+ export { Picker, Selector };