]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - themes/default/ajax.js
DynamicIncludePage and AJAX support
[SourceForge/phpwiki.git] / themes / default / ajax.js
1 // Ajax Javascript support functions, based on moacdropdown
2 // $Id: ajax.js 6521 2009-02-20 16:20:24Z vargenau $
3
4 function showHide( id ) {
5     this.init( id )
6 }
7
8 showHide.prototype.onXmlHttpLoad = function( ) {
9     if( this.hXMLHttp.readyState == 4 ) {
10         var hError = this.hXMLHttp.parseError;
11         var img = document.getElementById(this.id+'-img');
12         if( hError && hError.errorCode != 0 ) {
13             alert( hError.reason );
14         } else {
15             // insert external, same-domain XML tree into id-body as HTML
16             var body = document.getElementById(this.id+'-body');
17             var newbody = this.hXMLHttp.responseXML;
18             if (newbody != null) {
19                 // msie: newbody = document, newbody.firstChild.nodeName = xml
20                 if (newbody.firstChild && newbody.firstChild.nodeName == 'xml')
21                     newbody = newbody.firstChild.nextSibling.nextSibling;
22                 // gecko + chrome: skip firstChild = DOCTYPE html
23                 if (newbody.firstChild && newbody.firstChild.nodeName == 'html')
24                     newbody = newbody.childNodes[1];
25                 if (newbody == null) {
26                     alert("showHideDone "+this.id+"\nno xml children from "+this.hXMLHttp.responseText);
27                 }
28                 var hContainer = CreateHtmlFromXml(newbody);
29                 hContainer.className = 'wikitext';
30                 body.appendChild( hContainer );
31                 body.style.display = 'block';
32             } else {
33                 alert("showHideDone "+this.id+"\nerror no xml from "+this.hXMLHttp.responseText);
34             }
35         }
36         if (img)
37             img.src = stylepath + 'images/folderArrowOpen.png';
38     }
39 }
40
41 showHide.prototype.init = function (id) {
42     this.id = id;
43     this.hXMLHttp = XmlHttp.create()
44     var hAC = this
45     this.hXMLHttp.onreadystatechange = function() { hAC.onXmlHttpLoad() }
46 }
47
48 var cShowHide;
49
50 function CreateHtmlFromXml (xml) {
51     if (xml == null) {
52         return document.createElement('xml');
53     }
54     var xmltype = xml.nodeName;
55     var html;
56     if (xmltype == '#text') {
57         html = document.createTextNode( xml.nodeValue );
58         html.nodeValue = xml.nodeValue;
59         if (xml.attributes && (xml.attributes != null))
60             for (var i=0; i < xml.attributes.length; i++) {
61                 html.setAttribute( xml.attributes[i].name, xml.attributes[i].value );
62             }
63     } else {
64         html = document.createElement( xmltype );
65         if (xml.nodeValue)
66             html.nodeValue = xml.nodeValue;
67         if (xml.attributes && (xml.attributes != null))
68             for (var i=0; i < xml.attributes.length; i++) {
69                 html.setAttribute( xml.attributes[i].name, xml.attributes[i].value );
70             }
71         if (xml.hasChildNodes())
72             for (var i=0; i < xml.childNodes.length; i++) {
73                 html.appendChild( CreateHtmlFromXml(xml.childNodes[i]) );
74             }
75     }
76     return html;
77 }
78
79 // if body is empty, load page in background into id+"-body" and show/hide id
80 function showHideAsync(uri, id) {
81     var body = document.getElementById(id+'-body');
82     if (!body) {
83         alert("Error: id="+id+'-body'+" missing.");
84         return;
85     }
86     if (body.hasChildNodes()) {
87         //alert("showHideAsync "+uri+" "+id+"\nalready loaded");
88         showHideFolder(id);
89     }
90     else {
91         //alert("showHideAsync "+uri+" "+id+"\nloading...");
92         var img = document.getElementById(id+'-img');
93         if (img)
94             img.src = stylepath + 'images/folderArrowLoading.gif';
95         cShowHide = new showHide(id)
96         cShowHide.hXMLHttp.open( 'GET', uri, true )
97         cShowHide.hXMLHttp.send( null )
98     }
99 }
100
101 function showHideDone(id) {
102     // insert tree into id-body
103     var body = document.getElementById(id+'-body');
104     body.parentNode.replaceChild(cShowHide.hXMLHttp.responseText, body);
105     alert("showHideDone "+id+"\ngot "+cShowHide.hXMLHttp.responseText);
106     showHideFolder(id);
107 }
108
109 // hide after 0.4 secs
110 function showHideDelayed(id) {
111     window.setTimeout("doshowHide("+id+")",400);
112 }
113
114 function doshowHide(id) {
115     document.getElementById(id).style.display = "none";
116     var highlight = document.getElementById("LSHighlight");
117     if (highlight) {
118         highlight.removeAttribute("id");
119     }
120 }