1
0

vue.ts 4.7 KB

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