util.js 5.9 KB
Newer Older
1 2 3
import {
  isFn,
  noop,
4
  hasOwn,
5 6 7
  isPlainObject
} from 'uni-shared'

8 9 10 11 12 13 14 15 16 17 18
const MOCKS = ['__route__', '__wxExparserNodeId__', '__wxWebviewId__']

export function initMocks (vm) {
  const mpInstance = vm.$mp[vm.mpType]
  MOCKS.forEach(mock => {
    if (hasOwn(mpInstance, mock)) {
      vm[mock] = mpInstance[mock]
    }
  })
}

fxy060608's avatar
fxy060608 已提交
19
export function initHooks (mpOptions, hooks, delay = false) {
20 21
  hooks.forEach(hook => {
    mpOptions[hook] = function (args) {
fxy060608's avatar
fxy060608 已提交
22 23 24 25 26
      if (delay) {
        setTimeout(() => this.$vm.__call_hook(hook, args))
      } else {
        this.$vm.__call_hook(hook, args)
      }
27 28 29 30
    }
  })
}

31
export function getData (vueOptions) {
fxy060608's avatar
fxy060608 已提交
32 33
  let data = vueOptions.data || {}
  const methods = vueOptions.methods || {}
34 35 36

  if (typeof data === 'function') {
    try {
fxy060608's avatar
fxy060608 已提交
37
      data = data()
38
    } catch (e) {
39 40 41
      if (process.env.VUE_APP_DEBUG) {
        console.warn('根据 Vue 的 data 函数初始化小程序 data 失败,请尽量确保 data 函数中不访问 vm 对象,否则可能影响首次数据渲染速度。', data)
      }
42
    }
43 44 45 46 47
  } else {
    try {
      // 对 data 格式化
      data = JSON.parse(JSON.stringify(data))
    } catch (e) {}
48
  }
fxy060608's avatar
fxy060608 已提交
49

50 51 52 53 54 55 56
  Object.keys(methods).forEach(methodName => {
    if (!hasOwn(data, methodName)) {
      data[methodName] = methods[methodName]
    }
  })

  return data
57 58 59 60
}

const PROP_TYPES = [String, Number, Boolean, Object, Array, null]

fxy060608's avatar
fxy060608 已提交
61 62 63 64 65 66 67 68
function createObserver (name) {
  return function observer (newVal, oldVal) {
    if (this.$vm) {
      this.$vm[name] = newVal // 为了触发其他非 render watcher
    }
  }
}

69
export function getProperties (props) {
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  const properties = {
    vueSlots: { // 小程序不能直接定义 $slots 的 props,所以通过 vueSlots 转换到 $slots
      type: null,
      value: [],
      observer: function (newVal, oldVal) {
        const $slots = Object.create(null)
        newVal.forEach(slotName => {
          $slots[slotName] = true
        })
        this.setData({
          $slots
        })
      }
    }
  }
85 86
  if (Array.isArray(props)) { // ['title']
    props.forEach(key => {
fxy060608's avatar
fxy060608 已提交
87 88 89 90
      properties[key] = {
        type: null,
        observer: createObserver(key)
      }
91 92 93 94 95 96 97 98 99 100 101
    })
  } else if (isPlainObject(props)) { // {title:{type:String,default:''},content:String}
    Object.keys(props).forEach(key => {
      const opts = props[key]
      if (isPlainObject(opts)) { // title:{type:String,default:''}
        let value = opts['default']
        if (isFn(value)) {
          value = value()
        }
        properties[key] = {
          type: PROP_TYPES.includes(opts.type) ? opts.type : null,
fxy060608's avatar
fxy060608 已提交
102 103
          value,
          observer: createObserver(key)
104 105
        }
      } else { // content:String
fxy060608's avatar
fxy060608 已提交
106 107 108 109
        properties[key] = {
          type: PROP_TYPES.includes(opts) ? opts : null,
          observer: createObserver(key)
        }
110 111 112 113 114 115 116 117 118 119 120 121
      }
    })
  }
  return properties
}

