Files
webawesome/docs/assets/scripts/turbo.js

77 lines
2.3 KiB
JavaScript
Raw Normal View History

import 'https://cdn.jsdelivr.net/npm/@hotwired/turbo@8.0.10/+esm';
import { preventTurboFouce } from '/dist/webawesome.js';
2024-04-17 11:20:27 -04:00
if (!window.___turboScrollPositions___) {
window.___turboScrollPositions___ = {};
}
const positions = window.___turboScrollPositions___;
function saveScrollPosition() {
document.querySelectorAll('[data-remember-scroll]').forEach(element => {
if (element.id) {
positions[element.id] = {
top: element.scrollTop,
left: element.scrollLeft,
2024-04-17 11:20:27 -04:00
};
} else {
console.warn(`Can't save scroll position for elements without an id.`, el);
}
});
}
function restoreScrollPosition(event) {
if (event.detail && event.detail.newBody) {
event.detail.newBody.querySelectorAll('[data-remember-scroll]').forEach(element => {
if (!positions[element.id]) {
return;
}
const { top, left } = positions[element.id];
element.scrollTop = top;
element.scrollLeft = left;
});
}
2024-04-17 11:20:27 -04:00
document.querySelectorAll('[data-remember-scroll]').forEach(element => {
if (!positions[element.id]) {
return;
2024-04-17 11:20:27 -04:00
}
const { top, left } = positions[element.id];
element.scrollTop = top;
element.scrollLeft = left;
2024-04-17 11:20:27 -04:00
});
}
Initial SSR implementation (#157) * continued ssr work * continued ssr work * prettier * all components now rendering * everything finally works * fix type issues * working on breadcrumb * working on breadcrumb * radio group * convert all tests to ssr * prettier * test suite finally passing * add layout stuff * add changelog * fix TS issue * fix tests * fixing deploy stuff * get QR code displaying * fix tests * fix tests * prettier * condense hydration stuff * prettier * comment out range test * fixing issues * use base fixtures * fixing examples * dont vendor * fix import of hydration support * adding notes * add notesg * add ssr loader * fix build * prettier * add notes * add notes * prettier * fixing bundled stuff * remove cdn * remove cdn * prettier * fiixng tests * prettier * split jobs?? * prettier * fix build stuff * add reset mouse and await aTimeout * prettier * fix improper tests * prettier * bail on first * fix linting * only test form with client * redundancy on ssr-loader?? * maybe this will work * prettier * try callout now * fix form.test.ts * fix form.test.ts * prettier * fix forms * fix forms * try again * prettier * add some awaits * prettier * comment out broken SSR tests * prettier * comment out broken SSR tests * prettier * dont skip in CI * upgrade playwright to beta * prettier * try some trickery * try some trickery * await updateComplete * try to fix form.test.ts * import hydrateable elements 1 time * prettier * fix input defaultValue issues * fix form controls to behave like their native counterpartS * add changelog entry * prettier * fix unexpected behavior with range / button
2024-09-11 10:25:42 -04:00
function fixDSD(e) {
const newElement = e.detail.newBody || e.detail.newFrame || e.detail.newStream;
if (!newElement) {
return;
}
// https://developer.chrome.com/docs/css-ui/declarative-shadow-dom#polyfill
(function attachShadowRoots(root) {
root.querySelectorAll('template[shadowrootmode]').forEach(template => {
const mode = template.getAttribute('shadowrootmode');
const shadowRoot = template.parentNode.attachShadow({ mode });
shadowRoot.appendChild(template.content);
template.remove();
attachShadowRoots(shadowRoot);
});
})(newElement);
}
// Fixes an issue with DSD keeping the `<template>` elements hanging around in the lightdom.
// https://github.com/hotwired/turbo/issues/1292
['turbo:before-render', 'turbo:before-stream-render', 'turbo:before-frame-render'].forEach(eventName => {
document.addEventListener(eventName, fixDSD);
});
2024-04-17 11:20:27 -04:00
window.addEventListener('turbo:before-cache', saveScrollPosition);
window.addEventListener('turbo:before-render', restoreScrollPosition);
window.addEventListener('turbo:render', restoreScrollPosition);
preventTurboFouce();