Skip to content

Commit e188dfc

Browse files
nemesifierpandafy
authored andcommitted
[fix] Fixed unsaved_changes.js when location is present #388
Location geometry was not being parsed from JSON and hence the geometry JSON representation had some slight differences which where triggering the unsaved changes alert. I took advantage to simplify the code and to fix the non camelcase functions and variables in this part of the code. I also updated a global variable name strating with _njc (netjsonconfig) to owc (openwisp-controller). Fixes #388
1 parent 3a66124 commit e188dfc

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

openwisp_controller/config/static/config/js/unsaved_changes.js

+15-20
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
$('input, select, textarea', form).each(function (i, el) {
66
var field = $(el),
77
name = field.attr('name'),
8-
value = field.val();
8+
value = field.val(),
9+
jsonValues = ['config', 'config-0-config', 'config-0-context', 'devicelocation-0-geometry'];
910
// ignore fields that have no name attribute, begin with "_" or "initial-"
1011
if (!name || name.substr(0, 1) == '_' || name.substr(0, 8) == 'initial-' ||
1112
// ignore hidden fields
@@ -24,7 +25,7 @@
2425
}
2526
// convert JSON string to Javascript object in order
2627
// to perform object comparison with `objectIsEqual`
27-
if (name == 'config' || name == 'config-0-config' || name == 'config-0-context') {
28+
if (jsonValues.indexOf(name) > -1) {
2829
try {
2930
object[name] = JSON.parse(value);
3031
}
@@ -33,26 +34,20 @@
3334
});
3435
};
3536

36-
var unsaved_changes = function (e) {
37+
var unsavedChanges = function (e) {
3738
// get current values
38-
var current_values = {};
39-
mapValues(current_values);
39+
var currentValues = {};
40+
mapValues(currentValues);
4041
var changed = false,
4142
message = 'You haven\'t saved your changes yet!',
42-
initialField, initialValue,
43+
initialValue,
4344
name;
4445
if (gettext) { message = gettext(message); } // i18n if enabled
4546
// compare initial with current values
46-
for (name in django._njc_initial_values) {
47-
// use initial values from initial fields if present
48-
initialField = $('#initial-id_' + name);
49-
initialValue = initialField.length ? initialField.val() : django._njc_initial_values[name];
50-
// fix checkbox value inconsistency
51-
if (initialValue == 'True') { initialValue = true; }
52-
else if (initialValue == 'False') { initialValue = false; }
53-
if (name == 'config') { initialValue = JSON.parse(initialValue); }
47+
for (name in django._owcInitialValues) {
48+
initialValue = django._owcInitialValues[name];
5449

55-
if (!objectIsEqual(initialValue, current_values[name])) {
50+
if (!objectIsEqual(initialValue, currentValues[name])) {
5651
changed = true;
5752
break;
5853
}
@@ -94,13 +89,13 @@
9489
$(function ($) {
9590
if (!$('.submit-row').length) { return; }
9691
// populate initial map of form values
97-
django._njc_initial_values = {};
98-
mapValues(django._njc_initial_values);
99-
// do not perform unsaved_changes if submitting form
92+
django._owcInitialValues = {};
93+
mapValues(django._owcInitialValues);
94+
// do not perform unsavedChanges if submitting form
10095
$(form).submit(function () {
101-
$(window).unbind('beforeunload', unsaved_changes);
96+
$(window).unbind('beforeunload', unsavedChanges);
10297
});
10398
// bind unload event
104-
$(window).bind('beforeunload', unsaved_changes);
99+
$(window).bind('beforeunload', unsavedChanges);
105100
});
106101
}(django.jQuery));

openwisp_controller/config/static/config/js/widget.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
});
3737

3838
if (isLoading) {
39-
django._njc_initial_values['config-0-context'] = removeDefaultValues(
39+
django._owcInitialValues['config-0-context'] = removeDefaultValues(
4040
contextValue,
4141
defaultValues
4242
);

0 commit comments

Comments
 (0)