paulb@319 | 1 | // Area update functions. |
paulb@319 | 2 | |
paulb@319 | 3 | function requestUpdateArea(url, sourceAreasStr, targetName, targetAreasStr, elementPath) { |
paulb@319 | 4 | |
paulb@319 | 5 | var fieldNames = new Array(); |
paulb@319 | 6 | var targetFieldNames = new Array(); |
paulb@319 | 7 | |
paulb@319 | 8 | for (var i = 0; i < document.forms.length; i++) { |
paulb@319 | 9 | var form = document.forms[i]; |
paulb@319 | 10 | for (var j = 0; j < form.elements.length; j++) { |
paulb@319 | 11 | var fieldName = form.elements[j].name; |
paulb@319 | 12 | checkField(fieldName, sourceAreasStr, fieldNames); |
paulb@319 | 13 | checkField(fieldName, targetAreasStr, targetFieldNames); |
paulb@319 | 14 | } |
paulb@319 | 15 | } |
paulb@319 | 16 | |
paulb@319 | 17 | return _requestUpdate(url, fieldNames, targetName, targetFieldNames, elementPath); |
paulb@319 | 18 | } |
paulb@319 | 19 | |
paulb@319 | 20 | // Field list update functions. |
paulb@319 | 21 | |
paulb@319 | 22 | function requestUpdate(url, fieldNamesStr, targetName, targetFieldNamesStr, elementPath) { |
paulb@319 | 23 | |
paulb@319 | 24 | return _requestUpdate(url, fieldNamesStr.split(","), targetName, targetFieldNamesStr.split(","), elementPath); |
paulb@319 | 25 | } |
paulb@319 | 26 | |
paulb@319 | 27 | // Internal functions. |
paulb@319 | 28 | |
paulb@319 | 29 | function checkField(fieldName, areasStr, areaFieldNames) { |
paulb@319 | 30 | |
paulb@319 | 31 | // Process each area name. |
paulb@319 | 32 | |
paulb@319 | 33 | var areaArray = areasStr.split(","); |
paulb@319 | 34 | for (var i = 0; i < areaArray.length; i++) { |
paulb@319 | 35 | var areaName = areaArray[i]; |
paulb@319 | 36 | |
paulb@319 | 37 | // Skip empty area names (arising through empty elements in the CSV list). |
paulb@319 | 38 | |
paulb@319 | 39 | if (areaName == "") { |
paulb@319 | 40 | continue; |
paulb@319 | 41 | } |
paulb@319 | 42 | |
paulb@319 | 43 | if (fieldName.indexOf(areaName) == 0) { |
paulb@319 | 44 | areaFieldNames.push(fieldName); |
paulb@319 | 45 | } |
paulb@319 | 46 | } |
paulb@319 | 47 | } |
paulb@319 | 48 | |
paulb@319 | 49 | function _requestUpdate(url, fieldNames, targetName, targetFieldNames, elementPath) { |
paulb@75 | 50 | |
paulb@75 | 51 | // Note that XMLHttpRequest access may be denied if Mozilla believes that |
paulb@75 | 52 | // this resource's URL and the supplied URL are different. |
paulb@75 | 53 | |
paulb@75 | 54 | var xmlhttp = new XMLHttpRequest(); |
paulb@47 | 55 | xmlhttp.open("POST", url, false); |
paulb@56 | 56 | |
paulb@164 | 57 | // Add the element path specification. |
paulb@56 | 58 | |
paulb@164 | 59 | var requestBody = ("element-path=" + elementPath); |
paulb@56 | 60 | |
paulb@127 | 61 | // Send the controlling field value. |
paulb@127 | 62 | |
paulb@164 | 63 | requestBody += addFields(fieldNames, false); |
paulb@164 | 64 | requestBody += addFields(targetFieldNames, true); |
paulb@56 | 65 | |
paulb@56 | 66 | // Load the remote document with the given parameters sent as text in the request body. |
paulb@56 | 67 | |
paulb@193 | 68 | xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); |
paulb@60 | 69 | xmlhttp.send(requestBody); |
paulb@56 | 70 | |
paulb@56 | 71 | // Parse the result document. |
paulb@56 | 72 | |
paulb@44 | 73 | var newDocument = Sarissa.getDomDocument(); |
paulb@75 | 74 | newDocument = (new DOMParser()).parseFromString(xmlhttp.responseText, "text/xml"); |
paulb@56 | 75 | |
paulb@56 | 76 | // Find the definition of the affected field in the result document. |
paulb@56 | 77 | |
paulb@47 | 78 | var newElement = newDocument.getElementById(targetName); |
paulb@44 | 79 | var targetElement = document.getElementById(targetName); |
paulb@56 | 80 | |
paulb@56 | 81 | // Insert the new definition into the current document. |
paulb@44 | 82 | |
paulb@44 | 83 | if (newElement != null && targetElement != null) { |
paulb@44 | 84 | var importedElement = document.importNode(newElement, true); |
paulb@44 | 85 | targetElement.parentNode.replaceChild(importedElement, targetElement); |
paulb@44 | 86 | //importedElement.setAttribute("style", "background-color:red;"); |
paulb@44 | 87 | } |
paulb@127 | 88 | |
paulb@127 | 89 | // NOTE: Test Konqueror bug. |
paulb@127 | 90 | |
paulb@619 | 91 | //showMismatch(targetFieldNames); |
paulb@302 | 92 | |
paulb@302 | 93 | return false; |
paulb@44 | 94 | } |
paulb@127 | 95 | |
paulb@164 | 96 | function addFields(fieldNames, disable) { |
paulb@164 | 97 | |
paulb@164 | 98 | var requestBody = ""; |
paulb@164 | 99 | |
paulb@164 | 100 | // Process each target field name. |
paulb@164 | 101 | // Add the values of the dependent fields. |
paulb@164 | 102 | |
paulb@319 | 103 | for (var i = 0; i < fieldNames.length; i++) { |
paulb@319 | 104 | var fieldName = fieldNames[i]; |
paulb@164 | 105 | |
paulb@307 | 106 | // Skip empty field names (arising through empty elements in the CSV list). |
paulb@307 | 107 | |
paulb@307 | 108 | if (fieldName == "") { |
paulb@307 | 109 | continue; |
paulb@307 | 110 | } |
paulb@307 | 111 | |
paulb@164 | 112 | // Find the values of the target field. |
paulb@164 | 113 | |
paulb@164 | 114 | var fieldValue; |
paulb@164 | 115 | var fieldNodes = document.getElementsByName(fieldName); |
paulb@164 | 116 | for (var v = 0; v < fieldNodes.length; v++) { |
paulb@170 | 117 | |
paulb@170 | 118 | // Test for different field types. |
paulb@170 | 119 | |
paulb@170 | 120 | if (fieldNodes[v].options) { |
paulb@170 | 121 | for (var opt = 0; opt < fieldNodes[v].options.length; opt++) { |
paulb@170 | 122 | if (fieldNodes[v].options[opt].selected) { |
paulb@170 | 123 | fieldValue = fieldNodes[v].options[opt].value; |
paulb@200 | 124 | requestBody += ("&" + encodeURIComponent(fieldName) + "=" + encodeURIComponent(fieldValue)); |
paulb@170 | 125 | } |
paulb@170 | 126 | } |
paulb@421 | 127 | } else if (fieldNodes[v].type != 'checkbox' && fieldNodes[v].type != 'radio' || fieldNodes[v].checked) { |
paulb@170 | 128 | fieldValue = fieldNodes[v].value; |
paulb@200 | 129 | requestBody += ("&" + encodeURIComponent(fieldName) + "=" + encodeURIComponent(fieldValue)); |
paulb@170 | 130 | } |
paulb@164 | 131 | } |
paulb@164 | 132 | |
paulb@164 | 133 | // NOTE: Konqueror hack: disable fields. |
paulb@164 | 134 | |
paulb@164 | 135 | if (disable) { |
paulb@164 | 136 | disableFields(fieldName); |
paulb@164 | 137 | } |
paulb@164 | 138 | } |
paulb@164 | 139 | |
paulb@164 | 140 | return requestBody; |
paulb@164 | 141 | } |
paulb@164 | 142 | |
paulb@127 | 143 | function disableFields(targetFieldName) { |
paulb@127 | 144 | |
paulb@127 | 145 | for (var i = 0; i < document.forms.length; i++) { |
paulb@127 | 146 | var form = document.forms[i]; |
paulb@127 | 147 | for (var j = 0; j < form.elements.length; j++) { |
paulb@127 | 148 | if (form.elements[j].name == targetFieldName) { |
paulb@127 | 149 | form.elements[j].name = ""; |
paulb@127 | 150 | } |
paulb@127 | 151 | } |
paulb@127 | 152 | } |
paulb@127 | 153 | } |
paulb@127 | 154 | |
paulb@127 | 155 | function showMismatch(targetFieldNameArray) { |
paulb@127 | 156 | |
paulb@127 | 157 | // Show how the number of field elements with a given name can be different |
paulb@127 | 158 | // from the number known to the DOM Level 0 part of the API. |
paulb@127 | 159 | |
paulb@127 | 160 | for (var h = 0; h < targetFieldNameArray.length; h++) { |
paulb@127 | 161 | var targetFieldName = targetFieldNameArray[h]; |
paulb@127 | 162 | var targetFieldNodes = document.getElementsByName(targetFieldName); |
paulb@127 | 163 | alert("Nodes for " + targetFieldName + ": " + targetFieldNodes.length); |
paulb@127 | 164 | |
paulb@127 | 165 | var count = 0; |
paulb@127 | 166 | for (var i = 0; i < document.forms.length; i++) { |
paulb@127 | 167 | var form = document.forms[i]; |
paulb@127 | 168 | for (var j = 0; j < form.elements.length; j++) { |
paulb@127 | 169 | if (form.elements[j].name == targetFieldName) { |
paulb@127 | 170 | count++; |
paulb@127 | 171 | } |
paulb@127 | 172 | } |
paulb@127 | 173 | } |
paulb@127 | 174 | alert("Fields for " + targetFieldName + ": " + count); |
paulb@127 | 175 | } |
paulb@127 | 176 | } |
paulb@302 | 177 | |
paulb@302 | 178 | // vim: tabstop=4 expandtab shiftwidth=4 |