map = {
const image_orig = 10
var current_country = ""
const projectionTranslator = d3.geoNaturalEarth1()
.fitSize([width, height], countries);
const pathRenderer = d3.geoPath().projection(projectionTranslator);
const svg = d3
.select(DOM.element('svg'))
.attr('width', width)
.attr('height', height)
.attr('style', "font-family: 'Lato';")
svg.selectAll("clipPath")
.data(countries.features)
.join("clipPath")
.attr("id", d => d.properties.NAME.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"").replace(/\s+/g, ''))
.attr("test", d=> console.log(d.properties.NAME.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"").replace(/\s+/g, '')))
.append("circle")
.attr('cx', d => pathRenderer.centroid(d)[0])
.attr('cy', d => pathRenderer.centroid(d)[1])
.attr("r", (width/image_orig)/2)
const relevant_countries = countries.features.filter(function(d) { return d.properties.NAME in countryLookup; })
const map = svg.append("g").attr("id", "map")
map.selectAll("path")
.data(countries.features)
.join('path')
.attr("d", d => pathRenderer(d))
.attr("fill", d=> d.properties.NAME == current_country? "blue": "#eee")
.attr("stroke", "black")
.attr("stroke-width", 1)
.on("mouseover", (event, d) => (leafMouseOverCountry(event, d), event.stopPropagation()))
.on("mouseout", (event, d) => (leafMouseOutCountry(event, d), event.stopPropagation()))
.on("touchstart", (event, d) => leafMouseOver(event, d))
.on("touchend", (event, d) => (leafMouseOutCountry(event, d), event.stopPropagation()))
map
.selectAll("image")
.data(relevant_countries)
.join("image")
.attr("xlink:href", function(d) { return d.properties.NAME in countryLookup? countryLookup[d.properties.NAME].scr: ""})
.attr("height", function(d) { return d.properties.NAME in countryLookup? 1.2 *(width/image_orig): 0})
.attr('x', d => pathRenderer.centroid(d)[0] -(width/image_orig)/2 )
.attr('y', d => pathRenderer.centroid(d)[1] - (width/image_orig)/2)
.attr("clip-path", d => `url(#${d.properties.NAME.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"").replace(/\s+/g, '')})`)
.on("mouseover", (event, d) => (leafMouseOver(event, d), event.stopPropagation()))
.on("mouseout", (event, d) => (leafMouseOut(event, d), event.stopPropagation()))
.on("touchstart", (event, d) => leafMouseOver(event, d))
.on("touchend", (event, d) => (leafMouseOut(event, d), event.stopPropagation()))
map.selectAll("text")
.data(relevant_countries)
.join("text")
.attr('x', d => pathRenderer.centroid(d)[0])
.attr('y', d => pathRenderer.centroid(d)[1] + (width/image_orig)/2)
.text("Hover here")
.on("mouseover", (event, d) => (leafMouseOver(event, d), event.stopPropagation()))
.on("mouseout", (event, d) => (leafMouseOutText(event, d), event.stopPropagation()))
.on("touchstart", (event, d) => leafMouseOver(event, d))
.on("touchend", (event, d) => (leafMouseOutText(event, d), event.stopPropagation()))
const toolTip = d3.select("body")
.append("div")
.attr('class', 'hover-info')
.style("position", "absolute")
.style("display", "none")
.style("border-radius", "1px")
.style("width", width)
.style("height", "auto")
const toolTip_country = d3.select("body")
.append("div")
.attr('class', 'name-info')
.style("position", "absolute")
.style("display", "none")
.style("border-radius", "1px")
.style("width", "auto")
.style("height", "auto")
function leafMouseOver(event, d){
d3.selectAll(".current").remove()
current_country = d.properties.NAME
const current_countries_data = countries.features.filter(function(d) { return d.properties.NAME== current_country; })
map.selectAll("path")
.attr("fill", d=> d.properties.NAME == current_country? "darkblue": "#eee")
d3.selectAll('.hover-info')
.style("display", "none" )
d3.selectAll("image")
.transition()
.duration(500)
.style('opacity', .2)
d3.selectAll('.triangle')
.style("display", "none" )
var full_text = countryLookup[d.properties.NAME].long_text
d3.select(event.target)
.attr("fill", "darkred")
map
.selectAll(".current")
.data(current_countries_data)
.join("image")
.attr("class", "current")
.attr("xlink:href", function(d) { return d.properties.NAME in countryLookup? countryLookup[d.properties.NAME].scr: ""})
.attr("height", 150)
.style("display", "block" )
.attr('x', 0)
.attr('y', 0)
toolTip
.style('top', `${event.pageY + 5}px`)
.style('left', `0px`)
.style('z-index', 100)
.style("background-color", "white")
.html(
`${full_text}`,
)
.style("display", "block")
.append('div')
.attr('class', 'triangle')
.style("background-color", "white")
d3.selectAll('.triangle')
.style('top', `${-Math.sqrt(20) - 3}px`)
.style('left', `${event.pageX }px `)
d3.select(event.target)
.style('opacity', .9)
}
function leafMouseOut(event, d){
d3.select(event.target)
.attr("fill", "#eee")
d3.selectAll("image")
.style('opacity', 1)
d3.selectAll(".current").remove()
}
function leafMouseOutText(event, d){
d3.selectAll("text")
.attr("fill", "black")
d3.selectAll("image")
.style('opacity', 1)
d3.selectAll(".current").remove()
}
function leafMouseOverCountry(event, d){
d3.select(event.target)
.attr("fill", "#eee")
d3.selectAll("image")
.style('opacity', 1)
var full_text = d.properties.NAME
toolTip
.style("display", "none" )
d3.select(event.target)
.attr("fill", "darkblue")
toolTip_country
.style('top', `${event.pageY + 20}px`)
.style('left', `${event.pageX + 20}px`)
.html(
`${full_text}`,
)
.style("display", "block")
.append('div')
.attr('class', 'triangle')
d3.selectAll('.triangle')
.style('top', `${-Math.sqrt(20) - 3}px`)
.style('left', `${event.pageX - 20}px `)
d3.selectAll(".current").remove()
}
function leafMouseOutCountry(event, d){
d3.selectAll("image")
.style('opacity', 1)
toolTip
.style("display", "none" )
d3.selectAll('.hover-info')
.style("display", "none" )
d3.selectAll('.name-info')
.style("display", "none" )
d3.selectAll('.triangle')
.style("display", "none" )
d3.select(event.target)
.attr("fill", "#eee")
d3.selectAll(".current").remove()
}
return svg.node();
}