paulb@164 | 1 | function requestUpdate(url, fieldNames, targetName, targetFieldNames, elementPath) { |
paulb@75 | 2 | |
paulb@75 | 3 | // Note that XMLHttpRequest access may be denied if Mozilla believes that |
paulb@75 | 4 | // this resource's URL and the supplied URL are different. |
paulb@75 | 5 | |
paulb@75 | 6 | var xmlhttp = new XMLHttpRequest(); |
paulb@47 | 7 | xmlhttp.open("POST", url, false); |
paulb@56 | 8 | |
paulb@164 | 9 | // Add the element path specification. |
paulb@56 | 10 | |
paulb@164 | 11 | var requestBody = ("element-path=" + elementPath); |
paulb@56 | 12 | |
paulb@127 | 13 | // Send the controlling field value. |
paulb@127 | 14 | |
paulb@164 | 15 | requestBody += addFields(fieldNames, false); |
paulb@164 | 16 | requestBody += addFields(targetFieldNames, true); |
paulb@56 | 17 | |
paulb@56 | 18 | // Load the remote document with the given parameters sent as text in the request body. |
paulb@56 | 19 | |
paulb@193 | 20 | xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); |
paulb@60 | 21 | xmlhttp.send(requestBody); |
paulb@56 | 22 | |
paulb@56 | 23 | // Parse the result document. |
paulb@56 | 24 | |
paulb@44 | 25 | var newDocument = Sarissa.getDomDocument(); |
paulb@75 | 26 | newDocument = (new DOMParser()).parseFromString(xmlhttp.responseText, "text/xml"); |
paulb@56 | 27 | |
paulb@56 | 28 | // Find the definition of the affected field in the result document. |
paulb@56 | 29 | |
paulb@47 | 30 | var newElement = newDocument.getElementById(targetName); |
paulb@44 | 31 | var targetElement = document.getElementById(targetName); |
paulb@56 | 32 | |
paulb@56 | 33 | // Insert the new definition into the current document. |
paulb@44 | 34 | |
paulb@44 | 35 | if (newElement != null && targetElement != null) { |
paulb@44 | 36 | var importedElement = document.importNode(newElement, true); |
paulb@44 | 37 | targetElement.parentNode.replaceChild(importedElement, targetElement); |
paulb@44 | 38 | //importedElement.setAttribute("style", "background-color:red;"); |
paulb@44 | 39 | } |
paulb@127 | 40 | |
paulb@127 | 41 | // NOTE: Test Konqueror bug. |
paulb@127 | 42 | |
paulb@127 | 43 | //showMismatch(targetFieldNameArray); |
paulb@44 | 44 | } |
paulb@127 | 45 | |
paulb@164 | 46 | function addFields(fieldNames, disable) { |
paulb@164 | 47 | |
paulb@170 | 48 | // If no field names exist do not attempt to collect their values. |
paulb@170 | 49 | |
paulb@170 | 50 | if (fieldNames == "") { |
paulb@170 | 51 | return ""; |
paulb@170 | 52 | } |
paulb@170 | 53 | |
paulb@164 | 54 | var requestBody = ""; |
paulb@164 | 55 | |
paulb@164 | 56 | // Process each target field name. |
paulb@164 | 57 | |
paulb@164 | 58 | var fieldNameArray = fieldNames.split(","); |
paulb@164 | 59 | |
paulb@164 | 60 | // Add the values of the dependent fields. |
paulb@164 | 61 | |
paulb@164 | 62 | for (var i = 0; i < fieldNameArray.length; i++) { |
paulb@164 | 63 | var fieldName = fieldNameArray[i]; |
paulb@164 | 64 | |
paulb@164 | 65 | // Find the values of the target field. |
paulb@164 | 66 | |
paulb@164 | 67 | var fieldValue; |
paulb@164 | 68 | var fieldNodes = document.getElementsByName(fieldName); |
paulb@164 | 69 | for (var v = 0; v < fieldNodes.length; v++) { |
paulb@170 | 70 | |
paulb@170 | 71 | // Test for different field types. |
paulb@170 | 72 | |
paulb@170 | 73 | if (fieldNodes[v].options) { |
paulb@170 | 74 | for (var opt = 0; opt < fieldNodes[v].options.length; opt++) { |
paulb@170 | 75 | if (fieldNodes[v].options[opt].selected) { |
paulb@170 | 76 | fieldValue = fieldNodes[v].options[opt].value; |
paulb@193 | 77 | requestBody += ("&" + fieldName + "=" + fieldValue); |
paulb@170 | 78 | } |
paulb@170 | 79 | } |
paulb@170 | 80 | } else { |
paulb@170 | 81 | fieldValue = fieldNodes[v].value; |
paulb@193 | 82 | requestBody += ("&" + fieldName + "=" + fieldValue); |
paulb@170 | 83 | } |
paulb@164 | 84 | } |
paulb@164 | 85 | |
paulb@164 | 86 | // NOTE: Konqueror hack: disable fields. |
paulb@164 | 87 | |
paulb@164 | 88 | if (disable) { |
paulb@164 | 89 | disableFields(fieldName); |
paulb@164 | 90 | } |
paulb@164 | 91 | } |
paulb@164 | 92 | |
paulb@164 | 93 | return requestBody; |
paulb@164 | 94 | } |
paulb@164 | 95 | |
paulb@127 | 96 | function disableFields(targetFieldName) { |
paulb@127 | 97 | |
paulb@127 | 98 | for (var i = 0; i < document.forms.length; i++) { |
paulb@127 | 99 | var form = document.forms[i]; |
paulb@127 | 100 | for (var j = 0; j < form.elements.length; j++) { |
paulb@127 | 101 | if (form.elements[j].name == targetFieldName) { |
paulb@127 | 102 | form.elements[j].name = ""; |
paulb@127 | 103 | } |
paulb@127 | 104 | } |
paulb@127 | 105 | } |
paulb@127 | 106 | } |
paulb@127 | 107 | |
paulb@127 | 108 | function showMismatch(targetFieldNameArray) { |
paulb@127 | 109 | |
paulb@127 | 110 | // Show how the number of field elements with a given name can be different |
paulb@127 | 111 | // from the number known to the DOM Level 0 part of the API. |
paulb@127 | 112 | |
paulb@127 | 113 | for (var h = 0; h < targetFieldNameArray.length; h++) { |
paulb@127 | 114 | var targetFieldName = targetFieldNameArray[h]; |
paulb@127 | 115 | var targetFieldNodes = document.getElementsByName(targetFieldName); |
paulb@127 | 116 | alert("Nodes for " + targetFieldName + ": " + targetFieldNodes.length); |
paulb@127 | 117 | |
paulb@127 | 118 | var count = 0; |
paulb@127 | 119 | for (var i = 0; i < document.forms.length; i++) { |
paulb@127 | 120 | var form = document.forms[i]; |
paulb@127 | 121 | for (var j = 0; j < form.elements.length; j++) { |
paulb@127 | 122 | if (form.elements[j].name == targetFieldName) { |
paulb@127 | 123 | count++; |
paulb@127 | 124 | } |
paulb@127 | 125 | } |
paulb@127 | 126 | } |
paulb@127 | 127 | alert("Fields for " + targetFieldName + ": " + count); |
paulb@127 | 128 | } |
paulb@127 | 129 | } |