-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
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
Remove jQuery from 'New item' page #10208
Open
janfaracik
wants to merge
16
commits into
jenkinsci:master
Choose a base branch
from
janfaracik:remove-js-from-new-item-page
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+132
−121
Open
Changes from 11 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
01b673a
Update add-item.js
janfaracik 8649158
Update add-item.js
janfaracik 4b204ba
Update add-item.js
janfaracik 90fea2b
Update add-item.js
janfaracik 9a78d91
Update add-item.js
janfaracik d98cb27
Update add-item.js
janfaracik 6195d38
Update add-item.js
janfaracik 0e24ba1
Update add-item.js
janfaracik 7c8cf91
Update add-item.js
janfaracik bb49c71
Update add-item.js
janfaracik 3a05e63
Update add-item.js
janfaracik b8564fa
Attempt at a fix for the test
janfaracik b8fdc12
Update add-item.js
janfaracik 0eb23a8
Attempt using optional chaining...
janfaracik 3cb24f4
Merge branch 'master' into remove-js-from-new-item-page
janfaracik 2f031ae
Update add-item.js
janfaracik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,24 @@ | ||
import $ from "jquery"; | ||
import { createElementFromHtml } from "@/util/dom"; | ||
|
||
var getItems = function () { | ||
var d = $.Deferred(); | ||
$.get("itemCategories?depth=3&iconStyle=icon-xlg").done(function (data) { | ||
d.resolve(data); | ||
}); | ||
return d.promise(); | ||
const getItems = function () { | ||
return fetch("itemCategories?depth=3&iconStyle=icon-xlg").then((response) => | ||
response.json(), | ||
); | ||
}; | ||
|
||
var jRoot = $("head").attr("data-rooturl"); | ||
const jRoot = document.querySelector("head").getAttribute("data-rooturl"); | ||
|
||
$.when(getItems()).done(function (data) { | ||
$(function () { | ||
document.addEventListener("DOMContentLoaded", () => { | ||
getItems().then((data) => { | ||
////////////////////////// | ||
// helper functions... | ||
|
||
function parseResponseFromCheckJobName(data) { | ||
var html = $.parseHTML(data); | ||
var element = html[0]; | ||
if (element !== undefined) { | ||
return $(element).text(); | ||
var parser = new DOMParser(); | ||
var html = parser.parseFromString(data, "text/html"); | ||
var element = html.body.firstChild; | ||
if (element) { | ||
return element.textContent; | ||
} | ||
return undefined; | ||
} | ||
|
@@ -38,50 +37,49 @@ $.when(getItems()).done(function (data) { | |
} | ||
|
||
function getCopyFromValue() { | ||
return $('input[type="text"][name="from"]', "#createItem").val(); | ||
return document.querySelector( | ||
'#createItem input[type="text"][name="from"]', | ||
).value; | ||
} | ||
|
||
function isItemNameEmpty() { | ||
var itemName = $('input[name="name"]', "#createItem").val(); | ||
return itemName === "" ? true : false; | ||
var itemName = document.querySelector( | ||
'#createItem input[name="name"]', | ||
).value; | ||
return itemName.trim() === ""; | ||
} | ||
|
||
function getFieldValidationStatus(fieldId) { | ||
return $("#" + fieldId).data("valid"); | ||
return document.querySelector("#" + fieldId).dataset.valid === "true"; | ||
} | ||
|
||
function setFieldValidationStatus(fieldId, status) { | ||
$("#" + fieldId).data("valid", status); | ||
document.querySelector("#" + fieldId).dataset.valid = status; | ||
} | ||
|
||
function activateValidationMessage(messageId, context, message) { | ||
if (message !== undefined && message !== "") { | ||
$(messageId, context).text("» " + message); | ||
document.querySelector(context + " " + messageId).textContent = | ||
"» " + message; | ||
} | ||
cleanValidationMessages(context); | ||
$(messageId).removeClass("input-message-disabled"); | ||
enableSubmit(false); | ||
document | ||
.querySelector(messageId) | ||
.classList.remove("input-message-disabled"); | ||
refreshSubmitButtonState(); | ||
} | ||
|
||
function cleanValidationMessages(context) { | ||
$(context) | ||
.find(".input-validation-message") | ||
.addClass("input-message-disabled"); | ||
document | ||
.querySelectorAll(context + " .input-validation-message") | ||
.forEach((element) => element.classList.add("input-message-disabled")); | ||
} | ||
|
||
function enableSubmit(status) { | ||
var btn = $(".bottom-sticker-inner button[type=submit]"); | ||
if (status === true) { | ||
if (btn.hasClass("disabled")) { | ||
btn.removeClass("disabled"); | ||
btn.prop("disabled", false); | ||
} | ||
} else { | ||
if (!btn.hasClass("disabled")) { | ||
btn.addClass("disabled"); | ||
btn.prop("disabled", true); | ||
} | ||
} | ||
function refreshSubmitButtonState() { | ||
const submitButton = document.querySelector( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than accept a true/false parameter lets just have the function handle whether the button should be valid or not. |
||
".bottom-sticker-inner button[type=submit]", | ||
); | ||
submitButton.disabled = !getFormValidationStatus(); | ||
} | ||
|
||
function getFormValidationStatus() { | ||
|
@@ -95,36 +93,44 @@ $.when(getItems()).done(function (data) { | |
} | ||
|
||
function cleanItemSelection() { | ||
$(".categories").find('li[role="radio"]').attr("aria-checked", "false"); | ||
$("#createItem") | ||
.find('input[type="radio"][name="mode"]') | ||
.removeAttr("checked"); | ||
$(".categories").find(".active").removeClass("active"); | ||
document | ||
.querySelector('.categories li[role="radio"]') | ||
.setAttribute("aria-checked", "false"); | ||
document | ||
.querySelector('#createItem input[type="radio"][name="mode"]') | ||
.removeAttribute("checked"); | ||
document.querySelectorAll(".categories .active").forEach((item) => { | ||
item.classList.remove("active"); | ||
}); | ||
setFieldValidationStatus("items", false); | ||
} | ||
|
||
function cleanCopyFromOption() { | ||
$("#createItem") | ||
.find('input[type="radio"][value="copy"]') | ||
.removeAttr("checked"); | ||
$('input[type="text"][name="from"]', "#createItem").val(""); | ||
document | ||
.querySelector('#createItem input[type="radio"][value="copy"]') | ||
.removeAttribute("checked"); | ||
document.querySelector( | ||
'#createItem input[type="text"][name="from"]', | ||
).value = ""; | ||
setFieldValidationStatus("from", false); | ||
} | ||
|
||
////////////////////////////////// | ||
// Draw functions | ||
|
||
function drawCategory(category) { | ||
var $category = $("<div/>") | ||
.addClass("category") | ||
.attr("id", "j-add-item-type-" + cleanClassName(category.id)); | ||
var $items = $("<ul/>").addClass("j-item-options"); | ||
var $catHeader = $('<div class="header" />'); | ||
var $category = createElementFromHtml("<div class='category' />"); | ||
$category.setAttribute( | ||
"id", | ||
"j-add-item-type-" + cleanClassName(category.id), | ||
); | ||
var $items = createElementFromHtml(`<ul class="j-item-options" />`); | ||
var $catHeader = createElementFromHtml(`<div class="header" />`); | ||
var title = "<h2>" + category.name + "</h2>"; | ||
var description = "<p>" + category.description + "</p>"; | ||
|
||
// Add items | ||
$.each(category.items, function (i, elem) { | ||
category.items.forEach((elem) => { | ||
$items.append(drawItem(elem)); | ||
}); | ||
|
||
|
@@ -169,17 +175,13 @@ $.when(getItems()).done(function (data) { | |
cleanCopyFromOption(); | ||
cleanItemSelection(); | ||
|
||
$(this).attr("aria-checked", "true"); | ||
$(this).find('input[type="radio"][name="mode"]').prop("checked", true); | ||
$(this).addClass("active"); | ||
item.setAttribute("aria-checked", "true"); | ||
radio.checked = true; | ||
item.classList.add("active"); | ||
|
||
setFieldValidationStatus("items", true); | ||
if (!getFieldValidationStatus("name")) { | ||
$('input[name="name"][type="text"]', "#createItem").focus(); | ||
} else { | ||
if (getFormValidationStatus()) { | ||
enableSubmit(true); | ||
} | ||
if (getFieldValidationStatus("name")) { | ||
refreshSubmitButtonState(); | ||
} | ||
} | ||
|
||
|
@@ -253,98 +255,130 @@ $.when(getItems()).done(function (data) { | |
} | ||
|
||
// The main panel content is hidden by default via an inline style. We're ready to remove that now. | ||
$("#add-item-panel").removeAttr("style"); | ||
document.querySelector("#add-item-panel").removeAttribute("style"); | ||
|
||
// Render all categories | ||
var $categories = $("div.categories"); | ||
$.each(data.categories, function (i, elem) { | ||
drawCategory(elem).appendTo($categories); | ||
var $categories = document.querySelector("div.categories"); | ||
data.categories.forEach((elem) => { | ||
$categories.append(drawCategory(elem)); | ||
}); | ||
|
||
// Focus | ||
$("#add-item-panel").find("#name").focus(); | ||
document.querySelector("#add-item-panel #name").focus(); | ||
|
||
// Init NameField | ||
$('input[name="name"]', "#createItem").on("blur input", function () { | ||
function nameFieldEvent() { | ||
if (!isItemNameEmpty()) { | ||
var itemName = $('input[name="name"]', "#createItem").val(); | ||
$.get("checkJobName", { value: itemName }).done(function (data) { | ||
var message = parseResponseFromCheckJobName(data); | ||
if (message !== "") { | ||
activateValidationMessage( | ||
"#itemname-invalid", | ||
".add-item-name", | ||
message, | ||
); | ||
} else { | ||
cleanValidationMessages(".add-item-name"); | ||
setFieldValidationStatus("name", true); | ||
if (getFormValidationStatus()) { | ||
enableSubmit(true); | ||
} | ||
} | ||
}); | ||
var itemName = document.querySelector( | ||
'#createItem input[name="name"]', | ||
).value; | ||
|
||
fetch(`checkJobName?value=${encodeURIComponent(itemName)}`).then( | ||
(response) => { | ||
response.text().then((data) => { | ||
var message = parseResponseFromCheckJobName(data); | ||
if (message !== "") { | ||
activateValidationMessage( | ||
"#itemname-invalid", | ||
".add-item-name", | ||
message, | ||
); | ||
} else { | ||
cleanValidationMessages(".add-item-name"); | ||
setFieldValidationStatus("name", true); | ||
refreshSubmitButtonState(); | ||
} | ||
}); | ||
}, | ||
); | ||
} else { | ||
enableSubmit(false); | ||
setFieldValidationStatus("name", false); | ||
cleanValidationMessages(".add-item-name"); | ||
activateValidationMessage("#itemname-required", ".add-item-name"); | ||
refreshSubmitButtonState(); | ||
} | ||
}); | ||
} | ||
|
||
document | ||
.querySelector('#createItem input[name="name"]') | ||
.addEventListener("blur", nameFieldEvent); | ||
document | ||
.querySelector('#createItem input[name="name"]') | ||
.addEventListener("input", nameFieldEvent); | ||
|
||
// Init CopyFromField | ||
$('input[name="from"]', "#createItem").on("blur input", function () { | ||
function copyFromFieldEvent() { | ||
if (getCopyFromValue() === "") { | ||
$("#createItem") | ||
.find('input[type="radio"][value="copy"]') | ||
.removeAttr("checked"); | ||
document | ||
.querySelector('#createItem input[type="radio"][value="copy"]') | ||
.removeAttribute("checked"); | ||
} else { | ||
cleanItemSelection(); | ||
$("#createItem") | ||
.find('input[type="radio"][value="copy"]') | ||
.prop("checked", true); | ||
document | ||
.querySelector('#createItem input[type="radio"][value="copy"]') | ||
.setAttribute("checked", true); | ||
setFieldValidationStatus("from", true); | ||
if (!getFieldValidationStatus("name")) { | ||
activateValidationMessage("#itemname-required", ".add-item-name"); | ||
setTimeout(function () { | ||
var parentName = $('input[name="from"]', "#createItem").val(); | ||
$.get("job/" + parentName + "/api/json?tree=name").done( | ||
function (data) { | ||
if (data.name === parentName) { | ||
//if "name" is invalid, but "from" is a valid job, then switch focus to "name" | ||
$('input[name="name"][type="text"]', "#createItem").focus(); | ||
} | ||
var parentName = document.querySelector( | ||
'#createItem input[name="from"]', | ||
).value; | ||
|
||
fetch("job/" + parentName + "/api/json?tree=name").then( | ||
(response) => { | ||
response.json().then((data) => { | ||
if (data.name === parentName) { | ||
//if "name" is invalid, but "from" is a valid job, then switch focus to "name" | ||
document | ||
.querySelector( | ||
'#createItem input[name="name"][type="text"]', | ||
) | ||
.focus(); | ||
} | ||
}); | ||
}, | ||
); | ||
}, 400); | ||
} else { | ||
if (getFormValidationStatus()) { | ||
enableSubmit(true); | ||
} | ||
refreshSubmitButtonState(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
document | ||
.querySelector('#createItem input[name="from"]') | ||
.addEventListener("blur", copyFromFieldEvent); | ||
document | ||
.querySelector('#createItem input[name="from"]') | ||
.addEventListener("input", copyFromFieldEvent); | ||
|
||
// Client-side validation | ||
$("#createItem").submit(function (event) { | ||
if (!getFormValidationStatus()) { | ||
event.preventDefault(); | ||
if (!getFieldValidationStatus("name")) { | ||
activateValidationMessage("#itemname-required", ".add-item-name"); | ||
$('input[name="name"][type="text"]', "#createItem").focus(); | ||
} else { | ||
if ( | ||
!getFieldValidationStatus("items") && | ||
!getFieldValidationStatus("from") | ||
) { | ||
activateValidationMessage("#itemtype-required", ".add-item-name"); | ||
$('input[name="name"][type="text"]', "#createItem").focus(); | ||
document | ||
.querySelector("#createItem") | ||
.addEventListener("submit", function (event) { | ||
if (!getFormValidationStatus()) { | ||
event.preventDefault(); | ||
if (!getFieldValidationStatus("name")) { | ||
activateValidationMessage("#" + "", ".add-item-name"); | ||
document | ||
.querySelector('#createItem input[name="name"][type="text"]') | ||
.focus(); | ||
} else { | ||
if ( | ||
!getFieldValidationStatus("items") && | ||
!getFieldValidationStatus("from") | ||
) { | ||
activateValidationMessage("#itemtype-required", ".add-item-name"); | ||
document | ||
.querySelector('#createItem input[name="name"][type="text"]') | ||
.focus(); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
// Disable the submit button | ||
enableSubmit(false); | ||
refreshSubmitButtonState(); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now prevents names that are just spaces from being submitted.