2018-07-16 22:22:55 +08:00
|
|
|
// 组件
|
|
|
|
|
import d2ContainerFull from './components/d2-container-full.vue'
|
|
|
|
|
import d2ContainerFullBs from './components/d2-container-full-bs.vue'
|
2018-07-21 12:44:47 +08:00
|
|
|
import d2ContainerGhost from './components/d2-container-ghost.vue'
|
2018-07-21 13:27:35 +08:00
|
|
|
import d2ContainerGhostBs from './components/d2-container-ghost-bs.vue'
|
2018-07-21 14:13:25 +08:00
|
|
|
import d2ContainerCard from './components/d2-container-card.vue'
|
2018-07-21 15:09:30 +08:00
|
|
|
import d2ContainerCardBs from './components/d2-container-card-bs.vue'
|
2018-11-15 09:53:33 +08:00
|
|
|
import d2Source from './components/d2-source.vue'
|
|
|
|
|
|
2018-07-16 22:22:55 +08:00
|
|
|
export default {
|
|
|
|
|
name: 'd2-container',
|
|
|
|
|
props: {
|
|
|
|
|
// 容器样式
|
|
|
|
|
type: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: false,
|
2018-07-18 08:30:51 +08:00
|
|
|
default: 'full'
|
2018-11-14 10:25:38 +08:00
|
|
|
},
|
|
|
|
|
// 滚动优化
|
|
|
|
|
betterScroll: {
|
|
|
|
|
type: Boolean,
|
|
|
|
|
required: false,
|
|
|
|
|
default: false
|
2018-07-16 22:22:55 +08:00
|
|
|
}
|
2018-11-14 10:25:38 +08:00
|
|
|
},
|
|
|
|
|
computed: {
|
|
|
|
|
// 始终返回渲染组件
|
|
|
|
|
component () {
|
|
|
|
|
if (this.type === 'card' && !this.betterScroll) return d2ContainerCard
|
|
|
|
|
if (this.type === 'card' && this.betterScroll) return d2ContainerCardBs
|
|
|
|
|
if (this.type === 'ghost' && !this.betterScroll) return d2ContainerGhost
|
|
|
|
|
if (this.type === 'ghost' && this.betterScroll) return d2ContainerGhostBs
|
|
|
|
|
if (this.type === 'full' && !this.betterScroll) return d2ContainerFull
|
|
|
|
|
if (this.type === 'full' && this.betterScroll) return d2ContainerFullBs
|
|
|
|
|
else {
|
|
|
|
|
return 'div'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
render (h) {
|
|
|
|
|
const slots = [
|
|
|
|
|
h('div', this.$slots.default)
|
|
|
|
|
]
|
|
|
|
|
if (this.$slots.header) slots.push(h('div', { slot: 'header' }, [ this.$slots.header ]))
|
|
|
|
|
if (this.$slots.footer) slots.push(h('div', { slot: 'footer' }, [ this.$slots.footer ]))
|
|
|
|
|
return h('div', {
|
|
|
|
|
ref: 'container',
|
2018-11-14 11:32:07 +08:00
|
|
|
class: 'container-component'
|
2018-11-14 10:25:38 +08:00
|
|
|
}, [
|
|
|
|
|
h(this.component, {
|
2018-11-16 10:25:08 +08:00
|
|
|
ref: 'component',
|
2018-11-14 10:25:38 +08:00
|
|
|
props: this.$attrs,
|
|
|
|
|
on: {
|
|
|
|
|
scroll: e => this.$emit('scroll', e)
|
|
|
|
|
}
|
2018-11-15 09:53:33 +08:00
|
|
|
}, slots),
|
2018-11-15 22:22:29 +08:00
|
|
|
// 只在预览和开发模式下显示源码按钮
|
|
|
|
|
process.env.VUE_APP_BUILD_MODE === 'TRAVIS' || process.env.NODE_ENV === 'development' ? h(d2Source, {
|
2018-11-15 20:42:36 +08:00
|
|
|
props: this.$attrs
|
2018-11-15 22:22:29 +08:00
|
|
|
}) : undefined
|
2018-11-14 10:25:38 +08:00
|
|
|
])
|
2018-11-16 10:25:08 +08:00
|
|
|
},
|
|
|
|
|
methods: {
|
|
|
|
|
// 返回顶部
|
|
|
|
|
scrollToTop () {
|
|
|
|
|
this.$refs.component.scrollToTop()
|
2018-11-16 15:53:07 +08:00
|
|
|
// 如果开启了 better scroll 还需要手动触发一遍 scroll 事件
|
|
|
|
|
const bs = this.$refs.component.BS
|
|
|
|
|
if (bs) this.$refs.component.scroll()
|
2018-11-16 14:54:22 +08:00
|
|
|
},
|
|
|
|
|
// 用法同原生方法 scrollBy
|
2018-11-16 15:53:07 +08:00
|
|
|
scrollBy (x = 0, y = 0, time = 300) {
|
|
|
|
|
if (this.betterScroll) {
|
|
|
|
|
const bs = this.$refs.component.BS
|
|
|
|
|
if (bs) {
|
|
|
|
|
bs.scrollBy(-x, -y, time)
|
|
|
|
|
// 手动触发一遍 scroll 事件
|
|
|
|
|
this.$refs.component.scroll()
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.$refs.component.$refs.body.scrollBy(x, y)
|
|
|
|
|
}
|
2018-11-16 14:54:22 +08:00
|
|
|
},
|
|
|
|
|
// 用法同原生方法 scrollTo
|
2018-11-16 15:53:07 +08:00
|
|
|
scrollTo (x = 0, y = 0, time = 300) {
|
|
|
|
|
if (this.betterScroll) {
|
|
|
|
|
const bs = this.$refs.component.BS
|
|
|
|
|
if (bs) {
|
|
|
|
|
bs.scrollTo(-x, -y, time)
|
|
|
|
|
// 手动触发一遍 scroll 事件
|
|
|
|
|
this.$refs.component.scroll()
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.$refs.component.$refs.body.scrollTo(x, y)
|
|
|
|
|
}
|
2018-11-16 14:54:22 +08:00
|
|
|
},
|
|
|
|
|
// 用法同原生方法 scrollTop
|
2018-11-16 15:53:07 +08:00
|
|
|
scrollTop (top = 0, time = 300) {
|
|
|
|
|
if (this.betterScroll) {
|
|
|
|
|
const bs = this.$refs.component.BS
|
|
|
|
|
if (bs) {
|
|
|
|
|
bs.scrollTo(bs.x, -top, time)
|
|
|
|
|
// 手动触发一遍 scroll 事件
|
|
|
|
|
this.$refs.component.scroll()
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.$refs.component.$refs.body.scrollTop = top
|
|
|
|
|
}
|
2018-11-16 10:25:08 +08:00
|
|
|
}
|
2018-07-16 22:22:55 +08:00
|
|
|
}
|
|
|
|
|
}
|