function wrapper (event) {
  event.stopPropagation = noop
  event.preventDefault = noop

  event.target = event.target || {}
  event.detail = event.detail || {}
fxy060608's avatar
fxy060608 已提交
122 123 124 125 126 127 128

  if (__PLATFORM__ === 'mp-baidu') { // mp-baidu,checked=>value
    if (hasOwn(event.detail, 'checked') && !hasOwn(event.detail, 'value')) {
      event.detail.value = event.detail.checked
    }
  }

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
  // TODO 又得兼容 mpvue 的 mp 对象
  event.mp = event
  event.target = Object.assign({}, event.target, event.detail)
  return event
}

function processEventArgs (event, args = [], isCustom) {
  if (isCustom && !args.length) { // 无参数,直接传入 detail 数组
    return event.detail
  }
  const ret = []
  args.forEach(arg => {
    if (arg === '$event') {
      ret.push(isCustom ? event.detail[0] : event)
    } else {
      ret.push(arg)
    }
  })

  return ret
}

const ONCE = '~'
const CUSTOM = '^'

export function handleEvent (event) {
  event = wrapper(event)

  // [['tap',[['handle',[1,2,a]],['handle1',[1,2,a]]]]]
  const eventOpts = (event.currentTarget || event.target).dataset.eventOpts
  if (!eventOpts) {
    return console.warn(`事件信息不存在`)
  }

  // [['handle',[1,2,a]],['handle1',[1,2,a]]]
  const eventType = event.type
  eventOpts.forEach(eventOpt => {
    let type = eventOpt[0]
    const eventsArray = eventOpt[1]

    const isCustom = type.charAt(0) === CUSTOM
    type = isCustom ? type.slice(1) : type
    const isOnce = type.charAt(0) === ONCE
    type = isOnce ? type.slice(1) : type

    if (eventsArray && eventType === type) {
      eventsArray.forEach(eventArray => {
        const handler = this.$vm[eventArray[0]]
fxy060608's avatar
fxy060608 已提交
177 178 179
        if (!isFn(handler)) {
          throw new Error(` _vm.${eventArray[0]} is not a function`)
        }
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
        if (isOnce) {
          if (handler.once) {
            return
          }
          handler.once = true
        }
        handler.apply(this.$vm, processEventArgs(event, eventArray[1], isCustom))
      })
    }
  })
}

export function initRefs (vm) {
  const mpInstance = vm.$mp[vm.mpType]
  Object.defineProperty(vm, '$refs', {
    get () {
      const $refs = Object.create(null)
fxy060608's avatar
fxy060608 已提交
197
      const components = mpInstance.selectAllComponents('.vue-ref')
198
      components.forEach(component => {
fxy060608's avatar
fxy060608 已提交
199 200
        const ref = component.dataset.ref
        $refs[ref] = component.$vm
201
      })
fxy060608's avatar
fxy060608 已提交
202
      const forComponents = mpInstance.selectAllComponents('.vue-ref-in-for')
203
      forComponents.forEach(component => {
fxy060608's avatar
fxy060608 已提交
204 205 206
        const ref = component.dataset.ref
        if (!$refs[ref]) {
          $refs[ref] = []
207
        }
fxy060608's avatar
fxy060608 已提交
208
        $refs[ref].push(component.$vm)
209 210 211 212
      })
      return $refs
    }
  })
fxy060608's avatar
fxy060608 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226
}

function baiduComponentDestroy ($vm) {
  $vm.$children.forEach(childVm => {
    childVm.$mp.component.detached()
  })
  $vm.$mp.component.detached()
}

export function baiduPageDestroy ($vm) {
  $vm.$destroy()
  $vm.$children.forEach(childVm => {
    baiduComponentDestroy(childVm)
  })
227
}