Merge "Exclude servers with zero load from lag checks"
[mediawiki.git] / skins / common / ajax.js
blobca74b384b03b127a63f0db5456a96d1f96151d6d
1 /**
2  * Remote Scripting Library
3  * Copyright 2005 modernmethod, inc
4  * Under the open source BSD license
5  * http://www.modernmethod.com/sajax/
6  */
8 /*jshint camelcase:false, onevar:false */
9 /*global alert */
10 ( function ( mw ) {
12 /**
13  * if sajax_debug_mode is true, this function outputs given the message into
14  * the element with id = sajax_debug; if no such element exists in the document,
15  * it is injected.
16  */
17 function debug( text ) {
18         if ( !window.sajax_debug_mode ) {
19                 return false;
20         }
22         var e = document.getElementById( 'sajax_debug' );
24         if ( !e ) {
25                 e = document.createElement( 'p' );
26                 e.className = 'sajax_debug';
27                 e.id = 'sajax_debug';
29                 var b = document.getElementsByTagName( 'body' )[0];
31                 if ( b.firstChild ) {
32                         b.insertBefore( e, b.firstChild );
33                 } else {
34                         b.appendChild( e );
35                 }
36         }
38         var m = document.createElement( 'div' );
39         m.appendChild( document.createTextNode( text ) );
41         e.appendChild( m );
43         return true;
46 /**
47  * Compatibility wrapper for creating a new XMLHttpRequest object.
48  */
49 function createXhr() {
50         debug( 'sajax_init_object() called..' );
51         var a;
52         try {
53                 // Try the new style before ActiveX so we don't
54                 // unnecessarily trigger warnings in IE 7 when
55                 // set to prompt about ActiveX usage
56                 a = new XMLHttpRequest();
57         } catch ( xhrE ) {
58                 try {
59                         a = new window.ActiveXObject( 'Msxml2.XMLHTTP' );
60                 } catch ( msXmlE ) {
61                         try {
62                                 a = new window.ActiveXObject( 'Microsoft.XMLHTTP' );
63                         } catch ( msXhrE ) {
64                                 a = null;
65                         }
66                 }
67         }
68         if ( !a ) {
69                 debug( 'Could not create connection object.' );
70         }
72         return a;
75 /**
76  * Perform an AJAX call to MediaWiki. Calls are handled by AjaxDispatcher.php
77  *   func_name - the name of the function to call. Must be registered in $wgAjaxExportList
78  *   args - an array of arguments to that function
79  *   target - the target that will handle the result of the call. If this is a function,
80  *            if will be called with the XMLHttpRequest as a parameter; if it's an input
81  *            element, its value will be set to the resultText; if it's another type of
82  *            element, its innerHTML will be set to the resultText.
83  *
84  * Example:
85  *    sajax_do_call( 'doFoo', [1, 2, 3], document.getElementById( 'showFoo' ) );
86  *
87  * This will call the doFoo function via MediaWiki's AjaxDispatcher, with
88  * (1, 2, 3) as the parameter list, and will show the result in the element
89  * with id = showFoo
90  */
91 function doAjaxRequest( func_name, args, target ) {
92         var i, x;
93         var uri;
94         var post_data;
95         uri = mw.util.wikiScript() + '?action=ajax';
96         if ( window.sajax_request_type === 'GET' ) {
97                 if ( uri.indexOf( '?' ) === -1 ) {
98                         uri = uri + '?rs=' + encodeURIComponent( func_name );
99                 } else {
100                         uri = uri + '&rs=' + encodeURIComponent( func_name );
101                 }
102                 for ( i = 0; i < args.length; i++ ) {
103                         uri = uri + '&rsargs[]=' + encodeURIComponent( args[i] );
104                 }
105                 //uri = uri + '&rsrnd=' + new Date().getTime();
106                 post_data = null;
107         } else {
108                 post_data = 'rs=' + encodeURIComponent( func_name );
109                 for ( i = 0; i < args.length; i++ ) {
110                         post_data = post_data + '&rsargs[]=' + encodeURIComponent( args[i] );
111                 }
112         }
113         x = createXhr();
114         if ( !x ) {
115                 alert( 'AJAX not supported' );
116                 return false;
117         }
119         try {
120                 x.open( window.sajax_request_type, uri, true );
121         } catch ( e ) {
122                 if ( location.hostname === 'localhost' ) {
123                         alert( 'Your browser blocks XMLHttpRequest to "localhost", try using a real hostname for development/testing.' );
124                 }
125                 throw e;
126         }
127         if ( window.sajax_request_type === 'POST' ) {
128                 x.setRequestHeader( 'Method', 'POST ' + uri + ' HTTP/1.1' );
129                 x.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
130         }
131         x.setRequestHeader( 'Pragma', 'cache=yes' );
132         x.setRequestHeader( 'Cache-Control', 'no-transform' );
133         x.onreadystatechange = function () {
134                 if ( x.readyState !== 4 ) {
135                         return;
136                 }
138                 debug( 'received (' + x.status + ' ' + x.statusText + ') ' + x.responseText );
140                 //if ( x.status != 200 )
141                 //      alert( 'Error: ' + x.status + ' ' + x.statusText + ': ' + x.responseText );
142                 //else
144                 if ( typeof target === 'function' ) {
145                         target( x );
146                 } else if ( typeof target === 'object' ) {
147                         if ( target.tagName === 'INPUT' ) {
148                                 if ( x.status === 200 ) {
149                                         target.value= x.responseText;
150                                 }
151                                 //else alert( 'Error: ' + x.status + ' ' + x.statusText + ' (' + x.responseText + ')' );
152                         } else {
153                                 if ( x.status === 200 ) {
154                                         target.innerHTML = x.responseText;
155                                 } else {
156                                         target.innerHTML = '<div class="error">Error: ' + x.status +
157                                                 ' ' + x.statusText + ' (' + x.responseText + ')</div>';
158                                 }
159                         }
160                 } else {
161                         alert( 'Bad target for sajax_do_call: not a function or object: ' + target );
162                 }
163         };
165         debug( func_name + ' uri = ' + uri + ' / post = ' + post_data );
166         x.send( post_data );
167         debug( func_name + ' waiting..' );
169         return true;
173  * @return {boolean} Whether the browser supports AJAX
174  */
175 function wfSupportsAjax() {
176         var request = createXhr();
177         var supportsAjax = request ? true : false;
178         request = undefined;
179         return supportsAjax;
182 // Expose + Mark as deprecated
183 var deprecationNotice = 'Sajax is deprecated, use jQuery.ajax or mediawiki.api instead.';
185 // Variables
186 mw.log.deprecate( window, 'sajax_debug_mode', false, deprecationNotice );
187 mw.log.deprecate( window, 'sajax_request_type', 'GET', deprecationNotice );
188 // Methods
189 mw.log.deprecate( window, 'sajax_debug', debug, deprecationNotice );
190 mw.log.deprecate( window, 'sajax_init_object', createXhr, deprecationNotice );
191 mw.log.deprecate( window, 'sajax_do_call', doAjaxRequest, deprecationNotice );
192 mw.log.deprecate( window, 'wfSupportsAjax', wfSupportsAjax, deprecationNotice );
194 }( mediaWiki ) );