From fd417647b11b3541e35bb564f2a50666b26e7bcf Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Tue, 9 Apr 2019 12:00:11 +0300 Subject: [PATCH 1/2] Form extension for confirmation before closing the modal without saving in edit modals --- .../jquery/jquery-extensions.js | 32 +++++++++++++++++++ .../Localization/Resources/AbpUi/en.json | 3 +- .../Localization/Resources/AbpUi/tr.json | 3 +- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/jquery/jquery-extensions.js b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/jquery/jquery-extensions.js index 52ef81d536..17ee5a0355 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/jquery/jquery-extensions.js +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/jquery/jquery-extensions.js @@ -3,6 +3,10 @@ return; } + var localize = function (key) { + return abp.localization.getResource('AbpUi')(key); + }; + /* A simple jQuery plug-in to make a button busy. */ $.fn.buttonBusy = function (isBusy) { return $(this).each(function () { @@ -127,4 +131,32 @@ }); }; + $.fn.needConfirmationOnUnsavedClose = function () { + var $form = $(this); + var formSaved = false; + var unEditedForm = JSON.stringify($form.serializeFormToObject()); + + var modal = $(this).find('.modal'); + + $(modal).on("hide.bs.modal", function (e) { + var currentForm = JSON.stringify($form.serializeFormToObject()); + var thereAreUnsavedChanges = currentForm !== unEditedForm; + + if (!formSaved && thereAreUnsavedChanges) { + e.preventDefault(); + abp.message.confirm(localize('AreYouSureYouWantToCancelEditingWarningMessage'), + function (result) { + if (result) { + formSaved = true; + $(modal).modal('toggle'); + } + }); + } + }); + + $(this).bind('ajax:complete',function () { + formSaved = true; + }); + }; + })(jQuery); \ No newline at end of file diff --git a/framework/src/Volo.Abp.UI/Localization/Resources/AbpUi/en.json b/framework/src/Volo.Abp.UI/Localization/Resources/AbpUi/en.json index 9eb47477f4..20b521c7fa 100644 --- a/framework/src/Volo.Abp.UI/Localization/Resources/AbpUi/en.json +++ b/framework/src/Volo.Abp.UI/Localization/Resources/AbpUi/en.json @@ -45,6 +45,7 @@ "PagerShowMenuEntries": "Show _MENU_ entries", "DatatableActionDropdownDefaultText": "Actions", "ChangePassword": "Change password", - "PersonalInfo": "My profile" + "PersonalInfo": "My profile", + "AreYouSureYouWantToCancelEditingWarningMessage": "Your changes will be lost." } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.UI/Localization/Resources/AbpUi/tr.json b/framework/src/Volo.Abp.UI/Localization/Resources/AbpUi/tr.json index d45c353878..796c3d0ced 100644 --- a/framework/src/Volo.Abp.UI/Localization/Resources/AbpUi/tr.json +++ b/framework/src/Volo.Abp.UI/Localization/Resources/AbpUi/tr.json @@ -45,6 +45,7 @@ "PagerShowMenuEntries": "Sayfada _MENU_ kayıt göster.", "DatatableActionDropdownDefaultText": "İşlemler", "ChangePassword": "Şifre değiştir", - "PersonalInfo": "Profilim" + "PersonalInfo": "Profilim", + "AreYouSureYouWantToCancelEditingWarningMessage": "Yaptığınız değişiklikler kaybolacak." } } \ No newline at end of file From c9366774a4fe8f63df712019ad9c42a577c94fa5 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Wed, 10 Apr 2019 11:20:41 +0300 Subject: [PATCH 2/2] needConfirmationOnUnsavedClose is now default for all forms inside a modal --- .../bootstrap/modal-manager.js | 4 ++++ .../jquery/jquery-extensions.js | 10 ++++------ .../Volo.Abp.UI/Localization/Resources/AbpUi/en.json | 2 +- .../Volo.Abp.UI/Localization/Resources/AbpUi/tr.json | 2 +- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/bootstrap/modal-manager.js b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/bootstrap/modal-manager.js index e327bd79ed..5d55837b84 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/bootstrap/modal-manager.js +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/bootstrap/modal-manager.js @@ -69,6 +69,10 @@ $.validator.defaults.ignore = ''; //TODO: Would be better if we can apply only f //TODO: data-ajaxForm comparison seems wrong! if (_$form.attr('data-ajaxForm') === undefined || _$form.attr('data-ajaxForm') === false) { _$form.abpAjaxForm(); + } + + if (_$form.attr('data-check-form-on-close') === undefined || _$form.attr('data-check-form-on-close') != 'false') { + _$form.needConfirmationOnUnsavedClose(_$modal); } _$form.on('abp-ajax-success', diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/jquery/jquery-extensions.js b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/jquery/jquery-extensions.js index 17ee5a0355..c9d11b5fac 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/jquery/jquery-extensions.js +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/jquery/jquery-extensions.js @@ -131,14 +131,12 @@ }); }; - $.fn.needConfirmationOnUnsavedClose = function () { + $.fn.needConfirmationOnUnsavedClose = function ($modal) { var $form = $(this); var formSaved = false; var unEditedForm = JSON.stringify($form.serializeFormToObject()); - var modal = $(this).find('.modal'); - - $(modal).on("hide.bs.modal", function (e) { + $modal.on("hide.bs.modal", function (e) { var currentForm = JSON.stringify($form.serializeFormToObject()); var thereAreUnsavedChanges = currentForm !== unEditedForm; @@ -148,13 +146,13 @@ function (result) { if (result) { formSaved = true; - $(modal).modal('toggle'); + $modal.modal('hide'); } }); } }); - $(this).bind('ajax:complete',function () { + $(this).on('abp-ajax-success',function () { formSaved = true; }); }; diff --git a/framework/src/Volo.Abp.UI/Localization/Resources/AbpUi/en.json b/framework/src/Volo.Abp.UI/Localization/Resources/AbpUi/en.json index 20b521c7fa..7aafe07612 100644 --- a/framework/src/Volo.Abp.UI/Localization/Resources/AbpUi/en.json +++ b/framework/src/Volo.Abp.UI/Localization/Resources/AbpUi/en.json @@ -46,6 +46,6 @@ "DatatableActionDropdownDefaultText": "Actions", "ChangePassword": "Change password", "PersonalInfo": "My profile", - "AreYouSureYouWantToCancelEditingWarningMessage": "Your changes will be lost." + "AreYouSureYouWantToCancelEditingWarningMessage": "You have unsaved changes." } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.UI/Localization/Resources/AbpUi/tr.json b/framework/src/Volo.Abp.UI/Localization/Resources/AbpUi/tr.json index 796c3d0ced..5b236f8954 100644 --- a/framework/src/Volo.Abp.UI/Localization/Resources/AbpUi/tr.json +++ b/framework/src/Volo.Abp.UI/Localization/Resources/AbpUi/tr.json @@ -46,6 +46,6 @@ "DatatableActionDropdownDefaultText": "İşlemler", "ChangePassword": "Şifre değiştir", "PersonalInfo": "Profilim", - "AreYouSureYouWantToCancelEditingWarningMessage": "Yaptığınız değişiklikler kaybolacak." + "AreYouSureYouWantToCancelEditingWarningMessage": "Kaydedilmemiş değişiklikler var." } } \ No newline at end of file