Skip to content

Instantly share code, notes, and snippets.

@tristanz
Last active December 19, 2015 07:59
Show Gist options
  • Save tristanz/5922623 to your computer and use it in GitHub Desktop.
Save tristanz/5922623 to your computer and use it in GitHub Desktop.
(function() {
console.log('cartodb');
var INCLUDE_GMAPS = false ;
var VIZJSON_URL = 'https://sense.cartodb.com/api/v1/viz/15450/viz';
var ACCOUNT_TYPE = "free";
var IS_CUSTOM_INSTALL = false;
var doc = document;
var head = doc.getElementsByTagName('head')[0];
var global_info = window.__cartodb_embed = window.__cartodb_embed || {};
global_info.scripts_loaded = global_info.scripts_loaded || [];
global_info.load_queue = global_info.load_queue || [];
global_info.gmaps_queue = global_info.gmaps_queue || [];
var lib_url;
lib_url = "https://d3voyrscnb0slx.cloudfront.net/cartodb.js/v2/"
function contains(array, v) {
for(var i = 0; i < array.length; ++i) {
if(array[i] == v) return true;
}
return false;
}
function search_embed_script_url() {
var i, len, src;
var scripts = document.getElementsByTagName('script');
for (i = 0, len = scripts.length; i < len; ++i) {
src = scripts[i].src;
if(src && src.indexOf('embed_map.js') != -1 && !contains(global_info.scripts_loaded, src)) {
global_info.scripts_loaded.push(src);
return src;
}
}
return null;
}
function get_url_params(url, conversion) {
conversion = conversion || {};
if(!url) {
return {};
}
var search = url.split('?');
if(search.length < 2) {
return {};
}
search = search[1];
var tokens = search.split('&');
var params = {};
for(var i = 0; i < tokens.length; ++i) {
var tk = tokens[i].split('=');
var fn = conversion[tk[0]] || function(v) { return v };
if(tk.length === 2) {
params[tk[0]] = fn(decodeURIComponent(tk[1]));
}
}
return params;
}
var url = search_embed_script_url();
var string_fn = function(v) { return v; }
var bool_fn = function(v) { return v == 'true' }
var logo_fn = function(v) { return ( ACCOUNT_TYPE != "coronelli" || IS_CUSTOM_INSTALL) ? true : v == 'true' };
var opt = get_url_params(url, {
'search': bool_fn,
'title': bool_fn,
'description': bool_fn,
'shareable': bool_fn,
'cartodb_logo': logo_fn,
'scrollwheel': bool_fn,
'debug': bool_fn,
'version': string_fn
});
function load_script(url, done) {
var script = doc.createElement('script');
script.type = 'text/javascript';
script.src = url;
script.async = true;
var loaded = false;
script.onload = script.onreadystatechange = function () {
if ((script['readyState'] && script['readyState'] !== 'complete' && script['readyState'] !== 'loaded') || loaded) {
return false
}
loaded = true;
done && done();
}
head.appendChild(script)
}
var height = opt.height || '400';
if(height.indexOf('%') == -1) {
height += 'px';
}
opt.version = opt.version ? (opt.version + '/'): '';
if(!global_info.css_included) {
doc.write('<link rel="stylesheet" href="'+ lib_url + opt.version + 'themes/css/cartodb.css" />');
var ua = navigator.userAgent;
if(ua.indexOf('MSIE 8') != -1 || ua.indexOf('MSIE 7') != -1) {
doc.write('<link rel="stylesheet" href="' + lib_url + opt.version + 'themes/css/cartodb.ie.css" />');
}
global_info.css_included = true
}
var map_id = "cartodb-map-" + new Date().getTime();
doc.write('<div class="cartodb-embed-map" id="' + map_id + '" style="height: ' + height + '"></div>');
var load_cartodb = function() {
if(typeof(cartodb) == "undefined") {
if(global_info.loading) {
global_info.load_queue.push([map_id, opt.url || VIZJSON_URL, opt]);
} else {
load_script(lib_url + '' + (opt.debug ? ( opt.version + 'cartodb.uncompressed.js'):'cartodb.js'), function() {
cartodb.createVis(map_id, opt.url || VIZJSON_URL, opt);
for(var g = 0; g < global_info.load_queue.length; ++g) {
var params = global_info.load_queue[g];
cartodb.createVis.apply(cartodb, params);
}
})
}
global_info.loading = true;
} else {
cartodb.createVis(map_id, opt.url || VIZJSON_URL, opt);
}
}
if(INCLUDE_GMAPS) {
if(!global_info.loading_gmaps && typeof(google) != 'undefined' && typeof(google.maps) != 'undefined') {
load_cartodb();
} else {
if(global_info.loading_gmaps) {
global_info.gmaps_queue.push(load_cartodb);
return;
}
global_info.loading_gmaps = true;
load_script("https://www.google.com/jsapi", function() {
google.load("maps", "3.8", {
"callback" : function() {
global_info.loading_gmaps = false;
load_cartodb()
for(var i = 0; i < global_info.gmaps_queue.length; ++i) {
global_info.gmaps_queue[i]();
}
},
other_params: "sensor=false"});
});
}
} else {
load_cartodb();
}
var l = document.createElement("a");
l.href = url;
console.log(l);
if(l.protocol.indexOf('http') != -1) {
var port = l.port ? ':' + l.port: '';
doc.write('<iframe style="display: none" src="' + l.protocol + '//' + l.hostname + port + '/tables/track_embed"></iframe>');
console.log('wrote iframe');
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment