1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/examples/Common/VerySimpleWithOpenID/Resources/scripts/XSLForms.js Sun Feb 03 20:05:15 2008 +0000
1.3 @@ -0,0 +1,178 @@
1.4 +// Area update functions.
1.5 +
1.6 +function requestUpdateArea(url, sourceAreasStr, targetName, targetAreasStr, elementPath) {
1.7 +
1.8 + var fieldNames = new Array();
1.9 + var targetFieldNames = new Array();
1.10 +
1.11 + for (var i = 0; i < document.forms.length; i++) {
1.12 + var form = document.forms[i];
1.13 + for (var j = 0; j < form.elements.length; j++) {
1.14 + var fieldName = form.elements[j].name;
1.15 + checkField(fieldName, sourceAreasStr, fieldNames);
1.16 + checkField(fieldName, targetAreasStr, targetFieldNames);
1.17 + }
1.18 + }
1.19 +
1.20 + return _requestUpdate(url, fieldNames, targetName, targetFieldNames, elementPath);
1.21 +}
1.22 +
1.23 +// Field list update functions.
1.24 +
1.25 +function requestUpdate(url, fieldNamesStr, targetName, targetFieldNamesStr, elementPath) {
1.26 +
1.27 + return _requestUpdate(url, fieldNamesStr.split(","), targetName, targetFieldNamesStr.split(","), elementPath);
1.28 +}
1.29 +
1.30 +// Internal functions.
1.31 +
1.32 +function checkField(fieldName, areasStr, areaFieldNames) {
1.33 +
1.34 + // Process each area name.
1.35 +
1.36 + var areaArray = areasStr.split(",");
1.37 + for (var i = 0; i < areaArray.length; i++) {
1.38 + var areaName = areaArray[i];
1.39 +
1.40 + // Skip empty area names (arising through empty elements in the CSV list).
1.41 +
1.42 + if (areaName == "") {
1.43 + continue;
1.44 + }
1.45 +
1.46 + if (fieldName.indexOf(areaName) == 0) {
1.47 + areaFieldNames.push(fieldName);
1.48 + }
1.49 + }
1.50 +}
1.51 +
1.52 +function _requestUpdate(url, fieldNames, targetName, targetFieldNames, elementPath) {
1.53 +
1.54 + // Note that XMLHttpRequest access may be denied if Mozilla believes that
1.55 + // this resource's URL and the supplied URL are different.
1.56 +
1.57 + var xmlhttp = new XMLHttpRequest();
1.58 + xmlhttp.open("POST", url, false);
1.59 +
1.60 + // Add the element path specification.
1.61 +
1.62 + var requestBody = ("element-path=" + elementPath);
1.63 +
1.64 + // Send the controlling field value.
1.65 +
1.66 + requestBody += addFields(fieldNames, false);
1.67 + requestBody += addFields(targetFieldNames, true);
1.68 +
1.69 + // Load the remote document with the given parameters sent as text in the request body.
1.70 +
1.71 + xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
1.72 + xmlhttp.send(requestBody);
1.73 +
1.74 + // Parse the result document.
1.75 +
1.76 + var newDocument = Sarissa.getDomDocument();
1.77 + newDocument = (new DOMParser()).parseFromString(xmlhttp.responseText, "text/xml");
1.78 +
1.79 + // Find the definition of the affected field in the result document.
1.80 +
1.81 + var newElement = newDocument.getElementById(targetName);
1.82 + var targetElement = document.getElementById(targetName);
1.83 +
1.84 + // Insert the new definition into the current document.
1.85 +
1.86 + if (newElement != null && targetElement != null) {
1.87 + var importedElement = document.importNode(newElement, true);
1.88 + targetElement.parentNode.replaceChild(importedElement, targetElement);
1.89 + //importedElement.setAttribute("style", "background-color:red;");
1.90 + }
1.91 +
1.92 + // NOTE: Test Konqueror bug.
1.93 +
1.94 + //showMismatch(targetFieldNames);
1.95 +
1.96 + return false;
1.97 +}
1.98 +
1.99 +function addFields(fieldNames, disable) {
1.100 +
1.101 + var requestBody = "";
1.102 +
1.103 + // Process each target field name.
1.104 + // Add the values of the dependent fields.
1.105 +
1.106 + for (var i = 0; i < fieldNames.length; i++) {
1.107 + var fieldName = fieldNames[i];
1.108 +
1.109 + // Skip empty field names (arising through empty elements in the CSV list).
1.110 +
1.111 + if (fieldName == "") {
1.112 + continue;
1.113 + }
1.114 +
1.115 + // Find the values of the target field.
1.116 +
1.117 + var fieldValue;
1.118 + var fieldNodes = document.getElementsByName(fieldName);
1.119 + for (var v = 0; v < fieldNodes.length; v++) {
1.120 +
1.121 + // Test for different field types.
1.122 +
1.123 + if (fieldNodes[v].options) {
1.124 + for (var opt = 0; opt < fieldNodes[v].options.length; opt++) {
1.125 + if (fieldNodes[v].options[opt].selected) {
1.126 + fieldValue = fieldNodes[v].options[opt].value;
1.127 + requestBody += ("&" + encodeURIComponent(fieldName) + "=" + encodeURIComponent(fieldValue));
1.128 + }
1.129 + }
1.130 + } else if (fieldNodes[v].type != 'checkbox' && fieldNodes[v].type != 'radio' || fieldNodes[v].checked) {
1.131 + fieldValue = fieldNodes[v].value;
1.132 + requestBody += ("&" + encodeURIComponent(fieldName) + "=" + encodeURIComponent(fieldValue));
1.133 + }
1.134 + }
1.135 +
1.136 + // NOTE: Konqueror hack: disable fields.
1.137 +
1.138 + if (disable) {
1.139 + disableFields(fieldName);
1.140 + }
1.141 + }
1.142 +
1.143 + return requestBody;
1.144 +}
1.145 +
1.146 +function disableFields(targetFieldName) {
1.147 +
1.148 + for (var i = 0; i < document.forms.length; i++) {
1.149 + var form = document.forms[i];
1.150 + for (var j = 0; j < form.elements.length; j++) {
1.151 + if (form.elements[j].name == targetFieldName) {
1.152 + form.elements[j].name = "";
1.153 + }
1.154 + }
1.155 + }
1.156 +}
1.157 +
1.158 +function showMismatch(targetFieldNameArray) {
1.159 +
1.160 + // Show how the number of field elements with a given name can be different
1.161 + // from the number known to the DOM Level 0 part of the API.
1.162 +
1.163 + for (var h = 0; h < targetFieldNameArray.length; h++) {
1.164 + var targetFieldName = targetFieldNameArray[h];
1.165 + var targetFieldNodes = document.getElementsByName(targetFieldName);
1.166 + alert("Nodes for " + targetFieldName + ": " + targetFieldNodes.length);
1.167 +
1.168 + var count = 0;
1.169 + for (var i = 0; i < document.forms.length; i++) {
1.170 + var form = document.forms[i];
1.171 + for (var j = 0; j < form.elements.length; j++) {
1.172 + if (form.elements[j].name == targetFieldName) {
1.173 + count++;
1.174 + }
1.175 + }
1.176 + }
1.177 + alert("Fields for " + targetFieldName + ": " + count);
1.178 + }
1.179 +}
1.180 +
1.181 +// vim: tabstop=4 expandtab shiftwidth=4