]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - themes/default/ajax.js
Remove $Id$
[SourceForge/phpwiki.git] / themes / default / ajax.js
1 // Ajax Javascript support functions, based on moacdropdown XmlHttp
2 // Written from scratch by Reini Urban
3 // $Id$
4
5 function showHide( id ) {
6     this.init( id )
7 }
8
9 showHide.prototype.onXmlHttpLoad = function( ) {
10     if( this.hXMLHttp.readyState == 4 ) {
11         var hError = this.hXMLHttp.parseError;
12         var img = document.getElementById(this.id+'-img');
13         if( hError && hError.errorCode != 0 ) {
14             alert( hError.reason );
15         } else {
16             // insert external, same-domain XML tree into id-body as HTML
17             // we get this from any page&format=xml
18             var body = document.getElementById(this.id+'-body');
19             var newbody = this.hXMLHttp.responseXML;
20             if (newbody != null) {
21                 // DOM quirks with text/xml and DOCTYPE xhtml
22                 // msie: newbody = document, newbody.firstChild.nodeName = xml
23                 if (newbody.firstChild && newbody.firstChild.nodeName == 'xml')
24                     newbody = newbody.firstChild.nextSibling.nextSibling;
25                 // gecko + chrome no xml: skip only firstChild = DOCTYPE html
26                 if (newbody.firstChild && newbody.firstChild.nodeName == 'html')
27                     newbody = newbody.childNodes[1];
28                 if (newbody == null) {
29                     alert("showHideDone "+this.id+"\nno xml children from "+this.hXMLHttp.responseText);
30                 }
31                 // We cannot just insert the responseXML into the DOM.
32                 // well gecko can, but the others not.
33                 // So convert the XML tree it on the fly into HTML nodes.
34                 // I never saw this before. I needed that, so I think I
35                 // invented that sort of rich mashup.
36                 var hContainer = CreateHtmlFromXml(newbody);
37                 hContainer.className = 'wikitext';
38                 body.appendChild( hContainer );
39                 body.style.display = 'block';
40             } else {
41                 alert("showHideDone "+this.id+"\nerror no xml from "+this.hXMLHttp.responseText);
42             }
43         }
44         if (img) {
45             if (!folderArrowPath) folderArrowPath = stylepath + 'images/';
46             img.src = folderArrowPath + '/folderArrowOpen.png';
47         }
48     }
49 }
50
51 showHide.prototype.init = function (id) {
52     this.id = id;
53     this.hXMLHttp = XmlHttp.create()
54     var hAC = this
55     this.hXMLHttp.onreadystatechange = function() { hAC.onXmlHttpLoad() }
56 }
57
58 var cShowHide;
59
60 /* recursive xml => html converter. 
61    This might need a attribute type checker in a bad world. 
62    e.g. disable all on* events */
63 function CreateHtmlFromXml (xml) {
64     if (xml == null) {
65         return document.createElement('xml');
66     }
67     var xmltype = xml.nodeName;
68     var html;
69     if (xmltype == '#text') {
70         html = document.createTextNode( xml.nodeValue );
71         html.nodeValue = xml.nodeValue;
72         if (xml.attributes && (xml.attributes != null))
73             for (var i=0; i < xml.attributes.length; i++) {
74                 html.setAttribute( xml.attributes[i].name, xml.attributes[i].value );
75             }
76     } else {
77         html = document.createElement( xmltype );
78         if (xml.nodeValue)
79             html.nodeValue = xml.nodeValue;
80         if (xml.attributes && (xml.attributes != null))
81             for (var i=0; i < xml.attributes.length; i++) {
82                 html.setAttribute( xml.attributes[i].name, xml.attributes[i].value );
83             }
84         if (xml.hasChildNodes())
85             for (var i=0; i < xml.childNodes.length; i++) {
86                 html.appendChild( CreateHtmlFromXml(xml.childNodes[i]) );
87             }
88     }
89     return html;
90 }
91
92 // if body is empty, load page in background into id+"-body" and show/hide id
93 function showHideAsync(uri, id) {
94     var body = document.getElementById(id+'-body');
95     if (!body) {
96         alert("Error: id="+id+'-body'+" missing.");
97         return;
98     }
99     if (body.hasChildNodes()) {
100         //alert("showHideAsync "+uri+" "+id+"\nalready loaded");
101         showHideFolder(id);
102     }
103     else {
104         if (!folderArrowPath) folderArrowPath = stylepath + 'images/';
105         //alert("showHideAsync "+uri+" "+id+"\nloading...");
106         var img = document.getElementById(id+'-img');
107         if (img)
108             img.src = folderArrowPath + '/folderArrowLoading.gif';
109         cShowHide = new showHide(id)
110         cShowHide.hXMLHttp.open( 'GET', uri, true )
111         cShowHide.hXMLHttp.send( null )
112     }
113 }
114
115 function showHideDone(id) {
116     // insert tree into id-body
117     var body = document.getElementById(id+'-body');
118     body.parentNode.replaceChild(cShowHide.hXMLHttp.responseText, body);
119     alert("showHideDone "+id+"\ngot "+cShowHide.hXMLHttp.responseText);
120     showHideFolder(id);
121 }
122
123 // hide after 0.4 secs
124 function showHideDelayed(id) {
125     window.setTimeout("doshowHide("+id+")",400);
126 }
127
128 function doshowHide(id) {
129     document.getElementById(id).style.display = "none";
130     var highlight = document.getElementById("LSHighlight");
131     if (highlight) {
132         highlight.removeAttribute("id");
133     }
134 }