util.js 5.9 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3
import {
  isFn,
  cached,
4 5
  camelize,
  hasOwn
fxy060608's avatar
fxy060608 已提交
6 7 8 9 10 11 12 13 14 15 16 17
} from 'uni-shared'

import {
  handleLink as handleBaseLink
} from '../../../mp-weixin/runtime/wrapper/util'

import deepEqual from './deep-equal'

const customizeRE = /:/g

const customize = cached((str) => {
  return camelize(str.replace(customizeRE, '-'))
fxy060608's avatar
fxy060608 已提交
18
})
fxy060608's avatar
fxy060608 已提交
19

fxy060608's avatar
fxy060608 已提交
20
export const isComponent2 = my.canIUse('component2')
fxy060608's avatar
fxy060608 已提交
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

export const mocks = ['$id']

export function initRefs () {

}

export function initBehavior ({
  properties
}) {
  const props = {}

  Object.keys(properties).forEach(key => {
    props[key] = properties[key].value
  })

  return {
    props
  }
}

export function initRelation (detail) {
  this.props.onVueInit(detail)
}

fxy060608's avatar
fxy060608 已提交
46 47
export function initSpecialMethods (mpInstance) {
  if (!mpInstance.$vm) {
fxy060608's avatar
fxy060608 已提交
48 49
    return
  }
fxy060608's avatar
fxy060608 已提交
50 51 52 53 54 55 56 57 58 59 60 61
  let path = mpInstance.is || mpInstance.route
  if (!path) {
    return
  }
  if (path.indexOf('/') === 0) {
    path = path.substr(1)
  }
  const specialMethods = my.specialMethods && my.specialMethods[path]
  if (specialMethods) {
    specialMethods.forEach(method => {
      if (isFn(mpInstance.$vm[method])) {
        mpInstance[method] = function (event) {
62 63
          if (hasOwn(event, 'markerId')) {
            event.detail = typeof event.detail === 'object' ? event.detail : {}
64 65
            event.detail.markerId = event.markerId
          }
fxy060608's avatar
fxy060608 已提交
66 67 68 69 70 71
          // TODO normalizeEvent
          mpInstance.$vm[method](event)
        }
      }
    })
  }
fxy060608's avatar
fxy060608 已提交
72 73
}

fxy060608's avatar
fxy060608 已提交
74
export function initChildVues (mpInstance) {
fxy060608's avatar
fxy060608 已提交
75 76 77 78
  // 此时需保证当前 mpInstance 已经存在 $vm
  if (!mpInstance.$vm) {
    return
  }
fxy060608's avatar
fxy060608 已提交
79 80 81 82 83
  mpInstance._$childVues && mpInstance._$childVues.forEach(({
    vuePid,
    vueOptions,
    VueComponent,
    mpInstance: childMPInstance
fxy060608's avatar
fxy060608 已提交
84
  }) => {
fxy060608's avatar
fxy060608 已提交
85 86 87 88 89 90 91 92
    // 父子关系
    handleBaseLink.call(mpInstance, {
      detail: {
        vuePid,
        vueOptions
      }
    })

fxy060608's avatar
fxy060608 已提交
93 94
    childMPInstance.$vm = new VueComponent(vueOptions)

fxy060608's avatar
fxy060608 已提交
95 96
    initSpecialMethods(childMPInstance)

fxy060608's avatar
fxy060608 已提交
97 98
    handleRef.call(vueOptions.parent.$scope, childMPInstance)

fxy060608's avatar
fxy060608 已提交
99 100 101
    childMPInstance.$vm.$mount()

    initChildVues(childMPInstance)
fxy060608's avatar
fxy060608 已提交
102 103 104 105 106 107 108 109 110

    childMPInstance.$vm._isMounted = true
    childMPInstance.$vm.__call_hook('mounted')
    childMPInstance.$vm.__call_hook('onReady')
  })

  delete mpInstance._$childVues
}

