Skip to content

Commit 364afe2

Browse files
committed
Checking for unsaved changes when leaving demographics, any LBF visit form or layout editor.
1 parent 4eb6170 commit 364afe2

3 files changed

Lines changed: 109 additions & 12 deletions

File tree

interface/forms/LBF/new.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright (C) 2009-2014 Rod Roark <rod@sunsetsystems.com>
3+
* Copyright (C) 2009-2016 Rod Roark <rod@sunsetsystems.com>
44
*
55
* LICENSE: This program is free software; you can redistribute it and/or
66
* modify it under the terms of the GNU General Public License
@@ -256,7 +256,12 @@ function end_group() {
256256
<?php include_once("{$GLOBALS['srcdir']}/options.js.php"); ?>
257257

258258
<script language="JavaScript">
259+
260+
// Support for beforeunload handler.
261+
var somethingChanged = false;
262+
259263
$(document).ready(function() {
264+
260265
// fancy box
261266
if (window.enable_modals) {
262267
enable_modals();
@@ -278,6 +283,19 @@ function end_group() {
278283
// add drag and drop functionality to fancybox
279284
$("#fancy_outer").easydrag();
280285
});
286+
287+
// Support for beforeunload handler.
288+
$('.lbfdata input, .lbfdata select, .lbfdata textarea').change(function() {
289+
somethingChanged = true;
290+
});
291+
window.addEventListener("beforeunload", function (e) {
292+
if (somethingChanged) {
293+
var msg = "<?php echo xls('You have unsaved changes.'); ?>";
294+
e.returnValue = msg; // Gecko, Trident, Chrome 34+
295+
return msg; // Gecko, WebKit, Chrome <34
296+
}
297+
});
298+
281299
});
282300

283301
var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
@@ -340,12 +358,24 @@ function sel_related(elem, codetype) {
340358
dlgopen(url, '_blank', 500, 400);
341359
}
342360

361+
// Compute the length of a string without leading and trailing spaces.
362+
function trimlen(s) {
363+
var i = 0;
364+
var j = s.length - 1;
365+
for (; i <= j && s.charAt(i) == ' '; ++i);
366+
for (; i <= j && s.charAt(j) == ' '; --j);
367+
if (i > j) return 0;
368+
return j + 1 - i;
369+
}
370+
343371
// Validation logic for form submission.
344372
function validate(f) {
345373
<?php generate_layout_validation($formname); ?>
374+
somethingChanged = false; // turn off "are you sure you want to leave"
346375
top.restoreSession();
347376
return true;
348377
}
378+
349379
<?php if (function_exists($formname . '_javascript')) call_user_func($formname . '_javascript'); ?>
350380

351381
</script>
@@ -498,7 +528,7 @@ function validate(f) {
498528
echo "<div id='div_" . attr($group_seq) . "' class='section' style='display:" . attr($display_style) . ";'>\n";
499529
}
500530
// echo " <table border='0' cellpadding='0' width='100%'>\n";
501-
echo " <table border='0' cellspacing='0' cellpadding='0' width='100%'>\n";
531+
echo " <table border='0' cellspacing='0' cellpadding='0' width='100%' class='lbfdata'>\n";
502532
$display_style = 'none';
503533

504534
// Initialize historical data array and write date headers.

interface/patient_file/summary/demographics_full.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@
7171
<link rel="stylesheet" type="text/css" href="../../../library/js/fancybox/jquery.fancybox-1.2.6.css" media="screen" />
7272

7373
<script type="text/javascript">
74+
75+
// Support for beforeunload handler.
76+
var somethingChanged = false;
77+
7478
$(document).ready(function(){
7579
tabbify();
7680
enable_modals();
@@ -83,6 +87,17 @@
8387
'frameWidth' : 650
8488
});
8589

90+
// Support for beforeunload handler.
91+
$('.tab input, .tab select, .tab textarea').change(function() {
92+
somethingChanged = true;
93+
});
94+
window.addEventListener("beforeunload", function (e) {
95+
if (somethingChanged) {
96+
var msg = "<?php echo xls('You have unsaved changes.'); ?>";
97+
e.returnValue = msg; // Gecko, Trident, Chrome 34+
98+
return msg; // Gecko, WebKit, Chrome <34
99+
}
100+
});
86101
});
87102

