OOP'ed Ajax functions, embedded in index.php for better compatibility with wikipedia...
[lhc/web/wiklou.git] / skins / common / ajax.js
1 // remote scripting library
2 // (c) copyright 2005 modernmethod, inc
3 var sajax_debug_mode = false;
4 var sajax_request_type = "GET";
5
6 var started;
7 var typing;
8 var memory=null;
9 var body=null;
10 var oldbody=null;
11
12 function sajax_debug(text) {
13 if (sajax_debug_mode)
14 alert("RSD: " + text)
15 }
16
17
18 function sajax_init_object() {
19 sajax_debug("sajax_init_object() called..")
20 var A;
21 try {
22 A=new ActiveXObject("Msxml2.XMLHTTP");
23 } catch (e) {
24 try {
25 A=new ActiveXObject("Microsoft.XMLHTTP");
26 } catch (oc) {
27 A=null;
28 }
29 }
30 if(!A && typeof XMLHttpRequest != "undefined")
31 A = new XMLHttpRequest();
32 if (!A)
33 sajax_debug("Could not create connection object.");
34 return A;
35 }
36
37
38 function sajax_do_call(func_name, args) {
39 var i, x, n;
40 var uri;
41 var post_data;
42 uri = wgServer + "/" + wgScriptPath + "/index.php?action=ajax";
43 if (sajax_request_type == "GET") {
44 if (uri.indexOf("?") == -1)
45 uri = uri + "?rs=" + escape(func_name);
46 else
47 uri = uri + "&rs=" + escape(func_name);
48 for (i = 0; i < args.length-1; i++)
49 uri = uri + "&rsargs[]=" + escape(args[i]);
50 //uri = uri + "&rsrnd=" + new Date().getTime();
51 post_data = null;
52 } else {
53 post_data = "rs=" + escape(func_name);
54 for (i = 0; i < args.length-1; i++)
55 post_data = post_data + "&rsargs[]=" + escape(args[i]);
56 }
57 x = sajax_init_object();
58 x.open(sajax_request_type, uri, true);
59 if (sajax_request_type == "POST") {
60 x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
61 x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
62 }
63 x.setRequestHeader("Pragma", "cache=yes");
64 x.setRequestHeader("Cache-Control", "no-transform");
65 x.onreadystatechange = function() {
66 if (x.readyState != 4)
67 return;
68 sajax_debug("received " + x.responseText);
69 var status;
70 var data;
71 status = x.responseText.charAt(0);
72 data = x.responseText.substring(2);
73 if (status == "-")
74 alert("Error: " + data);
75 else
76 args[args.length-1](data);
77 }
78 x.send(post_data);
79 sajax_debug(func_name + " uri = " + uri + "/post = " + post_data);
80 sajax_debug(func_name + " waiting..");
81 delete x;
82 }
83
84 // Remove the typing barrier to allow call() to complete
85 function Search_doneTyping()
86 {
87 typing=false;
88 }
89
90 // Wait 500ms to run call()
91 function Searching_Go()
92 {
93 setTimeout("Searching_Call()", 500);
94 }
95
96 // If the user is typing wait until they are done.
97 function Search_Typing() {
98 started=true;
99 typing=true;
100 window.status = "Waiting until you're done typing...";
101 setTimeout("Search_doneTyping()", 500);
102
103 // I believe these are needed by IE for when the users press return?
104 if (window.event)
105 {
106 if (event.keyCode == 13)
107 {
108 event.cancelBubble = true;
109 event.returnValue = false;
110 }
111 }
112 }
113
114 // Set the body div to the results
115 function Searching_SetResult(result)
116 {
117 //body.innerHTML = result;
118 t = document.getElementById("searchTarget");
119 if ( t == null ) {
120 oldbody=body.innerHTML;
121 body.innerHTML= '<div id="searchTargetContainer"><div id="searchTarget" ></div></div>' ;
122 t = document.getElementById("searchTarget");
123 }
124 t.innerHTML = result;
125 t.style.display='block';
126 }
127
128 function Searching_Hide_Results()
129 {
130 t = document.getElementById("searchTarget");
131 t.style.display='none';
132 body.innerHTML = oldbody;
133 }
134
135
136 // This will call the php function that will eventually
137 // return a results table
138 function Searching_Call()
139 {
140 var x;
141 Searching_Go();
142
143 //Don't proceed if user is typing
144 if (typing)
145 return;
146
147 x = document.getElementById("searchInput").value;
148
149 // Don't search again if the query is the same
150 if (x==memory)
151 return;
152
153 memory=x;
154 if (started) {
155 // Don't search for blank or < 3 chars.
156 if ((x=="") || (x.length < 3))
157 {
158 return;
159 }
160 x_wfSajaxSearch(x, Searching_SetResult);
161 }
162 }
163
164 function x_wfSajaxSearch() {
165 sajax_do_call( "wfSajaxSearch", x_wfSajaxSearch.arguments );
166 }
167
168
169 //Initialize
170 function sajax_onload() {
171 x = document.getElementById( 'searchInput' );
172 x.onkeypress= function() { Search_Typing(); };
173 Searching_Go();
174 body = document.getElementById("content");
175 }
176
177 hookEvent("load", sajax_onload);