JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5.3
[ckeditor.git] / _source / plugins / ajax / plugin.js
diff --git a/_source/plugins/ajax/plugin.js b/_source/plugins/ajax/plugin.js
new file mode 100644 (file)
index 0000000..a4b713a
--- /dev/null
@@ -0,0 +1,152 @@
+/*\r
+Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
+For licensing, see LICENSE.html or http://ckeditor.com/license\r
+*/\r
+\r
+/**\r
+ * @fileOverview Defines the {@link CKEDITOR.ajax} object, which holds ajax methods for\r
+ *             data loading.\r
+ */\r
+\r
+(function()\r
+{\r
+       CKEDITOR.plugins.add( 'ajax',\r
+               {\r
+                       requires : [ 'xml' ]\r
+               });\r
+\r
+       /**\r
+        * Ajax methods for data loading.\r
+        * @namespace\r
+        * @example\r
+        */\r
+       CKEDITOR.ajax = (function()\r
+       {\r
+               var createXMLHttpRequest = function()\r
+               {\r
+                       // In IE, using the native XMLHttpRequest for local files may throw\r
+                       // "Access is Denied" errors.\r
+                       if ( !CKEDITOR.env.ie || location.protocol != 'file:' )\r
+                               try { return new XMLHttpRequest(); } catch(e) {}\r
+\r
+                       try { return new ActiveXObject( 'Msxml2.XMLHTTP' ); } catch (e) {}\r
+                       try { return new ActiveXObject( 'Microsoft.XMLHTTP' ); } catch (e) {}\r
+\r
+                       return null;\r
+               };\r
+\r
+               var checkStatus = function( xhr )\r
+               {\r
+                       // HTTP Status Codes:\r
+                       //       2xx : Success\r
+                       //       304 : Not Modified\r
+                       //         0 : Returned when running locally (file://)\r
+                       //      1223 : IE may change 204 to 1223 (see http://dev.jquery.com/ticket/1450)\r
+\r
+                       return ( xhr.readyState == 4 &&\r
+                                       (       ( xhr.status >= 200 && xhr.status < 300 ) ||\r
+                                               xhr.status == 304 ||\r
+                                               xhr.status === 0 ||\r
+                                               xhr.status == 1223 ) );\r
+               };\r
+\r
+               var getResponseText = function( xhr )\r
+               {\r
+                       if ( checkStatus( xhr ) )\r
+                               return xhr.responseText;\r
+                       return null;\r
+               };\r
+\r
+               var getResponseXml = function( xhr )\r
+               {\r
+                       if ( checkStatus( xhr ) )\r
+                       {\r
+                               var xml = xhr.responseXML;\r
+                               return new CKEDITOR.xml( xml && xml.firstChild ? xml : xhr.responseText );\r
+                       }\r
+                       return null;\r
+               };\r
+\r
+               var load = function( url, callback, getResponseFn )\r
+               {\r
+                       var async = !!callback;\r
+\r
+                       var xhr = createXMLHttpRequest();\r
+\r
+                       if ( !xhr )\r
+                               return null;\r
+\r
+                       xhr.open( 'GET', url, async );\r
+\r
+                       if ( async )\r
+                       {\r
+                               // TODO: perform leak checks on this closure.\r
+                               /** @ignore */\r
+                               xhr.onreadystatechange = function()\r
+                               {\r
+                                       if ( xhr.readyState == 4 )\r
+                                       {\r
+                                               callback( getResponseFn( xhr ) );\r
+                                               xhr = null;\r
+                                       }\r
+                               };\r
+                       }\r
+\r
+                       xhr.send(null);\r
+\r
+                       return async ? '' : getResponseFn( xhr );\r
+               };\r
+\r
+               return  /** @lends CKEDITOR.ajax */ {\r
+\r
+                       /**\r
+                        * Loads data from an URL as plain text.\r
+                        * @param {String} url The URL from which load data.\r
+                        * @param {Function} [callback] A callback function to be called on\r
+                        *              data load. If not provided, the data will be loaded\r
+                        *              synchronously.\r
+                        * @returns {String} The loaded data. For asynchronous requests, an\r
+                        *              empty string. For invalid requests, null.\r
+                        * @example\r
+                        * // Load data synchronously.\r
+                        * var data = CKEDITOR.ajax.load( 'somedata.txt' );\r
+                        * alert( data );\r
+                        * @example\r
+                        * // Load data asynchronously.\r
+                        * var data = CKEDITOR.ajax.load( 'somedata.txt', function( data )\r
+                        *     {\r
+                        *         alert( data );\r
+                        *     } );\r
+                        */\r
+                       load : function( url, callback )\r
+                       {\r
+                               return load( url, callback, getResponseText );\r
+                       },\r
+\r
+                       /**\r
+                        * Loads data from an URL as XML.\r
+                        * @param {String} url The URL from which load data.\r
+                        * @param {Function} [callback] A callback function to be called on\r
+                        *              data load. If not provided, the data will be loaded\r
+                        *              synchronously.\r
+                        * @returns {CKEDITOR.xml} An XML object holding the loaded data. For asynchronous requests, an\r
+                        *              empty string. For invalid requests, null.\r
+                        * @example\r
+                        * // Load XML synchronously.\r
+                        * var xml = CKEDITOR.ajax.loadXml( 'somedata.xml' );\r
+                        * alert( xml.getInnerXml( '//' ) );\r
+                        * @example\r
+                        * // Load XML asynchronously.\r
+                        * var data = CKEDITOR.ajax.loadXml( 'somedata.xml', function( xml )\r
+                        *     {\r
+                        *         alert( xml.getInnerXml( '//' ) );\r
+                        *     } );\r
+                        */\r
+                       loadXml : function( url, callback )\r
+                       {\r
+                               return load( url, callback, getResponseXml );\r
+                       }\r
+               };\r
+       })();\r
+\r
+})();\r