index.js 1.8 KB
Newer Older
1 2 3 4 5
export {
  initBehavior
}
  from '../../../mp-weixin/runtime/wrapper/index'

6
const instances = Object.create(null)
fxy060608's avatar
fxy060608 已提交
7

fxy060608's avatar
fxy060608 已提交
8 9
export const mocks = ['__route__', '__webviewId__', '__nodeid__']

fxy060608's avatar
fxy060608 已提交
10
export function initPage (pageOptions) {
11
  return initComponent(pageOptions)
fxy060608's avatar
fxy060608 已提交
12 13 14 15 16 17 18 19 20
}

export function initComponent (componentOptions) {
  if (componentOptions.properties) { // ref
    componentOptions.properties.vueRef = {
      type: String,
      value: ''
    }
  }
21
  return Component(componentOptions)
fxy060608's avatar
fxy060608 已提交
22 23
}

24 25 26
export function initRefs (vm) {
  const mpInstance = vm.$scope
  mpInstance.selectAllComponents('.vue-ref', (components) => {
fxy060608's avatar
fxy060608 已提交
27 28
    components.forEach(component => {
      const ref = component.data.vueRef // 头条的组件 dataset 竟然是空的
29
      vm.$refs[ref] = component.$vm || component
fxy060608's avatar
fxy060608 已提交
30 31
    })
  })
32
  mpInstance.selectAllComponents('.vue-ref-in-for', (forComponents) => {
fxy060608's avatar
fxy060608 已提交
33 34
    forComponents.forEach(component => {
      const ref = component.data.vueRef
35 36
      if (!vm.$refs[ref]) {
        vm.$refs[ref] = []
fxy060608's avatar
fxy060608 已提交
37
      }
38
      vm.$refs[ref].push(component.$vm || component)
fxy060608's avatar
fxy060608 已提交
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
export function triggerLink (mpInstance) {
  const nodeId = mpInstance.__nodeid__ + ''
  const webviewId = mpInstance.__webviewId__ + ''

  instances[webviewId + '_' + nodeId] = mpInstance.$vm

  mpInstance.triggerEvent('__l', {
    nodeId,
    webviewId
  }, {
    bubbles: true,
    composed: true
  })
}
// TODO 目前有 bug,composed 不生效
export function handleLink (event) {
  const nodeId = event.detail.nodeId
  const webviewId = event.detail.webviewId

  const childVm = instances[webviewId + '_' + nodeId]

  if (childVm) {
    childVm.$parent = this.$vm
    childVm.$parent.$children.push(event.detail)

    childVm.$root = this.$vm.$root
    delete instances[webviewId + '_' + nodeId]
  }
}