componentInstance.ts 5.2 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { EventChannel, invokeArrayFns } from '@dcloudio/uni-shared'
fxy060608's avatar
fxy060608 已提交
2 3 4 5 6 7
import {
  capitalize,
  hasOwn,
  isArray,
  toNumber,
  isObject,
8
  isPlainObject,
fxy060608's avatar
fxy060608 已提交
9 10 11
} from '@vue/shared'

import { ComponentPublicInstance, ComponentInternalInstance } from 'vue'
fxy060608's avatar
fxy060608 已提交
12
import { getEventChannel } from '../api/protocols/navigateTo'
fxy060608's avatar
fxy060608 已提交
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 77 78
import { MPComponentInstance } from '../index'

function setModel(
  this: ComponentPublicInstance,
  target: ComponentPublicInstance,
  key: string,
  value: any,
  modifiers: string[]
) {
  if (isArray(modifiers)) {
    if (modifiers.indexOf('trim') !== -1) {
      value = (value as string).trim()
    }
    if (modifiers.indexOf('number') !== -1) {
      value = toNumber(value)
    }
  }
  if (!target) {
    target = this
  }
  ;(target as any)[key] = value
}

function setSync(
  this: ComponentPublicInstance,
  target: ComponentPublicInstance,
  key: string,
  value: any
) {
  if (!target) {
    target = this
  }
  ;(target as any)[key] = value
}

function getOrig(data: unknown) {
  if (isPlainObject(data)) {
    return (data as any).$orig || data
  }
  return data
}

function map(val: unknown, iteratee: Function) {
  let ret, i, l, keys, key
  if (isArray(val)) {
    ret = new Array(val.length)
    for (i = 0, l = val.length; i < l; i++) {
      ret[i] = iteratee(val[i], i)
    }
    return ret
  } else if (isObject(val)) {
    keys = Object.keys(val)
    ret = Object.create(null)
    for (i = 0, l = keys.length; i < l; i++) {
      key = keys[i]
      ret[key] = iteratee(val[key], key, i)
    }
    return ret
  }
  return []
}

const MP_METHODS = [
  'createSelectorQuery',
  'createIntersectionObserver',
  'selectAllComponents',
79
  'selectComponent',
fxy060608's avatar
fxy060608 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
]

function createEmitFn(oldEmit: Function, ctx: Record<string, any>) {
  return function emit(
    this: ComponentPublicInstance,
    event: string,
    ...args: any[]
  ) {
    if (ctx.$scope && event) {
      ;(ctx.$scope as any).triggerEvent(event, { __args__: args })
    }
    if (__PLATFORM__ === 'mp-alipay') {
      const vnode = this.$.vnode
      const props = vnode && vnode.props
      if (props && props[`on${capitalize(event)}`]) {
        return
      }
    }
    return oldEmit.apply(this, [event, ...args])
  }
}

export interface CreateComponentOptions {
  mpType: 'app' | 'page' | 'component'
  mpInstance: any
  slots?: string[]
  parentComponent?: ComponentInternalInstance
  onBeforeSetup?: Function
}

export function initBaseInstance(
  instance: ComponentInternalInstance,
  options: CreateComponentOptions
) {
  const ctx = (instance as any).ctx

  // mp
  ctx.mpType = options.mpType // @deprecated
  ctx.$mpType = options.mpType
  ctx.$scope = options.mpInstance

fxy060608's avatar
fxy060608 已提交
121 122
  // TODO @deprecated
  ctx.$mp = {}
fxy060608's avatar
fxy060608 已提交
123 124 125
  if (__VUE_OPTIONS_API__) {
    ctx._self = {}
  }
fxy060608's avatar
fxy060608 已提交
126

fxy060608's avatar
fxy060608 已提交
127
  // $vm
fxy060608's avatar
fxy060608 已提交
128
  ctx.$scope.$vm = (instance as any).proxy!
fxy060608's avatar
fxy060608 已提交
129 130 131 132 133 134

  // slots
  if (__PLATFORM__ === 'mp-alipay') {
    Object.defineProperty(instance, 'slots', {
      get() {
        return this.$scope && this.$scope.props.$slots
135
      },
fxy060608's avatar
fxy060608 已提交
136 137 138 139
    })
  } else {
    instance.slots = {}
    if (isArray(options.slots) && options.slots.length) {
140
      options.slots.forEach((name) => {
fxy060608's avatar
fxy060608 已提交
141 142 143 144 145
        instance.slots[name] = true as any
      })
    }
  }

fxy060608's avatar
fxy060608 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159
  ctx.getOpenerEventChannel = function () {
    // 微信小程序使用自身getOpenerEventChannel
    if (__PLATFORM__ === 'mp-weixin') {
      return options.mpInstance.getOpenerEventChannel()
    }
    if (!this.__eventChannel__) {
      this.__eventChannel__ = new EventChannel()
    }
    return this.__eventChannel__
  }

  ctx.$hasHook = hasHook
  ctx.$callHook = callHook

fxy060608's avatar
fxy060608 已提交
160 161 162 163 164 165 166 167 168 169 170
  // $emit
  instance.emit = createEmitFn(instance.emit, ctx)
}

export function initComponentInstance(
  instance: ComponentInternalInstance,
  options: CreateComponentOptions
) {
  initBaseInstance(instance, options)

  const ctx = (instance as any).ctx
171 172
  MP_METHODS.forEach((method) => {
    ctx[method] = function (...args: any[]) {
fxy060608's avatar
fxy060608 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
      const mpInstance = ctx.$scope as MPComponentInstance
      if (mpInstance && mpInstance[method]) {
        return (mpInstance[method] as Function).apply(mpInstance, args)
      }
      if (__PLATFORM__ === 'mp-alipay') {
        return (my as any)[method] && (my as any)[method].apply(my, args)
      }
    }
  })

  // TODO other
  ctx.__set_model = setModel
  ctx.__set_sync = setSync
  ctx.__get_orig = getOrig
  // TODO
  // ctx.__get_style = getStyle
  ctx.__map = map
}

export function initMocks(
  instance: ComponentInternalInstance,
  mpInstance: MPComponentInstance,
  mocks: string[]
) {
  const ctx = (instance as any).ctx
198
  mocks.forEach((mock) => {
fxy060608's avatar
fxy060608 已提交
199 200 201 202 203
    if (hasOwn(mpInstance, mock)) {
      ctx[mock] = mpInstance[mock]
    }
  })
}
fxy060608's avatar
fxy060608 已提交
204 205 206 207 208 209 210 211 212 213

function hasHook(this: ComponentPublicInstance, name: string) {
  const hooks = (this.$ as any)[name]
  if (hooks && hooks.length) {
    return true
  }
  return false
}

function callHook(this: ComponentPublicInstance, name: string, args?: unknown) {
fxy060608's avatar
fxy060608 已提交
214 215 216 217 218
  if (name === 'mounted') {
    callHook.call(this, 'bm') // beforeMount
    this.$.isMounted = true
    name = 'm'
  } else if (name === 'onLoad' && args && (args as any).__id__) {
fxy060608's avatar
fxy060608 已提交
219 220 221 222 223 224
    ;(this as any).__eventChannel__ = getEventChannel((args as any).__id__)
    delete (args as any).__id__
  }
  const hooks = (this.$ as any)[name]
  return hooks && invokeArrayFns(hooks, args)
}