2019-07-19 16:27:03 +00:00
|
|
|
import * as bodyScrollLock from 'body-scroll-lock'
|
|
|
|
|
|
|
|
let previousNavPaddingRight
|
|
|
|
let previousAppBgWrapperRight
|
2019-10-17 20:03:41 +00:00
|
|
|
let lockerEls = []
|
2019-07-19 16:27:03 +00:00
|
|
|
|
|
|
|
const disableBodyScroll = (el) => {
|
|
|
|
const scrollBarGap = window.innerWidth - document.documentElement.clientWidth
|
|
|
|
bodyScrollLock.disableBodyScroll(el, {
|
|
|
|
reserveScrollBarGap: true
|
|
|
|
})
|
2019-10-17 20:03:41 +00:00
|
|
|
if (!lockerEls.includes(el)) {
|
|
|
|
lockerEls.push(el)
|
|
|
|
}
|
2019-07-19 16:27:03 +00:00
|
|
|
setTimeout(() => {
|
2019-10-17 20:03:41 +00:00
|
|
|
if (lockerEls.length <= 1) {
|
|
|
|
// If previousNavPaddingRight is already set, don't set it again.
|
|
|
|
if (previousNavPaddingRight === undefined) {
|
|
|
|
const navEl = document.getElementById('nav')
|
|
|
|
previousNavPaddingRight = window.getComputedStyle(navEl).getPropertyValue('padding-right')
|
|
|
|
navEl.style.paddingRight = previousNavPaddingRight ? `calc(${previousNavPaddingRight} + ${scrollBarGap}px)` : `${scrollBarGap}px`
|
|
|
|
}
|
|
|
|
// If previousAppBgWrapeprRight is already set, don't set it again.
|
|
|
|
if (previousAppBgWrapperRight === undefined) {
|
|
|
|
const appBgWrapperEl = document.getElementById('app_bg_wrapper')
|
|
|
|
previousAppBgWrapperRight = window.getComputedStyle(appBgWrapperEl).getPropertyValue('right')
|
|
|
|
appBgWrapperEl.style.right = previousAppBgWrapperRight ? `calc(${previousAppBgWrapperRight} + ${scrollBarGap}px)` : `${scrollBarGap}px`
|
|
|
|
}
|
|
|
|
document.body.classList.add('scroll-locked')
|
2019-07-19 16:27:03 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const enableBodyScroll = (el) => {
|
2019-10-17 20:03:41 +00:00
|
|
|
if (lockerEls.includes(el)) {
|
|
|
|
lockerEls = lockerEls.filter(e => e !== el)
|
|
|
|
}
|
2019-07-19 16:27:03 +00:00
|
|
|
setTimeout(() => {
|
2019-10-21 19:56:16 +00:00
|
|
|
if (lockerEls.length === 0) {
|
2019-10-17 20:03:41 +00:00
|
|
|
if (previousNavPaddingRight !== undefined) {
|
|
|
|
document.getElementById('nav').style.paddingRight = previousNavPaddingRight
|
|
|
|
// Restore previousNavPaddingRight to undefined so disableBodyScroll knows it can be set again.
|
|
|
|
previousNavPaddingRight = undefined
|
|
|
|
}
|
|
|
|
if (previousAppBgWrapperRight !== undefined) {
|
|
|
|
document.getElementById('app_bg_wrapper').style.right = previousAppBgWrapperRight
|
|
|
|
// Restore previousAppBgWrapperRight to undefined so disableBodyScroll knows it can be set again.
|
|
|
|
previousAppBgWrapperRight = undefined
|
|
|
|
}
|
|
|
|
document.body.classList.remove('scroll-locked')
|
2019-07-19 16:27:03 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
bodyScrollLock.enableBodyScroll(el)
|
|
|
|
}
|
|
|
|
|
|
|
|
const directive = {
|
|
|
|
inserted: (el, binding) => {
|
|
|
|
if (binding.value) {
|
|
|
|
disableBodyScroll(el)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
componentUpdated: (el, binding) => {
|
|
|
|
if (binding.oldValue === binding.value) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (binding.value) {
|
|
|
|
disableBodyScroll(el)
|
|
|
|
} else {
|
|
|
|
enableBodyScroll(el)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
unbind: (el) => {
|
|
|
|
enableBodyScroll(el)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default (Vue) => {
|
|
|
|
Vue.directive('body-scroll-lock', directive)
|
|
|
|
}
|