webpack-bootstrap-ui-kit/src/js_old/_components/_ui.sidebar.js

85 lines
2.2 KiB
JavaScript
Raw Normal View History

2022-05-03 20:50:57 +02:00
'use strict'
2020-01-29 10:09:39 +01:00
2022-05-03 20:50:57 +02:00
// import StickySidebar from 'sticky-sidebar/src/sticky-sidebar';
import $ from 'jquery'
import Events from '../_events'
2020-01-29 10:09:39 +01:00
const SidebarUI = (($) => {
2022-05-03 20:50:57 +02:00
const D = document
const W = window
const $Body = $('body')
const NAME = 'SidebarUI'
const CLASSNAME = `js${NAME}`
const CONTENTHOLDER = 'content-holder'
const INNERWRAPPER = `${CLASSNAME}__inner`
2020-01-29 10:09:39 +01:00
class SidebarUI {
2022-05-03 20:50:57 +02:00
static init () {
const ui = this
ui.dispose()
2020-01-29 10:09:39 +01:00
2020-01-29 17:08:34 +01:00
if (!$(`.${CLASSNAME}`).length) {
2022-05-03 20:50:57 +02:00
return
2020-01-29 17:08:34 +01:00
}
2022-05-03 20:50:57 +02:00
console.log(`${NAME}: init ...`)
// const fontSize = parseInt($Body.css('font-size'));
const fontSize = 0
const contentElement = $(`.${CONTENTHOLDER}`)[0]
2020-01-29 10:09:39 +01:00
2022-05-03 20:50:57 +02:00
// $(`.${CLASSNAME}`).wrapInner(`<div class="${INNERWRAPPER}"></div>`);
const $el = $(`.${CLASSNAME}`)
const $innerWrapper = $(`.${INNERWRAPPER}`)
2020-07-30 12:57:28 +02:00
2022-05-03 20:50:57 +02:00
/* const sticky = new StickySidebar(`.${CLASSNAME}`, {
2020-07-30 12:57:28 +02:00
topSpacing: fontSize,
bottomSpacing: fontSize,
containerSelector: CONTENTHOLDER,
innerWrapperSelector: INNERWRAPPER,
2022-05-03 20:50:57 +02:00
}); */
2020-07-30 12:57:28 +02:00
2022-05-03 20:50:57 +02:00
$el.addClass(`${CLASSNAME}-active`)
2020-01-29 10:09:39 +01:00
$Body.on(`${Events.SCROLL} ${Events.RESIZE}`, (e) => {
2022-05-03 20:50:57 +02:00
const contentOffset = parseInt(contentElement.offsetTop) + fontSize
const contentOffsetHeight =
2022-05-03 20:50:57 +02:00
parseInt(contentElement.offsetHeight) - fontSize
const sidebarWidth = $el[0].offsetWidth
2020-07-30 12:57:28 +02:00
2022-05-03 20:50:57 +02:00
const scrollPos = parseInt($Body.scrollTop())
2020-07-30 12:57:28 +02:00
// normal pos
if (contentOffset >= scrollPos) {
2022-05-03 20:50:57 +02:00
$innerWrapper.attr('style', '')
} else if (
scrollPos >=
contentOffset + contentOffsetHeight - $innerWrapper[0].offsetHeight
) {
2020-07-30 12:57:28 +02:00
// bottom pos
2022-05-03 20:50:57 +02:00
$innerWrapper.attr('style', `position:absolute;bottom:${fontSize}px`)
} else {
// scrolled pos
$innerWrapper.attr(
2022-05-03 20:50:57 +02:00
'style',
2021-08-18 20:51:15 +02:00
`position:fixed;top:${fontSize}px;width:${sidebarWidth}px`
2022-05-03 20:50:57 +02:00
)
2020-07-30 12:57:28 +02:00
}
2022-05-03 20:50:57 +02:00
})
2020-01-29 10:09:39 +01:00
}
2022-05-03 20:50:57 +02:00
static dispose () {
console.log(`${NAME}: dispose`)
2020-01-29 10:09:39 +01:00
}
}
2020-09-09 17:40:58 +02:00
$(W).on(`${NAME}.init ${Events.LODEDANDREADY}`, () => {
2022-05-03 20:50:57 +02:00
SidebarUI.init()
})
2020-01-29 10:09:39 +01:00
2022-05-03 20:50:57 +02:00
W.SidebarUI = new SidebarUI()
2020-01-29 10:09:39 +01:00
2022-05-03 20:50:57 +02:00
return SidebarUI
})($)
2020-01-29 10:09:39 +01:00
2022-05-03 20:50:57 +02:00
export default SidebarUI