From a391cbb90254714672ae3c10cd009378504781a5 Mon Sep 17 00:00:00 2001 From: Emma Lewis Date: Mon, 5 Aug 2019 12:44:48 +0100 Subject: [PATCH] Add optional container to allow containers to wrap table rows-and-columns separately --- datasette_vega/__init__.py | 16 ++++++++++++++++ src/index.js | 9 +++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/datasette_vega/__init__.py b/datasette_vega/__init__.py index 4173ee0..bc12673 100644 --- a/datasette_vega/__init__.py +++ b/datasette_vega/__init__.py @@ -27,3 +27,19 @@ def extra_css_urls(): @hookimpl def extra_js_urls(): return cached_filepaths_for_extension('js') + +@hookimpl +def extra_body_script(template, database, table, datasette): + config = ( + datasette.plugin_config("datasette-vega", database=database, table=table) + or {} + ) + js = [] + value = config.get("container") + if value: + js.append( + "window.DATASETTE_VEGA_{} = '{}';".format( + "CONTAINER", value + ) + ) + return "\n".join(js) diff --git a/src/index.js b/src/index.js index 98adc8f..d7f751b 100644 --- a/src/index.js +++ b/src/index.js @@ -47,8 +47,13 @@ document.addEventListener('DOMContentLoaded', () => { jsonUrl = jsonEl.getAttribute('href'); // Create elements for adding graph tool to page visTool = document.createElement('div'); - let table = document.querySelector('table.rows-and-columns'); - table.parentNode.insertBefore(visTool, table); + const container = window.DATASETTE_VEGA_CONTAINER; + if (container && document.querySelector(container)) { + document.querySelector(container).appendChild(visTool); + } else { + let table = document.querySelector('table.rows-and-columns'); + table.parentNode.insertBefore(visTool, table); + } } } if (jsonUrl) {