createDemo = (config) => {
const wStyles = Object.assign({}, wrapperStyles, config.wrapper);
const wrapper = htl.html`<div class="demo-wrapper" style=${wStyles}></div>`;
const buttonStyles =
config.copyButton === undefined ? copyButtonStyles : config.copyButton;
const plotWrapper = htl.html`<div style="flex-grow:1; padding-left: 15px; margin:10px 0 10px 0; ">`;
const buttonWrapper = htl.html`<div style=${buttonStyles}>`;
const controlsWrapper = htl.html`<div style="padding:10px 10px 0px 10px; background-color:#f3f3f3; min-width:250px; position:relative">`;
const reset = htl.html`<text style="position:absolute; cursor: pointer; bottom: 3px; right: 10px;font-family: sans-serif; font-size: 13px; opacity: .7;">reset`;
reset.onclick = () => {
const newControls = createForm(config.controls);
controlsWrapper.replaceChild(
newControls,
controlsWrapper.querySelector(".code_form")
);
const form = d3.select(newControls).select(".newInput");
form.on("input", function () {
render(this.value);
});
form.dispatch("input");
};
const controls = createForm(config.controls);
controlsWrapper.append(controls);
controlsWrapper.append(buttonWrapper);
controlsWrapper.append(reset);
wrapper.appendChild(controlsWrapper);
wrapper.appendChild(plotWrapper);
const render = (inputValue) => {
const plotCode = config.plot(inputValue);
plotWrapper.replaceChildren(evalCodeStr(plotCode));
const copyButton = makeCopyIcon(plotCode, {
instructions: `copy`
});
buttonWrapper.replaceChildren(copyButton);
};
const form = d3.select(controls).select(".newInput");
form.on("input", function () {
render(this.value);
});
form.dispatch("input");
return wrapper;
}