Merge "Fix Oracle 1.23 missing patches (rebase)"
[lhc/web/wiklou.git] / skins / common / ajax.js
1 /**
2 * Remote Scripting Library
3 * Copyright 2005 modernmethod, inc
4 * Under the open source BSD license
5 * http://www.modernmethod.com/sajax/
6 */
7
8 /*jshint camelcase:false */
9 /*global alert */
10 ( function ( mw ) {
11
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 }
21
22 var e = document.getElementById( 'sajax_debug' );
23
24 if ( !e ) {
25 e = document.createElement( 'p' );
26 e.className = 'sajax_debug';
27 e.id = 'sajax_debug';
28
29 var b = document.getElementsByTagName( 'body' )[0];
30
31 if ( b.firstChild ) {
32 b.insertBefore( e, b.firstChild );
33 } else {
34 b.appendChild( e );
35 }
36 }
37
38 var m = document.createElement( 'div' );
39 m.appendChild( document.createTextNode( text ) );
40
41 e.appendChild( m );
42
43 return true;
44 }
45
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 }
71
72 return a;
73 }
74
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, uri, post_data;
93 uri = mw.util.wikiScript() + '?action=ajax';
94 if ( window.sajax_request_type === 'GET' ) {
95 if ( uri.indexOf( '?' ) === -1 ) {
96 uri = uri + '?rs=' + encodeURIComponent( func_name );
97 } else {
98 uri = uri + '&rs=' + encodeURIComponent( func_name );
99 }
100 for ( i = 0; i < args.length; i++ ) {
101 uri = uri + '&rsargs[]=' + encodeURIComponent( args[i] );
102 }
103 //uri = uri + '&rsrnd=' + new Date().getTime();
104 post_data = null;
105 } else {
106 post_data = 'rs=' + encodeURIComponent( func_name );
107 for ( i = 0; i < args.length; i++ ) {
108 post_data = post_data + '&rsargs[]=' + encodeURIComponent( args[i] );
109 }
110 }
111 x = createXhr();
112 if ( !x ) {
113 alert( 'AJAX not supported' );
114 return false;
115 }
116
117 try {
118 x.open( window.sajax_request_type, uri, true );
119 } catch ( e ) {
120 if ( location.hostname === 'localhost' ) {
121 alert( 'Your browser blocks XMLHttpRequest to "localhost", try using a real hostname for development/testing.' );
122 }
123 throw e;
124 }
125 if ( window.sajax_request_type === 'POST' ) {
126 x.setRequestHeader( 'Method', 'POST ' + uri + ' HTTP/1.1' );
127 x.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
128 }
129 x.setRequestHeader( 'Pragma', 'cache=yes' );
130 x.setRequestHeader( 'Cache-Control', 'no-transform' );
131 x.onreadystatechange = function () {
132 if ( x.readyState !== 4 ) {
133 return;
134 }
135
136 debug( 'received (' + x.status + ' ' + x.statusText + ') ' + x.responseText );
137
138 //if ( x.status != 200 )
139 // alert( 'Error: ' + x.status + ' ' + x.statusText + ': ' + x.responseText );
140 //else
141
142 if ( typeof target === 'function' ) {
143 target( x );
144 } else if ( typeof target === 'object' ) {
145 if ( target.tagName === 'INPUT' ) {
146 if ( x.status === 200 ) {
147 target.value = x.responseText;
148 }
149 //else alert( 'Error: ' + x.status + ' ' + x.statusText + ' (' + x.responseText + ')' );
150 } else {
151 if ( x.status === 200 ) {
152 target.innerHTML = x.responseText;
153 } else {
154 target.innerHTML = '<div class="error">Error: ' + x.status +
155 ' ' + x.statusText + ' (' + x.responseText + ')</div>';
156 }
157 }
158 } else {
159 alert( 'Bad target for sajax_do_call: not a function or object: ' + target );
160 }
161 };
162
163 debug( func_name + ' uri = ' + uri + ' / post = ' + post_data );
164 x.send( post_data );
165 debug( func_name + ' waiting..' );
166
167 return true;
168 }
169
170 /**
171 * @return {boolean} Whether the browser supports AJAX
172 */
173 function wfSupportsAjax() {
174 var request = createXhr(),
175 supportsAjax = request ? true : false;
176
177 request = undefined;
178 return supportsAjax;
179 }
180
181 // Expose + Mark as deprecated
182 var deprecationNotice = 'Sajax is deprecated, use jQuery.ajax or mediawiki.api instead.';
183
184 // Variables
185 mw.log.deprecate( window, 'sajax_debug_mode', false, deprecationNotice );
186 mw.log.deprecate( window, 'sajax_request_type', 'GET', deprecationNotice );
187 // Methods
188 mw.log.deprecate( window, 'sajax_debug', debug, deprecationNotice );
189 mw.log.deprecate( window, 'sajax_init_object', createXhr, deprecationNotice );
190 mw.log.deprecate( window, 'sajax_do_call', doAjaxRequest, deprecationNotice );
191 mw.log.deprecate( window, 'wfSupportsAjax', wfSupportsAjax, deprecationNotice );
192
193 }( mediaWiki ) );