popup warning box if user tries to add instance while form has dirty data
diff --git a/xos/core/templates/slice_instance_tab.html b/xos/core/templates/slice_instance_tab.html
index d2aa3fc..abcfea4 100644
--- a/xos/core/templates/slice_instance_tab.html
+++ b/xos/core/templates/slice_instance_tab.html
@@ -1,15 +1,14 @@
{% if slice_id %}
-<a href="/admin/core/instance/add/?_to_field=id&slice={{ slice_id }}" class="add-another" id="add_instance_advanced"
-onclick="return showAddInstancePopup(this);">
+<a href="/admin/core/instance/add/?_to_field=id&slice={{ slice_id }}" id="add_instance_advanced"
+onclick="return addInstanceClicked(this);">
Add Instance
</a>
-<!-- div id="instance_advanced" style="display: node;" onchange="console.log('changed');" -->
<input type="hidden" id="instance_advanced" name="instance_advanced" onchange="console.log('changed');" value="initial">
<script>
-// ugly - poll for djange to change "instance_advanced", and it it does, then refresh the
+// ugly - poll for django to change "instance_advanced", and it it does, then refresh the
// page so the instance list gets updated.
var last_instance_advanced = $("#instance_advanced").val();
checkInstanceAdvanced = function() {
@@ -20,7 +19,7 @@
}
};
-setInterval(function() { checkInstanceAdvanced(); }, 1000);
+setInterval(function() { checkInstanceAdvanced(); }, 1000);
function showAddInstancePopup(triggeringLink) {
var name = triggeringLink.id.replace(/^add_/, '');
@@ -36,6 +35,44 @@
return false;
}
+function formIsDirty(form) {
+ console.log(form);
+ for (var i = 0; i < form.elements.length; i++) {
+ var element = form.elements[i];
+ var type = element.type;
+ if (type == "checkbox" || type == "radio") {
+ if (element.checked != element.defaultChecked) {
+ return true;
+ }
+ }
+ else if (type == "hidden" || type == "password" ||
+ type == "text" || type == "textarea") {
+ if (element.value != element.defaultValue) {
+ return true;
+ }
+ }
+ else if (type == "select-one" || type == "select-multiple") {
+ for (var j = 0; j < element.options.length; j++) {
+ if (element.options[j].selected !=
+ element.options[j].defaultSelected) {
+ return true;
+ }
+ }
+ }
+ }
+ return false;
+}
+
+function addInstanceClicked(link) {
+ if (formIsDirty($("#slice_form")[0])) {
+ console.log("dirty!!");
+ alert("Slice has modified fields. Please save the slice before adding instances.");
+ return false;
+ } else {
+ return showAddInstancePopup(link);
+ }
+}
+
</script>
{% else %}