Skip to content

Commit

Permalink
only show warning if saved forms is turned off; also rename some methods
Browse files Browse the repository at this point in the history
  • Loading branch information
AddisonDunn committed Jun 24, 2024
1 parent 968c7ca commit 5f92d8c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
38 changes: 23 additions & 15 deletions corehq/apps/cloudcare/static/cloudcare/js/formplayer/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ hqDefine("cloudcare/js/formplayer/router", [
});

FormplayerFrontend.on("breadcrumbSelect", function (index) {
if (!FormplayerFrontend.userWantsToNavigateAwayFromForm()) {
if (!FormplayerFrontend.confirmUserWantsToNavigateAwayFromForm()) {
return;
}
FormplayerFrontend.trigger("clearForm");
Expand Down

0 comments on commit 5f92d8c

Please sign in to comment.