33htmx . defineExtension ( 'htmx-plotly' , {
44 transformResponse : ( text , xhr , elt ) => {
55
6- // const dataCurr = window['plotlyTestData']
7- // console.log('htmx-plotly: plotlyTestData:' + JSON.stringify(dataCurr) ) // debug
8-
9- const dataNew = JSON . parse ( text )
10- // console.log('htmx-plotly: new data: ' + JSON.stringify(dataNew))
11- // window['plotlyTestData'] = dataNew
12- Plotly . restyle ( 'my-plot' , dataNew ) ;
6+ var plotEl = htmx . closest ( elt , "[plot-id]" ) ; // closest including div element
7+ if ( plotEl ) {
8+ const dataNew = JSON . parse ( text )
9+ const plotId = plotEl . getAttribute ( 'plot-id' ) ; // lookup value of ? in < .. plot-id="?">
10+ var plotDiv = htmx . find ( "#" + plotId ) ; // div element pointed at
11+ if ( plotDiv ) {
12+ // https://plotly.com/javascript/plotlyjs-function-reference/#plotlyrestyle
13+ Plotly . restyle ( plotId , dataNew )
14+ } else {
15+ throw "Cannot find plot id: " + plotId
16+ }
17+ } else {
18+ console . log ( 'No plot-id attribute defined' )
19+ }
1320
1421 return ''
1522
16- // var plotIdAttr = htmx.closest(elt, "[plot-id]"); // closest including div element
17- // if (plotIdAttr) {
18- // var data = JSON.parse(text);
19- // var plotId = plotIdAttr.getAttribute('plot-id'); // < .. plot-id="?"> value of ?
20-
21- // var plotDiv = htmx.find("#" + plotId); // div element pointed at
22- // if (plotDiv) {
23- // // https://plotly.com/javascript/plotlyjs-function-reference/#plotlyrestyle
24- // Plotly.restyle(plotId, 'y', [data]);
25-
26- // } else {
27- // throw "Cannot find plot id:" + plotDiv
28- // }
29- // }
30-
3123 }
3224}
3325)
0 commit comments