111 112 113
function handleProps (ref) {
  const eventProps = {}
  let refProps = ref.props
114
  const eventList = (refProps['data-event-list'] || '').split(',')
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  // 初始化支付宝小程序组件事件
  Object.keys(refProps).forEach(key => {
    if (eventList.includes(key)) {
      const handler = refProps[key]
      const res = key.match(/^on([A-Z])(\S*)/)
      const event = res && (res[1].toLowerCase() + res[2])
      refProps[key] = eventProps[key] = function () {
        const props = Object.assign({}, refProps)
        props[key] = handler
        // 由于支付宝事件可能包含多个参数,不使用微信小程序事件格式
        delete props['data-com-type']
        triggerEvent.bind({ props })(event, {
          __args__: [...arguments]
        })
      }
    }
  })
  // 处理 props 重写
  Object.defineProperty(ref, 'props', {
    get () {
      return refProps
    },
    set (value) {
      refProps = Object.assign(value, eventProps)
    }
  })
}

fxy060608's avatar
fxy060608 已提交
143
export function handleRef (ref) {
F
fw6 已提交
144
  if (!(ref && this.$vm)) {
fxy060608's avatar
fxy060608 已提交
145 146
    return
  }
147
  if (ref.props['data-com-type'] === 'wx') {
148
    handleProps(ref)
149
  }
fxy060608's avatar
fxy060608 已提交
150 151 152 153 154
  const refName = ref.props['data-ref']
  const refInForName = ref.props['data-ref-in-for']
  if (refName) {
    this.$vm.$refs[refName] = ref.$vm || ref
  } else if (refInForName) {
fxy060608's avatar
fxy060608 已提交
155
    (this.$vm.$refs[refInForName] || (this.$vm.$refs[refInForName] = [])).push(ref.$vm || ref)
fxy060608's avatar
fxy060608 已提交
156 157 158 159
  }
}

export function triggerEvent (type, detail, options) {
160
  const handler = this.props && this.props[customize('on-' + type)]
fxy060608's avatar
fxy060608 已提交
161 162 163 164 165
  if (!handler) {
    return
  }

  const eventOpts = this.props['data-event-opts']
166
  const eventParams = this.props['data-event-params']
167
  const comType = this.props['data-com-type']
fxy060608's avatar
fxy060608 已提交
168 169 170

  const target = {
    dataset: {
171
      eventOpts,
172 173
      eventParams,
      comType
fxy060608's avatar
fxy060608 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    }
  }

  handler({
    type: customize(type),
    target,
    currentTarget: target,
    detail
  })
}

const IGNORES = ['$slots', '$scopedSlots']

export function createObserver (isDidUpdate) {
  return function observe (props) {
    const prevProps = isDidUpdate ? props : this.props
    const nextProps = isDidUpdate ? this.props : props
    if (deepEqual(prevProps, nextProps)) {
      return
    }
    Object.keys(prevProps).forEach(name => {
      if (IGNORES.indexOf(name) === -1) {
        const prevValue = prevProps[name]
        const nextValue = nextProps[name]
        if (!isFn(prevValue) && !isFn(nextValue) && !deepEqual(prevValue, nextValue)) {
          this.$vm[name] = nextProps[name]
        }
      }
    })
  }
}

export const handleLink = (function () {
  if (isComponent2) {
    return function handleLink (detail) {
      return handleBaseLink.call(this, {
        detail
      })
    }
  }
  return function handleLink (detail) {
fxy060608's avatar
fxy060608 已提交
215
    if (this.$vm && this.$vm._isMounted) { // 父已初始化
fxy060608's avatar
fxy060608 已提交
216 217 218 219 220 221 222 223 224 225
      return handleBaseLink.call(this, {
        detail: {
          vuePid: detail.vuePid,
          vueOptions: detail.vueOptions
        }
      })
    }
    // 支付宝通过 didMount 来实现,先子后父,故等父 ready 之后,统一初始化
    (this._$childVues || (this._$childVues = [])).unshift(detail)
  }
226
})()
227 228 229

export const handleWrap = function (mp, destory) {
  const vueId = mp.props.vueId
230
  const list = (mp.props['data-event-list'] || '').split(',')
231 232 233 234 235 236 237 238 239 240
  list.forEach(eventName => {
    const key = `${eventName}${vueId}`
    if (destory) {
      delete this[key]
    } else {
      this[key] = function () {
        mp.props[eventName].apply(this, arguments)
      }
    }
  })
241 242 243
  if (!destory) {
    handleProps(mp)
  }
244
}