summaryrefslogtreecommitdiff
path: root/skins/common/ajax.js
blob: ca74b384b03b127a63f0db5456a96d1f96151d6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
 * Remote Scripting Library
 * Copyright 2005 modernmethod, inc
 * Under the open source BSD license
 * http://www.modernmethod.com/sajax/
 */

/*jshint camelcase:false, onevar:false */
/*global alert */
( function ( mw ) {

/**
 * if sajax_debug_mode is true, this function outputs given the message into
 * the element with id = sajax_debug; if no such element exists in the document,
 * it is injected.
 */
function debug( text ) {
	if ( !window.sajax_debug_mode ) {
		return false;
	}

	var e = document.getElementById( 'sajax_debug' );

	if ( !e ) {
		e = document.createElement( 'p' );
		e.className = 'sajax_debug';
		e.id = 'sajax_debug';

		var b = document.getElementsByTagName( 'body' )[0];

		if ( b.firstChild ) {
			b.insertBefore( e, b.firstChild );
		} else {
			b.appendChild( e );
		}
	}

	var m = document.createElement( 'div' );
	m.appendChild( document.createTextNode( text ) );

	e.appendChild( m );

	return true;
}

/**
 * Compatibility wrapper for creating a new XMLHttpRequest object.
 */
function createXhr() {
	debug( 'sajax_init_object() called..' );
	var a;
	try {
		// Try the new style before ActiveX so we don't
		// unnecessarily trigger warnings in IE 7 when
		// set to prompt about ActiveX usage
		a = new XMLHttpRequest();
	} catch ( xhrE ) {
		try {
			a = new window.ActiveXObject( 'Msxml2.XMLHTTP' );
		} catch ( msXmlE ) {
			try {
				a = new window.ActiveXObject( 'Microsoft.XMLHTTP' );
			} catch ( msXhrE ) {
				a = null;
			}
		}
	}
	if ( !a ) {
		debug( 'Could not create connection object.' );
	}

	return a;
}

/**
 * Perform an AJAX call to MediaWiki. Calls are handled by AjaxDispatcher.php
 *   func_name - the name of the function to call. Must be registered in $wgAjaxExportList
 *   args - an array of arguments to that function
 *   target - the target that will handle the result of the call. If this is a function,
 *            if will be called with the XMLHttpRequest as a parameter; if it's an input
 *            element, its value will be set to the resultText; if it's another type of
 *            element, its innerHTML will be set to the resultText.
 *
 * Example:
 *    sajax_do_call( 'doFoo', [1, 2, 3], document.getElementById( 'showFoo' ) );
 *
 * This will call the doFoo function via MediaWiki's AjaxDispatcher, with
 * (1, 2, 3) as the parameter list, and will show the result in the element
 * with id = showFoo
 */
function doAjaxRequest( func_name, args, target ) {
	var i, x;
	var uri;
	var post_data;
	uri = mw.util.wikiScript() + '?action=ajax';
	if ( window.sajax_request_type === 'GET' ) {
		if ( uri.indexOf( '?' ) === -1 ) {
			uri = uri + '?rs=' + encodeURIComponent( func_name );
		} else {
			uri = uri + '&rs=' + encodeURIComponent( func_name );
		}
		for ( i = 0; i < args.length; i++ ) {
			uri = uri + '&rsargs[]=' + encodeURIComponent( args[i] );
		}
		//uri = uri + '&rsrnd=' + new Date().getTime();
		post_data = null;
	} else {
		post_data = 'rs=' + encodeURIComponent( func_name );
		for ( i = 0; i < args.length; i++ ) {
			post_data = post_data + '&rsargs[]=' + encodeURIComponent( args[i] );
		}
	}
	x = createXhr();
	if ( !x ) {
		alert( 'AJAX not supported' );
		return false;
	}

	try {
		x.open( window.sajax_request_type, uri, true );
	} catch ( e ) {
		if ( location.hostname === 'localhost' ) {
			alert( 'Your browser blocks XMLHttpRequest to "localhost", try using a real hostname for development/testing.' );
		}
		throw e;
	}
	if ( window.sajax_request_type === 'POST' ) {
		x.setRequestHeader( 'Method', 'POST ' + uri + ' HTTP/1.1' );
		x.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
	}
	x.setRequestHeader( 'Pragma', 'cache=yes' );
	x.setRequestHeader( 'Cache-Control', 'no-transform' );
	x.onreadystatechange = function () {
		if ( x.readyState !== 4 ) {
			return;
		}

		debug( 'received (' + x.status + ' ' + x.statusText + ') ' + x.responseText );

		//if ( x.status != 200 )
		//	alert( 'Error: ' + x.status + ' ' + x.statusText + ': ' + x.responseText );
		//else

		if ( typeof target === 'function' ) {
			target( x );
		} else if ( typeof target === 'object' ) {
			if ( target.tagName === 'INPUT' ) {
				if ( x.status === 200 ) {
					target.value= x.responseText;
				}
				//else alert( 'Error: ' + x.status + ' ' + x.statusText + ' (' + x.responseText + ')' );
			} else {
				if ( x.status === 200 ) {
					target.innerHTML = x.responseText;
				} else {
					target.innerHTML = '<div class="error">Error: ' + x.status +
						' ' + x.statusText + ' (' + x.responseText + ')</div>';
				}
			}
		} else {
			alert( 'Bad target for sajax_do_call: not a function or object: ' + target );
		}
	};

	debug( func_name + ' uri = ' + uri + ' / post = ' + post_data );
	x.send( post_data );
	debug( func_name + ' waiting..' );

	return true;
}

/**
 * @return {boolean} Whether the browser supports AJAX
 */
function wfSupportsAjax() {
	var request = createXhr();
	var supportsAjax = request ? true : false;
	request = undefined;
	return supportsAjax;
}

// Expose + Mark as deprecated
var deprecationNotice = 'Sajax is deprecated, use jQuery.ajax or mediawiki.api instead.';

// Variables
mw.log.deprecate( window, 'sajax_debug_mode', false, deprecationNotice );
mw.log.deprecate( window, 'sajax_request_type', 'GET', deprecationNotice );
// Methods
mw.log.deprecate( window, 'sajax_debug', debug, deprecationNotice );
mw.log.deprecate( window, 'sajax_init_object', createXhr, deprecationNotice );
mw.log.deprecate( window, 'sajax_do_call', doAjaxRequest, deprecationNotice );
mw.log.deprecate( window, 'wfSupportsAjax', wfSupportsAjax, deprecationNotice );

}( mediaWiki ) );