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