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