88103
var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
@@ -311,6 +326,7 @@ function validate(f) {
311326
function submitme() {
312327
var f = document.forms[0];
313328
if (validate(f)) {
329+
somethingChanged = false; // turn off "are you sure you want to leave"
314330
top.restoreSession();
315331
f.submit();
316332
}

interface/super/edit_layout.php

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright (C) 2014-2015 Rod Roark <rod@sunsetsystems.com>
3+
* Copyright (C) 2014-2016 Rod Roark <rod@sunsetsystems.com>
44
*
55
* LICENSE: This program is free software; you can redistribute it and/or
66
* modify it under the terms of the GNU General Public License
@@ -926,6 +926,26 @@ function cidChanged(lino, seq) {
926926
setListItemOptions(lino, seq, false);
927927
}
928928

929+
// Call this to disable the warning about unsaved changes and submit the form.
930+
function mySubmit() {
931+
somethingChanged = false;
932+
top.restoreSession();
933+
document.forms[0].submit();
934+
}
935+
936+
// User is about to do something that would discard any unsaved changes.
937+
// Return true if that is OK.
938+
function myChangeCheck() {
939+
if (somethingChanged) {
940+
if (!confirm('<?php echo xls('You have unsaved changes. Abandon them?'); ?>')) {
941+
return false;
942+
}
943+
// Do not set somethingChanged to false here because if they cancel the
944+
// action then the previously changed values will still be of interest.
945+
}
946+
return true;
947+
}
948+
929949
</script>
930950

931951
</head>
@@ -1238,6 +1258,9 @@ function cidChanged(lino, seq) {
12381258
// used when selecting a list-name for a field
12391259
var selectedfield;
12401260

1261+
// Support for beforeunload handler.
1262+
var somethingChanged = false;
1263+
12411264
// Get the next logical sequence number for a field in the specified group.
12421265
// Note it guesses and uses the existing increment value.
12431266
function getNextSeq(group) {
@@ -1261,7 +1284,13 @@ function getNextSeq(group) {
12611284

12621285
$(document).ready(function(){
12631286
$("#save").click(function() { SaveChanges(); });
1264-
$("#layout_id").change(function() { $('#theform').submit(); });
1287+
$("#layout_id").change(function() {
1288+
if (!myChangeCheck()) {
1289+
$("#layout_id").val("<?php echo $layout_id; ?>");
1290+
return;
1291+
}
1292+
mySubmit();
1293+
});
12651294

12661295
$(".addgroup").click(function() { AddGroup(this); });
12671296
$(".savenewgroup").click(function() { SaveNewGroup(this); });
@@ -1304,7 +1333,7 @@ function getNextSeq(group) {
13041333
// Save the changes made to the form
13051334
var SaveChanges = function () {
13061335
$("#formaction").val("save");
1307-
$("#theform").submit();
1336+
mySubmit();
13081337
}
13091338

13101339
/****************************************************/
@@ -1313,6 +1342,8 @@ function getNextSeq(group) {
13131342

13141343
// display the 'new group' DIV
13151344
var AddGroup = function(btnObj) {
1345+
if (!myChangeCheck()) return;
1346+
$("#save").attr("disabled", true);
13161347
// show the field details DIV
13171348
$('#groupdetail').css('visibility', 'visible');
13181349
$('#groupdetail').css('display', 'block');
@@ -1381,7 +1412,7 @@ function getNextSeq(group) {
13811412

13821413
// submit the form to add a new field to a specific group
13831414
$("#formaction").val("addgroup");
1384-
$("#theform").submit();
1415+
mySubmit();
13851416
}
13861417

13871418
// actually delete an entire group from the database
@@ -1403,24 +1434,27 @@ function getNextSeq(group) {
14031434
$('#groupdetail').css('display', 'none');
14041435
// reset the new group values to a default
14051436
$('#groupdetail > #newgroupname').val("");
1437+
$("#save").attr("disabled", false);
14061438
};
14071439

14081440
// display the 'new field' DIV
14091441
var MoveGroup = function(btnObj) {
1442+
if (!myChangeCheck()) return;
14101443
var btnid = $(btnObj).attr("id");
14111444
var parts = btnid.split("~");
14121445
var groupid = parts[0];
14131446
var direction = parts[1];
1414-
14151447
// submit the form to change group order
14161448
$("#formaction").val("movegroup");
14171449
$("#movegroupname").val(groupid);
14181450
$("#movedirection").val(direction);
1419-
$("#theform").submit();
1451+
mySubmit();
14201452
}
14211453

14221454
// show the rename group DIV
14231455
var RenameGroup = function(btnObj) {
1456+
if (!myChangeCheck()) return;
1457+
$("#save").attr("disabled", true);
14241458
$('#renamegroupdetail').css('visibility', 'visible');
14251459
$('#renamegroupdetail').css('display', 'block');
14261460
$(btnObj).parent().append($("#renamegroupdetail"));
@@ -1441,7 +1475,7 @@ function getNextSeq(group) {
14411475

14421476
// submit the form to add a new field to a specific group
14431477
$("#formaction").val("renamegroup");
1444-
$("#theform").submit();
1478+
mySubmit();
14451479
}
14461480

14471481
// just hide the new field DIV
@@ -1460,6 +1494,8 @@ function getNextSeq(group) {
14601494

14611495
// display the 'new field' DIV
14621496
var AddField = function(btnObj) {
1497+
if (!myChangeCheck()) return;
1498+
$("#save").attr("disabled", true);
14631499
// update the fieldgroup value to be the groupid
14641500
var btnid = $(btnObj).attr("id");
14651501
var parts = btnid.split("~");
@@ -1474,6 +1510,7 @@ function getNextSeq(group) {
14741510
};
14751511

14761512
var DeleteFields = function(btnObj) {
1513+
if (!myChangeCheck()) return;
14771514
if (confirm("<?php xl('WARNING','e','',' - ') . xl('This action cannot be undone.','e','','\n') . xl('Are you sure you wish to delete the selected fields?','e'); ?>")) {
14781515
var delim = "";
14791516
$(".selectfield").each(function(i) {
@@ -1487,7 +1524,7 @@ function getNextSeq(group) {
14871524
});
14881525
// submit the form to delete the field(s)
14891526
$("#formaction").val("deletefields");
1490-
$("#theform").submit();
1527+
mySubmit();
14911528
}
14921529
};
14931530

@@ -1532,7 +1569,7 @@ function getNextSeq(group) {
15321569

15331570
// submit the form to add a new field to a specific group
15341571
$("#formaction").val("addfield");
1535-
$("#theform").submit();
1572+
mySubmit();
15361573
};
15371574

15381575
// just hide the new field DIV
@@ -1542,6 +1579,7 @@ function getNextSeq(group) {
15421579
$('#fielddetail').css('display', 'none');
15431580
// reset the new field values to a default
15441581
ResetNewFieldValues();
1582+
$("#save").attr("disabled", false);
15451583
};
15461584

15471585
// show the popup choice of lists
@@ -1552,6 +1590,7 @@ function getNextSeq(group) {
15521590

15531591
// show the popup choice of groups
15541592
var ShowGroups = function(btnObj) {
1593+
if (!myChangeCheck()) return;
15551594
window.open("./show_groups_popup.php?layout_id=<?php echo $layout_id;?>", "groups", "width=300,height=300,scrollbars=yes");
15561595
};
15571596

@@ -1585,6 +1624,18 @@ function getNextSeq(group) {
15851624
}
15861625
}
15871626

1627+
// Support for beforeunload handler.
1628+
$('tbody input, tbody select, tbody textarea').not('.selectfield').change(function() {
1629+
somethingChanged = true;
1630+
});
1631+
window.addEventListener("beforeunload", function (e) {
1632+
if (somethingChanged) {
1633+
var msg = "<?php echo xls('You have unsaved changes.'); ?>";
1634+
e.returnValue = msg; // Gecko, Trident, Chrome 34+
1635+
return msg; // Gecko, WebKit, Chrome <34
1636+
}
1637+
});
1638+
15881639
});
15891640

15901641
function NationNotesContext(lineitem,val){
@@ -1672,7 +1723,7 @@ function MoveFields(targetgroup) {
16721723
}
16731724
});
16741725
$("#formaction").val("movefields");
1675-
$("#theform").submit();
1726+
mySubmit();
16761727
};
16771728

16781729
// set the new-field values to a default state

0 commit comments

Comments
 (0)