From 0e52ef704b5eae9ba41623ad2573205c4c673b4c Mon Sep 17 00:00:00 2001
From: Julia Radzhabova <julia.radzhabova@onlyoffice.com>
Date: Sun, 28 Mar 2021 23:59:55 +0300
Subject: [PATCH] Check unsaved changes before onRequestClose event

---
 .../main/app/controller/Main.js               | 28 +++++++++++++++++--
 apps/documenteditor/main/locale/en.json       |  1 +
 .../main/app/controller/Main.js               | 27 ++++++++++++++++--
 apps/presentationeditor/main/locale/en.json   |  1 +
 .../main/app/controller/Main.js               | 27 ++++++++++++++++--
 apps/spreadsheeteditor/main/locale/en.json    |  1 +
 6 files changed, 76 insertions(+), 9 deletions(-)

diff --git a/apps/documenteditor/main/app/controller/Main.js b/apps/documenteditor/main/app/controller/Main.js
index 535dfe7d4..321334bc1 100644
--- a/apps/documenteditor/main/app/controller/Main.js
+++ b/apps/documenteditor/main/app/controller/Main.js
@@ -709,8 +709,29 @@ define([
             goBack: function(current) {
                 if ( !Common.Controllers.Desktop.process('goback') ) {
                     if (this.appOptions.customization.goback.requestClose && this.appOptions.canRequestClose) {
-                        Common.Gateway.requestClose();
-                        // Common.Controllers.Desktop.requestClose();
+                        if (this.api.isDocumentModified()) {
+                            var me = this;
+                            this.api.asc_stopSaving();
+                            Common.UI.warning({
+                                closable: false,
+                                width: 500,
+                                title: this.notcriticalErrorTitle,
+                                msg: this.leavePageTextOnClose,
+                                buttons: ['ok', 'cancel'],
+                                primary: 'ok',
+                                callback: function(btn) {
+                                    if (btn == 'ok') {
+                                        me.api.asc_undoAllChanges();
+                                        Common.Gateway.requestClose();
+                                        // Common.Controllers.Desktop.requestClose();
+                                    } else
+                                        me.api.asc_continueSaving();
+                                }
+                            });
+                        } else {
+                            Common.Gateway.requestClose();
+                            // Common.Controllers.Desktop.requestClose();
+                        }
                     } else {
                         var href = this.appOptions.customization.goback.url;
                         if (!current && this.appOptions.customization.goback.blank!==false) {
@@ -2847,7 +2868,8 @@ define([
             textLongName: 'Enter a name that is less than 128 characters.',
             textGuest: 'Guest',
             errorSubmit: 'Submit failed.',
-            txtClickToLoad: 'Click to load image'
+            txtClickToLoad: 'Click to load image',
+            leavePageTextOnClose: 'All unsaved changes in this document will be lost.<br> Click \'Cancel\' then \'Save\' to save them. Click \'OK\' to discard all the unsaved changes.'
         }
     })(), DE.Controllers.Main || {}))
 });
\ No newline at end of file
diff --git a/apps/documenteditor/main/locale/en.json b/apps/documenteditor/main/locale/en.json
index cc0245ee8..ab3c2caa0 100644
--- a/apps/documenteditor/main/locale/en.json
+++ b/apps/documenteditor/main/locale/en.json
@@ -842,6 +842,7 @@
   "DE.Controllers.Main.warnNoLicense": "You've reached the limit for simultaneous connections to %1 editors. This document will be opened for viewing only.<br>Contact %1 sales team for personal upgrade terms.",
   "DE.Controllers.Main.warnNoLicenseUsers": "You've reached the user limit for %1 editors. Contact %1 sales team for personal upgrade terms.",
   "DE.Controllers.Main.warnProcessRightsChange": "You have been denied the right to edit the file.",
+  "DE.Controllers.Main.leavePageTextOnClose": "All unsaved changes in this document will be lost.<br> Click \"Cancel\" then \"Save\" to save them. Click \"OK\" to discard all the unsaved changes.",
   "DE.Controllers.Navigation.txtBeginning": "Beginning of document",
   "DE.Controllers.Navigation.txtGotoBeginning": "Go to the beginning of the document",
   "DE.Controllers.Statusbar.textHasChanges": "New changes have been tracked",
diff --git a/apps/presentationeditor/main/app/controller/Main.js b/apps/presentationeditor/main/app/controller/Main.js
index 2a40265c2..8fc8bc90a 100644
--- a/apps/presentationeditor/main/app/controller/Main.js
+++ b/apps/presentationeditor/main/app/controller/Main.js
@@ -489,8 +489,28 @@ define([
                 var me = this;
                 if ( !Common.Controllers.Desktop.process('goback') ) {
                     if (me.appOptions.customization.goback.requestClose && me.appOptions.canRequestClose) {
-                        Common.Gateway.requestClose();
-                        // Common.Controllers.Desktop.requestClose();
+                        if (this.api.isDocumentModified()) {
+                            this.api.asc_stopSaving();
+                            Common.UI.warning({
+                                closable: false,
+                                width: 500,
+                                title: this.notcriticalErrorTitle,
+                                msg: this.leavePageTextOnClose,
+                                buttons: ['ok', 'cancel'],
+                                primary: 'ok',
+                                callback: function(btn) {
+                                    if (btn == 'ok') {
+                                        me.api.asc_undoAllChanges();
+                                        Common.Gateway.requestClose();
+                                        // Common.Controllers.Desktop.requestClose();
+                                    } else
+                                        me.api.asc_continueSaving();
+                                }
+                            });
+                        } else {
+                            Common.Gateway.requestClose();
+                            // Common.Controllers.Desktop.requestClose();
+                        }
                     } else {
                         var href = me.appOptions.customization.goback.url;
                         if (!current && me.appOptions.customization.goback.blank!==false) {
@@ -2480,7 +2500,8 @@ define([
             textRenameLabel: 'Enter a name to be used for collaboration',
             textRenameError: 'User name must not be empty.',
             textLongName: 'Enter a name that is less than 128 characters.',
-            textGuest: 'Guest'
+            textGuest: 'Guest',
+            leavePageTextOnClose: 'All unsaved changes in this document will be lost.<br> Click \'Cancel\' then \'Save\' to save them. Click \'OK\' to discard all the unsaved changes.'
         }
     })(), PE.Controllers.Main || {}))
 });
diff --git a/apps/presentationeditor/main/locale/en.json b/apps/presentationeditor/main/locale/en.json
index 88aff64ca..1d831b199 100644
--- a/apps/presentationeditor/main/locale/en.json
+++ b/apps/presentationeditor/main/locale/en.json
@@ -709,6 +709,7 @@
   "PE.Controllers.Main.warnNoLicense": "You've reached the limit for simultaneous connections to %1 editors. This document will be opened for viewing only.<br>Contact %1 sales team for personal upgrade terms.",
   "PE.Controllers.Main.warnNoLicenseUsers": "You've reached the user limit for %1 editors. Contact %1 sales team for personal upgrade terms.",
   "PE.Controllers.Main.warnProcessRightsChange": "You have been denied the right to edit the file.",
+  "PE.Controllers.Main.leavePageTextOnClose": "All unsaved changes in this presentation will be lost.<br> Click \"Cancel\" then \"Save\" to save them. Click \"OK\" to discard all the unsaved changes.",
   "PE.Controllers.Statusbar.zoomText": "Zoom {0}%",
   "PE.Controllers.Toolbar.confirmAddFontName": "The font you are going to save is not available on the current device.<br>The text style will be displayed using one of the system fonts, the saved font will be used when it is available.<br>Do you want to continue?",
   "PE.Controllers.Toolbar.textAccent": "Accents",
diff --git a/apps/spreadsheeteditor/main/app/controller/Main.js b/apps/spreadsheeteditor/main/app/controller/Main.js
index 1e5c0283a..f4bdcded5 100644
--- a/apps/spreadsheeteditor/main/app/controller/Main.js
+++ b/apps/spreadsheeteditor/main/app/controller/Main.js
@@ -552,8 +552,28 @@ define([
                 var me = this;
                 if ( !Common.Controllers.Desktop.process('goback') ) {
                     if (me.appOptions.customization.goback.requestClose && me.appOptions.canRequestClose) {
-                        Common.Gateway.requestClose();
-                        // Common.Controllers.Desktop.requestClose();
+                        if (this.api.asc_isDocumentModified()) {
+                            this.api.asc_stopSaving();
+                            Common.UI.warning({
+                                closable: false,
+                                width: 500,
+                                title: this.notcriticalErrorTitle,
+                                msg: this.leavePageTextOnClose,
+                                buttons: ['ok', 'cancel'],
+                                primary: 'ok',
+                                callback: function(btn) {
+                                    if (btn == 'ok') {
+                                        me.api.asc_undoAllChanges();
+                                        Common.Gateway.requestClose();
+                                        // Common.Controllers.Desktop.requestClose();
+                                    } else
+                                        me.api.asc_continueSaving();
+                                }
+                            });
+                        } else {
+                            Common.Gateway.requestClose();
+                            // Common.Controllers.Desktop.requestClose();
+                        }
                     } else {
                         var href = me.appOptions.customization.goback.url;
                         if (!current && me.appOptions.customization.goback.blank!==false) {
@@ -2837,7 +2857,8 @@ define([
             txtMonths: 'Months',
             txtQuarters: 'Quarters',
             txtYears: 'Years',
-            errorPivotGroup: 'Cannot group that selection.'
+            errorPivotGroup: 'Cannot group that selection.',
+            leavePageTextOnClose: 'All unsaved changes in this document will be lost.<br> Click \'Cancel\' then \'Save\' to save them. Click \'OK\' to discard all the unsaved changes.'
         }
     })(), SSE.Controllers.Main || {}))
 });
diff --git a/apps/spreadsheeteditor/main/locale/en.json b/apps/spreadsheeteditor/main/locale/en.json
index 4b33eae30..f928dafb9 100644
--- a/apps/spreadsheeteditor/main/locale/en.json
+++ b/apps/spreadsheeteditor/main/locale/en.json
@@ -921,6 +921,7 @@
   "SSE.Controllers.Main.warnNoLicense": "You've reached the limit for simultaneous connections to %1 editors. This document will be opened for viewing only.<br>Contact %1 sales team for personal upgrade terms.",
   "SSE.Controllers.Main.warnNoLicenseUsers": "You've reached the user limit for %1 editors. Contact %1 sales team for personal upgrade terms.",
   "SSE.Controllers.Main.warnProcessRightsChange": "You have been denied the right to edit the file.",
+  "SSE.Controllers.Main.leavePageTextOnClose": "All unsaved changes in this spreadsheet will be lost.<br> Click \"Cancel\" then \"Save\" to save them. Click \"OK\" to discard all the unsaved changes.",
   "SSE.Controllers.Print.strAllSheets": "All Sheets",
   "SSE.Controllers.Print.textFirstCol": "First column",
   "SSE.Controllers.Print.textFirstRow": "First row",