mh-two-thousand-and-two
2024-04-12 3d2ec2fd0578d3ba0a414b0cc4e4a2ae60878596
1
2
3
4
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
function parseDataset (attr) {
  const dataset = {}
 
  Object.keys(attr || {}).forEach(key => {
    if (key.indexOf('data') === 0) {
      let str = key.replace('data', '')
      str = str.charAt(0).toLowerCase() + str.slice(1)
      dataset[str] = attr[key]
    }
  })
 
  return dataset
}
 
function findAttrs (ids, elm, result) {
  const nodes = elm.children
  if (!Array.isArray(nodes)) {
    return false
  }
  for (let i = 0; i < nodes.length; i++) {
    const node = nodes[i]
    if (node.attr) {
      const index = ids.indexOf(node.attr.id)
      if (index >= 0) {
        result[index] = {
          id: ids[index],
          ref: node.ref,
          dataset: parseDataset(node.attr)
        }
        if (ids.length === 1) {
          break
        }
      }
    }
    if (node.children) {
      findAttrs(ids, node, result)
    }
  }
}
 
function getSelectors (queue) {
  const ids = []
  for (let i = 0; i < queue.length; i++) {
    const selector = queue[i].selector
    if (selector.indexOf('#') === 0) {
      ids.push(selector.substring(1))
    }
  }
  return ids
}
 
function getComponentRectAll (dom, attrs, index, result, callback) {
  const attr = attrs[index]
  dom.getComponentRect(attr.ref, option => {
    option.size.id = attr.id
    option.size.dataset = attr.dataset
    result.push(option.size)
    index += 1
    if (index < attrs.length) {
      getComponentRectAll(dom, attrs, index, result, callback)
    } else {
      callback(result)
    }
  })
}
 
export function requestComponentInfo (pageVm, queue, callback) {
  // TODO 重构,逻辑不对,queue 里的每一项可能有单独的作用域查找(即 component)
  const dom = pageVm._$weex.requireModule('dom')
  const selectors = getSelectors(queue)
  const outAttrs = new Array(selectors.length)
  findAttrs(selectors, pageVm.$el, outAttrs)
  getComponentRectAll(dom, outAttrs, 0, [], (result) => {
    callback(result)
  })
}