@nent/core
Version:
49 lines (48 loc) • 1.46 kB
JavaScript
/*!
* NENT 2022
*/
import { getDataProvider } from '../../../services/data/factory';
const RouterScrollKey = 'scrollPositions';
/* It's a class that stores scroll positions in a map and saves them to the session storage */
export class ScrollHistory {
/**
* We're getting the scroll position from the session storage and setting it to the scrollPositions
* variable
* @param {Window} win - Window - This is the window object.
*/
constructor(win) {
this.win = win;
this.scrollPositions = new Map();
getDataProvider('session').then(provider => {
this.provider = provider;
return provider === null || provider === void 0 ? void 0 : provider.get(RouterScrollKey).then(scrollData => {
if (scrollData)
this.scrollPositions = new Map(JSON.parse(scrollData));
});
});
if (win && 'scrollRestoration' in win.history) {
win.history.scrollRestoration = 'manual';
}
}
set(key, value) {
this.scrollPositions.set(key, value);
if (this.provider) {
const arrayData = [];
this.scrollPositions.forEach((v, k) => {
arrayData.push([k, v]);
});
this.provider
.set(RouterScrollKey, JSON.stringify(arrayData))
.then(() => { });
}
}
get(key) {
return this.scrollPositions.get(key);
}
has(key) {
return this.scrollPositions.has(key);
}
capture(key) {
this.set(key, [this.win.scrollX, this.win.scrollY]);
}
}