create-selector-query.uvue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view class="page-scroll-view">
  4. <!-- #endif -->
  5. <view class="page uni-list-cell-db-text" id="page">
  6. <page-head :title="title"></page-head>
  7. <button class="btn btn-get-node-info" @click="getNodeInfo">getNodeInfo</button>
  8. <button class="btn btn-get-all-node-info" @click="getAllNodeInfo">getAllNodeInfo</button>
  9. <view id="rect-1-2" class="rect-1-2">
  10. <view class="rect rect1"></view>
  11. <view class="rect rect2"></view>
  12. </view>
  13. <view class="rect-info-1-2">
  14. <view class="rect-info" v-for="(nodeInfo, index) in nodeInfoList" :key="index">
  15. <view class="node-info-item">
  16. <text class="node-info-item-k">left: </text>
  17. <text class="node-info-item-v">{{nodeInfo.left}}</text>
  18. </view>
  19. <view class="node-info-item">
  20. <text class="node-info-item-k">top: </text>
  21. <text class="node-info-item-v">{{nodeInfo.top}}</text>
  22. </view>
  23. <view class="node-info-item">
  24. <text class="node-info-item-k">right: </text>
  25. <text class="node-info-item-v">{{nodeInfo.right}}</text>
  26. </view>
  27. <view class="node-info-item">
  28. <text class="node-info-item-k">bottom: </text>
  29. <text class="node-info-item-v">{{nodeInfo.bottom}}</text>
  30. </view>
  31. <view class="node-info-item">
  32. <text class="node-info-item-k">width: </text>
  33. <text class="node-info-item-v">{{nodeInfo.width}}</text>
  34. </view>
  35. <view class="node-info-item">
  36. <text class="node-info-item-k">height: </text>
  37. <text class="node-info-item-v">{{nodeInfo.height}}</text>
  38. </view>
  39. </view>
  40. </view>
  41. <node-child class="node-child"></node-child>
  42. <text>子组件多根节点</text>
  43. <multi-child ref="multi-child" id="multi-child"></multi-child>
  44. <text>子组件多根节点(仅测试,用于验证查询是否超出范围)</text>
  45. <multi-child id="multi-child-2"></multi-child>
  46. <view>
  47. <text>测试.fields</text>
  48. <text>{{fieldsResultContainNode}}</text>
  49. </view>
  50. <view>
  51. <text>测试.node</text>
  52. <text>{{nodeResultContainNode}}</text>
  53. </view>
  54. <canvas id="canvas1"></canvas>
  55. </view>
  56. <!-- #ifdef APP -->
  57. </scroll-view>
  58. <!-- #endif -->
  59. </template>
  60. <script>
  61. import nodeChild from './nodes-info-child.uvue'
  62. import multiChild from './selector-query-child-multi.uvue'
  63. type NodeInfoType = {
  64. left : number | null,
  65. top : number | null,
  66. right : number | null,
  67. bottom : number | null,
  68. width : number | null,
  69. height : number | null,
  70. }
  71. export default {
  72. components: {
  73. nodeChild,
  74. multiChild
  75. },
  76. data() {
  77. return {
  78. title: 'createSelectorQuery',
  79. nodeInfoList: [] as NodeInfoType[],
  80. // 仅用于自动化测试
  81. rootNodeInfo: null as NodeInfoType | null,
  82. //供自动化测试使用
  83. // resizeRectValid: false
  84. // TODO
  85. selectCount: 0,
  86. selectAllCount: 0,
  87. fieldsResultContainNode: false,
  88. nodeResultContainNode: false
  89. }
  90. },
  91. onLoad() {
  92. uni.$on('childReady', this.onChildReady)
  93. },
  94. onUnload() {
  95. uni.$off('childReady', this.onChildReady)
  96. },
  97. onReady() {
  98. this.testFields()
  99. this.testNode()
  100. },
  101. onResize() {
  102. //供自动化测试使用
  103. /* var rect12Element = uni.getElementById("rect-1-2")
  104. if(rect12Element != null) {
  105. var domRect = rect12Element.getBoundingClientRect()
  106. if(domRect.width > 100) {
  107. this.resizeRectValid = true
  108. }
  109. } */
  110. },
  111. methods: {
  112. // 仅用于自动化测试
  113. onChildReady() {
  114. const instance2 = (this.$refs['multi-child'] as ComponentPublicInstance);
  115. this.selectCount = instance2.$data['selectCount'] as number;
  116. this.selectAllCount = instance2.$data['selectAllCount'] as number;
  117. },
  118. // 仅用于自动化测试
  119. getRootNodeInfo(selector : string) {
  120. uni.createSelectorQuery().select(selector).boundingClientRect().exec((ret) => {
  121. if (ret.length == 1) {
  122. const nodeInfo = ret[0] as NodeInfo;
  123. const nodeType = {
  124. left: nodeInfo.left,
  125. top: nodeInfo.top,
  126. right: nodeInfo.right,
  127. bottom: nodeInfo.bottom,
  128. width: nodeInfo.width,
  129. height: nodeInfo.height,
  130. } as NodeInfoType;
  131. this.rootNodeInfo = nodeType
  132. }
  133. })
  134. },
  135. getNodeInfo() {
  136. uni.createSelectorQuery().select('.rect1').boundingClientRect().exec((ret) => {
  137. this.nodeInfoList.length = 0
  138. const i = ret[0] as NodeInfo
  139. this.nodeInfoList.push({
  140. left: i.left,
  141. top: i.top,
  142. right: i.right,
  143. bottom: i.bottom,
  144. width: i.width,
  145. height: i.height,
  146. } as NodeInfoType)
  147. })
  148. },
  149. getAllNodeInfo() {
  150. uni.createSelectorQuery().selectAll('.rect').boundingClientRect().exec((ret) => {
  151. this.nodeInfoList.length = 0
  152. const array = ret[0] as NodeInfo[]
  153. array.forEach((i) => {
  154. this.nodeInfoList.push({
  155. left: i.left,
  156. top: i.top,
  157. right: i.right,
  158. bottom: i.bottom,
  159. width: i.width,
  160. height: i.height,
  161. } as NodeInfoType)
  162. })
  163. })
  164. },
  165. // test .fields
  166. testFields() {
  167. uni.createSelectorQuery().select('.rect1').fields({
  168. node: true
  169. } as NodeField, (ret) => {
  170. const isElement = (ret as NodeInfo).node instanceof UniElement
  171. if (isElement) {
  172. this.fieldsResultContainNode = true
  173. } else {
  174. this.fieldsResultContainNode = false
  175. }
  176. }).exec()
  177. },
  178. // test .node
  179. testNode() {
  180. uni.createSelectorQuery().select('#canvas1').node((ret) => {
  181. const isElement = (ret as NodeInfo).node instanceof UniElement
  182. const isCanvasElement = ((ret as NodeInfo).node as UniCanvasElement).tagName == 'CANVAS'
  183. if (isElement && isCanvasElement) {
  184. this.nodeResultContainNode = true
  185. } else {
  186. this.nodeResultContainNode = false
  187. }
  188. }).exec()
  189. },
  190. }
  191. }
  192. </script>
  193. <style>
  194. .page {
  195. padding: 15px;
  196. }
  197. .btn {
  198. margin-top: 15px;
  199. }
  200. .rect-1-2 {
  201. flex-direction: row;
  202. margin-top: 15px;
  203. }
  204. .rect {
  205. width: 150px;
  206. height: 100px;
  207. }
  208. .rect1 {
  209. background-color: dodgerblue;
  210. }
  211. .rect2 {
  212. margin-left: auto;
  213. background-color: seagreen;
  214. }
  215. .rect-info-1-2 {
  216. flex-direction: row;
  217. margin-top: 15px;
  218. }
  219. .rect-info {
  220. flex: 1;
  221. flex-direction: column;
  222. }
  223. .node-info-item {
  224. flex-direction: row;
  225. }
  226. .node-info-item-k {
  227. width: 72px;
  228. line-height: 2;
  229. }
  230. .node-info-item-v {
  231. font-weight: bold;
  232. line-height: 2;
  233. }
  234. </style>