From 5f92d8cab30eeda60abc8300eecbb252bd6ecd95 Mon Sep 17 00:00:00 2001 From: AddisonDunn Date: Mon, 24 Jun 2024 14:22:09 -0400 Subject: [PATCH] only show warning if saved forms is turned off; also rename some methods --- .../static/cloudcare/js/formplayer/app.js | 38 +++++++++++-------- .../cloudcare/js/formplayer/menus/views.js | 3 ++ .../static/cloudcare/js/formplayer/router.js | 2 +- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/corehq/apps/cloudcare/static/cloudcare/js/formplayer/app.js b/corehq/apps/cloudcare/static/cloudcare/js/formplayer/app.js index fba86a9c8e0c3..ec7a2a39b598a 100644 --- a/corehq/apps/cloudcare/static/cloudcare/js/formplayer/app.js +++ b/corehq/apps/cloudcare/static/cloudcare/js/formplayer/app.js @@ -85,14 +85,14 @@ hqDefine("cloudcare/js/formplayer/app", [ return Backbone.Radio.channel('formplayer'); }; - FormplayerFrontend.userWantsToNavigateAwayFromForm = function () { - if (FormplayerFrontend.formInProgress) { + FormplayerFrontend.confirmUserWantsToNavigateAwayFromForm = function () { + if (FormplayerFrontend.unsavedFormInProgress) { const userConfirmedYes = window.confirm("You have a form in progress. Are you sure you want to navigate away?"); if (!userConfirmedYes) { return false; } } - FormplayerFrontend.trigger('setFormNotInProgress'); + FormplayerFrontend.trigger('setUnsavedFormNotInProgress'); return true; }; @@ -195,7 +195,7 @@ hqDefine("cloudcare/js/formplayer/app", [ FormplayerFrontend.on('startForm', function (data) { FormplayerFrontend.permitIntervalSync = false; - FormplayerFrontend.trigger('setFormInProgress'); + FormplayerFrontend.trigger('setUnsavedFormInProgress'); FormplayerFrontend.getChannel().request("clearMenu"); data.onLoading = CloudcareUtils.formplayerLoading; @@ -723,9 +723,6 @@ hqDefine("cloudcare/js/formplayer/app", [ // switches tab back from the application name document.title = gettext("Web Apps - CommCare HQ"); - if (!FormplayerFrontend.userWantsToNavigateAwayFromForm()) { - return; - } var urlObject = FormplayerUtils.currentUrlToObject(), appId, currentUser = UsersModels.getCurrentUser(); @@ -772,16 +769,27 @@ hqDefine("cloudcare/js/formplayer/app", [ }, }); - FormplayerFrontend.on("setFormInProgress", function () { - FormplayerFrontend.formInProgress = true; - window.onbeforeunload = function () { - return true; - }; + FormplayerFrontend.on("setUnsavedFormInProgress", function () { + const appId = FormplayerFrontend.getChannel().request('getCurrentAppId'); + var app = AppsAPI.getAppEntity(appId); + if (!app) { + return; + } + app = app.toJSON(); + const savedFormsSettingOff = (app.profile.properties || {})['cc-show-saved'] === 'no'; + if (savedFormsSettingOff) { + FormplayerFrontend.unsavedFormInProgress = true; + window.onbeforeunload = function () { + return true; + }; + } }); - FormplayerFrontend.on("setFormNotInProgress", function () { - FormplayerFrontend.formInProgress = false; - window.onbeforeunload = null; + FormplayerFrontend.on("setUnsavedFormNotInProgress", function () { + if (FormplayerFrontend.unsavedFormInProgress) { + FormplayerFrontend.unsavedFormInProgress = false; + window.onbeforeunload = null; + } }); return FormplayerFrontend; diff --git a/corehq/apps/cloudcare/static/cloudcare/js/formplayer/menus/views.js b/corehq/apps/cloudcare/static/cloudcare/js/formplayer/menus/views.js index 1e342ec36f7de..dc8b9f4953db1 100644 --- a/corehq/apps/cloudcare/static/cloudcare/js/formplayer/menus/views.js +++ b/corehq/apps/cloudcare/static/cloudcare/js/formplayer/menus/views.js @@ -1319,6 +1319,9 @@ hqDefine("cloudcare/js/formplayer/menus/views", [ 'keydown .js-home': 'onKeyActionHome', }, onClickHome: function () { + if (!FormplayerFrontend.confirmUserWantsToNavigateAwayFromForm()) { + return; + } FormplayerFrontend.trigger('navigateHome'); }, onKeyActionHome: function (e) { diff --git a/corehq/apps/cloudcare/static/cloudcare/js/formplayer/router.js b/corehq/apps/cloudcare/static/cloudcare/js/formplayer/router.js index 9341c9ab4b44f..1810464d6ad4c 100644 --- a/corehq/apps/cloudcare/static/cloudcare/js/formplayer/router.js +++ b/corehq/apps/cloudcare/static/cloudcare/js/formplayer/router.js @@ -275,7 +275,7 @@ hqDefine("cloudcare/js/formplayer/router", [ }); FormplayerFrontend.on("breadcrumbSelect", function (index) { - if (!FormplayerFrontend.userWantsToNavigateAwayFromForm()) { + if (!FormplayerFrontend.confirmUserWantsToNavigateAwayFromForm()) { return; } FormplayerFrontend.trigger("clearForm");