|
1 | 1 | /**
|
2 |
| - * Updates rule description in the form of a proper sentence. |
| 2 | + * Include rule description in the form of a proper sentence in the extraction rules table. |
3 | 3 | */
|
4 |
| -updateRuleDescription = function( id ) { |
| 4 | +function updateRuleDescription(id) { |
5 | 5 | const id_prefix = "id_processingrule_set-" + id + "-";
|
6 |
| - const field = $("[id^=" + id_prefix + "field]").val(); |
7 |
| - const operator = $("[id^=" + id_prefix + "comparison_operator]").val(); |
8 |
| - const comp_value = $("[id^=" + id_prefix + "comparison_value]").val(); |
9 |
| - const repl_value = $("[id^=" + id_prefix + "replacement_value]").val(); |
| 6 | + const field = document.querySelector(`[id^="${id_prefix}field"]`)?.value; |
| 7 | + const operator = document.querySelector(`[id^="${id_prefix}comparison_operator"]`)?.value; |
| 8 | + const comp_value = document.querySelector(`[id^="${id_prefix}comparison_value"]`)?.value; |
| 9 | + const repl_value = document.querySelector(`[id^="${id_prefix}replacement_value"]`)?.value; |
10 | 10 |
|
11 | 11 | let msg = "";
|
12 |
| - if ( field !== "" ) { |
13 |
| - if ( operator === "" && field !== "" ){ |
| 12 | + if (field !== "") { |
| 13 | + if (operator === "" && field !== "") { |
14 | 14 | msg = "Keep field '" + field + "' in uploaded data.";
|
15 |
| - } else if ( operator === "regex-delete-match" ) { |
| 15 | + } else if (operator === "regex-delete-match") { |
16 | 16 | msg = "Delete parts of '" + field + "' field that match the following regex expression: '" + comp_value + "'.";
|
17 |
| - } else if ( operator === "regex-replace-match" ) { |
| 17 | + } else if (operator === "regex-replace-match") { |
18 | 18 | msg = "Replace parts of '" + field + "' field that match the regex expression '" + comp_value + "' with '" + repl_value + "'.";
|
19 |
| - } else if ( operator === "regex-delete-row" ) { |
| 19 | + } else if (operator === "regex-delete-row") { |
20 | 20 | msg = "Delete entry if '" + field + "' field value matches the following regex expression: '" + comp_value + "'.";
|
21 | 21 | } else {
|
22 | 22 | msg = "Delete row if current value of field '" + field + "' " + operator + " '" + comp_value + "'.";
|
23 | 23 | }
|
24 |
| - $("[id=step-description-" + id + "]").html(msg); |
| 24 | + |
| 25 | + const descriptionElement = document.querySelector(`[id=step-description-${id}]`); |
| 26 | + if (descriptionElement) { |
| 27 | + descriptionElement.textContent = msg; |
| 28 | + } |
25 | 29 | }
|
26 |
| -}; |
27 | 30 |
|
| 31 | +} |
28 | 32 |
|
29 | 33 | /**
|
30 |
| - * Updates the field value placeholders in the filter settings overview. |
| 34 | + * Updates the field value placeholders in the extraction rules table. |
31 | 35 | */
|
32 |
| -updateFieldValue = function( id ) { |
33 |
| - $("#configuration-" + id).find("input").each(function() { |
34 |
| - if (!$(this).is("button")) { |
35 |
| - let fieldName = $(this).attr("id").split("-").pop(); |
36 |
| - let targetId = "#" + fieldName + "-" + id; |
37 |
| - $( targetId ).html($(this).val()); |
| 36 | +function updateRuleTable(id) { |
| 37 | + let container = document.getElementById("configuration-" + id); |
| 38 | + let inputs = container.querySelectorAll("input"); |
| 39 | + |
| 40 | + inputs.forEach(function(input) { |
| 41 | + if (input.type !== "button") { |
| 42 | + let fieldName = input.id.split("-").pop(); |
| 43 | + let targetId = fieldName + "-" + id; |
| 44 | + let targetElement = document.getElementById(targetId); |
| 45 | + |
| 46 | + if (targetElement) { |
| 47 | + targetElement.textContent = input.value; |
| 48 | + } |
38 | 49 | }
|
39 | 50 | });
|
40 |
| -}; |
41 | 51 |
|
42 |
| -hideErrorMessages = function(id) { |
| 52 | + updateRuleDescription(id); |
| 53 | +} |
| 54 | + |
| 55 | +function hideErrorMessages(id) { |
43 | 56 | const independentFields = ["execution_order", "name", "field"];
|
44 | 57 | for (let i = 0; i < independentFields.length; i++) {
|
45 |
| - let curElement = $("#id_processingrule_set-" + id + "-" + independentFields[i]); |
46 |
| - curElement.siblings( ".form-error" ).hide(); |
| 58 | + const curElement = document.getElementById(`id_processingrule_set-${id}-${independentFields[i]}`); |
| 59 | + if (curElement) { |
| 60 | + const errorElements = curElement.parentElement.querySelectorAll(".form-error"); |
| 61 | + errorElements.forEach(function(errorElement) { |
| 62 | + errorElement.style.display = "none"; |
| 63 | + }); |
| 64 | + } |
47 | 65 | }
|
48 | 66 | }
|
49 | 67 |
|
50 |
| -checkNoFieldsMissing = function( id ) { |
| 68 | +function checkNoFieldsMissing(id) { |
51 | 69 | const independentFields = ["execution_order", "name", "field"];
|
52 | 70 |
|
53 | 71 | for (let i = 0; i < independentFields.length; i++) {
|
54 |
| - let val = $("[id$=" + id + "-" + independentFields[i] + "]").val(); |
| 72 | + const fieldElement = document.querySelector(`[id$="${id}-${independentFields[i]}"]`); |
| 73 | + const val = fieldElement ? fieldElement.value : ""; |
55 | 74 |
|
56 | 75 | if (val === "") {
|
57 |
| - let curElement = $("#id_processingrule_set-" + id + "-" + independentFields[i]); |
58 |
| - curElement.siblings( ".form-error" ).show(); |
59 |
| - return(false); |
| 76 | + const curElement = document.getElementById(`id_processingrule_set-${id}-${independentFields[i]}`); |
| 77 | + if (curElement) { |
| 78 | + const errorElements = curElement.parentElement.querySelectorAll(".form-error"); |
| 79 | + errorElements.forEach(function (errorElement) { |
| 80 | + errorElement.style.display = "block"; |
| 81 | + }); |
| 82 | + } |
| 83 | + return false; |
60 | 84 | }
|
61 | 85 | }
|
62 | 86 |
|
63 |
| - let comparisonOperator = $("#id_processingrule_set-" + id + "-comparison_operator"); |
64 |
| - if (comparisonOperator.val() !== "") { |
65 |
| - let comparisonValue = $("#id_processingrule_set-" + id + "-comparison_value").val(); |
66 |
| - if (comparisonValue === "") { |
67 |
| - let curElement = $("#id_processingrule_set-" + id + "-comparison_value"); |
68 |
| - curElement.siblings( ".form-error" ).show(); |
69 |
| - return(false); |
| 87 | + const comparisonOperator = document.getElementById(`id_processingrule_set-${id}-comparison_operator`); |
| 88 | + if (comparisonOperator && comparisonOperator.value !== "") { |
| 89 | + const comparisonValue = document.getElementById(`id_processingrule_set-${id}-comparison_value`); |
| 90 | + if (comparisonValue && comparisonValue.value === "") { |
| 91 | + const errorElements = comparisonValue.parentElement.querySelectorAll(".form-error"); |
| 92 | + errorElements.forEach(function (errorElement) { |
| 93 | + errorElement.style.display = "block"; |
| 94 | + }); |
| 95 | + return false; |
70 | 96 | }
|
71 | 97 | }
|
72 |
| - return(true); |
| 98 | + |
| 99 | + return true; |
73 | 100 | }
|
74 | 101 |
|
75 |
| -closeModal = function(id) { |
76 |
| - let modal = $("#configuration-" + id); |
77 |
| - modal.hide(); |
78 |
| - $("body").removeClass("modal-open").removeAttr("style"); |
79 |
| - $(".modal-backdrop").remove(); |
| 102 | +function closeModal(id) { |
| 103 | + const modal = document.getElementById(`configuration-${id}`); |
| 104 | + if (modal) { |
| 105 | + modal.style.display = "none"; |
| 106 | + } |
| 107 | + |
| 108 | + const body = document.body; |
| 109 | + body.classList.remove("modal-open"); |
| 110 | + body.removeAttribute("style"); |
| 111 | + |
| 112 | + const modalBackdrop = document.querySelectorAll(".modal-backdrop"); |
| 113 | + modalBackdrop.forEach(function (backdrop) { |
| 114 | + backdrop.remove(); |
| 115 | + }); |
80 | 116 | }
|
81 | 117 |
|
82 | 118 | /**
|
83 | 119 | * On OK-click in modal, update filter settings overview.
|
84 | 120 | */
|
85 |
| -$( "body" ).on("click", "button[class*='ddm-modal-ok']", function() { |
86 |
| - const current_id = $(this).attr("id").match(/\d+/)[0]; |
87 |
| - hideErrorMessages(current_id); |
88 |
| - if (checkNoFieldsMissing(current_id)) { |
89 |
| - updateRuleDescription(current_id); |
90 |
| - updateFieldValue(current_id); |
91 |
| - closeModal(current_id); |
| 121 | +document.body.addEventListener("click", function (event) { |
| 122 | + if (event.target.tagName === "BUTTON" && event.target.className.includes("ddm-modal-ok")) { |
| 123 | + const current_id = event.target.id.match(/\d+/)[0]; |
| 124 | + hideErrorMessages(current_id); |
| 125 | + if (checkNoFieldsMissing(current_id)) { |
| 126 | + updateRuleTable(current_id); |
| 127 | + closeModal(current_id); |
| 128 | + } |
92 | 129 | }
|
93 | 130 | });
|
94 | 131 |
|
95 |
| -$( "body" ).on("click", "button[class*='ddm-modal-cancel']", function() { |
96 |
| - const current_id = $(this).attr("id").match(/\d+/)[0]; |
97 |
| - let e = $("#execution_order-" + current_id); |
98 | 132 |
|
99 |
| - if ($.trim(e.text()) === 'None') { |
100 |
| - $("#configuration-" + current_id).remove(); |
101 |
| - $('#inlineform-table tr:last').remove(); |
| 133 | +document.body.addEventListener("click", function (event) { |
| 134 | + if (event.target.tagName === "BUTTON" && event.target.className.includes("ddm-modal-cancel")) { |
| 135 | + const current_id = event.target.id.match(/\d+/)[0]; |
| 136 | + const e = document.getElementById(`execution_order-${current_id}`); |
| 137 | + |
| 138 | + if (e && e.textContent.trim() === "None") { |
| 139 | + const configurationElement = document.getElementById(`configuration-${current_id}`); |
| 140 | + if (configurationElement) { |
| 141 | + configurationElement.remove(); |
| 142 | + } |
| 143 | + |
| 144 | + const lastRow = document.querySelector("#inlineform-table tr:last-child"); |
| 145 | + if (lastRow) { |
| 146 | + lastRow.remove(); |
| 147 | + } |
102 | 148 |
|
103 |
| - let totalFormElement = $("#id_processingrule_set-TOTAL_FORMS"); |
104 |
| - let currentFormN = totalFormElement.val(); |
105 |
| - totalFormElement.val(parseInt(currentFormN) - 1); |
| 149 | + const totalFormElement = document.getElementById("id_processingrule_set-TOTAL_FORMS"); |
| 150 | + if (totalFormElement) { |
| 151 | + const currentFormN = parseInt(totalFormElement.value, 10); |
| 152 | + totalFormElement.value = currentFormN - 1; |
| 153 | + } |
| 154 | + } |
| 155 | + closeModal(current_id); |
106 | 156 | }
|
107 |
| - closeModal(current_id); |
108 | 157 | });
|
109 | 158 |
|
110 | 159 |
|
111 |
| -$(document).ready(function() { |
112 |
| - let IDs = new Set(); |
113 |
| - $("[id^=id_processingrule_set-]").each(function() { |
114 |
| - if( /\d+/.test($( this ).attr("id")) ) { |
115 |
| - IDs.add($( this ).attr("id").match(/\d+/)[0]); |
| 160 | +document.addEventListener("DOMContentLoaded", function () { |
| 161 | + const IDs = new Set(); |
| 162 | + const elements = document.querySelectorAll("[id^=id_processingrule_set-]"); |
| 163 | + |
| 164 | + elements.forEach(function (element) { |
| 165 | + const idMatch = element.id.match(/\d+/); |
| 166 | + if (idMatch) { |
| 167 | + IDs.add(idMatch[0]); |
116 | 168 | }
|
117 | 169 | });
|
118 |
| - for ( const id of IDs ) { |
| 170 | + |
| 171 | + for (const id of IDs) { |
119 | 172 | updateRuleDescription(id);
|
120 | 173 | }
|
121 | 174 | });
|
|
0 commit comments