request-component-info.js 4.0 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import {
2
  getTargetDataset
fxy060608's avatar
fxy060608 已提交
3
} from 'uni-helpers/index'
fxy060608's avatar
fxy060608 已提交
4

5 6
import getWindowOffset from 'uni-platform/helpers/get-window-offset'

7 8 9 10
import {
  findElm
} from './util'

fxy060608's avatar
fxy060608 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
function getRootInfo (fields) {
  const info = {}
  if (fields.id) {
    info.id = ''
  }
  if (fields.dataset) {
    info.dataset = {}
  }
  if (fields.rect) {
    info.left = 0
    info.right = 0
    info.top = 0
    info.bottom = 0
  }
  if (fields.size) {
    info.width = document.documentElement.clientWidth
    info.height = document.documentElement.clientHeight
  }
  if (fields.scrollOffset) {
30 31 32 33 34 35
    const documentElement = document.documentElement
    const body = document.body
    info.scrollLeft = documentElement.scrollLeft || body.scrollLeft || 0
    info.scrollTop = documentElement.scrollTop || body.scrollTop || 0
    info.scrollHeight = documentElement.scrollHeight || body.scrollHeight || 0
    info.scrollWidth = documentElement.scrollWidth || body.scrollWidth || 0
fxy060608's avatar
fxy060608 已提交
36 37 38 39 40 41
  }
  return info
}

function getNodeInfo (el, fields) {
  const info = {}
42 43 44
  const {
    top
  } = getWindowOffset()
fxy060608's avatar
fxy060608 已提交
45 46 47 48
  if (fields.id) {
    info.id = el.id
  }
  if (fields.dataset) {
49
    info.dataset = getTargetDataset(el)
fxy060608's avatar
fxy060608 已提交
50 51 52 53 54 55
  }
  if (fields.rect || fields.size) {
    const rect = el.getBoundingClientRect()
    if (fields.rect) {
      info.left = rect.left
      info.right = rect.right
56
      info.top = rect.top - top
Q
qiang 已提交
57
      info.bottom = rect.bottom - top
fxy060608's avatar
fxy060608 已提交
58 59 60 61 62 63 64
    }
    if (fields.size) {
      info.width = rect.width
      info.height = rect.height
    }
  }
  // TODO 组件 props
65
  if (Array.isArray(fields.properties)) {
fxy060608's avatar
fxy060608 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78
    fields.properties.forEach(prop => {
      prop = prop.replace(/-([a-z])/g, function (e, t) {
        return t.toUpperCase()
      })
      // props
    })
  }
  if (fields.scrollOffset) {
    if (el.tagName === 'UNI-SCROLL-VIEW' && el.__vue__ && el.__vue__.getScrollPosition) {
      Object.assign(info, el.__vue__.getScrollPosition())
    } else {
      info.scrollLeft = 0
      info.scrollTop = 0
79 80
      info.scrollHeight = 0
      info.scrollWidth = 0
fxy060608's avatar
fxy060608 已提交
81 82
    }
  }
83 84 85 86 87 88
  if (Array.isArray(fields.computedStyle)) {
    const sytle = getComputedStyle(el)
    fields.computedStyle.forEach(name => {
      info[name] = sytle[name]
    })
  }
Q
qiang 已提交
89 90 91 92 93
  if (fields.context) {
    if (el.__vue__ && el.__vue__._getContextInfo) {
      info.context = el.__vue__._getContextInfo()
    }
  }
fxy060608's avatar
fxy060608 已提交
94 95 96 97
  return info
}

function getNodesInfo (pageVm, component, selector, single, fields) {
98
  const $el = findElm(component, pageVm)
99 100 101
  if (!$el || ($el && $el.nodeType === 8)) { // Comment
    return single ? null : []
  }
fxy060608's avatar
fxy060608 已提交
102
  if (single) {
103
    const node = $el.matches(selector) ? $el : $el.querySelector(selector)
fxy060608's avatar
fxy060608 已提交
104 105 106 107 108
    if (node) {
      return getNodeInfo(node, fields)
    }
    return null
  } else {
109
    let infos = []
fxy060608's avatar
fxy060608 已提交
110 111
    const nodeList = $el.querySelectorAll(selector)
    if (nodeList && nodeList.length) {
112
      infos = ([]).map.call(nodeList, node => {
fxy060608's avatar
fxy060608 已提交
113 114 115
        return getNodeInfo(node, fields)
      })
    }
116
    if ($el.matches(selector)) {
fxy060608's avatar
fxy060608 已提交
117
      infos.unshift(getNodeInfo($el, fields))
118 119
    }
    return infos
fxy060608's avatar
fxy060608 已提交
120 121 122
  }
}

fxy060608's avatar
fxy060608 已提交
123
export function requestComponentInfo ({
fxy060608's avatar
fxy060608 已提交
124 125 126
  reqId,
  reqs
}, pageId) {
127 128 129 130 131 132 133 134 135 136
  let pageVm
  if (pageId._isVue) {
    pageVm = pageId
  } else {
    const pages = getCurrentPages() // 跨平台时,View 层也应该实现该方法,举例 App 上,View 层的 getCurrentPages 返回长度为1的当前页面数组
    const page = pages.find(page => page.$page.id === pageId)
    if (!page) {
      throw new Error(`Not Found:Page[${pageId}]`)
    }
    pageVm = page.$vm
fxy060608's avatar
fxy060608 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
  }
  const result = []
  reqs.forEach(function ({
    component,
    selector,
    single,
    fields
  }) {
    if (component === 0) {
      result.push(getRootInfo(fields))
    } else {
      result.push(getNodesInfo(pageVm, component, selector, single, fields))
    }
  })

  UniViewJSBridge.publishHandler('onRequestComponentInfo', {
    reqId,
    res: result
155
  })
156
}