componentInstance.ts 4.1 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6
import {
  capitalize,
  hasOwn,
  isArray,
  toNumber,
  isObject,
7
  isPlainObject,
fxy060608's avatar
fxy060608 已提交
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
} from '@vue/shared'

import { ComponentPublicInstance, ComponentInternalInstance } from 'vue'
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',
77
  'selectComponent',
fxy060608's avatar
fxy060608 已提交
78 79 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
]

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 已提交
119 120
  // TODO @deprecated
  ctx.$mp = {}
fxy060608's avatar
fxy060608 已提交
121 122 123
  if (__VUE_OPTIONS_API__) {
    ctx._self = {}
  }
fxy060608's avatar
fxy060608 已提交
124

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

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

  // $emit
  instance.emit = createEmitFn(instance.emit, ctx)
}

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

  const ctx = (instance as any).ctx
155 156
  MP_METHODS.forEach((method) => {
    ctx[method] = function (...args: any[]) {
fxy060608's avatar
fxy060608 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
      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
182
  mocks.forEach((mock) => {
fxy060608's avatar
fxy060608 已提交
183 184 185 186 187
    if (hasOwn(mpInstance, mock)) {
      ctx[mock] = mpInstance[mock]
    }
  })
}