Много разных изменений
This commit is contained in:
1 parent
3599d850f6
commit
a1c4ee4298
78 files changed
+4698
-428
No files matched your search
@@ -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;
|
||||
}
|
||||
Reference in new issue
Block a user