Added WoltLab/WCF/Ajax/Jsonp to issue JSONP requests
authorAlexander Ebert <ebert@woltlab.com>
Sun, 24 May 2015 14:00:20 +0000 (16:00 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Sun, 24 May 2015 14:00:44 +0000 (16:00 +0200)
wcfsetup/install/files/js/WoltLab/WCF/Ajax/Jsonp.js [new file with mode: 0644]
wcfsetup/install/files/js/require.config.js

diff --git a/wcfsetup/install/files/js/WoltLab/WCF/Ajax/Jsonp.js b/wcfsetup/install/files/js/WoltLab/WCF/Ajax/Jsonp.js
new file mode 100644 (file)
index 0000000..12a7de8
--- /dev/null
@@ -0,0 +1,68 @@
+/**
+ * Provides a utility class to issue JSONP requests.
+ * 
+ * @author     Alexander Ebert
+ * @copyright  2001-2015 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module     WoltLab/WCF/Ajax/Jsonp
+ */
+define(['Core'], function(Core) {
+       "use strict";
+       
+       /**
+        * @constructor
+        */
+       function AjaxJsonp() {};
+       AjaxJsonp.prototype = {
+               /**
+                * Issues a JSONP request.
+                * 
+                * @param       {string}                url             source URL, must not contain callback parameter
+                * @param       {function}              success         success callback
+                * @param       {function=}             failure         timeout callback
+                * @param       {object<string, *>=}    options         request options
+                */
+               send: function(url, success, failure, options) {
+                       url = (typeof url === 'string') ? url.trim() : '';
+                       if (url.length === 0) {
+                               throw new Error("Expected a non-empty string for parameter 'url'.");
+                       }
+                       
+                       if (typeof success !== 'function') {
+                               throw new TypeError("Expected a valid callback function for parameter 'success'.");
+                       }
+                       
+                       options = Core.extend({
+                               parameterName: 'callback',
+                               timeout: 10
+                       }, options || {});
+                       
+                       var callbackName = 'wcf_jsonp_' + Core.getUuid().replace(/-/g, '').substr(0, 8);
+                       
+                       var timeout = window.setTimeout(function() {
+                               window[callbackName] = function() {};
+                               
+                               if (typeof failure === 'function') {
+                                       failure();
+                               }
+                       }, (~~options.timeout || 10) * 1000);
+                       
+                       window[callbackName] = function() {
+                               window.clearTimeout(timeout);
+                               
+                               success.apply(null, arguments);
+                       };
+                       
+                       url += (url.indexOf('?') === -1) ? '?' : '&';
+                       url += options.parameterName + '=' + callbackName;
+                       
+                       var script = document.createElement('script');
+                       script.setAttribute('async', true);
+                       script.setAttribute('src', url);
+                       
+                       document.head.appendChild(script);
+               }
+       };
+       
+       return new AjaxJsonp();
+});
index e1995978224706f58de38e57f32fad7422c585c0..17a8071165a06411efcb0e3262c99759130c6587 100644 (file)
@@ -6,6 +6,7 @@ requirejs.config({
        map: {
                '*': {
                        'Ajax': 'WoltLab/WCF/Ajax',
+                       'AjaxJsonp': 'WoltLab/WCF/Ajax/Jsonp',
                        'AjaxRequest': 'WoltLab/WCF/Ajax/Request',
                        'CallbackList': 'WoltLab/WCF/CallbackList',
                        'Core': 'WoltLab/WCF/Core',