Add support of tablet device in ResizeHandler

This commit is contained in:
Angelina Filippova 2020-02-17 17:02:40 +03:00
parent 436934109a
commit e697d7eadf

View file

@ -1,8 +1,9 @@
import store from '@/store' import store from '@/store'
const { body } = document const { body } = document
const WIDTH = 1024 const mobileWidth = 480
const RATIO = 3 const tabletWidth = 801
const ratio = 3
export default { export default {
watch: { watch: {
@ -17,23 +18,31 @@ export default {
}, },
mounted() { mounted() {
const isMobile = this.isMobile() const isMobile = this.isMobile()
if (isMobile) { const isTablet = this.isTablet()
store.dispatch('toggleDevice', 'mobile') if (isMobile || isTablet) {
store.dispatch('toggleDevice', isMobile ? 'mobile' : 'tablet')
store.dispatch('closeSideBar', { withoutAnimation: true }) store.dispatch('closeSideBar', { withoutAnimation: true })
} }
}, },
methods: { methods: {
isMobile() { isMobile() {
const rect = body.getBoundingClientRect() const rect = body.getBoundingClientRect()
return rect.width - RATIO < WIDTH return rect.width - ratio < mobileWidth
},
isTablet() {
const rect = body.getBoundingClientRect()
return rect.width - ratio < tabletWidth && rect.width - ratio > mobileWidth
}, },
resizeHandler() { resizeHandler() {
if (!document.hidden) { if (!document.hidden) {
const isMobile = this.isMobile() const isMobile = this.isMobile()
store.dispatch('toggleDevice', isMobile ? 'mobile' : 'desktop') const isTablet = this.isTablet()
if (isMobile) { if (isMobile || isTablet) {
store.dispatch('toggleDevice', isMobile ? 'mobile' : 'tablet')
store.dispatch('closeSideBar', { withoutAnimation: true }) store.dispatch('closeSideBar', { withoutAnimation: true })
} else {
store.dispatch('toggleDevice', 'desktop')
} }
} }
} }