source: package_branches/invirt-web/cherrypy/code/static/ajaxterm/sarissa_dhtml.js @ 2485

Last change on this file since 2485 was 2427, checked in by quentin, 15 years ago

Import upstream ajaxterm 0.10

File size: 4.3 KB
Line 
1/**
2 * ====================================================================
3 * About
4 * ====================================================================
5 * Sarissa cross browser XML library - AJAX module
6 * @version 0.9.6.1
7 * @author: Copyright Manos Batsis, mailto: mbatsis at users full stop sourceforge full stop net
8 *
9 * This module contains some convinient AJAX tricks based on Sarissa
10 *
11 * ====================================================================
12 * Licence
13 * ====================================================================
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 or
16 * the GNU Lesser General Public License version 2.1 as published by
17 * the Free Software Foundation (your choice between the two).
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 * GNU General Public License or GNU Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * or GNU Lesser General Public License along with this program; if not,
26 * write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * or visit http://www.gnu.org
28 *
29 */
30/**
31 * Update an element with response of a GET request on the given URL.
32 * @addon
33 * @param sFromUrl the URL to make the request to
34 * @param oTargetElement the element to update
35 * @param xsltproc (optional) the transformer to use on the returned
36 *                  content before updating the target element with it
37 */
38Sarissa.updateContentFromURI = function(sFromUrl, oTargetElement, xsltproc) {
39    try{
40        oTargetElement.style.cursor = "wait";
41        var xmlhttp = new XMLHttpRequest();
42        xmlhttp.open("GET", sFromUrl);
43        function sarissa_dhtml_loadHandler() {
44            if (xmlhttp.readyState == 4) {
45                oTargetElement.style.cursor = "auto";
46                Sarissa.updateContentFromNode(xmlhttp.responseXML, oTargetElement, xsltproc);
47            };
48        };
49        xmlhttp.onreadystatechange = sarissa_dhtml_loadHandler;
50        xmlhttp.send(null);
51        oTargetElement.style.cursor = "auto";
52    }
53    catch(e){
54        oTargetElement.style.cursor = "auto";
55        throw e;
56    };
57};
58
59/**
60 * Update an element's content with the given DOM node.
61 * @addon
62 * @param sFromUrl the URL to make the request to
63 * @param oTargetElement the element to update
64 * @param xsltproc (optional) the transformer to use on the given
65 *                  DOM node before updating the target element with it
66 */
67Sarissa.updateContentFromNode = function(oNode, oTargetElement, xsltproc) {
68    try {
69        oTargetElement.style.cursor = "wait";
70        Sarissa.clearChildNodes(oTargetElement);
71        // check for parsing errors
72        var ownerDoc = oNode.nodeType == Node.DOCUMENT_NODE?oNode:oNode.ownerDocument;
73        if(ownerDoc.parseError && ownerDoc.parseError != 0) {
74            var pre = document.createElement("pre");
75            pre.appendChild(document.createTextNode(Sarissa.getParseErrorText(ownerDoc)));
76            oTargetElement.appendChild(pre);
77        }
78        else {
79            // transform if appropriate
80            if(xsltproc) {
81                oNode = xsltproc.transformToDocument(oNode);
82            };
83            // be smart, maybe the user wants to display the source instead
84            if(oTargetElement.tagName.toLowerCase == "textarea" || oTargetElement.tagName.toLowerCase == "input") {
85                oTargetElement.value = Sarissa.serialize(oNode);
86            }
87            else {
88                // ok that was not smart; it was paranoid. Keep up the good work by trying to use DOM instead of innerHTML
89                if(oNode.nodeType == Node.DOCUMENT_NODE || oNode.ownerDocument.documentElement == oNode) {
90                    oTargetElement.innerHTML = Sarissa.serialize(oNode);
91                }
92                else{
93                    oTargetElement.appendChild(oTargetElement.ownerDocument.importNode(oNode, true));
94                };
95            }; 
96        };
97    }
98    catch(e) {
99        throw e;
100    }
101    finally{
102        oTargetElement.style.cursor = "auto";
103    };
104};
105
Note: See TracBrowser for help on using the repository browser.