mirror of
https://github.com/piskelapp/piskel.git
synced 2023-08-10 21:12:52 +03:00
add confirmation message when closing desktop app with unsaved changes
This commit is contained in:
parent
c6c64af2fd
commit
974450837e
@ -6,15 +6,38 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
ns.BeforeUnloadService.prototype.init = function () {
|
ns.BeforeUnloadService.prototype.init = function () {
|
||||||
|
if (pskl.utils.Environment.detectNodeWebkit()) {
|
||||||
|
// Add a dedicated listener to window 'close' event in nwjs environment.
|
||||||
|
var win = require('nw.gui').Window.get();
|
||||||
|
win.on('close', this.onNwWindowClose.bind(this, win));
|
||||||
|
}
|
||||||
|
|
||||||
window.addEventListener('beforeunload', this.onBeforeUnload.bind(this));
|
window.addEventListener('beforeunload', this.onBeforeUnload.bind(this));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* In nw.js environment "onbeforeunload" is not triggered when closing the window.
|
||||||
|
* Polyfill the behavior here.
|
||||||
|
*/
|
||||||
|
ns.BeforeUnloadService.prototype.onNwWindowClose = function (win) {
|
||||||
|
var msg = this.onBeforeUnload();
|
||||||
|
if (msg) {
|
||||||
|
if (!window.confirm(msg)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
win.close(true);
|
||||||
|
};
|
||||||
|
|
||||||
ns.BeforeUnloadService.prototype.onBeforeUnload = function (evt) {
|
ns.BeforeUnloadService.prototype.onBeforeUnload = function (evt) {
|
||||||
pskl.app.backupService.backup();
|
pskl.app.backupService.backup();
|
||||||
if (pskl.app.savedStatusService.isDirty()) {
|
if (pskl.app.savedStatusService.isDirty()) {
|
||||||
var confirmationMessage = 'Your Piskel seems to have unsaved changes';
|
var confirmationMessage = 'Your Piskel seems to have unsaved changes';
|
||||||
|
|
||||||
(evt || window.event).returnValue = confirmationMessage;
|
evt = evt || window.event;
|
||||||
|
if (evt) {
|
||||||
|
evt.returnValue = confirmationMessage;
|
||||||
|
}
|
||||||
return confirmationMessage;
|
return confirmationMessage;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user