coding style tweaks for ajax.js
[lhc/web/wiklou.git] / skins / common / ajax.js
1 // remote scripting library
2 // (c) copyright 2005 modernmethod, inc
3 var sajax_debug_mode = false;
4 var sajax_request_type = 'GET';
5
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.
10 */
11 function sajax_debug( text ) {
12 if ( !sajax_debug_mode ) {
13 return false;
14 }
15
16 var e = document.getElementById( 'sajax_debug' );
17
18 if ( !e ) {
19 e = document.createElement( 'p' );
20 e.className = 'sajax_debug';
21 e.id = 'sajax_debug';
22
23 var b = document.getElementsByTagName( 'body' )[0];
24
25 if ( b.firstChild ) {
26 b.insertBefore( e, b.firstChild );
27 } else {
28 b.appendChild( e );
29 }
30 }
31
32 var m = document.createElement( 'div' );
33 m.appendChild( document.createTextNode( text ) );
34
35 e.appendChild( m );
36
37 return true;
38 }
39
40 /**
41 * Compatibility wrapper for creating a new XMLHttpRequest object.
42 */
43 function sajax_init_object() {
44 sajax_debug( 'sajax_init_object() called..' );
45 var A;
46 try {
47 // Try the new style before ActiveX so we don't
48 // unnecessarily trigger warnings in IE 7 when
49 // set to prompt about ActiveX usage
50 A = new XMLHttpRequest();
51 } catch ( e ) {
52 try {
53 A = new ActiveXObject( 'Msxml2.XMLHTTP' );
54 } catch ( e ) {
55 try {
56 A = new ActiveXObject( 'Microsoft.XMLHTTP' );
57 } catch ( oc ) {
58 A = null;
59 }
60 }
61 }
62 if ( !A ) {
63 sajax_debug( 'Could not create connection object.' );
64 }
65
66 return A;
67 }
68
69 /**
70 * Perform an AJAX call to MediaWiki. Calls are handled by AjaxDispatcher.php
71 * func_name - the name of the function to call. Must be registered in $wgAjaxExportList
72 * args - an array of arguments to that function
73 * target - the target that will handle the result of the call. If this is a function,
74 * if will be called with the XMLHttpRequest as a parameter; if it's an input
75 * element, its value will be set to the resultText; if it's another type of
76 * element, its innerHTML will be set to the resultText.
77 *
78 * Example:
79 * sajax_do_call( 'doFoo', [1, 2, 3], document.getElementById( 'showFoo' ) );
80 *
81 * This will call the doFoo function via MediaWiki's AjaxDispatcher, with
82 * (1, 2, 3) as the parameter list, and will show the result in the element
83 * with id = showFoo
84 */
85 function sajax_do_call( func_name, args, target ) {
86 var i, x, n;
87 var uri;
88 var post_data;
89 uri = wgServer +
90 ( ( wgScript == null ) ? ( wgScriptPath + '/index.php' ) : wgScript ) +
91 '?action=ajax';
92 if ( sajax_request_type == 'GET' ) {
93 if ( uri.indexOf( '?' ) == -1 ) {
94 uri = uri + '?rs=' + encodeURIComponent( func_name );
95 } else {
96 uri = uri + '&rs=' + encodeURIComponent( func_name );
97 }
98 for ( i = 0; i < args.length; i++ ) {
99 uri = uri + '&rsargs[]=' + encodeURIComponent( args[i] );
100 }
101 //uri = uri + '&rsrnd=' + new Date().getTime();
102 post_data = null;
103 } else {
104 post_data = 'rs=' + encodeURIComponent( func_name );
105 for ( i = 0; i < args.length; i++ ) {
106 post_data = post_data + '&rsargs[]=' + encodeURIComponent( args[i] );
107 }
108 }
109 x = sajax_init_object();
110 if ( !x ) {
111 alert( 'AJAX not supported' );
112 return false;
113 }
114
115 try {
116 x.open( sajax_request_type, uri, true );
117 } catch ( e ) {
118 if ( window.location.hostname == 'localhost' ) {
119 alert( "Your browser blocks XMLHttpRequest to 'localhost', try using a real hostname for development/testing." );
120 }
121 throw e;
122 }
123 if ( sajax_request_type == 'POST' ) {
124 x.setRequestHeader( 'Method', 'POST ' + uri + ' HTTP/1.1' );
125 x.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
126 }
127 x.setRequestHeader( 'Pragma', 'cache=yes' );
128 x.setRequestHeader( 'Cache-Control', 'no-transform' );
129 x.onreadystatechange = function() {
130 if ( x.readyState != 4 ) {
131 return;
132 }
133
134 sajax_debug( 'received (' + x.status + ' ' + x.statusText + ') ' + x.responseText );
135
136 //if ( x.status != 200 )
137 // alert( 'Error: ' + x.status + ' ' + x.statusText + ': ' + x.responseText );
138 //else
139
140 if ( typeof( target ) == 'function' ) {
141 target( x );
142 } else if ( typeof( target ) == 'object' ) {
143 if ( target.tagName == 'INPUT' ) {
144 if ( x.status == 200 ) {
145 target.value= x.responseText;
146 }
147 //else alert( 'Error: ' + x.status + ' ' + x.statusText + ' (' + x.responseText + ')' );
148 } else {
149 if ( x.status == 200 ) {
150 target.innerHTML = x.responseText;
151 } else {
152 target.innerHTML = '<div class="error">Error: ' + x.status +
153 ' ' + x.statusText + ' (' + x.responseText + ')</div>';
154 }
155 }
156 } else {
157 alert( 'bad target for sajax_do_call: not a function or object: ' + target );
158 }
159
160 return;
161 }
162
163 sajax_debug( func_name + ' uri = ' + uri + ' / post = ' + post_data );
164 x.send( post_data );
165 sajax_debug( func_name + ' waiting..' );
166 delete x;
167
168 return true;
169 }
170
171 /**
172 * @return boolean whether the browser supports XMLHttpRequest
173 */
174 function wfSupportsAjax() {
175 var request = sajax_init_object();
176 var supportsAjax = request ? true : false;
177 delete request;
178 return supportsAjax;
179 }