MCPcopy Create free account
hub / github.com/BloombergGraphics/whatiscode / highlightTerms

Function highlightTerms

scripts/glossary.js:36–54  ·  view source on GitHub ↗
(dictionary)

Source from the content-addressed store, hash-verified

34}
35
36function highlightTerms(dictionary) {
37 var html = d3.select("article").html();
38 dictionary.forEach(function(value, index) {
39 var re = new RegExp(escapeRegExp(value.title),"gi");
40 html = html.replace(re, function(match) { return "<span class='glossary-item' data-term='"+value.title+"'>"+match+"</span>"; });
41 });
42 d3.select("article").html(html);
43
44 var tooltip = d3.select("body").append("div").classed("glossary-tooltip", true);
45 d3.selectAll(".glossary-item").on("mouseover", function(d) {
46 console.log(this.dataset.term, _.findWhere(glossaryLite, {"title": this.dataset.term}))
47 tooltip.classed("on", true)
48 .style("top", (this.getBoundingClientRect().top+d3.select("body").node().scrollTop)+"px")
49 .style("left", this.getBoundingClientRect().left+"px")
50 .text(_.findWhere(glossaryLite, {"title": this.dataset.term}).description);
51 }).on("mouseout", function(d) {
52 tooltip.classed("on", false);
53 })
54}
55
56function escapeRegExp(str) {
57 return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");

Callers 1

buildGlossaryFunction · 0.85

Calls 1

escapeRegExpFunction · 0.85

Tested by

no test coverage detected