Skip to content

Commit

Permalink
[JENKINS-73949] Extract inline event handlers from `LockableResources…
Browse files Browse the repository at this point in the history
…RootAction/tableResources/table.jelly` (#720)
  • Loading branch information
yaroslavafenkin authored Oct 22, 2024
1 parent 1f0dff5 commit 83131d9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ THE SOFTWARE.
</div>
<div class="col-auto jenkins-!-margin-right-2">
<l:hasPermission permission="${it.RESERVE}">
<a class="jenkins-table__link" id="note-link" href="editNote" onclick="return replaceNote(this, '${resource.name}');">
<a class="jenkins-table__link lockable-resources-replace-note" data-resource-name="${resource.name}"
id="note-link" href="editNote">
<l:icon class="symbol-edit-note icon-sm" />
${%btn.editNote}
</a>
Expand Down Expand Up @@ -251,17 +252,17 @@ THE SOFTWARE.
<j:when test="${resource.locked}">
<l:hasPermission permission="${it.UNLOCK}">
<button
onClick="resource_action(this, 'unlock');"
class="jenkins-button j.jenkins-!-warning-color"
data-action="unlock"
class="jenkins-button j.jenkins-!-warning-color lockable-resources-action-button"
tooltip="${%btn.unlock.detail}"
>
${%btn.unlock}
</button>
</l:hasPermission>
<l:hasPermission permission="${it.STEAL}">
<button
onClick="resource_action(this, 'steal');"
class="jenkins-button jenkins-!-destructive-color"
data-action="steal"
class="jenkins-button jenkins-!-destructive-color lockable-resources-action-button"
tooltip="${%btn.steal.detail}"
>
${%btn.steal}
Expand All @@ -272,8 +273,8 @@ THE SOFTWARE.
<l:hasPermission permission="${it.RESERVE}">
<j:if test="${resource.isReservedByCurrentUser() or h.hasPermission(app.ADMINISTER)}">
<button
onClick="resource_action(this, 'unreserve');"
class="jenkins-button jenkins-!-success-color"
data-action="unreserve"
class="jenkins-button jenkins-!-success-color lockable-resources-action-button"
tooltip="${%btn.unReserve.detail}"
>
${%btn.unReserve}
Expand All @@ -283,8 +284,8 @@ THE SOFTWARE.
<l:hasPermission permission="${it.STEAL}">
<j:if test="${!resource.isReservedByCurrentUser()}">
<button
onClick="resource_action(this, 'reassign');"
class="jenkins-button jenkins-button--primary"
data-action="reassign"
class="jenkins-button jenkins-button--primary lockable-resources-action-button"
tooltip="${%btn.reassign.detail}"
>
${%btn.reassign}
Expand All @@ -295,8 +296,8 @@ THE SOFTWARE.
<j:when test="${resource.queued}">
<l:hasPermission permission="${it.UNLOCK}">
<button
onClick="resource_action(this, 'reset');"
class="jenkins-button jenkins-!-destructive-color"
data-action="reset"
class="jenkins-button jenkins-!-destructive-color lockable-resources-action-button"
tooltip="${%btn.reset.detail}"
>
${%btn.reset}
Expand All @@ -306,8 +307,8 @@ THE SOFTWARE.
<j:otherwise>
<l:hasPermission permission="${it.RESERVE}">
<button
onClick="resource_action(this, 'reserve');"
class="jenkins-button jenkins-button--primary"
data-action="reserve"
class="jenkins-button jenkins-button--primary lockable-resources-action-button"
tooltip="${%btn.reserve.detail}"
>
${%btn.reserve}
Expand Down
18 changes: 16 additions & 2 deletions src/main/webapp/js/lockable-resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function resource_action(button, action) {
});
}

function replaceNote(element, resourceName) {
function replaceNote(resourceName) {
var d = document.getElementById("note-" + resourceName);
d.innerHTML = "<div class='spinner-right' style='flex-grow: 1;'>loading...</div>";
fetch("noteForm", {
Expand All @@ -80,7 +80,6 @@ function replaceNote(element, resourceName) {
layoutUpdateCallback.call();
});
});
return false;
}

function format(d) {
Expand Down Expand Up @@ -146,6 +145,21 @@ jQuery(document).ready(function () {
tr.addClass('shown');
}
});

document.querySelectorAll(".lockable-resources-action-button").forEach(function (button) {
button.addEventListener("click", function (event) {
const target = event.target;
const action = target.dataset.action;
resource_action(target, action);
});
});

document.querySelectorAll(".lockable-resources-replace-note").forEach(function (anchor) {
anchor.addEventListener("click", function (event) {
event.preventDefault();
replaceNote(event.target.dataset.resourceName);
});
});
});


Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ private void checkXssWithResourceName(String resourceName) throws Exception {

HtmlElement reserveButton = null;
for (HtmlElement b : allButtons) {
String onClick = b.getAttribute("onClick");
if (onClick != null && onClick.contains("reserve")) {
String action = b.getAttribute("data-action");
if (action != null && action.contains("reserve")) {
reserveButton = b;
}
}
Expand Down

0 comments on commit 83131d9

Please sign in to comment.