@leafer-in/find 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023-present, Chao (Leafer) Wan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # @leafer-in/find
package/dist/find.cjs ADDED
@@ -0,0 +1,150 @@
1
+ 'use strict';
2
+
3
+ var draw = require('@leafer-ui/draw');
4
+
5
+ const { Yes, NoAndSkip, YesAndSkip } = draw.Answer;
6
+ const idCondition = {}, classNameCondition = {}, tagCondition = {};
7
+ class Finder {
8
+ constructor(target) {
9
+ this.innerIdMap = {};
10
+ this.idMap = {};
11
+ this.methods = {
12
+ id: (leaf, name) => leaf.id === name ? (this.target && (this.idMap[name] = leaf), 1) : 0,
13
+ innerId: (leaf, innerId) => leaf.innerId === innerId ? (this.target && (this.innerIdMap[innerId] = leaf), 1) : 0,
14
+ className: (leaf, name) => leaf.className === name ? 1 : 0,
15
+ tag: (leaf, name) => leaf.__tag === name ? 1 : 0,
16
+ tags: (leaf, nameMap) => nameMap[leaf.__tag] ? 1 : 0
17
+ };
18
+ if (this.target = target)
19
+ this.__listenEvents();
20
+ }
21
+ getBy(condition, branch, one, options) {
22
+ switch (typeof condition) {
23
+ case 'number':
24
+ const leaf = this.getByInnerId(condition, branch);
25
+ return one ? leaf : (leaf ? [leaf] : []);
26
+ case 'string':
27
+ switch (condition[0]) {
28
+ case '#':
29
+ idCondition.id = condition.substring(1), condition = idCondition;
30
+ break;
31
+ case '.':
32
+ classNameCondition.className = condition.substring(1), condition = classNameCondition;
33
+ break;
34
+ default:
35
+ tagCondition.tag = condition, condition = tagCondition;
36
+ }
37
+ case 'object':
38
+ if (condition.id !== undefined) {
39
+ const leaf = this.getById(condition.id, branch);
40
+ return one ? leaf : (leaf ? [leaf] : []);
41
+ }
42
+ else if (condition.tag) {
43
+ const { tag } = condition, isArray = tag instanceof Array;
44
+ return this.getByMethod(isArray ? this.methods.tags : this.methods.tag, branch, one, isArray ? draw.DataHelper.toMap(tag) : tag);
45
+ }
46
+ else {
47
+ return this.getByMethod(this.methods.className, branch, one, condition.className);
48
+ }
49
+ case 'function':
50
+ return this.getByMethod(condition, branch, one, options);
51
+ }
52
+ }
53
+ getByInnerId(innerId, branch) {
54
+ const cache = this.innerIdMap[innerId];
55
+ if (cache)
56
+ return cache;
57
+ this.eachFind(this.toChildren(branch), this.methods.innerId, null, innerId);
58
+ return this.findLeaf;
59
+ }
60
+ getById(id, branch) {
61
+ const cache = this.idMap[id];
62
+ if (cache && draw.LeafHelper.hasParent(cache, branch || this.target))
63
+ return cache;
64
+ this.eachFind(this.toChildren(branch), this.methods.id, null, id);
65
+ return this.findLeaf;
66
+ }
67
+ getByClassName(className, branch) {
68
+ return this.getByMethod(this.methods.className, branch, false, className);
69
+ }
70
+ getByTag(tag, branch) {
71
+ return this.getByMethod(this.methods.tag, branch, false, tag);
72
+ }
73
+ getByMethod(method, branch, one, options) {
74
+ const list = one ? null : [];
75
+ this.eachFind(this.toChildren(branch), method, list, options);
76
+ return list || this.findLeaf;
77
+ }
78
+ eachFind(children, method, list, options) {
79
+ let child, result;
80
+ for (let i = 0, len = children.length; i < len; i++) {
81
+ child = children[i];
82
+ result = method(child, options);
83
+ if (result === Yes || result === YesAndSkip) {
84
+ if (list) {
85
+ list.push(child);
86
+ }
87
+ else {
88
+ this.findLeaf = child;
89
+ return;
90
+ }
91
+ }
92
+ if (child.isBranch && result < NoAndSkip)
93
+ this.eachFind(child.children, method, list, options);
94
+ }
95
+ }
96
+ toChildren(branch) {
97
+ this.findLeaf = null;
98
+ return [branch || this.target];
99
+ }
100
+ __onRemoveChild(event) {
101
+ const { id, innerId } = event.child;
102
+ if (this.idMap[id])
103
+ delete this.idMap[id];
104
+ if (this.innerIdMap[innerId])
105
+ delete this.innerIdMap[innerId];
106
+ }
107
+ __checkIdChange(event) {
108
+ if (event.attrName === 'id') {
109
+ const id = event.oldValue;
110
+ if (this.idMap[id])
111
+ delete this.idMap[id];
112
+ }
113
+ }
114
+ __listenEvents() {
115
+ this.__eventIds = [
116
+ this.target.on_(draw.ChildEvent.REMOVE, this.__onRemoveChild, this),
117
+ this.target.on_(draw.PropertyEvent.CHANGE, this.__checkIdChange, this)
118
+ ];
119
+ }
120
+ __removeListenEvents() {
121
+ this.target.off_(this.__eventIds);
122
+ this.__eventIds.length = 0;
123
+ }
124
+ destroy() {
125
+ const { __eventIds } = this;
126
+ if (__eventIds && __eventIds.length) {
127
+ this.__removeListenEvents();
128
+ this.innerIdMap = {};
129
+ this.idMap = {};
130
+ }
131
+ this.findLeaf = null;
132
+ }
133
+ }
134
+
135
+ const ui = draw.UI.prototype;
136
+ function getSelector(ui) {
137
+ return ui.leafer ? ui.leafer.selector : (draw.Platform.selector || (draw.Platform.selector = draw.Creator.selector()));
138
+ }
139
+ ui.find = function (condition, options) {
140
+ return getSelector(this).getBy(condition, this, false, options);
141
+ };
142
+ ui.findOne = function (condition, options) {
143
+ return getSelector(this).getBy(condition, this, true, options);
144
+ };
145
+
146
+ draw.Creator.finder = function (target) {
147
+ return new Finder(target);
148
+ };
149
+
150
+ exports.Finder = Finder;
@@ -0,0 +1,148 @@
1
+ import { DataHelper, LeafHelper, ChildEvent, PropertyEvent, Answer, UI, Platform, Creator } from '@leafer-ui/draw';
2
+
3
+ const { Yes, NoAndSkip, YesAndSkip } = Answer;
4
+ const idCondition = {}, classNameCondition = {}, tagCondition = {};
5
+ class Finder {
6
+ constructor(target) {
7
+ this.innerIdMap = {};
8
+ this.idMap = {};
9
+ this.methods = {
10
+ id: (leaf, name) => leaf.id === name ? (this.target && (this.idMap[name] = leaf), 1) : 0,
11
+ innerId: (leaf, innerId) => leaf.innerId === innerId ? (this.target && (this.innerIdMap[innerId] = leaf), 1) : 0,
12
+ className: (leaf, name) => leaf.className === name ? 1 : 0,
13
+ tag: (leaf, name) => leaf.__tag === name ? 1 : 0,
14
+ tags: (leaf, nameMap) => nameMap[leaf.__tag] ? 1 : 0
15
+ };
16
+ if (this.target = target)
17
+ this.__listenEvents();
18
+ }
19
+ getBy(condition, branch, one, options) {
20
+ switch (typeof condition) {
21
+ case 'number':
22
+ const leaf = this.getByInnerId(condition, branch);
23
+ return one ? leaf : (leaf ? [leaf] : []);
24
+ case 'string':
25
+ switch (condition[0]) {
26
+ case '#':
27
+ idCondition.id = condition.substring(1), condition = idCondition;
28
+ break;
29
+ case '.':
30
+ classNameCondition.className = condition.substring(1), condition = classNameCondition;
31
+ break;
32
+ default:
33
+ tagCondition.tag = condition, condition = tagCondition;
34
+ }
35
+ case 'object':
36
+ if (condition.id !== undefined) {
37
+ const leaf = this.getById(condition.id, branch);
38
+ return one ? leaf : (leaf ? [leaf] : []);
39
+ }
40
+ else if (condition.tag) {
41
+ const { tag } = condition, isArray = tag instanceof Array;
42
+ return this.getByMethod(isArray ? this.methods.tags : this.methods.tag, branch, one, isArray ? DataHelper.toMap(tag) : tag);
43
+ }
44
+ else {
45
+ return this.getByMethod(this.methods.className, branch, one, condition.className);
46
+ }
47
+ case 'function':
48
+ return this.getByMethod(condition, branch, one, options);
49
+ }
50
+ }
51
+ getByInnerId(innerId, branch) {
52
+ const cache = this.innerIdMap[innerId];
53
+ if (cache)
54
+ return cache;
55
+ this.eachFind(this.toChildren(branch), this.methods.innerId, null, innerId);
56
+ return this.findLeaf;
57
+ }
58
+ getById(id, branch) {
59
+ const cache = this.idMap[id];
60
+ if (cache && LeafHelper.hasParent(cache, branch || this.target))
61
+ return cache;
62
+ this.eachFind(this.toChildren(branch), this.methods.id, null, id);
63
+ return this.findLeaf;
64
+ }
65
+ getByClassName(className, branch) {
66
+ return this.getByMethod(this.methods.className, branch, false, className);
67
+ }
68
+ getByTag(tag, branch) {
69
+ return this.getByMethod(this.methods.tag, branch, false, tag);
70
+ }
71
+ getByMethod(method, branch, one, options) {
72
+ const list = one ? null : [];
73
+ this.eachFind(this.toChildren(branch), method, list, options);
74
+ return list || this.findLeaf;
75
+ }
76
+ eachFind(children, method, list, options) {
77
+ let child, result;
78
+ for (let i = 0, len = children.length; i < len; i++) {
79
+ child = children[i];
80
+ result = method(child, options);
81
+ if (result === Yes || result === YesAndSkip) {
82
+ if (list) {
83
+ list.push(child);
84
+ }
85
+ else {
86
+ this.findLeaf = child;
87
+ return;
88
+ }
89
+ }
90
+ if (child.isBranch && result < NoAndSkip)
91
+ this.eachFind(child.children, method, list, options);
92
+ }
93
+ }
94
+ toChildren(branch) {
95
+ this.findLeaf = null;
96
+ return [branch || this.target];
97
+ }
98
+ __onRemoveChild(event) {
99
+ const { id, innerId } = event.child;
100
+ if (this.idMap[id])
101
+ delete this.idMap[id];
102
+ if (this.innerIdMap[innerId])
103
+ delete this.innerIdMap[innerId];
104
+ }
105
+ __checkIdChange(event) {
106
+ if (event.attrName === 'id') {
107
+ const id = event.oldValue;
108
+ if (this.idMap[id])
109
+ delete this.idMap[id];
110
+ }
111
+ }
112
+ __listenEvents() {
113
+ this.__eventIds = [
114
+ this.target.on_(ChildEvent.REMOVE, this.__onRemoveChild, this),
115
+ this.target.on_(PropertyEvent.CHANGE, this.__checkIdChange, this)
116
+ ];
117
+ }
118
+ __removeListenEvents() {
119
+ this.target.off_(this.__eventIds);
120
+ this.__eventIds.length = 0;
121
+ }
122
+ destroy() {
123
+ const { __eventIds } = this;
124
+ if (__eventIds && __eventIds.length) {
125
+ this.__removeListenEvents();
126
+ this.innerIdMap = {};
127
+ this.idMap = {};
128
+ }
129
+ this.findLeaf = null;
130
+ }
131
+ }
132
+
133
+ const ui = UI.prototype;
134
+ function getSelector(ui) {
135
+ return ui.leafer ? ui.leafer.selector : (Platform.selector || (Platform.selector = Creator.selector()));
136
+ }
137
+ ui.find = function (condition, options) {
138
+ return getSelector(this).getBy(condition, this, false, options);
139
+ };
140
+ ui.findOne = function (condition, options) {
141
+ return getSelector(this).getBy(condition, this, true, options);
142
+ };
143
+
144
+ Creator.finder = function (target) {
145
+ return new Finder(target);
146
+ };
147
+
148
+ export { Finder };
@@ -0,0 +1 @@
1
+ import{DataHelper as t,LeafHelper as e,ChildEvent as i,PropertyEvent as s,Answer as n,UI as h,Platform as r,Creator as a}from"@leafer-ui/draw";const{Yes:d,NoAndSkip:o,YesAndSkip:c}=n,l={},g={},f={};class u{constructor(t){this.innerIdMap={},this.idMap={},this.methods={id:(t,e)=>t.id===e?(this.target&&(this.idMap[e]=t),1):0,innerId:(t,e)=>t.innerId===e?(this.target&&(this.innerIdMap[e]=t),1):0,className:(t,e)=>t.className===e?1:0,tag:(t,e)=>t.__tag===e?1:0,tags:(t,e)=>e[t.__tag]?1:0},(this.target=t)&&this.__listenEvents()}getBy(e,i,s,n){switch(typeof e){case"number":const h=this.getByInnerId(e,i);return s?h:h?[h]:[];case"string":switch(e[0]){case"#":l.id=e.substring(1),e=l;break;case".":g.className=e.substring(1),e=g;break;default:f.tag=e,e=f}case"object":if(void 0!==e.id){const t=this.getById(e.id,i);return s?t:t?[t]:[]}if(e.tag){const{tag:n}=e,h=n instanceof Array;return this.getByMethod(h?this.methods.tags:this.methods.tag,i,s,h?t.toMap(n):n)}return this.getByMethod(this.methods.className,i,s,e.className);case"function":return this.getByMethod(e,i,s,n)}}getByInnerId(t,e){const i=this.innerIdMap[t];return i||(this.eachFind(this.toChildren(e),this.methods.innerId,null,t),this.findLeaf)}getById(t,i){const s=this.idMap[t];return s&&e.hasParent(s,i||this.target)?s:(this.eachFind(this.toChildren(i),this.methods.id,null,t),this.findLeaf)}getByClassName(t,e){return this.getByMethod(this.methods.className,e,!1,t)}getByTag(t,e){return this.getByMethod(this.methods.tag,e,!1,t)}getByMethod(t,e,i,s){const n=i?null:[];return this.eachFind(this.toChildren(e),t,n,s),n||this.findLeaf}eachFind(t,e,i,s){let n,h;for(let r=0,a=t.length;r<a;r++){if(n=t[r],h=e(n,s),h===d||h===c){if(!i)return void(this.findLeaf=n);i.push(n)}n.isBranch&&h<o&&this.eachFind(n.children,e,i,s)}}toChildren(t){return this.findLeaf=null,[t||this.target]}__onRemoveChild(t){const{id:e,innerId:i}=t.child;this.idMap[e]&&delete this.idMap[e],this.innerIdMap[i]&&delete this.innerIdMap[i]}__checkIdChange(t){if("id"===t.attrName){const e=t.oldValue;this.idMap[e]&&delete this.idMap[e]}}__listenEvents(){this.__eventIds=[this.target.on_(i.REMOVE,this.__onRemoveChild,this),this.target.on_(s.CHANGE,this.__checkIdChange,this)]}__removeListenEvents(){this.target.off_(this.__eventIds),this.__eventIds.length=0}destroy(){const{__eventIds:t}=this;t&&t.length&&(this.__removeListenEvents(),this.innerIdMap={},this.idMap={}),this.findLeaf=null}}const _=h.prototype;function m(t){return t.leafer?t.leafer.selector:r.selector||(r.selector=a.selector())}_.find=function(t,e){return m(this).getBy(t,this,!1,e)},_.findOne=function(t,e){return m(this).getBy(t,this,!0,e)},a.finder=function(t){return new u(t)};export{u as Finder};
package/dist/find.js ADDED
@@ -0,0 +1,154 @@
1
+ this.LeaferIN = this.LeaferIN || {};
2
+ this.LeaferIN.find = (function (exports, draw) {
3
+ 'use strict';
4
+
5
+ const { Yes, NoAndSkip, YesAndSkip } = draw.Answer;
6
+ const idCondition = {}, classNameCondition = {}, tagCondition = {};
7
+ class Finder {
8
+ constructor(target) {
9
+ this.innerIdMap = {};
10
+ this.idMap = {};
11
+ this.methods = {
12
+ id: (leaf, name) => leaf.id === name ? (this.target && (this.idMap[name] = leaf), 1) : 0,
13
+ innerId: (leaf, innerId) => leaf.innerId === innerId ? (this.target && (this.innerIdMap[innerId] = leaf), 1) : 0,
14
+ className: (leaf, name) => leaf.className === name ? 1 : 0,
15
+ tag: (leaf, name) => leaf.__tag === name ? 1 : 0,
16
+ tags: (leaf, nameMap) => nameMap[leaf.__tag] ? 1 : 0
17
+ };
18
+ if (this.target = target)
19
+ this.__listenEvents();
20
+ }
21
+ getBy(condition, branch, one, options) {
22
+ switch (typeof condition) {
23
+ case 'number':
24
+ const leaf = this.getByInnerId(condition, branch);
25
+ return one ? leaf : (leaf ? [leaf] : []);
26
+ case 'string':
27
+ switch (condition[0]) {
28
+ case '#':
29
+ idCondition.id = condition.substring(1), condition = idCondition;
30
+ break;
31
+ case '.':
32
+ classNameCondition.className = condition.substring(1), condition = classNameCondition;
33
+ break;
34
+ default:
35
+ tagCondition.tag = condition, condition = tagCondition;
36
+ }
37
+ case 'object':
38
+ if (condition.id !== undefined) {
39
+ const leaf = this.getById(condition.id, branch);
40
+ return one ? leaf : (leaf ? [leaf] : []);
41
+ }
42
+ else if (condition.tag) {
43
+ const { tag } = condition, isArray = tag instanceof Array;
44
+ return this.getByMethod(isArray ? this.methods.tags : this.methods.tag, branch, one, isArray ? draw.DataHelper.toMap(tag) : tag);
45
+ }
46
+ else {
47
+ return this.getByMethod(this.methods.className, branch, one, condition.className);
48
+ }
49
+ case 'function':
50
+ return this.getByMethod(condition, branch, one, options);
51
+ }
52
+ }
53
+ getByInnerId(innerId, branch) {
54
+ const cache = this.innerIdMap[innerId];
55
+ if (cache)
56
+ return cache;
57
+ this.eachFind(this.toChildren(branch), this.methods.innerId, null, innerId);
58
+ return this.findLeaf;
59
+ }
60
+ getById(id, branch) {
61
+ const cache = this.idMap[id];
62
+ if (cache && draw.LeafHelper.hasParent(cache, branch || this.target))
63
+ return cache;
64
+ this.eachFind(this.toChildren(branch), this.methods.id, null, id);
65
+ return this.findLeaf;
66
+ }
67
+ getByClassName(className, branch) {
68
+ return this.getByMethod(this.methods.className, branch, false, className);
69
+ }
70
+ getByTag(tag, branch) {
71
+ return this.getByMethod(this.methods.tag, branch, false, tag);
72
+ }
73
+ getByMethod(method, branch, one, options) {
74
+ const list = one ? null : [];
75
+ this.eachFind(this.toChildren(branch), method, list, options);
76
+ return list || this.findLeaf;
77
+ }
78
+ eachFind(children, method, list, options) {
79
+ let child, result;
80
+ for (let i = 0, len = children.length; i < len; i++) {
81
+ child = children[i];
82
+ result = method(child, options);
83
+ if (result === Yes || result === YesAndSkip) {
84
+ if (list) {
85
+ list.push(child);
86
+ }
87
+ else {
88
+ this.findLeaf = child;
89
+ return;
90
+ }
91
+ }
92
+ if (child.isBranch && result < NoAndSkip)
93
+ this.eachFind(child.children, method, list, options);
94
+ }
95
+ }
96
+ toChildren(branch) {
97
+ this.findLeaf = null;
98
+ return [branch || this.target];
99
+ }
100
+ __onRemoveChild(event) {
101
+ const { id, innerId } = event.child;
102
+ if (this.idMap[id])
103
+ delete this.idMap[id];
104
+ if (this.innerIdMap[innerId])
105
+ delete this.innerIdMap[innerId];
106
+ }
107
+ __checkIdChange(event) {
108
+ if (event.attrName === 'id') {
109
+ const id = event.oldValue;
110
+ if (this.idMap[id])
111
+ delete this.idMap[id];
112
+ }
113
+ }
114
+ __listenEvents() {
115
+ this.__eventIds = [
116
+ this.target.on_(draw.ChildEvent.REMOVE, this.__onRemoveChild, this),
117
+ this.target.on_(draw.PropertyEvent.CHANGE, this.__checkIdChange, this)
118
+ ];
119
+ }
120
+ __removeListenEvents() {
121
+ this.target.off_(this.__eventIds);
122
+ this.__eventIds.length = 0;
123
+ }
124
+ destroy() {
125
+ const { __eventIds } = this;
126
+ if (__eventIds && __eventIds.length) {
127
+ this.__removeListenEvents();
128
+ this.innerIdMap = {};
129
+ this.idMap = {};
130
+ }
131
+ this.findLeaf = null;
132
+ }
133
+ }
134
+
135
+ const ui = draw.UI.prototype;
136
+ function getSelector(ui) {
137
+ return ui.leafer ? ui.leafer.selector : (draw.Platform.selector || (draw.Platform.selector = draw.Creator.selector()));
138
+ }
139
+ ui.find = function (condition, options) {
140
+ return getSelector(this).getBy(condition, this, false, options);
141
+ };
142
+ ui.findOne = function (condition, options) {
143
+ return getSelector(this).getBy(condition, this, true, options);
144
+ };
145
+
146
+ draw.Creator.finder = function (target) {
147
+ return new Finder(target);
148
+ };
149
+
150
+ exports.Finder = Finder;
151
+
152
+ return exports;
153
+
154
+ })({}, LeaferUI);
@@ -0,0 +1 @@
1
+ "use strict";var t=require("@leafer-ui/draw");const{Yes:e,NoAndSkip:i,YesAndSkip:s}=t.Answer,n={},h={},r={};class a{constructor(t){this.innerIdMap={},this.idMap={},this.methods={id:(t,e)=>t.id===e?(this.target&&(this.idMap[e]=t),1):0,innerId:(t,e)=>t.innerId===e?(this.target&&(this.innerIdMap[e]=t),1):0,className:(t,e)=>t.className===e?1:0,tag:(t,e)=>t.__tag===e?1:0,tags:(t,e)=>e[t.__tag]?1:0},(this.target=t)&&this.__listenEvents()}getBy(e,i,s,a){switch(typeof e){case"number":const d=this.getByInnerId(e,i);return s?d:d?[d]:[];case"string":switch(e[0]){case"#":n.id=e.substring(1),e=n;break;case".":h.className=e.substring(1),e=h;break;default:r.tag=e,e=r}case"object":if(void 0!==e.id){const t=this.getById(e.id,i);return s?t:t?[t]:[]}if(e.tag){const{tag:n}=e,h=n instanceof Array;return this.getByMethod(h?this.methods.tags:this.methods.tag,i,s,h?t.DataHelper.toMap(n):n)}return this.getByMethod(this.methods.className,i,s,e.className);case"function":return this.getByMethod(e,i,s,a)}}getByInnerId(t,e){const i=this.innerIdMap[t];return i||(this.eachFind(this.toChildren(e),this.methods.innerId,null,t),this.findLeaf)}getById(e,i){const s=this.idMap[e];return s&&t.LeafHelper.hasParent(s,i||this.target)?s:(this.eachFind(this.toChildren(i),this.methods.id,null,e),this.findLeaf)}getByClassName(t,e){return this.getByMethod(this.methods.className,e,!1,t)}getByTag(t,e){return this.getByMethod(this.methods.tag,e,!1,t)}getByMethod(t,e,i,s){const n=i?null:[];return this.eachFind(this.toChildren(e),t,n,s),n||this.findLeaf}eachFind(t,n,h,r){let a,d;for(let o=0,c=t.length;o<c;o++){if(a=t[o],d=n(a,r),d===e||d===s){if(!h)return void(this.findLeaf=a);h.push(a)}a.isBranch&&d<i&&this.eachFind(a.children,n,h,r)}}toChildren(t){return this.findLeaf=null,[t||this.target]}__onRemoveChild(t){const{id:e,innerId:i}=t.child;this.idMap[e]&&delete this.idMap[e],this.innerIdMap[i]&&delete this.innerIdMap[i]}__checkIdChange(t){if("id"===t.attrName){const e=t.oldValue;this.idMap[e]&&delete this.idMap[e]}}__listenEvents(){this.__eventIds=[this.target.on_(t.ChildEvent.REMOVE,this.__onRemoveChild,this),this.target.on_(t.PropertyEvent.CHANGE,this.__checkIdChange,this)]}__removeListenEvents(){this.target.off_(this.__eventIds),this.__eventIds.length=0}destroy(){const{__eventIds:t}=this;t&&t.length&&(this.__removeListenEvents(),this.innerIdMap={},this.idMap={}),this.findLeaf=null}}const d=t.UI.prototype;function o(e){return e.leafer?e.leafer.selector:t.Platform.selector||(t.Platform.selector=t.Creator.selector())}d.find=function(t,e){return o(this).getBy(t,this,!1,e)},d.findOne=function(t,e){return o(this).getBy(t,this,!0,e)},t.Creator.finder=function(t){return new a(t)},exports.Finder=a;
@@ -0,0 +1 @@
1
+ this.LeaferIN=this.LeaferIN||{},this.LeaferIN.find=function(t,e){"use strict";const{Yes:i,NoAndSkip:s,YesAndSkip:n}=e.Answer,h={},r={},a={};class d{constructor(t){this.innerIdMap={},this.idMap={},this.methods={id:(t,e)=>t.id===e?(this.target&&(this.idMap[e]=t),1):0,innerId:(t,e)=>t.innerId===e?(this.target&&(this.innerIdMap[e]=t),1):0,className:(t,e)=>t.className===e?1:0,tag:(t,e)=>t.__tag===e?1:0,tags:(t,e)=>e[t.__tag]?1:0},(this.target=t)&&this.__listenEvents()}getBy(t,i,s,n){switch(typeof t){case"number":const d=this.getByInnerId(t,i);return s?d:d?[d]:[];case"string":switch(t[0]){case"#":h.id=t.substring(1),t=h;break;case".":r.className=t.substring(1),t=r;break;default:a.tag=t,t=a}case"object":if(void 0!==t.id){const e=this.getById(t.id,i);return s?e:e?[e]:[]}if(t.tag){const{tag:n}=t,h=n instanceof Array;return this.getByMethod(h?this.methods.tags:this.methods.tag,i,s,h?e.DataHelper.toMap(n):n)}return this.getByMethod(this.methods.className,i,s,t.className);case"function":return this.getByMethod(t,i,s,n)}}getByInnerId(t,e){const i=this.innerIdMap[t];return i||(this.eachFind(this.toChildren(e),this.methods.innerId,null,t),this.findLeaf)}getById(t,i){const s=this.idMap[t];return s&&e.LeafHelper.hasParent(s,i||this.target)?s:(this.eachFind(this.toChildren(i),this.methods.id,null,t),this.findLeaf)}getByClassName(t,e){return this.getByMethod(this.methods.className,e,!1,t)}getByTag(t,e){return this.getByMethod(this.methods.tag,e,!1,t)}getByMethod(t,e,i,s){const n=i?null:[];return this.eachFind(this.toChildren(e),t,n,s),n||this.findLeaf}eachFind(t,e,h,r){let a,d;for(let o=0,c=t.length;o<c;o++){if(a=t[o],d=e(a,r),d===i||d===n){if(!h)return void(this.findLeaf=a);h.push(a)}a.isBranch&&d<s&&this.eachFind(a.children,e,h,r)}}toChildren(t){return this.findLeaf=null,[t||this.target]}__onRemoveChild(t){const{id:e,innerId:i}=t.child;this.idMap[e]&&delete this.idMap[e],this.innerIdMap[i]&&delete this.innerIdMap[i]}__checkIdChange(t){if("id"===t.attrName){const e=t.oldValue;this.idMap[e]&&delete this.idMap[e]}}__listenEvents(){this.__eventIds=[this.target.on_(e.ChildEvent.REMOVE,this.__onRemoveChild,this),this.target.on_(e.PropertyEvent.CHANGE,this.__checkIdChange,this)]}__removeListenEvents(){this.target.off_(this.__eventIds),this.__eventIds.length=0}destroy(){const{__eventIds:t}=this;t&&t.length&&(this.__removeListenEvents(),this.innerIdMap={},this.idMap={}),this.findLeaf=null}}const o=e.UI.prototype;function c(t){return t.leafer?t.leafer.selector:e.Platform.selector||(e.Platform.selector=e.Creator.selector())}return o.find=function(t,e){return c(this).getBy(t,this,!1,e)},o.findOne=function(t,e){return c(this).getBy(t,this,!0,e)},e.Creator.finder=function(t){return new d(t)},t.Finder=d,t}({},LeaferUI);
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@leafer-in/find",
3
+ "version": "1.3.0",
4
+ "description": "@leafer-in/find",
5
+ "author": "Chao (Leafer) Wan",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "main": "dist/find.esm.js",
9
+ "exports": {
10
+ "import": "./dist/find.esm.js",
11
+ "require": "./dist/find.cjs",
12
+ "types": "./types/index.d.ts"
13
+ },
14
+ "types": "types/index.d.ts",
15
+ "unpkg": "dist/find.js",
16
+ "jsdelivr": "dist/find.js",
17
+ "files": [
18
+ "src",
19
+ "types",
20
+ "dist"
21
+ ],
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "https://github.com/leaferjs/leafer-in.git"
25
+ },
26
+ "homepage": "https://github.com/leaferjs/leafer-in/tree/main/packages/find",
27
+ "bugs": "https://github.com/leaferjs/leafer-in/issues",
28
+ "keywords": [
29
+ "leafer find",
30
+ "leafer-find",
31
+ "leafer-in",
32
+ "find",
33
+ "leafer-ui",
34
+ "leaferjs"
35
+ ],
36
+ "peerDependencies": {
37
+ "@leafer-ui/draw": "^1.3.0",
38
+ "@leafer-ui/interface": "^1.3.0",
39
+ "@leafer-in/interface": "^1.3.0"
40
+ }
41
+ }
package/src/Finder.ts ADDED
@@ -0,0 +1,150 @@
1
+ import { ILeaf, ILeafMap, IEventListenerId, IFindMethod, IAnswer, IFindCondition, IBooleanMap, IFinder } from '@leafer-ui/interface'
2
+ import { ChildEvent, DataHelper, Answer, PropertyEvent, LeafHelper } from '@leafer-ui/draw'
3
+
4
+
5
+ const { Yes, NoAndSkip, YesAndSkip } = Answer
6
+ const idCondition = {} as IFindCondition, classNameCondition = {} as IFindCondition, tagCondition = {} as IFindCondition
7
+
8
+ export class Finder implements IFinder {
9
+
10
+ public target?: ILeaf // target 不存在时,为临时选择器(不能缓存数据)
11
+
12
+ protected innerIdMap: ILeafMap = {}
13
+ protected idMap: ILeafMap = {}
14
+
15
+ protected findLeaf: ILeaf
16
+
17
+ protected methods = {
18
+ id: (leaf: ILeaf, name: string) => leaf.id === name ? (this.target && (this.idMap[name] = leaf), 1) : 0,
19
+ innerId: (leaf: ILeaf, innerId: number) => leaf.innerId === innerId ? (this.target && (this.innerIdMap[innerId] = leaf), 1) : 0,
20
+ className: (leaf: ILeaf, name: string) => leaf.className === name ? 1 : 0,
21
+ tag: (leaf: ILeaf, name: string) => leaf.__tag === name ? 1 : 0,
22
+ tags: (leaf: ILeaf, nameMap: IBooleanMap) => nameMap[leaf.__tag] ? 1 : 0
23
+ }
24
+
25
+ protected __eventIds: IEventListenerId[]
26
+
27
+
28
+ constructor(target: ILeaf) {
29
+ if (this.target = target) this.__listenEvents()
30
+ }
31
+
32
+ public getBy(condition: number | string | IFindCondition | IFindMethod, branch?: ILeaf, one?: boolean, options?: any): ILeaf | ILeaf[] {
33
+ switch (typeof condition) {
34
+ case 'number':
35
+ const leaf = this.getByInnerId(condition, branch)
36
+ return one ? leaf : (leaf ? [leaf] : [])
37
+ case 'string':
38
+ switch (condition[0]) {
39
+ case '#':
40
+ idCondition.id = condition.substring(1), condition = idCondition; break
41
+ case '.':
42
+ classNameCondition.className = condition.substring(1), condition = classNameCondition; break
43
+ default:
44
+ tagCondition.tag = condition, condition = tagCondition
45
+ }
46
+ case 'object':
47
+ if (condition.id !== undefined) {
48
+ const leaf = this.getById(condition.id as string, branch)
49
+ return one ? leaf : (leaf ? [leaf] : [])
50
+ } else if (condition.tag) {
51
+ const { tag } = condition, isArray = tag instanceof Array
52
+ return this.getByMethod(isArray ? this.methods.tags : this.methods.tag, branch, one, isArray ? DataHelper.toMap(tag) : tag)
53
+ } else {
54
+ return this.getByMethod(this.methods.className, branch, one, condition.className)
55
+ }
56
+ case 'function':
57
+ return this.getByMethod(condition as IFindMethod, branch, one, options)
58
+ }
59
+ }
60
+
61
+
62
+ public getByInnerId(innerId: number, branch?: ILeaf): ILeaf {
63
+ const cache = this.innerIdMap[innerId]
64
+ if (cache) return cache
65
+ this.eachFind(this.toChildren(branch), this.methods.innerId, null, innerId)
66
+ return this.findLeaf
67
+ }
68
+
69
+ public getById(id: string, branch?: ILeaf): ILeaf {
70
+ const cache = this.idMap[id]
71
+ if (cache && LeafHelper.hasParent(cache, branch || this.target)) return cache
72
+ this.eachFind(this.toChildren(branch), this.methods.id, null, id)
73
+ return this.findLeaf
74
+ }
75
+
76
+ public getByClassName(className: string, branch?: ILeaf): ILeaf[] {
77
+ return this.getByMethod(this.methods.className, branch, false, className) as ILeaf[]
78
+ }
79
+
80
+ public getByTag(tag: string, branch?: ILeaf): ILeaf[] {
81
+ return this.getByMethod(this.methods.tag, branch, false, tag) as ILeaf[]
82
+ }
83
+
84
+ public getByMethod(method: IFindMethod, branch?: ILeaf, one?: boolean, options?: any): ILeaf[] | ILeaf {
85
+ const list: ILeaf[] = one ? null : []
86
+ this.eachFind(this.toChildren(branch), method, list, options)
87
+ return list || this.findLeaf
88
+ }
89
+
90
+
91
+ protected eachFind(children: ILeaf[], method: IFindMethod, list?: ILeaf[], options?: any): void {
92
+ let child: ILeaf, result: IAnswer
93
+ for (let i = 0, len = children.length; i < len; i++) {
94
+ child = children[i]
95
+ result = method(child, options)
96
+ if (result === Yes || result === YesAndSkip) {
97
+ if (list) {
98
+ list.push(child)
99
+ } else {
100
+ this.findLeaf = child
101
+ return
102
+ }
103
+ }
104
+ if (child.isBranch && result < NoAndSkip) this.eachFind(child.children, method, list, options)
105
+ }
106
+ }
107
+
108
+ protected toChildren(branch: ILeaf): ILeaf[] {
109
+ this.findLeaf = null
110
+ return [branch || this.target]
111
+ }
112
+
113
+
114
+ protected __onRemoveChild(event: ChildEvent): void {
115
+ const { id, innerId } = event.child
116
+ if (this.idMap[id]) delete this.idMap[id]
117
+ if (this.innerIdMap[innerId]) delete this.innerIdMap[innerId]
118
+ }
119
+
120
+ protected __checkIdChange(event: PropertyEvent): void {
121
+ if (event.attrName === 'id') {
122
+ const id = event.oldValue as string
123
+ if (this.idMap[id]) delete this.idMap[id]
124
+ }
125
+ }
126
+
127
+
128
+ protected __listenEvents(): void {
129
+ this.__eventIds = [
130
+ this.target.on_(ChildEvent.REMOVE, this.__onRemoveChild, this),
131
+ this.target.on_(PropertyEvent.CHANGE, this.__checkIdChange, this)
132
+ ]
133
+ }
134
+
135
+ protected __removeListenEvents(): void {
136
+ this.target.off_(this.__eventIds)
137
+ this.__eventIds.length = 0
138
+ }
139
+
140
+ public destroy(): void {
141
+ const { __eventIds } = this
142
+ if (__eventIds && __eventIds.length) {
143
+ this.__removeListenEvents()
144
+ this.innerIdMap = {}
145
+ this.idMap = {}
146
+ }
147
+ this.findLeaf = null
148
+ }
149
+
150
+ }
package/src/find.ts ADDED
@@ -0,0 +1,18 @@
1
+
2
+ import { IFindMethod, ISelector, IFindUIMethod, IUI } from '@leafer-ui/interface'
3
+ import { UI, Creator, Platform } from '@leafer-ui/draw'
4
+
5
+
6
+ const ui = UI.prototype
7
+
8
+ function getSelector(ui: IUI): ISelector {
9
+ return ui.leafer ? ui.leafer.selector : (Platform.selector || (Platform.selector = Creator.selector()))
10
+ }
11
+
12
+ ui.find = function (condition: number | string | IFindUIMethod, options?: any): IUI[] {
13
+ return getSelector(this).getBy(condition as IFindMethod, this, false, options) as IUI[]
14
+ }
15
+
16
+ ui.findOne = function (condition: number | string | IFindUIMethod, options?: any): IUI | undefined {
17
+ return getSelector(this).getBy(condition as IFindMethod, this, true, options) as IUI
18
+ }
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ export { Finder } from './Finder'
2
+
3
+ import { Creator } from '@leafer-ui/draw'
4
+ import { Finder } from './Finder'
5
+ import './find'
6
+
7
+ Creator.finder = function (target) {
8
+ return new Finder(target)
9
+ }
@@ -0,0 +1,33 @@
1
+ import { IFinder, ILeaf, ILeafMap, IBooleanMap, IEventListenerId, IFindCondition, IFindMethod } from '@leafer-ui/interface';
2
+ import { ChildEvent, PropertyEvent } from '@leafer-ui/draw';
3
+
4
+ declare class Finder implements IFinder {
5
+ target?: ILeaf;
6
+ protected innerIdMap: ILeafMap;
7
+ protected idMap: ILeafMap;
8
+ protected findLeaf: ILeaf;
9
+ protected methods: {
10
+ id: (leaf: ILeaf, name: string) => 1 | 0;
11
+ innerId: (leaf: ILeaf, innerId: number) => 1 | 0;
12
+ className: (leaf: ILeaf, name: string) => 1 | 0;
13
+ tag: (leaf: ILeaf, name: string) => 1 | 0;
14
+ tags: (leaf: ILeaf, nameMap: IBooleanMap) => 1 | 0;
15
+ };
16
+ protected __eventIds: IEventListenerId[];
17
+ constructor(target: ILeaf);
18
+ getBy(condition: number | string | IFindCondition | IFindMethod, branch?: ILeaf, one?: boolean, options?: any): ILeaf | ILeaf[];
19
+ getByInnerId(innerId: number, branch?: ILeaf): ILeaf;
20
+ getById(id: string, branch?: ILeaf): ILeaf;
21
+ getByClassName(className: string, branch?: ILeaf): ILeaf[];
22
+ getByTag(tag: string, branch?: ILeaf): ILeaf[];
23
+ getByMethod(method: IFindMethod, branch?: ILeaf, one?: boolean, options?: any): ILeaf[] | ILeaf;
24
+ protected eachFind(children: ILeaf[], method: IFindMethod, list?: ILeaf[], options?: any): void;
25
+ protected toChildren(branch: ILeaf): ILeaf[];
26
+ protected __onRemoveChild(event: ChildEvent): void;
27
+ protected __checkIdChange(event: PropertyEvent): void;
28
+ protected __listenEvents(): void;
29
+ protected __removeListenEvents(): void;
30
+ destroy(): void;
31
+ }
32
+
33
+ export { Finder };