2021-03-02 19:10:10 +02:00
|
|
|
/**
|
|
|
|
* --------------------------------------------------------------------------
|
2021-10-09 09:33:12 +03:00
|
|
|
* Bootstrap (v5.1.3): util/scrollBar.js
|
2021-03-02 19:10:10 +02:00
|
|
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
|
|
|
* --------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
import SelectorEngine from '../dom/selector-engine'
|
|
|
|
import Manipulator from '../dom/manipulator'
|
2021-06-06 09:26:36 +03:00
|
|
|
import { isElement } from './index'
|
2021-03-02 19:10:10 +02:00
|
|
|
|
2021-10-13 15:19:28 +03:00
|
|
|
/**
|
|
|
|
* Constants
|
|
|
|
*/
|
|
|
|
|
2021-04-11 09:37:59 +03:00
|
|
|
const SELECTOR_FIXED_CONTENT = '.fixed-top, .fixed-bottom, .is-fixed, .sticky-top'
|
2021-03-02 19:10:10 +02:00
|
|
|
const SELECTOR_STICKY_CONTENT = '.sticky-top'
|
2021-12-09 15:49:28 +02:00
|
|
|
const PROPERTY_PADDING = 'padding-right'
|
|
|
|
const PROPERTY_MARGIN = 'margin-right'
|
2021-03-02 19:10:10 +02:00
|
|
|
|
2021-10-13 15:19:28 +03:00
|
|
|
/**
|
|
|
|
* Class definition
|
|
|
|
*/
|
|
|
|
|
2021-06-06 09:26:36 +03:00
|
|
|
class ScrollBarHelper {
|
|
|
|
constructor() {
|
|
|
|
this._element = document.body
|
|
|
|
}
|
2021-03-02 19:10:10 +02:00
|
|
|
|
2021-10-13 15:19:28 +03:00
|
|
|
// Public
|
2021-06-06 09:26:36 +03:00
|
|
|
getWidth() {
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/Window/innerWidth#usage_notes
|
|
|
|
const documentWidth = document.documentElement.clientWidth
|
|
|
|
return Math.abs(window.innerWidth - documentWidth)
|
|
|
|
}
|
2021-04-25 06:50:16 +03:00
|
|
|
|
2021-06-06 09:26:36 +03:00
|
|
|
hide() {
|
|
|
|
const width = this.getWidth()
|
|
|
|
this._disableOverFlow()
|
|
|
|
// give padding to element to balance the hidden scrollbar width
|
2021-12-09 15:05:50 +02:00
|
|
|
this._setElementAttributes(this._element, PROPERTY_PADDING, calculatedValue => calculatedValue + width)
|
2021-06-06 09:26:36 +03:00
|
|
|
// trick: We adjust positive paddingRight and negative marginRight to sticky-top elements to keep showing fullwidth
|
2021-12-09 15:05:50 +02:00
|
|
|
this._setElementAttributes(SELECTOR_FIXED_CONTENT, PROPERTY_PADDING, calculatedValue => calculatedValue + width)
|
|
|
|
this._setElementAttributes(SELECTOR_STICKY_CONTENT, PROPERTY_MARGIN, calculatedValue => calculatedValue - width)
|
2021-04-25 06:50:16 +03:00
|
|
|
}
|
|
|
|
|
2021-10-13 15:19:28 +03:00
|
|
|
reset() {
|
|
|
|
this._resetElementAttributes(this._element, 'overflow')
|
2021-12-09 15:05:50 +02:00
|
|
|
this._resetElementAttributes(this._element, PROPERTY_PADDING)
|
|
|
|
this._resetElementAttributes(SELECTOR_FIXED_CONTENT, PROPERTY_PADDING)
|
|
|
|
this._resetElementAttributes(SELECTOR_STICKY_CONTENT, PROPERTY_MARGIN)
|
2021-10-13 15:19:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
isOverflowing() {
|
|
|
|
return this.getWidth() > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private
|
2021-06-06 09:26:36 +03:00
|
|
|
_disableOverFlow() {
|
|
|
|
this._saveInitialAttribute(this._element, 'overflow')
|
|
|
|
this._element.style.overflow = 'hidden'
|
|
|
|
}
|
2021-03-02 19:10:10 +02:00
|
|
|
|
2021-10-29 10:38:35 +03:00
|
|
|
_setElementAttributes(selector, styleProperty, callback) {
|
2021-06-06 09:26:36 +03:00
|
|
|
const scrollbarWidth = this.getWidth()
|
|
|
|
const manipulationCallBack = element => {
|
|
|
|
if (element !== this._element && window.innerWidth > element.clientWidth + scrollbarWidth) {
|
2021-03-02 19:10:10 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-29 10:38:35 +03:00
|
|
|
this._saveInitialAttribute(element, styleProperty)
|
|
|
|
const calculatedValue = window.getComputedStyle(element).getPropertyValue(styleProperty)
|
|
|
|
element.style.setProperty(styleProperty, `${callback(Number.parseFloat(calculatedValue))}px`)
|
2021-06-06 09:26:36 +03:00
|
|
|
}
|
2021-03-02 19:10:10 +02:00
|
|
|
|
2021-06-06 09:26:36 +03:00
|
|
|
this._applyManipulationCallback(selector, manipulationCallBack)
|
|
|
|
}
|
|
|
|
|
2021-10-29 10:38:35 +03:00
|
|
|
_saveInitialAttribute(element, styleProperty) {
|
|
|
|
const actualValue = element.style.getPropertyValue(styleProperty)
|
2021-06-06 09:26:36 +03:00
|
|
|
if (actualValue) {
|
2021-10-29 10:38:35 +03:00
|
|
|
Manipulator.setDataAttribute(element, styleProperty, actualValue)
|
2021-06-06 09:26:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-29 10:38:35 +03:00
|
|
|
_resetElementAttributes(selector, styleProperty) {
|
2021-06-06 09:26:36 +03:00
|
|
|
const manipulationCallBack = element => {
|
2021-10-29 10:38:35 +03:00
|
|
|
const value = Manipulator.getDataAttribute(element, styleProperty)
|
2021-12-09 15:49:28 +02:00
|
|
|
// We only want to remove the property if the value is `null`; the value can also be zero
|
|
|
|
if (value === null) {
|
2021-10-29 10:38:35 +03:00
|
|
|
element.style.removeProperty(styleProperty)
|
2021-12-09 15:05:50 +02:00
|
|
|
return
|
2021-06-06 09:26:36 +03:00
|
|
|
}
|
2021-12-09 15:05:50 +02:00
|
|
|
|
2021-10-29 10:38:35 +03:00
|
|
|
Manipulator.removeDataAttribute(element, styleProperty)
|
|
|
|
element.style.setProperty(styleProperty, value)
|
2021-06-06 09:26:36 +03:00
|
|
|
}
|
2021-03-02 19:10:10 +02:00
|
|
|
|
2021-06-06 09:26:36 +03:00
|
|
|
this._applyManipulationCallback(selector, manipulationCallBack)
|
|
|
|
}
|
|
|
|
|
|
|
|
_applyManipulationCallback(selector, callBack) {
|
|
|
|
if (isElement(selector)) {
|
|
|
|
callBack(selector)
|
2021-12-09 15:05:50 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const sel of SelectorEngine.find(selector, this._element)) {
|
|
|
|
callBack(sel)
|
2021-03-02 19:10:10 +02:00
|
|
|
}
|
2021-06-06 09:26:36 +03:00
|
|
|
}
|
2021-03-02 19:10:10 +02:00
|
|
|
}
|
|
|
|
|
2021-06-06 09:26:36 +03:00
|
|
|
export default ScrollBarHelper
|