Много разных изменений

This commit is contained in:
Zuev
2026-08-20 00:55:27 +03:00
parent 3599d850f6
commit a1c4ee4298
78 changed files with 4698 additions and 428 deletions

View File

@@ -0,0 +1,36 @@
import { confirmAction } from './dialog.js';
const scopes = new Map();
if (typeof window !== 'undefined') {
window.addEventListener('beforeunload', event => {
if (![...scopes.values()].some(scope => scope.dirty)) return;
event.preventDefault();
event.returnValue = '';
});
}
export function createDirtyScope(name, message = 'Есть несохранённые изменения. Покинуть страницу и потерять их?') {
const scope = { name, message, dirty: false };
scopes.set(name, scope);
return {
mark() { scope.dirty = true; document.dispatchEvent(new CustomEvent('magistr:dirty-change')); },
clear() { scope.dirty = false; document.dispatchEvent(new CustomEvent('magistr:dirty-change')); },
isDirty() { return scope.dirty; },
dispose() { scopes.delete(name); }
};
}
export async function confirmAbandonDirtyChanges() {
const dirty = [...scopes.values()].filter(scope => scope.dirty);
if (!dirty.length) return true;
const confirmed = await confirmAction({
title: 'Несохранённые изменения',
message: dirty[0].message,
confirmLabel: 'Покинуть без сохранения',
cancelLabel: 'Остаться',
danger: true
});
if (confirmed) dirty.forEach(scope => { scope.dirty = false; });
return confirmed;
}