www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README

ajax.js?207 (4777B)


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