From 38d31c1751f582eccf85feb2c66eb0eeb3b6e77b Mon Sep 17 00:00:00 2001 From: Daniel Beck <1831569+daniel-beck@users.noreply.github.com> Date: Wed, 13 Dec 2023 21:00:08 +0100 Subject: [PATCH] [JENKINS-60866][JENKINS-71051][JENKINS-71048] Un-inline 'Build Now' JS (#7635) * [JENKINS-60866] Un-inline 'Build Now' JS * Fix formatting * Use MorphTagLibrary to simplify API * Update docs for l:task attributes * This is a collosal waste of time Copied from 8aee10ea8a1c03c71868bd116e08efa1c9bbc1af, this is just stupid * Rename adjunct file, restructure to work around broken formatter * Exclude all documented attributes except data-* * Fix onclick behavior * Address review feedback --------- Co-authored-by: Daniel Beck --- .../lib/hudson/project/configurable.jelly | 16 ++------ .../project/configurable/configurable.js | 17 +++++++++ core/src/main/resources/lib/layout/task.jelly | 37 +++++++++++-------- .../main/resources/lib/layout/task/task.js | 28 ++++++++++++++ 4 files changed, 70 insertions(+), 28 deletions(-) create mode 100644 core/src/main/resources/lib/hudson/project/configurable/configurable.js create mode 100644 core/src/main/resources/lib/layout/task/task.js diff --git a/core/src/main/resources/lib/hudson/project/configurable.jelly b/core/src/main/resources/lib/hudson/project/configurable.jelly index 9bc320fd9266..44bac9b2b9bf 100644 --- a/core/src/main/resources/lib/hudson/project/configurable.jelly +++ b/core/src/main/resources/lib/hudson/project/configurable.jelly @@ -24,20 +24,10 @@ THE SOFTWARE. --> - + - - - + + diff --git a/core/src/main/resources/lib/hudson/project/configurable/configurable.js b/core/src/main/resources/lib/hudson/project/configurable/configurable.js new file mode 100644 index 000000000000..a96e64219502 --- /dev/null +++ b/core/src/main/resources/lib/hudson/project/configurable/configurable.js @@ -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; +})(); diff --git a/core/src/main/resources/lib/layout/task.jelly b/core/src/main/resources/lib/layout/task.jelly index ec17b8e92a53..0d75a2b0b32d 100644 --- a/core/src/main/resources/lib/layout/task.jelly +++ b/core/src/main/resources/lib/layout/task.jelly @@ -77,6 +77,18 @@ THE SOFTWARE. (onclick supersedes this except in the context menu.) (since 1.512) + + Onclick inline JS handler. Deprecated, specify data-callback attribute instead. + + + 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. + 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. + If set, displays the value as a small badge on the right side of the sidepanel item. (since 2.401) @@ -161,19 +173,6 @@ THE SOFTWARE. ${taskTags!=null and attrs.contextMenu!='false' ? taskTags.add(href, iconSrc, iconXml, title, post == 'true', requiresConfirmation == 'true', null, confirmationMessage ?: null) : null} - - - - @@ -191,7 +190,14 @@ THE SOFTWARE. - + + @@ -201,7 +207,8 @@ THE SOFTWARE. ${attrs.badge.text} - + + diff --git a/core/src/main/resources/lib/layout/task/task.js b/core/src/main/resources/lib/layout/task/task.js new file mode 100644 index 000000000000..5d6fd6fd008c --- /dev/null +++ b/core/src/main/resources/lib/layout/task/task.js @@ -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(); + }; + } +});