|
5 | 5 | $('input, select, textarea', form).each(function (i, el) {
|
6 | 6 | var field = $(el),
|
7 | 7 | name = field.attr('name'),
|
8 |
| - value = field.val(); |
| 8 | + value = field.val(), |
| 9 | + jsonValues = ['config', 'config-0-config', 'config-0-context', 'devicelocation-0-geometry']; |
9 | 10 | // ignore fields that have no name attribute, begin with "_" or "initial-"
|
10 | 11 | if (!name || name.substr(0, 1) == '_' || name.substr(0, 8) == 'initial-' ||
|
11 | 12 | // ignore hidden fields
|
|
24 | 25 | }
|
25 | 26 | // convert JSON string to Javascript object in order
|
26 | 27 | // to perform object comparison with `objectIsEqual`
|
27 |
| - if (name == 'config' || name == 'config-0-config' || name == 'config-0-context') { |
| 28 | + if (jsonValues.indexOf(name) > -1) { |
28 | 29 | try {
|
29 | 30 | object[name] = JSON.parse(value);
|
30 | 31 | }
|
|
33 | 34 | });
|
34 | 35 | };
|
35 | 36 |
|
36 |
| - var unsaved_changes = function (e) { |
| 37 | + var unsavedChanges = function (e) { |
37 | 38 | // get current values
|
38 |
| - var current_values = {}; |
39 |
| - mapValues(current_values); |
| 39 | + var currentValues = {}; |
| 40 | + mapValues(currentValues); |
40 | 41 | var changed = false,
|
41 | 42 | message = 'You haven\'t saved your changes yet!',
|
42 |
| - initialField, initialValue, |
| 43 | + initialValue, |
43 | 44 | name;
|
44 | 45 | if (gettext) { message = gettext(message); } // i18n if enabled
|
45 | 46 | // 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]; |
54 | 49 |
|
55 |
| - if (!objectIsEqual(initialValue, current_values[name])) { |
| 50 | + if (!objectIsEqual(initialValue, currentValues[name])) { |
56 | 51 | changed = true;
|
57 | 52 | break;
|
58 | 53 | }
|
|
94 | 89 | $(function ($) {
|
95 | 90 | if (!$('.submit-row').length) { return; }
|
96 | 91 | // 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 |
100 | 95 | $(form).submit(function () {
|
101 |
| - $(window).unbind('beforeunload', unsaved_changes); |
| 96 | + $(window).unbind('beforeunload', unsavedChanges); |
102 | 97 | });
|
103 | 98 | // bind unload event
|
104 |
| - $(window).bind('beforeunload', unsaved_changes); |
| 99 | + $(window).bind('beforeunload', unsavedChanges); |
105 | 100 | });
|
106 | 101 | }(django.jQuery));
|
0 commit comments