Skip to content

Commit 8bf109b

Browse files
committed
FIX(plot.jupy) PanZoom CDN-URL; use ids vs class >
(a brain-twisted way of arming pan-zoom based on the last script loaded, which didn't always work for all graphs visible at the ame time). - fix: GH-repo for PanZoom seemed to have renamed its owner from ariutta --> https://github.com/bumbu/svg-pan-zoom - fix: load remote-script only once pan-zoom code.
1 parent c850ac6 commit 8bf109b

File tree

4 files changed

+47
-25
lines changed

4 files changed

+47
-25
lines changed

docs/source/arch.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ Architecture
670670
the :ref:`plotting` section.
671671

672672
SVGs are in rendered with the `zoom-and-pan javascript library
673-
<https://github.com/ariutta/svg-pan-zoom>`_
673+
<https://github.com/bumbu/svg-pan-zoom>`_
674674

675675
.. include:: plotting.rst
676676
:start-after: .. serve-sphinx-warn-start

docs/source/plotting.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ Directives
286286
.. rst:directive:option:: zoomable-opts: <empty>, (true, 1, yes, on) | (false, 0, no, off)
287287
:type: `str`
288288
289-
A JS-object with `the options <https://github.com/ariutta/svg-pan-zoom#how-to-use>`_
289+
A JS-object with `the options <https://github.com/bumbu/svg-pan-zoom#how-to-use>`_
290290
for the interactive zoom+pan pf SVGs.
291291
If missing, :confval:`graphtik_zoomable_options` assumed.
292292
Specify ``{}`` explicitly to force library's default options.
@@ -362,7 +362,7 @@ Configurations
362362
- default: ``True``
363363

364364
Whether to render SVGs with the `zoom-and-pan javascript library
365-
<https://github.com/ariutta/svg-pan-zoom>`_, unless the ``:zoomable:``
365+
<https://github.com/bumbu/svg-pan-zoom>`_, unless the ``:zoomable:``
366366
directive-option is given (and not empty).
367367

368368
.. serve-sphinx-warn-start
@@ -380,7 +380,7 @@ Configurations
380380
- type: `str`
381381
- default: ``{controlIconsEnabled: true, fit: true}``
382382

383-
A JS-object with `the options <https://github.com/ariutta/svg-pan-zoom#how-to-use>`_
383+
A JS-object with `the options <https://github.com/bumbu/svg-pan-zoom#how-to-use>`_
384384
for the interactive zoom+pan pf SVGs, when the ``:zoomable-opts:`` directive option
385385
is missing.
386386
If empty, ``{}`` assumed (library's default options).

graphtik/plot.py

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def is_empty_frame(val):
9191
#:
9292
#: :param svg_pan_zoom_json:
9393
#: arguments controlling the rendering of a zoomable SVG in
94-
#: Jupyter notebooks, as defined in https://github.com/ariutta/svg-pan-zoom#how-to-use
94+
#: Jupyter notebooks, as defined in https://github.com/bumbu/svg-pan-zoom#how-to-use
9595
#: if `None`, defaults to string (also maps supported)::
9696
#:
9797
#: "{controlIconsEnabled: true, fit: true}"
@@ -159,27 +159,49 @@ def _dot2svg(dot):
159159
Or with plotly https://plot.ly/~empet/14007.embed
160160
161161
"""
162+
# Load js-script only once from: https://stackoverflow.com/a/57572107/548792
163+
164+
import random
165+
162166
pan_zoom_json, element_styles, container_styles = _parse_jupyter_render(dot)
163167
svg_txt = dot.create_svg().decode()
168+
svg_id = f"graphtik-plot-{random.randrange(int(1<<32)):08x}"
164169
html_txt = f"""
165-
<div class="svg_container">
166-
<style>
167-
.svg_container {{
168-
{container_styles}
169-
}}
170-
.svg_container SVG {{
171-
{element_styles}
172-
}}
173-
</style>
174-
<script src="https://ariutta.github.io/svg-pan-zoom/dist/svg-pan-zoom.min.js"></script>
175-
<script type="text/javascript">
176-
var scriptTag = document.scripts[document.scripts.length - 1];
177-
var parentTag = scriptTag.parentNode;
178-
var svg_el = parentTag.querySelector(".svg_container svg");
179-
svgPanZoom(svg_el, {pan_zoom_json});
180-
</script>
181-
{svg_txt}
182-
</</>
170+
<div id="{svg_id}" class="svg_container">
171+
<style>
172+
#{svg_id} {{
173+
{container_styles}
174+
}}
175+
#{svg_id} SVG {{
176+
{element_styles}
177+
}}
178+
</style>
179+
<script type="text/javascript">
180+
function graphtik_renderPlot() {{
181+
var svg_el = document.querySelector("#{svg_id} svg");
182+
svgPanZoom(svg_el, {pan_zoom_json});
183+
}}
184+
185+
function graphtik_load_panZoom_script_n_render() {{
186+
let id = 'graphtik_panZoomScript'
187+
if(document.getElementById(id) === null) {{
188+
let script = document.createElement('script')
189+
script.setAttribute(
190+
'src',
191+
'https://cdn.jsdelivr.net/npm/[email protected]/dist/svg-pan-zoom.min.js')
192+
script.setAttribute('id', id)
193+
document.body.appendChild(script)
194+
195+
script.onload = graphtik_renderPlot
196+
}} else {{
197+
graphtik_renderPlot()
198+
}}
199+
}}
200+
201+
graphtik_load_panZoom_script_n_render()
202+
</script>
203+
{svg_txt}
204+
</</>
183205
"""
184206
return html_txt
185207

graphtik/sphinxext/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ def _enact_zoomable_svg(self: HTMLTranslator, node: dynaimage, tag: str):
9292
:param node:
9393
Assign a special *class* for the zoom js-code to select by it.
9494
95-
NOTE: does not work locally with chrome: ariutta/svg-pan-zoom#326
95+
NOTE: does not work locally with chrome: bumbu/svg-pan-zoom#326
9696
"""
9797
if tag == "object" and "graphtik-zoomable-svg" in node["classes"]:
9898
## # Setup pan+zoom JS-code only once.
9999
#
100100
if not hasattr(self.builder, "svg_zoomer_doc_scripts"):
101101
self.builder.add_js_file(
102-
"https://ariutta.github.io/svg-pan-zoom/dist/svg-pan-zoom.min.js"
102+
"https://cdn.jsdelivr.net/npm/svg-pan-zoom@3.5.0/dist/svg-pan-zoom.min.js"
103103
)
104104
self.builder.add_js_file(
105105
None,

0 commit comments

Comments
 (0)