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@60 | 20 | xmlhttp.send(requestBody); |
paulb@56 | 21 | |
paulb@56 | 22 | // Parse the result document. |
paulb@56 | 23 | |
paulb@44 | 24 | var newDocument = Sarissa.getDomDocument(); |
paulb@75 | 25 | newDocument = (new DOMParser()).parseFromString(xmlhttp.responseText, "text/xml"); |
paulb@56 | 26 | |
paulb@56 | 27 | // Find the definition of the affected field in the result document. |
paulb@56 | 28 | |
paulb@47 | 29 | var newElement = newDocument.getElementById(targetName); |
paulb@44 | 30 | var targetElement = document.getElementById(targetName); |
paulb@56 | 31 | |
paulb@56 | 32 | // Insert the new definition into the current document. |
paulb@44 | 33 | |
paulb@44 | 34 | if (newElement != null && targetElement != null) { |
paulb@44 | 35 | var importedElement = document.importNode(newElement, true); |
paulb@44 | 36 | targetElement.parentNode.replaceChild(importedElement, targetElement); |
paulb@44 | 37 | //importedElement.setAttribute("style", "background-color:red;"); |
paulb@44 | 38 | } |
paulb@127 | 39 | |
paulb@127 | 40 | // NOTE: Test Konqueror bug. |
paulb@127 | 41 | |
paulb@127 | 42 | //showMismatch(targetFieldNameArray); |
paulb@44 | 43 | } |
paulb@127 | 44 | |
paulb@164 | 45 | function addFields(fieldNames, disable) { |
paulb@164 | 46 | |
paulb@170 | 47 | // If no field names exist do not attempt to collect their values. |
paulb@170 | 48 | |
paulb@170 | 49 | if (fieldNames == "") { |
paulb@170 | 50 | return ""; |
paulb@170 | 51 | } |
paulb@170 | 52 | |
paulb@164 | 53 | var requestBody = ""; |
paulb@164 | 54 | |
paulb@164 | 55 | // Process each target field name. |
paulb@164 | 56 | |
paulb@164 | 57 | var fieldNameArray = fieldNames.split(","); |
paulb@164 | 58 | |
paulb@164 | 59 | // Add the values of the dependent fields. |
paulb@164 | 60 | |
paulb@164 | 61 | for (var i = 0; i < fieldNameArray.length; i++) { |
paulb@164 | 62 | var fieldName = fieldNameArray[i]; |
paulb@164 | 63 | |
paulb@164 | 64 | // Find the values of the target field. |
paulb@164 | 65 | |
paulb@164 | 66 | var fieldValue; |
paulb@164 | 67 | var fieldNodes = document.getElementsByName(fieldName); |
paulb@164 | 68 | for (var v = 0; v < fieldNodes.length; v++) { |
paulb@170 | 69 | |
paulb@170 | 70 | // Test for different field types. |
paulb@170 | 71 | |
paulb@170 | 72 | if (fieldNodes[v].options) { |
paulb@170 | 73 | for (var opt = 0; opt < fieldNodes[v].options.length; opt++) { |
paulb@170 | 74 | if (fieldNodes[v].options[opt].selected) { |
paulb@170 | 75 | fieldValue = fieldNodes[v].options[opt].value; |
paulb@170 | 76 | requestBody += ("\r\n" + fieldName + "=" + fieldValue); |
paulb@170 | 77 | } |
paulb@170 | 78 | } |
paulb@170 | 79 | } else { |
paulb@170 | 80 | fieldValue = fieldNodes[v].value; |
paulb@170 | 81 | requestBody += ("\r\n" + fieldName + "=" + fieldValue); |
paulb@170 | 82 | } |
paulb@164 | 83 | } |
paulb@164 | 84 | |
paulb@164 | 85 | // NOTE: Konqueror hack: disable fields. |
paulb@164 | 86 | |
paulb@164 | 87 | if (disable) { |
paulb@164 | 88 | disableFields(fieldName); |
paulb@164 | 89 | } |
paulb@164 | 90 | } |
paulb@164 | 91 | |
paulb@164 | 92 | return requestBody; |
paulb@164 | 93 | } |
paulb@164 | 94 | |
paulb@127 | 95 | function disableFields(targetFieldName) { |
paulb@127 | 96 | |
paulb@127 | 97 | for (var i = 0; i < document.forms.length; i++) { |
paulb@127 | 98 | var form = document.forms[i]; |
paulb@127 | 99 | for (var j = 0; j < form.elements.length; j++) { |
paulb@127 | 100 | if (form.elements[j].name == targetFieldName) { |
paulb@127 | 101 | form.elements[j].name = ""; |
paulb@127 | 102 | } |
paulb@127 | 103 | } |
paulb@127 | 104 | } |
paulb@127 | 105 | } |
paulb@127 | 106 | |
paulb@127 | 107 | function showMismatch(targetFieldNameArray) { |
paulb@127 | 108 | |
paulb@127 | 109 | // Show how the number of field elements with a given name can be different |
paulb@127 | 110 | // from the number known to the DOM Level 0 part of the API. |
paulb@127 | 111 | |
paulb@127 | 112 | for (var h = 0; h < targetFieldNameArray.length; h++) { |
paulb@127 | 113 | var targetFieldName = targetFieldNameArray[h]; |
paulb@127 | 114 | var targetFieldNodes = document.getElementsByName(targetFieldName); |
paulb@127 | 115 | alert("Nodes for " + targetFieldName + ": " + targetFieldNodes.length); |
paulb@127 | 116 | |
paulb@127 | 117 | var count = 0; |
paulb@127 | 118 | for (var i = 0; i < document.forms.length; i++) { |
paulb@127 | 119 | var form = document.forms[i]; |
paulb@127 | 120 | for (var j = 0; j < form.elements.length; j++) { |
paulb@127 | 121 | if (form.elements[j].name == targetFieldName) { |
paulb@127 | 122 | count++; |
paulb@127 | 123 | } |
paulb@127 | 124 | } |
paulb@127 | 125 | } |
paulb@127 | 126 | alert("Fields for " + targetFieldName + ": " + count); |
paulb@127 | 127 | } |
paulb@127 | 128 | } |