uvue.uts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // @ts-nocheck
  2. import { type ComponentPublicInstance } from 'vue';
  3. // #ifdef APP
  4. function findChildren(selector: string, context: ComponentPublicInstance, needAll: boolean): ComponentPublicInstance [] | null{
  5. let result:ComponentPublicInstance[] = []
  6. if(context !== null && context.$children.length > 0) {
  7. const queue:ComponentPublicInstance[] = [...context.$children];
  8. while(queue.length > 0) {
  9. const child = queue.shift();
  10. const name = child?.$options?.name;
  11. if(name == selector) {
  12. result.push(child as ComponentPublicInstance)
  13. } else {
  14. const children = child?.$children
  15. if(children !== null) {
  16. queue.push(...children)
  17. }
  18. }
  19. if(result.length > 0 && !needAll) {
  20. break;
  21. }
  22. }
  23. }
  24. if(result.length > 0) {
  25. return result
  26. }
  27. return null
  28. }
  29. class Query {
  30. context : ComponentPublicInstance | null = null
  31. selector : string = ''
  32. // components : ComponentPublicInstance[] = []
  33. constructor(selector : string, context : ComponentPublicInstance | null) {
  34. this.selector = selector
  35. this.context = context
  36. }
  37. in(context : ComponentPublicInstance) : Query {
  38. return new Query(this.selector, context)
  39. }
  40. find(): ComponentPublicInstance | null {
  41. const selector = this.selector
  42. if(selector == '') return null
  43. const component = findChildren(selector, this.context!, false)
  44. return component != null ? component[0]: null
  45. }
  46. findAll():ComponentPublicInstance[] | null {
  47. const selector = this.selector
  48. if(selector == '') return null
  49. return findChildren(selector, this.context!, true)
  50. }
  51. closest(): ComponentPublicInstance | null {
  52. const selector = this.selector
  53. if(selector == '') return null
  54. let parent = this.context!.$parent
  55. let name = parent?.$options?.name;
  56. while (parent != null && (name == null || selector != name)) {
  57. parent = parent.$parent
  58. if (parent != null) {
  59. name = parent.$options.name
  60. }
  61. }
  62. return parent
  63. }
  64. }
  65. export function selectComponent(selector: string): Query{
  66. return new Query(selector, null)
  67. }
  68. // #endif
  69. // selectComponent('selector').in(this).find()
  70. // selectComponent('selector').in(this).findAll()
  71. // selectComponent('selector').in(this).closest()