2023-11-14 17:58:38 +08:00
|
|
|
function mountWithTurbolinks(app, target, callback = null) {
|
2023-11-10 02:17:51 +08:00
|
|
|
const originalHtml = document.querySelector(target).innerHTML;
|
2023-11-20 18:38:41 +08:00
|
|
|
const cacheDisabled = document.querySelector('#cache-directive');
|
|
|
|
const event = cacheDisabled ? 'turbolinks:before-render' : 'turbolinks:before-cache';
|
2023-11-10 02:17:51 +08:00
|
|
|
|
2023-11-20 18:38:41 +08:00
|
|
|
document.addEventListener(event, () => {
|
2023-11-09 02:32:43 +08:00
|
|
|
app.unmount();
|
2023-11-14 17:58:38 +08:00
|
|
|
if (document.querySelector(target)) {
|
|
|
|
document.querySelector(target).innerHTML = originalHtml;
|
|
|
|
}
|
|
|
|
if (callback) callback();
|
2023-11-09 02:32:43 +08:00
|
|
|
}, { once: true });
|
2023-11-10 02:17:51 +08:00
|
|
|
|
|
|
|
return app.mount(target);
|
2023-11-09 02:32:43 +08:00
|
|
|
}
|
|
|
|
|
2023-11-10 02:17:51 +08:00
|
|
|
export { mountWithTurbolinks };
|