Skip to content

Commit

Permalink
Merge pull request #19 from AnalyticalGraphicsInc/refactor_jsonp
Browse files Browse the repository at this point in the history
Replace JSONP implementation.
  • Loading branch information
pjcozzi committed May 10, 2012
2 parents 58a86ee + e819912 commit 02e8a3c
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 232 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ What's New
* Renamed Tipsify.CalculateACMR to Tipsify.calculateACMR.
* Renamed LeapSecond.CompareLeapSecondDate to LeapSecond.compareLeapSecondDate.
* Geoscope.JSONP.get is now Cesium.jsonp.
* Cesium.jsonp now takes a url, a callback function, and an options object. The
previous 2nd and 4th parameters are now specified using the options object.
* TWEEN is no longer globally defined, and is instead available as Cesium.Tween.
* Chain.js functions such as run are now moved to Cesium.Chain.run, etc.
* Geoscope.CollectionAlgorithms.binarySearch is now Cesium.binarySearch.
Expand Down
8 changes: 0 additions & 8 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,6 @@ chain.js
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Lightweight-JSONP

https://github.com/IntoMethod/Lightweight-JSONP

Lightweight JSONP fetcher
Copyright 2010 Erik Karlsson. All rights reserved.
BSD licensed

webgl-noise

https://github.com/ashima/webgl-noise
Expand Down
5 changes: 3 additions & 2 deletions Source/Core/createGuid.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ define(function() {
function createGuid() {
// http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
var r = Math.random() * 16 | 0;
var v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}

return createGuid;
});
});
88 changes: 88 additions & 0 deletions Source/Core/jsonp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*global define*/
define(['./DeveloperError'], function(DeveloperError) {
'use strict';

function pushQueryParameter(array, name, value) {
array.push(encodeURIComponent(name) + '=' + encodeURIComponent(value));
}

/**
* Requests a resource using JSONP.
*
* @param {String} url The URL to request.
* @param {Function} callback The callback function to call, passing the requested resource as the single parameter.
* @param {Object} [options.parameters] Any extra query parameters to append to the URL.
* @param {String} [options.callbackParameterName='callback'] The callback parameter name that the server expects.
* @param {Object} [options.proxy] A proxy to use for the request. This object is expected to have a getURL function which returns the proxied URL, if needed.
*/
function jsonp(url, callback, options) {
if (typeof url === 'undefined') {
throw new DeveloperError('url is required.', 'url');
}

if (typeof callback === 'undefined') {
throw new DeveloperError('callback is required.', 'callback');
}

options = typeof options !== 'undefined' ? options : {};

//generate a unique function name
var functionName;
do {
functionName = 'jsonp' + Math.random().toString().substring(2, 8);
} while (typeof window[functionName] !== 'undefined');

//assign a function with that name in the global scope
window[functionName] = function(data) {
callback(data);

try {
delete window[functionName];
} catch (e) {
window[functionName] = undefined;
}
};

var callbackParameterName = typeof options.callbackParameterName !== 'undefined' ? options.callbackParameterName : 'callback';
var queryParts = [];
pushQueryParameter(queryParts, callbackParameterName, functionName);

var parameters = options.parameters;
if (typeof parameters !== 'undefined') {
for ( var name in parameters) {
if (parameters.hasOwnProperty(name)) {
pushQueryParameter(queryParts, name, parameters[name]);
}
}
}

if (queryParts.length > 0) {
if (url.indexOf('?') === -1) {
url += '?';
} else {
url += '&';
}

url += queryParts.join('&');
}

var proxy = options.proxy;
if (typeof proxy !== 'undefined') {
url = proxy.getURL(url);
}

var script = document.createElement('script');
script.async = true;
script.src = url;

var head = document.getElementsByTagName('head')[0];
script.onload = function() {
script.onload = undefined;
head.removeChild(script);
};

head.appendChild(script);
}

return jsonp;
});
39 changes: 12 additions & 27 deletions Source/Scene/ArcGISTileProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
define([
'../Core/DeveloperError',
'../Core/Math',
'../ThirdParty/jsonp',
'../Core/jsonp',
'./Projections'
], function(
DeveloperError,
Expand Down Expand Up @@ -85,12 +85,7 @@ define([
*/
this.service = desc.service;

/**
* A proxy to use for requests. This object is expected to have a getURL
* function which returns the proxied URL, if needed.
* @type {Object}
*/
this.proxy = desc.proxy;
this._proxy = desc.proxy;

// TODO: Get this information from the server

Expand Down Expand Up @@ -147,14 +142,7 @@ define([
this._logoLoaded = false;

var that = this;
var url = this._url;
if (typeof this.proxy !== 'undefined') {
url = this.proxy.getURL(url);
}

jsonp(url, {
f : 'json'
}, function(data) {
jsonp(this._url, function(data) {
var credit = data.copyrightText;

var canvas = document.createElement("canvas");
Expand All @@ -169,6 +157,11 @@ define([

that._logo = canvas;
that._logoLoaded = true;
}, {
parameters : {
f : 'json'
},
proxy : this._proxy
});
}

Expand All @@ -190,21 +183,13 @@ define([
}

var image = new Image();
if (onload && typeof onload === "function") {
image.onload = function() {
onload();
};
}
if (onerror && typeof onerror === "function") {
image.onerror = function() {
onerror();
};
}
image.onload = onload;
image.onerror = onerror;
image.crossOrigin = '';

var url = this._url + '/tile/' + tile.zoom + '/' + tile.y + '/' + tile.x;
if (typeof this.proxy !== 'undefined') {
url = this.proxy.getURL(url);
if (typeof this._proxy !== 'undefined') {
url = this._proxy.getURL(url);
}

image.src = url;
Expand Down

0 comments on commit 02e8a3c

Please sign in to comment.