createEmbedCode = function (uid, type = "javascript") {
let parent = d3
.select("#dropdowncontainer" + uid)
.select(function () {
return this.parentNode;
})
.select(function () {
return this.parentNode;
})
.select(function () {
return this.parentNode;
})
.select(function () {
return this.parentNode;
});
let script = parent.select("script[type=module]");
if (script.empty()) {
return "embed code generation works only if the chart is embeded somewhere using the javascript runtime";
}
script = parent.select("script[type=module]").text();
let regex = /api\.observablehq\.com\/(.+)\.js/gm;
let m;
let urlslug;
while ((m = regex.exec(script)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
urlslug = m[1];
// The result can be accessed through the `m`-variable.
/*m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});*/
}
let regexcells = /name === "(.+)"\) return new/gm;
let cells = [];
while ((m = regexcells.exec(script)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
cells.push(m[1]);
// The result can be accessed through the `m`-variable.
/*m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});*/
}
let regexhiddencells = /name === "(.+)"\) return true/gm;
let hiddencells = [];
while ((m = regexhiddencells.exec(script)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
hiddencells.push(m[1]);
// The result can be accessed through the `m`-variable.
/*m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});*/
}
let hex = randomHex(4);
let code = ``;
if (type == "javascript") {
for (let cell of cells) {
code =
code +
`<div id="observablehq-` +
cell.replace(" ", "-") +
`-` +
hex +
`"></div>
`;
}
code =
code +
`<p>Quelle: <a href="https://observablehq.com/` +
urlslug +
`">Kanton Zürich</a></p>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@observablehq/inspector@5/dist/inspector.css">
<script type="module">
import {Runtime, Inspector} from "https://cdn.jsdelivr.net/npm/@observablehq/runtime@5/dist/runtime.js";
import define from "https://api.observablehq.com/` +
urlslug +
`.js?v=3";
new Runtime().module(define, name => {
`;
for (let cell of cells) {
code =
code +
` if (name === "` +
cell +
`") return new Inspector(document.querySelector("#observablehq-` +
cell.replace(" ", "-") +
`-` +
hex +
`"));
`;
}
for (let hiddencell of hiddencells) {
code =
code +
` if (name === "` +
hiddencell +
`") return true;
`;
}
code =
code +
`});
</script>`;
}
if (type == "iframe") {
/*code =
code +
`<iframe width="100%" height="` +
parent.node().getBoundingClientRect().height +
`" frameborder="0"
src="https://observablehq.com/embed/` +
urlslug +
`?cells=` +
cells.join("%2C") +
`"></iframe>`;*/
code =
`<iframe width="100%" height="` +
parent.node().getBoundingClientRect().height +
`" frameborder="0" src="https://www.web.statistik.zh.ch/bista/obsembed/?slug=` +
urlslug +
`&elements[]=` +
cells.join("&elements[]=").replace(" ", "-") +
`&hiddenelements[]=` +
hiddencells.join("&hiddenelements[]=").replace(" ", "-") +
`"></iframe>`;
}
return code;
}