create-selector-query.uvue 6.8 KB
Newer Older
1 2 3 4
<template>
  <!-- #ifdef APP -->
  <scroll-view class="page-scroll-view">
  <!-- #endif -->
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
  <view class="page" id="page">
    <page-head :title="title"></page-head>
    <button class="btn btn-get-node-info" @click="getNodeInfo">getNodeInfo</button>
    <button class="btn btn-get-all-node-info" @click="getAllNodeInfo">getAllNodeInfo</button>
    <view id="rect-1-2" class="rect-1-2">
      <view class="rect rect1"></view>
      <view class="rect rect2"></view>
    </view>
    <view class="rect-info-1-2">
      <view class="rect-info" v-for="(nodeInfo, index) in nodeInfoList" :key="index">
        <view class="node-info-item">
          <text class="node-info-item-k">left: </text>
          <text class="node-info-item-v">{{nodeInfo.left}}</text>
        </view>
        <view class="node-info-item">
          <text class="node-info-item-k">top: </text>
          <text class="node-info-item-v">{{nodeInfo.top}}</text>
        </view>
        <view class="node-info-item">
          <text class="node-info-item-k">right: </text>
          <text class="node-info-item-v">{{nodeInfo.right}}</text>
        </view>
        <view class="node-info-item">
          <text class="node-info-item-k">bottom: </text>
          <text class="node-info-item-v">{{nodeInfo.bottom}}</text>
        </view>
        <view class="node-info-item">
          <text class="node-info-item-k">width: </text>
          <text class="node-info-item-v">{{nodeInfo.width}}</text>
        </view>
        <view class="node-info-item">
          <text class="node-info-item-k">height: </text>
          <text class="node-info-item-v">{{nodeInfo.height}}</text>
        </view>
H
hdx 已提交
39 40
      </view>
    </view>
41 42 43 44
    <node-child class="node-child"></node-child>
    <text>子组件多根节点</text>
    <multi-child ref="multi-child" id="multi-child"></multi-child>
    <text>子组件多根节点(仅测试,用于验证查询是否超出范围)</text>
45 46 47 48 49 50 51 52 53 54
    <multi-child id="multi-child-2"></multi-child>
    <view>
      <text>测试.fields</text>
      <text>{{fieldsResultContainNode}}</text>
    </view>
    <view>
      <text>测试.node</text>
      <text>{{nodeResultContainNode}}</text>
    </view>
    <canvas id="canvas1"></canvas>
55 56 57 58
  </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
59 60 61
</template>

<script>
62 63
  import nodeChild from './nodes-info-child.uvue'
  import multiChild from './selector-query-child-multi.uvue'
64 65 66 67 68 69 70 71 72 73 74 75

  type NodeInfoType = {
    left : number | null,
    top : number | null,
    right : number | null,
    bottom : number | null,
    width : number | null,
    height : number | null,
  }

  export default {
    components: {
76 77
      nodeChild,
      multiChild
78 79 80 81 82 83 84 85
    },
    data() {
      return {
        title: 'createSelectorQuery',
        nodeInfoList: [] as NodeInfoType[],
        // 仅用于自动化测试
        rootNodeInfo: null as NodeInfoType | null,
        //供自动化测试使用
86 87 88
        // resizeRectValid: false
        // TODO
        selectCount: 0,
89 90 91
        selectAllCount: 0,
        fieldsResultContainNode: false,
        nodeResultContainNode: false
92
      }
93 94
    },
    onReady() {
95 96 97
      const instance2 = (this.$refs['multi-child'] as ComponentPublicInstance)
      this.selectCount = instance2.$data['selectCount'] as Number
      this.selectAllCount = instance2.$data['selectAllCount'] as Number
98 99 100

      this.testFields()
      this.testNode()
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 150 151 152 153 154 155 156 157 158
    },
    onResize() {
      //供自动化测试使用
      /* var rect12Element = uni.getElementById("rect-1-2")
      if(rect12Element != null) {
        var domRect = rect12Element.getBoundingClientRect()
        if(domRect.width > 100) {
          this.resizeRectValid = true
        }
      } */
    },
    methods: {
      // 仅用于自动化测试
      getRootNodeInfo(selector : string) {
        uni.createSelectorQuery().select(selector).boundingClientRect().exec((ret) => {
          if (ret.length == 1) {
            const nodeInfo = ret[0] as NodeInfo;
            const nodeType = {
              left: nodeInfo.left,
              top: nodeInfo.top,
              right: nodeInfo.right,
              bottom: nodeInfo.bottom,
              width: nodeInfo.width,
              height: nodeInfo.height,
            } as NodeInfoType;
            this.rootNodeInfo = nodeType
          }
        })
      },
      getNodeInfo() {
        uni.createSelectorQuery().select('.rect1').boundingClientRect().exec((ret) => {
          this.nodeInfoList.length = 0
          const i = ret[0] as NodeInfo
          this.nodeInfoList.push({
            left: i.left,
            top: i.top,
            right: i.right,
            bottom: i.bottom,
            width: i.width,
            height: i.height,
          } as NodeInfoType)
        })
      },
      getAllNodeInfo() {
        uni.createSelectorQuery().selectAll('.rect').boundingClientRect().exec((ret) => {
          this.nodeInfoList.length = 0
          const array = ret[0] as NodeInfo[]
          array.forEach((i) => {
            this.nodeInfoList.push({
              left: i.left,
              top: i.top,
              right: i.right,
              bottom: i.bottom,
              width: i.width,
              height: i.height,
            } as NodeInfoType)
          })
        })
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
      },
      // test .fields
      testFields() {
        uni.createSelectorQuery().select('.rect1').fields({
         node: true
        } as NodeField, (ret) => {
          const isElement = (ret as NodeInfo).node instanceof UniElement
          if(isElement){
            this.fieldsResultContainNode = true
          } else {
            this.fieldsResultContainNode = false
          }
        }).exec()
      },
      // test .node
      testNode() {
        uni.createSelectorQuery().select('#canvas1').node((ret) => {
          const isElement = (ret as NodeInfo).node instanceof UniElement
          const isCanvasElement = ((ret as NodeInfo).node as UniCanvasElement).tagName == 'CANVAS'
         if(isElement && isCanvasElement){
           this.nodeResultContainNode = true
         } else {
           this.nodeResultContainNode = false
         }
        }).exec()
      },
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    }
  }
</script>

<style>
  .page {
    padding: 15px;
  }

  .btn {
    margin-top: 15px;
  }

  .rect-1-2 {
    flex-direction: row;
    margin-top: 15px;
  }

  .rect {
    width: 150px;
    height: 100px;
  }

  .rect1 {
    background-color: dodgerblue;
  }

  .rect2 {
    margin-left: auto;
    background-color: seagreen;
  }

  .rect-info-1-2 {
    flex-direction: row;
    margin-top: 15px;
  }

  .rect-info {
    flex: 1;
    flex-direction: column;
  }

  .node-info-item {
    flex-direction: row;
  }

  .node-info-item-k {
    width: 72px;
    line-height: 2;
  }

  .node-info-item-v {
    font-weight: bold;
    line-height: 2;
  }
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
240
</style>