XSLTools

examples/Common/VerySimpleWithOpenID/Resources/scripts/XSLForms.js

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