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

85 lines
2.2 KiB
JavaScript
Raw Normal View History

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