Merging resourceloader branch into trunk. Full documentation is at http://www.mediawi...
[lhc/web/wiklou.git] / resources / mediawiki / legacy / mediawiki.legacy.ajax.js
1 /*
2 * Legacy emulation for the now depricated skins/common/ajax.js
3 *
4 * Original licensing information:
5 * Remote Scripting Library
6 * (c) Copyright 2005 ModernMethod, Inc.
7 */
8
9 ( function( $, mw ) {
10
11 /* Extension */
12
13 $.extend( true, mw.legacy, {
14
15 /* Global Variables */
16
17 'sajax_debug_mode': false,
18 'sajax_debug_mode': 'GET',
19
20 /* Functions */
21
22 /**
23 * If sajax_debug_mode is true, this function outputs given the message into the element with id = sajax_debug; if no
24 * such element exists in the document, it is injected
25 *
26 * @param string text debug message to append to log
27 * @return boolean true when in debug mode, false when not
28 */
29 'sajax_debug': function( text ) {
30 if ( mw.legacy.sajax_debug_mode ) {
31 var $e = $( '#sajax_debug' );
32 if ( !$e.length ) {
33 $e = $( '<p class="sajax_debug" id="sajax_debug"></p>' ).prependTo( $( 'body' ) );
34 }
35 $e.append( $( '<div></div>' ).text( text ) );
36 return true;
37 }
38 return false;
39 },
40 /**
41 * Gets an XMLHttpRequest or equivilant ActiveXObject
42 *
43 * @reuturn mixed request object on success, boolean false on failure
44 */
45 'sajax_init_object': function() {
46 mw.legacy.sajax_debug( 'sajax_init_object() called..' );
47 var request = false;
48 try {
49 // Try the 'new' style before ActiveX so we don't unnecessarily trigger warnings in IE 7 when the user's
50 // security settings are set to prompt about ActiveX usage
51 request = new XMLHttpRequest();
52 } catch ( e ) {
53 try {
54 request = new ActiveXObject( 'Msxml2.XMLHTTP' );
55 } catch ( e ) {
56 try {
57 request = new ActiveXObject( 'Microsoft.XMLHTTP' );
58 } catch ( oc ) {
59 request = null;
60 }
61 }
62 }
63 if ( !request ) {
64 mw.legacy.sajax_debug( 'Could not create connection object.' );
65 }
66 return request;
67 },
68 /**
69 * Performs an ajax call to mediawiki. Calls are handeled by AjaxDispatcher.php
70 *
71 * @param string method name of the function to call. Must be registered in $wgAjaxExportList
72 * @param array arguments arguments to that function
73 * @param mixed target the target that will handle the result of the call. If this is a function, if will be called
74 * with the XMLHttpRequest as a parameter; if it's an input element, its value will be set to the resultText; if
75 * it's another type of element, its innerHTML will be set to the resultText.
76 *
77 * @example
78 * // This will call the doFoo function via MediaWiki's AjaxDispatcher, with (1, 2, 3) as the parameter list,
79 * // and will show the result in the element with id = showFoo
80 * sajax_do_call( 'doFoo', [1, 2, 3], document.getElementById( 'showFoo' ) );
81 */
82 'sajax_do_call': function( method, arguments, target ) {
83 var post_data;
84 var uri = mw.legacy.wgServer +
85 ( ( mw.legacy.wgScript == null ) ? ( mw.legacy.wgScriptPath + '/index.php' ) : mw.legacy.wgScript ) +
86 '?action=ajax';
87 if ( mw.legacy.sajax_request_type == 'GET' ) {
88 if ( uri.indexOf( '?' ) == -1 ) {
89 uri = uri + '?rs=' + encodeURIComponent( method );
90 } else {
91 uri = uri + '&rs=' + encodeURIComponent( method );
92 }
93 for ( var i = 0; i < arguments.length; i++ ) {
94 uri = uri + '&rsargs[]=' + encodeURIComponent( arguments[i] );
95 }
96 post_data = null;
97 } else {
98 post_data = 'rs=' + encodeURIComponent( method );
99 for ( var i = 0; i < arguments.length; i++ ) {
100 post_data = post_data + '&rsargs[]=' + encodeURIComponent( arguments[i] );
101 }
102 }
103 var request = mw.legacy.sajax_init_object();
104 if ( !request ) {
105 alert( 'AJAX not supported' );
106 return false;
107 }
108 try {
109 request.open( mw.legacy.sajax_request_type, uri, true );
110 } catch ( e ) {
111 if ( window.location.hostname == 'localhost' ) {
112 alert(
113 'Your browser blocks XMLHttpRequest to \'localhost\', ' +
114 'try using a real hostname for development/testing.'
115 );
116 }
117 throw e;
118 }
119 if ( mw.legacy.sajax_request_type == 'POST' ) {
120 request.setRequestHeader( 'Method', 'POST ' + uri + ' HTTP/1.1' );
121 request.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
122 }
123 request.setRequestHeader( 'Pragma', 'cache=yes' );
124 request.setRequestHeader( 'Cache-Control', 'no-transform' );
125 request.onreadystatechange = function() {
126 if ( request.readyState != 4 ) {
127 return;
128 }
129 mw.legacy.sajax_debug(
130 'received (' + request.status + ' ' + request.statusText + ') ' + request.responseText
131 );
132 if ( typeof( target ) == 'function' ) {
133 target( request );
134 } else if ( typeof( target ) == 'object' ) {
135 $target = $( target );
136 if ( $target.is( 'input' ) ) {
137 if ( request.status == 200 ) {
138 $target.val();
139 }
140 } else {
141 if ( request.status == 200 ) {
142 $target.html( request.responseText );
143 } else {
144 $target.html(
145 '<div class="error">' +
146 'Error: ' + request.status + ' ' + request.statusText +
147 ' (' + request.responseText + ')' +
148 '</div>'
149 );
150 }
151 }
152 } else {
153 alert( 'Bad target for sajax_do_call: not a function or object: ' + target );
154 }
155 return;
156 }
157 mw.legacy.sajax_debug( method + ' uri = ' + uri + ' / post = ' + post_data );
158 request.send( post_data );
159 mw.legacy.sajax_debug( method + ' waiting..' );
160 delete x;
161 return true;
162 },
163 /**
164 * Ajax compatibility test
165 *
166 * @return boolean whether the browser supports XMLHttpRequest
167 */
168 'wfSupportsAjax': function() {
169 var request = mw.legacy.sajax_init_object();
170 var result = request ? true : false;
171 delete request;
172 return result;
173 }
174 } );
175
176 } )( jQuery, mediaWiki );