Ticket #724: gs_ajax_utils.js

File gs_ajax_utils.js, 3.0 KB (added by zcarter, 8 years ago)

Fixed newOpenTag. Fixed a coding style inconsistency.

Line 
1function loadAsync(sUri, SOAPMessage) {
2    var xmlHttp = XmlHttp.create();
3    var async = true;
4    xmlHttp.open("POST", sUri, async);
5    xmlHttp.onreadystatechange = function () {
6        if (xmlHttp.readyState == 4) {
7            var result = xmlHttp.responseText;
8            getTitle2(xmlHttp.responseXML, xmlHttp.responseText);
9        }     
10    }
11    xmlHttp.setRequestHeader("SOAPAction", " ");
12    xmlHttp.setRequestHeader("Content-Type", "Content-Type: text/xml; charset=utf-8");
13    xmlHttp.send(SOAPMessage);
14}
15
16function messageToSOAP(message) {
17    return [
18        '<?xml version="1.0" encoding="UTF-8"?>',
19        '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">',
20        '<soapenv:Body>',
21        message,
22        '</soapenv:Body>',
23        '</soapenv:Envelope>'
24    ].join('');
25}
26
27function getText(element) { // Get the text from possibly multiple text nodes
28    if (element.hasChildNodes()) {
29        tempString = [];
30        // TODO: Remove .length; slow.
31        for (var i = 0; i < element.childNodes.length; ++i) {
32            if (element.childNodes[i].nodeType == Node.TEXT_NODE ) { // =2
33                tempString.push(element.childNodes[i].nodeValue);
34            } else {
35                tempString.push('non text node: ');
36            }
37        }
38        return tempString.join('');
39    } else {
40        return 'getText: element has no ChildNodes from which to extract Text';
41    }
42}
43
44function newOpenTag(name) {
45    return ['<', name, '>'].join('');
46}
47
48function newCloseTag(name) {
49    return ['</', name, '>'].join('');
50}
51
52function newEmptyTag(name) {
53    return ['<', name, '/>'].join('');
54}
55
56function newElement(name, content) {
57    return ['<', name, '>', content, '</', name, '>'].join('');
58}
59
60function newElementAtt1(name, content, attName, attValue) {
61    return ['<', name, ' ', attName, '="', attValue, '">', content, '</', name,
62            '>'].join('');
63}
64
65function newElementAtt(name, content, nameArray, valueArray) {
66    e = ['<', name, ' '];
67    // TODO: Remove .length; slow.
68    for (var i = 0; i < nameArray.length; ++i) {
69        e.push(newAttribute(nameArray[i], valueArray[i]));
70    }
71    e.push('>', content, '</', name, '>';
72    return e.join('');
73}
74
75
76function newAttribute(name, value) {
77    return [' ', name, '="', value, '"'].join('');
78}
79
80// Tests?
81/*
82var a = [];
83var b = [];
84a[0] = 'title';
85b[0] = 'test';
86a[1] = 'title2';
87b[1] = 'test2';
88
89alert(newElement('message', 'some content', a=[], b=[]));
90*/
91
92function countElementChildren(node) {
93    count = 0;
94    childList = node.childNodes;
95    // TODO: Remove .length; slow.
96    for (var i = 0; i < childList.length; ++i) {
97        childNode = childList.item(i);
98        if (childNode.nodeType == 1) { // Only count elements
99            ++count;
100        }
101    }
102    return count;
103}
104
105function removeAllChildren(node) {
106    while (node.hasChildNodes()) {
107        node.removeChild(node.firstChild);
108    }
109}
110
111function isElement(node) {
112    return node.nodeType == 1;
113}