hae
Version:
Mobile web UI based on Vux
79 lines (69 loc) • 1.91 kB
JavaScript
import objectAssign from 'object-assign'
import ToastComponent from '../../components/toast'
import { mergeOptions } from '../../libs/plugin_helper'
let $vm
let watcher
const plugin = {
install (vue, pluginOptions = {}) {
const Toast = vue.extend(ToastComponent)
if (!$vm) {
$vm = new Toast({
el: document.createElement('div')
})
document.body.appendChild($vm.$el)
}
const defaults = {}
for (let i in $vm.$options.props) {
if (i !== 'value') {
defaults[i] = $vm.$options.props[i].default
}
}
const toast = {
show (options = {}) {
// destroy watcher
watcher && watcher()
if (typeof options === 'string') {
mergeOptions($vm, objectAssign({}, pluginOptions, {text: options}))
} else if (typeof options === 'object') {
mergeOptions($vm, objectAssign({}, pluginOptions, options))
}
if (typeof options === 'object' && options.onShow || options.onHide) {
watcher = $vm.$watch('show', (val) => {
val && options.onShow && options.onShow($vm)
val === false && options.onHide && options.onHide($vm)
})
}
$vm.show = true
},
text (text, position = 'default') {
this.show({
type: 'text',
width: 'auto',
position,
text
})
},
hide () {
$vm.show = false
},
isVisible () {
return $vm.show
}
}
// all Vux's plugins are included in this.$vux
if (!vue.$vux) {
vue.$vux = {
toast
}
} else {
vue.$vux.toast = toast
}
vue.mixin({
created: function () {
this.$vux = vue.$vux
}
})
}
}
export default plugin
export const install = plugin.install