1 /* 2 * CKEditor - The text editor for Internet - http://ckeditor.com 3 * Copyright (C) 2003-2008 Frederico Caldeira Knabben 4 * 5 * == BEGIN LICENSE == 6 * 7 * Licensed under the terms of any of the following licenses at your 8 * choice: 9 * 10 * - GNU General Public License Version 2 or later (the "GPL") 11 * http://www.gnu.org/licenses/gpl.html 12 * 13 * - GNU Lesser General Public License Version 2.1 or later (the "LGPL") 14 * http://www.gnu.org/licenses/lgpl.html 15 * 16 * - Mozilla Public License Version 1.1 or later (the "MPL") 17 * http://www.mozilla.org/MPL/MPL-1.1.html 18 * 19 * == END LICENSE == 20 */ 21 22 /** 23 * @fileOverview Defines the {@link CKEDITOR.ajax} object, which holds ajax methods for 24 * data loading. 25 */ 26 27 /** 28 * Ajax methods for data loading. 29 * @namespace 30 * @example 31 */ 32 CKEDITOR.ajax = (function() 33 { 34 var createXMLHttpRequest = function() 35 { 36 // In IE, using the native XMLHttpRequest for local files may throw 37 // "Access is Denied" errors. 38 if ( !CKEDITOR.env.IE || location.protocol != 'file:' ) 39 try { return new XMLHttpRequest(); } catch(e) {} 40 41 try { return new ActiveXObject( 'Msxml2.XMLHTTP' ); } catch (e) {} 42 try { return new ActiveXObject( 'Microsoft.XMLHTTP' ); } catch (e) {} 43 44 return null; 45 }; 46 47 var checkStatus = function( xhr ) 48 { 49 // HTTP Status Codes: 50 // 2xx : Success 51 // 304 : Not Modified 52 // 0 : Returned when running locally (file://) 53 // 1223 : IE may change 204 to 1223 (see http://dev.jquery.com/ticket/1450) 54 55 return ( xhr.readyState == 4 && 56 ( ( xhr.status >= 200 && xhr.status < 300 ) || 57 xhr.status == 304 || 58 xhr.status === 0 || 59 xhr.status == 1223 ) ); 60 }; 61 62 var getResponseText = function( xhr ) 63 { 64 if ( checkStatus( xhr ) ) 65 return xhr.responseText; 66 return null; 67 }; 68 69 var getResponseXml = function( xhr ) 70 { 71 if ( checkStatus( xhr ) ) 72 { 73 var xml = xhr.responseXML; 74 return new CKEDITOR.xml( xml && xml.firstChild ? xml : xhr.responseText ); 75 } 76 return null; 77 }; 78 79 var load = function( url, callback, getResponseFn ) 80 { 81 var async = !!callback; 82 83 var xhr = createXMLHttpRequest(); 84 85 if ( !xhr ) 86 return null; 87 88 xhr.open( 'GET', url, async ); 89 90 if ( async ) 91 { 92 // TODO: perform leak checks on this closure. 93 /** @ignore */ 94 xhr.onreadystatechange = function() 95 { 96 if ( xhr.readyState == 4 ) 97 { 98 callback( getResponseFn( xhr ) ); 99 xhr = null; 100 } 101 }; 102 } 103 104 xhr.send(null); 105 106 return async ? '' : getResponseFn( xhr ); 107 }; 108 109 return /** @lends CKEDITOR.ajax */ { 110 111 /** 112 * Loads data from an URL as plain text. 113 * @param {String} url The URL from which load data. 114 * @param {Function} [callback] A callback function to be called on 115 * data load. If not provided, the data will be loaded 116 * asynchronously, passing the data value the function on load. 117 * @returns {String} The loaded data. For asynchronous requests, an 118 * empty string. For invalid requests, null. 119 * @example 120 * // Load data synchronously. 121 * var data = CKEDITOR.ajax.load( 'somedata.txt' ); 122 * alert( data ); 123 * @example 124 * // Load data asynchronously. 125 * var data = CKEDITOR.ajax.load( 'somedata.txt', function( data ) 126 * { 127 * alert( data ); 128 * } ); 129 */ 130 load : function( url, callback ) 131 { 132 return load( url, callback, getResponseText ); 133 }, 134 135 /** 136 * Loads data from an URL as XML. 137 * @param {String} url The URL from which load data. 138 * @param {Function} [callback] A callback function to be called on 139 * data load. If not provided, the data will be loaded 140 * asynchronously, passing the data value the function on load. 141 * @returns {CKEDITOR.xml} An XML object holding the loaded data. For asynchronous requests, an 142 * empty string. For invalid requests, null. 143 * @example 144 * // Load XML synchronously. 145 * var xml = CKEDITOR.ajax.loadXml( 'somedata.xml' ); 146 * alert( xml.getOuterXml() ); 147 * @example 148 * // Load XML asynchronously. 149 * var data = CKEDITOR.ajax.loadXml( 'somedata.xml', function( xml ) 150 * { 151 * alert( xml.getOuterXml() ); 152 * } ); 153 */ 154 loadXml : function( url, callback ) 155 { 156 return load( url, callback, getResponseXml ); 157 } 158 }; 159 })(); 160