web
17 小时以前 49fa0d82a40345342966e810b44429aec0480ef3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// @ts-nocheck
// #ifdef MP
function findChildren(selector : string, context : ComponentPublicInstance, needAll : boolean) {
    const { proxy, $vm } = context
    context = $vm || proxy
    if ((selector.startsWith('.') || selector.startsWith('#'))) {
        const queue = [context]
        let result = null
        while (queue.length > 0) {
            const child = queue.shift();
            const flag = child?.selectComponent(selector)
            if (flag) {
                if (!needAll) { return result = flag.$vm }
                return result = child.selectAllComponents(selector).map(item => item.$vm)
            } else {
                child.$children && (queue.push(...child.$children));
            }
        }
        return result
    } else {
        const { $templateRefs } = context.$
        const selectorValue = /#|\.|@|$/.test(selector) ? selector.substring(1) : selector
        const nameMap = {}
        for (var i = 0; i < $templateRefs.length; i++) {
            const item = $templateRefs[i]
            nameMap[item.i] = item.r
        }
        let result = []
        if (context.$children.length) {
            const queue = [...context.$children]
            while (queue.length > 0) {
                const child = queue.shift();
                if (child.type?.name === selectorValue || child.$?.type?.name === selectorValue) {
                    result.push(child)
                } else if (child.$refs && child.$refs[selectorValue]) {
                    result = child.$refs[selectorValue]
                } else if (nameMap[child.id] === selectorValue) {
                    result.push(child)
                } else {
                    child.$children && (queue.push(...child.$children));
                }
                if (result.length && !needAll) {
                    return;
                }
            }
        }
        return needAll ? result : result[0]
    }
}
// #endif
 
// #ifdef H5
function findChildren(selector : string, context : ComponentPublicInstance, needAll : boolean){
    const {_, component } = context
    const child = {component: _ || component || context, children: null , subTree: null, props: null}
    let result = []
    let queue = [child]
    const selectorValue = /#|\.|@|$/.test(selector) ? selector.substring(1) : selector
    while(queue.length > 0 ) {
        const child = queue.shift()
        const {component, children , props, subTree} = child
        if(component?.type?.name == selectorValue) {
            result.push(component)
        } else if(selector.startsWith('$') && component && (props?.ref == selectorValue || component[key][selectorValue])) {
            if(props?.ref == selectorValue) {
                //exposed
                result.push(component)
            } else if(component[key][selectorValue]) {
                result.push(component[key][selectorValue])
            }
        } else if(!selector.startsWith('$') && component?.exposed && new RegExp(`\\b${selectorValue}\\b`).test(component.attrs[key])) {
            // exposed
            result.push(component)
        } else if(children && Array.isArray(children)) {
            queue.push(...children)
        } else if(!component && subTree) {
            queue.push(subTree)
        } else if(component?.subTree) {
            queue.push(component.subTree)
        }
        if(result.length && !needAll) {
            break
        }
    }
    return needAll ? result : result[0]
}
// #endif
 
// #ifdef APP
function findChildren(selector : string, context : ComponentPublicInstance, needAll : boolean){
    let result = []
    const selectorValue = /#|\.|@|$/.test(selector) ? selector.substring(1) : selector
    const queue = [context]
    while(queue.length > 0) {
        const child = queue.shift()
        const {component, children, props, subTree} = child
        const isComp = component && props && component.exposed && !node
        if(child.type && child.type.name === selectorValue) {
            result.push(component)
        } else if(props?.[key] === selectorValue && node) {
            result.push(child)
        } else if(selector.startsWith('$') && isComp && (props.ref === selectorValue || props.ref_key === selectorValue)) {
            // exposed
            result.push(component)
        } else if(!selector.startsWith('$') && isComp && new RegExp(`\\b${selectorValue}\\b`).test(props[key])) {
            // exposed
            result.push(component)
        }
        else if(subTree) {
            queue.push(subTree)
        } else if(component && component.subTree){
            queue.push(component.subTree)
        } 
         else if(children && Array.isArray(children)) {
            queue.push(...children)
        }
        if(result.length && !needAll) {
            break;
        }
    }
    return needAll ? result : result[0]
}
// #endif
 
class Query {
    context : ComponentPublicInstance | null = null
    selector : string = ''
    // components : ComponentPublicInstance[] = []
    constructor(selector : string, context : ComponentPublicInstance | null) {
        this.selector = selector
        this.context = context
    }
    in(context : ComponentPublicInstance) : Query {
        return new Query(this.selector, context)
    }
    find() : ComponentPublicInstance | null {
        return findChildren(this.selector, this.context, false)
    }
    findAll() : ComponentPublicInstance[] | null {
        return findChildren(this.selector, this.context, true)
    }
    closest() : ComponentPublicInstance | null {
        return null
    }
}
 
export function selectComponent(selector: string) {
    return new Query(selector)
}