* API: Allow for query extensions
[mediawiki.git] / skins / common / ajax.js
blobd90bea091f0946ba4d3ff4489d176e98cf8ec4cc
1 // remote scripting library
2 // (c) copyright 2005 modernmethod, inc
3 var sajax_debug_mode = false;
4 var sajax_request_type = "GET";
6 /**
7 * if sajax_debug_mode is true, this function outputs given the message into 
8 * the element with id = sajax_debug; if no such element exists in the document, 
9 * it is injected.
11 function sajax_debug(text) {
12         if (!sajax_debug_mode) return false;
14         var e= document.getElementById('sajax_debug');
16         if (!e) {
17                 e= document.createElement("p");
18                 e.className= 'sajax_debug';
19                 e.id= 'sajax_debug';
21                 var b= document.getElementsByTagName("body")[0];
23                 if (b.firstChild) b.insertBefore(e, b.firstChild);
24                 else b.appendChild(e);
25         }
27         var m= document.createElement("div");
28         m.appendChild( document.createTextNode( text ) );
30         e.appendChild( m );
32         return true;
35 /**
36 * compatibility wrapper for creating a new XMLHttpRequest object.
38 function sajax_init_object() {
39         sajax_debug("sajax_init_object() called..")
40         var A;
41         try {
42                 A=new ActiveXObject("Msxml2.XMLHTTP");
43         } catch (e) {
44                 try {
45                         A=new ActiveXObject("Microsoft.XMLHTTP");
46                 } catch (oc) {
47                         A=null;
48                 }
49         }
50         if(!A && typeof XMLHttpRequest != "undefined")
51                 A = new XMLHttpRequest();
52         if (!A)
53                 sajax_debug("Could not create connection object.");
55         return A;
58 /**
59 * Perform an ajax call to mediawiki. Calls are handeled by AjaxDispatcher.php
60 *   func_name - the name of the function to call. Must be registered in $wgAjaxExportList
61 *   args - an array of arguments to that function
62 *   target - the target that will handle the result of the call. If this is a function,
63 *            if will be called with the XMLHttpRequest as a parameter; if it's an input
64 *            element, its value will be set to the resultText; if it's another type of
65 *            element, its innerHTML will be set to the resultText.
67 * Example:
68 *    sajax_do_call('doFoo', [1, 2, 3], document.getElementById("showFoo"));
70 * This will call the doFoo function via MediaWiki's AjaxDispatcher, with
71 * (1, 2, 3) as the parameter list, and will show the result in the element
72 * with id = showFoo
74 function sajax_do_call(func_name, args, target) {
75         var i, x, n;
76         var uri;
77         var post_data;
78         uri = wgServer + wgScriptPath + "/index.php?action=ajax";
79         if (sajax_request_type == "GET") {
80                 if (uri.indexOf("?") == -1)
81                         uri = uri + "?rs=" + encodeURIComponent(func_name);
82                 else
83                         uri = uri + "&rs=" + encodeURIComponent(func_name);
84                 for (i = 0; i < args.length; i++)
85                         uri = uri + "&rsargs[]=" + encodeURIComponent(args[i]);
86                 //uri = uri + "&rsrnd=" + new Date().getTime();
87                 post_data = null;
88         } else {
89                 post_data = "rs=" + encodeURIComponent(func_name);
90                 for (i = 0; i < args.length; i++)
91                         post_data = post_data + "&rsargs[]=" + encodeURIComponent(args[i]);
92         }
93         x = sajax_init_object();
94         if (!x) {
95                 alert("AJAX not supported");
96                 return false;
97         }
99         try {
100                 x.open(sajax_request_type, uri, true);
101         } catch (e) {
102                 if (window.location.hostname == "localhost") {
103                         alert("Your browser blocks XMLHttpRequest to 'localhost', try using a real hostname for development/testing.");
104                 }
105                 throw e;
106         }
107         if (sajax_request_type == "POST") {
108                 x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
109                 x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
110         }
111         x.setRequestHeader("Pragma", "cache=yes");
112         x.setRequestHeader("Cache-Control", "no-transform");
113         x.onreadystatechange = function() {
114                 if (x.readyState != 4)
115                         return;
117                 sajax_debug("received (" + x.status + " " + x.statusText + ") " + x.responseText);
119                 //if (x.status != 200)
120                 //      alert("Error: " + x.status + " " + x.statusText + ": " + x.responseText);
121                 //else
123                 if ( typeof( target ) == 'function' ) {
124                         target( x );
125                 }
126                 else if ( typeof( target ) == 'object' ) {
127                         if ( target.tagName == 'INPUT' ) {
128                                 if (x.status == 200) target.value= x.responseText;
129                                 //else alert("Error: " + x.status + " " + x.statusText + " (" + x.responseText + ")");
130                         }
131                         else {
132                                 if (x.status == 200) target.innerHTML = x.responseText;
133                                 else target.innerHTML= "<div class='error'>Error: " + x.status + " " + x.statusText + " (" + x.responseText + ")</div>";
134                         }
135                 }
136                 else {
137                         alert("bad target for sajax_do_call: not a function or object: " + target);
138                 }
140                 return;
141         }
143         sajax_debug(func_name + " uri = " + uri + " / post = " + post_data);
144         x.send(post_data);
145         sajax_debug(func_name + " waiting..");
146         delete x;
148         return true;