[ Index ] |
PHP Cross Reference of MediaWiki-1.24.0 |
[Summary view] [Print] [Text view]
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 b, m, 23 e = document.getElementById( 'sajax_debug' ); 24 25 if ( !e ) { 26 e = document.createElement( 'p' ); 27 e.className = 'sajax_debug'; 28 e.id = 'sajax_debug'; 29 30 b = document.getElementsByTagName( 'body' )[0]; 31 32 if ( b.firstChild ) { 33 b.insertBefore( e, b.firstChild ); 34 } else { 35 b.appendChild( e ); 36 } 37 } 38 39 m = document.createElement( 'div' ); 40 m.appendChild( document.createTextNode( text ) ); 41 42 e.appendChild( m ); 43 44 return true; 45 } 46 47 /** 48 * Compatibility wrapper for creating a new XMLHttpRequest object. 49 */ 50 function createXhr() { 51 debug( 'sajax_init_object() called..' ); 52 var a; 53 try { 54 // Try the new style before ActiveX so we don't 55 // unnecessarily trigger warnings in IE 7 when 56 // set to prompt about ActiveX usage 57 a = new XMLHttpRequest(); 58 } catch ( xhrE ) { 59 try { 60 a = new window.ActiveXObject( 'Msxml2.XMLHTTP' ); 61 } catch ( msXmlE ) { 62 try { 63 a = new window.ActiveXObject( 'Microsoft.XMLHTTP' ); 64 } catch ( msXhrE ) { 65 a = null; 66 } 67 } 68 } 69 if ( !a ) { 70 debug( 'Could not create connection object.' ); 71 } 72 73 return a; 74 } 75 76 /** 77 * Perform an AJAX call to MediaWiki. Calls are handled by AjaxDispatcher.php 78 * func_name - the name of the function to call. Must be registered in $wgAjaxExportList 79 * args - an array of arguments to that function 80 * target - the target that will handle the result of the call. If this is a function, 81 * if will be called with the XMLHttpRequest as a parameter; if it's an input 82 * element, its value will be set to the resultText; if it's another type of 83 * element, its innerHTML will be set to the resultText. 84 * 85 * Example: 86 * sajax_do_call( 'doFoo', [1, 2, 3], document.getElementById( 'showFoo' ) ); 87 * 88 * This will call the doFoo function via MediaWiki's AjaxDispatcher, with 89 * (1, 2, 3) as the parameter list, and will show the result in the element 90 * with id = showFoo 91 */ 92 function doAjaxRequest( func_name, args, target ) { 93 var i, x, uri, post_data; 94 uri = mw.util.wikiScript() + '?action=ajax'; 95 if ( window.sajax_request_type === 'GET' ) { 96 if ( uri.indexOf( '?' ) === -1 ) { 97 uri = uri + '?rs=' + encodeURIComponent( func_name ); 98 } else { 99 uri = uri + '&rs=' + encodeURIComponent( func_name ); 100 } 101 for ( i = 0; i < args.length; i++ ) { 102 uri = uri + '&rsargs[]=' + encodeURIComponent( args[i] ); 103 } 104 //uri = uri + '&rsrnd=' + new Date().getTime(); 105 post_data = null; 106 } else { 107 post_data = 'rs=' + encodeURIComponent( func_name ); 108 for ( i = 0; i < args.length; i++ ) { 109 post_data = post_data + '&rsargs[]=' + encodeURIComponent( args[i] ); 110 } 111 } 112 x = createXhr(); 113 if ( !x ) { 114 alert( 'AJAX not supported' ); 115 return false; 116 } 117 118 try { 119 x.open( window.sajax_request_type, uri, true ); 120 } catch ( e ) { 121 if ( location.hostname === 'localhost' ) { 122 alert( 'Your browser blocks XMLHttpRequest to "localhost", try using a real hostname for development/testing.' ); 123 } 124 throw e; 125 } 126 if ( window.sajax_request_type === 'POST' ) { 127 x.setRequestHeader( 'Method', 'POST ' + uri + ' HTTP/1.1' ); 128 x.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' ); 129 } 130 x.setRequestHeader( 'Pragma', 'cache=yes' ); 131 x.setRequestHeader( 'Cache-Control', 'no-transform' ); 132 x.onreadystatechange = function () { 133 if ( x.readyState !== 4 ) { 134 return; 135 } 136 137 debug( 'received (' + x.status + ' ' + x.statusText + ') ' + x.responseText ); 138 139 //if ( x.status != 200 ) 140 // alert( 'Error: ' + x.status + ' ' + x.statusText + ': ' + x.responseText ); 141 //else 142 143 if ( typeof target === 'function' ) { 144 target( x ); 145 } else if ( typeof target === 'object' ) { 146 if ( target.tagName === 'INPUT' ) { 147 if ( x.status === 200 ) { 148 target.value = x.responseText; 149 } 150 //else alert( 'Error: ' + x.status + ' ' + x.statusText + ' (' + x.responseText + ')' ); 151 } else { 152 if ( x.status === 200 ) { 153 target.innerHTML = x.responseText; 154 } else { 155 target.innerHTML = '<div class="error">Error: ' + x.status + 156 ' ' + x.statusText + ' (' + x.responseText + ')</div>'; 157 } 158 } 159 } else { 160 alert( 'Bad target for sajax_do_call: not a function or object: ' + target ); 161 } 162 }; 163 164 debug( func_name + ' uri = ' + uri + ' / post = ' + post_data ); 165 x.send( post_data ); 166 debug( func_name + ' waiting..' ); 167 168 return true; 169 } 170 171 /** 172 * @return {boolean} Whether the browser supports AJAX 173 */ 174 function wfSupportsAjax() { 175 var request = createXhr(), 176 supportsAjax = request ? true : false; 177 178 request = undefined; 179 return supportsAjax; 180 } 181 182 // Expose + Mark as deprecated 183 var deprecationNotice = 'Sajax is deprecated, use jQuery.ajax or mediawiki.api instead.'; 184 185 // Variables 186 mw.log.deprecate( window, 'sajax_debug_mode', false, deprecationNotice ); 187 mw.log.deprecate( window, 'sajax_request_type', 'GET', deprecationNotice ); 188 // Methods 189 mw.log.deprecate( window, 'sajax_debug', debug, deprecationNotice ); 190 mw.log.deprecate( window, 'sajax_init_object', createXhr, deprecationNotice ); 191 mw.log.deprecate( window, 'sajax_do_call', doAjaxRequest, deprecationNotice ); 192 mw.log.deprecate( window, 'wfSupportsAjax', wfSupportsAjax, deprecationNotice ); 193 194 }( mediaWiki ) );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 14:03:12 2014 | Cross-referenced by PHPXref 0.7.1 |