Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-73911] Un-inline JavaScript in trend-chart.jelly #357

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/main/resources/charts/trend-chart.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@

</st:documentation>

<j:set var="chartId" value="history-chart-${h.generateId()}" />
<j:set var="generatedId" value="${h.generateId()}" />
<j:set var="chartId" value="history-chart-${generatedId}" />
<j:if test="${it.isTrendVisible()}">

<div class="test-trend-caption">
Expand All @@ -42,8 +43,9 @@
<st:adjunct includes="io.jenkins.plugins.echarts-trend-default-setup"/>

</j:if>
<st:bind value="${it}" var="trendProxy"/>
<st:bind value="${it}" var="trendProxy${generatedId}" />
<span class="echarts-api-trend-chart-data-holder"
data-generated-id="${generatedId}"
data-chart-id="${chartId}"
data-enable-links="${enableLinks}"
data-configuration-id="${configurationId}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ window.addEventListener("DOMContentLoaded", () => {

dataHolders.forEach(dataHolder => {
const chartId = dataHolder.getAttribute("data-chart-id");
const enableLinks = dataHolder.getAttribute("data-enable-links") || false;
const enableLinks = dataHolder.getAttribute("data-enable-links");
const configurationId = dataHolder.getAttribute("data-configuration-id");
const generatedId = dataHolder.getAttribute("data-generated-id");

echartsJenkinsApi.renderConfigurableTrendChart(`${chartId}`, `${enableLinks}`, `${configurationId}`, trendProxy);
echartsJenkinsApi.renderConfigurableTrendChart(`${chartId}`, `${enableLinks}`, `${configurationId}`, window[`trendProxy${generatedId}`]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain what the window[...] does? It works but I do not understand the semantics.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a way of accessing a global variable.
The variable is declared by the internals of <st:bind value="${it}" var="trendProxy${generatedId}" /> and it's in the global scope. I do not know upfront what the name of the variable will be because generatedId is generated in runtime. I'm passing the generatedId as a data attribute to the script so that I can access the object the name of which I can only know in runtime.

I could alternatively pass the name of the variable to the script instead of generatedId as I did in jenkinsci/warnings-ng-plugin#1862. Though the window[...] syntax still stays.

If the name was static we would simply access the variable by its name (like e.g. echartsJenkinsApi is accessed on this exact line).

Let me know if it's more clear now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see thanks! So this trick is to get the actual value while using the key trendProxy${generatedId}.

});
});
Loading