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-60866][JENKINS-71051][JENKINS-71048] Un-inline 'Build Now' JS #7635

Merged
merged 14 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 3 additions & 13 deletions core/src/main/resources/lib/hudson/project/configurable.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,10 @@ THE SOFTWARE.
-->
<?jelly escape-by-default='true'?>
<!-- Displayed by projects (assignable to ParameterizedJobMixIn.ParameterizedJob) which can be configured and built. -->
<j:jelly xmlns:j="jelly:core" xmlns:l="/lib/layout">
<j:jelly xmlns:j="jelly:core" xmlns:l="/lib/layout" xmlns:st="jelly:stapler">
<j:if test="${it.buildable}">
<j:set var="id" value="${h.generateId()}"/>
<l:task href="${url}/build?delay=0sec" icon="icon-clock icon-md" onclick="${it.parameterized?null:'return build_' + id + '(this)'}" permission="${it.BUILD}" post="${!it.parameterized}" title="${it.buildNowText}"/>
<script>
function build_${id}(a) {
fetch(a.href, {
method: 'post',
headers: crumb.wrap({}),
})
hoverNotification('${%Build scheduled}',a.parentNode);
return false;
}
</script>
<st:adjunct includes="lib.hudson.project.configurable.configurable"/>
daniel-beck marked this conversation as resolved.
Show resolved Hide resolved
<l:task href="${url}/build?delay=0sec" icon="icon-clock icon-md" permission="${it.BUILD}" post="${!it.parameterized}" data-callback="lib_hudson_project_configurable_build_now_callback" data-build-success="${%Build scheduled}" data-parameterized="${it.parameterized}" title="${it.buildNowText}"/>
Copy link
Contributor

Choose a reason for hiding this comment

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

The only purpose of the callback seems to be to change the message from Done to Build scheduled.
So if we generalize the success message for the post we don't need the callback and the js here.
But I like the idea with the callback

Copy link
Member Author

Choose a reason for hiding this comment

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

So if we generalize the success message for the post we don't need the callback and the js here.

I didn't want to remove functionality unless it really was necessary or required crazy workarounds. The solution here seems generally sensible and supportable.

Doing it this way also provides an additional nontrivial example that can help plugin developers adapt their own code.

</j:if>
<j:choose>
<j:when test="${h.hasPermission(it,it.CONFIGURE)}">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(function () {
/* The JS formatting rules enforced in this repo can result in function declarations incompatible with HTMLUnit.
As a workaround, split function definition and assignment. */
function foo(el, ev) {
let parameterized = el.dataset.parameterized;
let success = el.dataset.buildSuccess;
if (parameterized === "false") {
fetch(el.href, {
method: "post",
headers: crumb.wrap({}),
});
hoverNotification(success, ev.target.parentNode);
ev.preventDefault();
}
}
window.lib_hudson_project_configurable_build_now_callback = foo;
})();
37 changes: 22 additions & 15 deletions core/src/main/resources/lib/layout/task.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ THE SOFTWARE.
(onclick supersedes this except in the context menu.)
(since 1.512)
</st:attribute>
<st:attribute name="onclick" deprecated="true">
Onclick inline JS handler. Deprecated, specify data-callback attribute instead.
</st:attribute>
<st:attribute name="data-callback" since="TODO">
Name of a global function to call when clicked.
It will be called with an element (currently 'a') as first argument, and the event as second argument.
You can specify further data-* attributes to customize behavior, those will be defined on the element passed as first argument.
daniel-beck marked this conversation as resolved.
Show resolved Hide resolved
See https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset for more information about these attributes.
Has no effect if you specify the 'requiresConfirmation' attribute.
Has no effect on menu items showing in context menus, only 'href', 'post', and 'requiresConfirmation' attributes substantially change behavior there.
Set 'contextMenu' to 'false' to remove this task from those menus.
</st:attribute>
<st:attribute name="badge" type="jenkins.management.Badge">
If set, displays the value as a small badge on the right side of the sidepanel item.
(since 2.401)
Expand Down Expand Up @@ -161,19 +173,6 @@ THE SOFTWARE.
<l:icon src="${icon}" />
</j:set>
${taskTags!=null and attrs.contextMenu!='false' ? taskTags.add(href, iconSrc, iconXml, title, post == 'true', requiresConfirmation == 'true', null, confirmationMessage ?: null) : null}
<j:set var="id" value="${h.generateId()}"/>
<j:if test="${attrs.onclick == null and post and not requiresConfirmation}">
<script>
function postRequest_${id}(a) {
fetch(a.href, {
method: 'post',
headers: crumb.wrap({}),
})
hoverNotification('${%Done.}', a.parentNode);
return false;
}
</script>
</j:if>

<j:choose>
<j:when test="${requiresConfirmation and not attrs.onClick}">
Expand All @@ -191,7 +190,14 @@ THE SOFTWARE.
</j:when>

<j:otherwise>
<a href="${href}" class="task-link ${isCurrent ? 'task-link--active' : ''}" onclick="${attrs.onclick ?: (post ? 'return postRequest_' + id + '(this)' : null)}">
<m:a xmlns:m="jelly:hudson.util.jelly.MorphTagLibrary"
href="${href}"
class="task-link task-link-no-confirm ${isCurrent ? 'task-link--active' : ''}"
data-task-success="${%Done.}"
data-task-post="${attrs.post}"
ATTRIBUTES="${attrs}"
EXCEPT="badge confirmationMessage contextMenu destructive enabled href icon permission permissions post requiresConfirmation title">
<!-- all documented attributes except onclick and data-* are in EXCEPT -->
<span class="task-icon-link">
<l:icon src="${icon}" />
</span>
Expand All @@ -201,7 +207,8 @@ THE SOFTWARE.
${attrs.badge.text}
</span>
</j:if>
</a>
</m:a>
<st:adjunct includes="lib.layout.task.task" />
daniel-beck marked this conversation as resolved.
Show resolved Hide resolved
</j:otherwise>
</j:choose>
</span>
Expand Down
28 changes: 28 additions & 0 deletions core/src/main/resources/lib/layout/task/task.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Behaviour.specify("a.task-link-no-confirm", "task-link", 0, function (el) {
if (el.onclick !== null) {
return;
}

let post = el.dataset.taskPost;
let callback = el.dataset.callback;
let success = el.dataset.taskSuccess;
let href = el.href;

if (callback !== undefined) {
el.onclick = function (ev) {
window[callback](el, ev);
};
return;
}

if (post === "true") {
el.onclick = function (ev) {
fetch(href, {
method: "post",
headers: crumb.wrap({}),
});
hoverNotification(success, el.parentNode);
ev.preventDefault();
};
}
});
Loading