mirror of
https://github.com/FoggedLens/iD.git
synced 2026-02-13 01:02:58 +00:00
44 lines
1019 B
JavaScript
44 lines
1019 B
JavaScript
import { select as d3_select } from 'd3-selection';
|
|
|
|
|
|
var jsonpCache = {};
|
|
window.jsonpCache = jsonpCache;
|
|
|
|
export function jsonpRequest(url, callback) {
|
|
|
|
if (window.JSONP_FIX) {
|
|
if (window.JSONP_DELAY === 0) {
|
|
callback(window.JSONP_FIX);
|
|
} else {
|
|
setTimeout(function() {
|
|
callback(window.JSONP_FIX);
|
|
}, window.JSONP_DELAY || 0);
|
|
}
|
|
return;
|
|
}
|
|
|
|
function rand() {
|
|
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
|
|
c = '', i = -1;
|
|
while (++i < 15) c += chars.charAt(Math.floor(Math.random() * 52));
|
|
return c;
|
|
}
|
|
|
|
function create(url) {
|
|
var e = url.match(/callback=(\w+)/),
|
|
c = e ? e[1] : rand();
|
|
jsonpCache[c] = function(data) {
|
|
callback(data);
|
|
delete jsonpCache[c];
|
|
script.remove();
|
|
};
|
|
return 'jsonpCache.' + c;
|
|
}
|
|
|
|
var cb = create(url),
|
|
script = d3_select('head')
|
|
.append('script')
|
|
.attr('type', 'text/javascript')
|
|
.attr('src', url.replace(/(\{|%7B)callback(\}|%7D)/, cb));
|
|
}
|