impress/app/assets/javascripts/wardrobe/show.js

37 lines
1.1 KiB
JavaScript
Raw Normal View History

2025-11-02 00:43:54 -07:00
// Wardrobe v2 - Simple Rails+Turbo outfit editor
//
2026-02-05 20:47:05 -08:00
// This page uses Turbo for instant updates when changing species/color.
2025-11-02 00:43:54 -07:00
// The outfit_viewer Web Component handles the pet rendering.
2026-02-05 20:47:05 -08:00
// Unsaved changes warning: use a MutationObserver to watch the
// data-has-unsaved-changes attribute on the wardrobe container. This is more
// robust than event listeners because it works regardless of how the DOM is
// updated (Turbo morph, direct manipulation, etc.).
function setupUnsavedChangesObserver() {
const container = document.querySelector("[data-has-unsaved-changes]");
if (!container) return;
function update() {
if (container.dataset.hasUnsavedChanges === "true") {
window.onbeforeunload = (e) => {
e.preventDefault();
return "";
};
} else {
window.onbeforeunload = null;
}
}
// Set initial state
update();
// Watch for attribute changes
const observer = new MutationObserver(update);
observer.observe(container, {
attributes: true,
attributeFilter: ["data-has-unsaved-changes"],
});
}
setupUnsavedChangesObserver();