util.js 2.1 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import {
Q
qiang 已提交
2
  findVmByVueId,
Q
qiang 已提交
3 4
  initRefs as initRefsBase,
  toSkip
fxy060608's avatar
fxy060608 已提交
5 6 7 8 9 10 11 12
} from '../../../mp-weixin/runtime/wrapper/util'

export const mocks = ['__route__', '__webviewId__', '__nodeid__', '__nodeId__']

export function isPage () {
  return this.__nodeid__ === 0 || this.__nodeId__ === 0
}

Q
qiang 已提交
13
export function initRefs (vm) {
fxy060608's avatar
fxy060608 已提交
14
  const mpInstance = vm.$scope
15
  /* eslint-disable no-undef */
fxy060608's avatar
fxy060608 已提交
16 17
  const minorVersion = parseInt(tt.getSystemInfoSync().SDKVersion.split('.')[1])
  if (minorVersion > 16) {
Q
qiang 已提交
18
    initRefsBase(vm)
fxy060608's avatar
fxy060608 已提交
19 20 21 22
  } else {
    mpInstance.selectAllComponents('.vue-ref', (components) => {
      components.forEach(component => {
        const ref = component.dataset.ref
Q
qiang 已提交
23
        vm.$refs[ref] = component.$vm || toSkip(component)
fxy060608's avatar
fxy060608 已提交
24 25 26 27 28 29 30 31
      })
    })
    mpInstance.selectAllComponents('.vue-ref-in-for', (forComponents) => {
      forComponents.forEach(component => {
        const ref = component.dataset.ref
        if (!vm.$refs[ref]) {
          vm.$refs[ref] = []
        }
Q
qiang 已提交
32
        vm.$refs[ref].push(component.$vm || toSkip(component))
fxy060608's avatar
fxy060608 已提交
33 34 35
      })
    })
  }
fxy060608's avatar
fxy060608 已提交
36 37
}

fxy060608's avatar
fxy060608 已提交
38
export const instances = Object.create(null)
39
export const components = Object.create(null)
fxy060608's avatar
fxy060608 已提交
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 77 78 79 80 81 82 83 84 85 86 87 88

export function initRelation ({
  vuePid,
  mpInstance
}) {
  // 头条 triggerEvent 后,接收事件时机特别晚,已经到了 ready 之后
  const nodeId = (mpInstance.__nodeId__ || mpInstance.__nodeid__) + ''
  const webviewId = mpInstance.__webviewId__ + ''

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

  this.triggerEvent('__l', {
    vuePid,
    nodeId,
    webviewId
  })
}

export function handleLink ({
  detail: {
    vuePid,
    nodeId,
    webviewId
  }
}) {
  const vm = instances[webviewId + '_' + nodeId]
  if (!vm) {
    return
  }

  let parentVm

  if (vuePid) {
    parentVm = findVmByVueId(this.$vm, vuePid)
  }

  if (!parentVm) {
    parentVm = this.$vm
  }

  vm.$parent = parentVm
  vm.$root = parentVm.$root
  parentVm.$children.push(vm)

  vm.__call_hook('created')
  vm.__call_hook('beforeMount')
  vm._isMounted = true
  vm.__call_hook('mounted')
  vm.__call_hook('onReady')
89
}