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-74131] Extract inline JavaScript in FolderAuthorizationStrategyManagementLink/index.jelly #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
<input id="assign-sid-global-${index}" type="text"/>
</div>
<div class="center" style="margin-top: 5px">
<button type="button" class="submit-button" onclick="assignSid('global', ${index})">
<button type="button" class="sid-action-button submit-button" data-action="assignSid" data-index="${index}" data-role-type="global">
${%assign}
</button>
<button type="button" class="submit-button" onclick="removeSid('global', ${index})">
<button type="button" class="sid-action-button submit-button" data-action="removeSid" data-index="${index}" data-role-type="global">
${%remove}
</button>
</div>
Expand All @@ -53,8 +53,8 @@
</j:forEach>
</ul>
</div>
<form method="POST" action="${rootURL}/${it.urlName}/deleteGlobalRole"
onsubmit="return confirm('${%confirmDelete}');">
<form method="POST" action="${rootURL}/${it.urlName}/deleteGlobalRole" class="delete-role-form"
data-confirm-delete-text="${%confirmDelete}">
<input type="hidden" name="roleName" value="${globalRole.name}"/>
<input type="submit" value="X" class="delete-role"/>
</form>
Expand Down Expand Up @@ -82,7 +82,7 @@
</option>
</j:forEach>
</select>
<button type="button" onclick="addGlobalRole();" class="submit-button">
<button type="button" data-action="addGlobalRole" class="add-role-button submit-button">
${%addRole}
</button>
</div>
Expand Down Expand Up @@ -123,10 +123,10 @@
<input id="assign-sid-folder-${index}" type="text" name="sid"/>
</div>
<div class="center" style="margin-top: 5px;">
<button type="button" class="submit-button" onclick="assignSid('folder', ${index})">
<button type="button" class="sid-action-button submit-button" data-action="assignSid" data-index="${index}" data-role-type="folder">
${%assign}
</button>
<button type="button" class="submit-button" onclick="removeSid('folder', ${index})">
<button type="button" class="sid-action-button submit-button" data-action="removeSid" data-index="${index}" data-role-type="folder">
${%remove}
</button>
</div>
Expand All @@ -141,8 +141,8 @@
</j:forEach>
</ul>
</div>
<form method="POST" action="${rootURL}/${it.urlName}/deleteFolderRole"
onsubmit="return confirm('${%confirmDelete}');">
<form method="POST" action="${rootURL}/${it.urlName}/deleteFolderRole" class="delete-role-form"
data-confirm-delete-text="${%confirmDelete}">
<input type="hidden" name="roleName" value="${folderRole.name}"/>
<input type="submit" value="X" class="delete-role"/>
</form>
Expand All @@ -163,10 +163,6 @@
</label>
</div>
<div class="form-row">
<script>
// asynchronously render folder names
getFolders();
</script>
<label class="form-label" for="folder-select">
${%applyOn}:
</label>
Expand All @@ -188,8 +184,8 @@
</j:forEach>
</select>
<div class="form-row">
<button id="add-folder-role-button" type="button" class="submit-button"
onclick="addFolderRole();" disabled="disabled">
<button id="add-folder-role-button" type="button" class="add-role-button submit-button"
data-action="addFolderRole" disabled="disabled">
${%addRole}
</button>
</div>
Expand Down Expand Up @@ -231,10 +227,10 @@
<input id="assign-sid-agent-${index}" type="text"/>
</div>
<div class="center" style="margin-top: 10px;">
<button type="button" class="submit-button" onclick="assignSid('agent', ${index})">
<button type="button" class="sid-action-button submit-button" data-action="assignSid" data-index="${index}" data-role-type="agent">
${%assign}
</button>
<button type="button" class="submit-button" onclick="removeSid('agent', ${index})">
<button type="button" class="sid-action-button submit-button" data-action="removeSid" data-index="${index}" data-role-type="agent">
${%remove}
</button>
</div>
Expand All @@ -249,8 +245,8 @@
</j:forEach>
</ul>
</div>
<form method="POST" action="${rootURL}/${it.urlName}/deleteAgentRole"
onsubmit="return confirm('${%confirmDelete}');">
<form method="POST" action="${rootURL}/${it.urlName}/deleteAgentRole" class="delete-role-form"
data-confirm-delete-text="${%confirmDelete}">
<input type="hidden" name="roleName" value="${agentRole.name}"/>
<input type="submit" value="X" class="delete-role"/>
</form>
Expand Down Expand Up @@ -291,7 +287,7 @@
</j:forEach>
</select>
<div class="form-row">
<button type="button" class="submit-button" onclick="addAgentRole();">${%addRole}</button>
<button type="button" class="add-role-button submit-button" data-action="addAgentRole">${%addRole}</button>
</div>
</div>
</div>
Expand Down
44 changes: 36 additions & 8 deletions src/main/webapp/js/addrole.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/**
* Adds a global role
*/
const addGlobalRole = () => {
function addGlobalRole() {
Copy link
Author

Choose a reason for hiding this comment

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

Changing the way function is declared allows me to call these the way I do on line 128 of this file (https://github.com/jenkinsci/folder-auth-plugin/pull/105/files#diff-acf15339083ce65e46e34d649d67bf58880b907daaddcb899dd15454fef2a978R128).
Otherwise I'd have had to add a specific class to every element that needs a handler (global, folder, agent) and attach each event handler separately.

const roleName = document.getElementById('globalRoleName').value;
if (!roleName || roleName.length < 3) {
alert('Please enter a valid name for the role to be added.');
Expand All @@ -13,7 +13,9 @@ const addGlobalRole = () => {

const response = {
name: roleName,
permissions: document.getElementById('global-permission-select').getValue(),
permissions: Array.from(document.getElementById('global-permission-select').options)
.filter(option => option.selected)
.map(option => option.value)
};

if (response.permissions.length <= 0) {
Expand All @@ -28,7 +30,7 @@ const addGlobalRole = () => {
/**
* Adds a Folder Role
*/
const addFolderRole = () => {
function addFolderRole() {
const roleName = document.getElementById('folderRoleName').value;
if (!roleName || roleName.length < 3) {
alert('Please enter a valid name for the role to be added');
Expand All @@ -37,8 +39,12 @@ const addFolderRole = () => {

const response = {
name: roleName,
permissions: document.getElementById('folder-permission-select').getValue(),
folderNames: document.getElementById('folder-select').getValue(),
permissions: Array.from(document.getElementById('folder-permission-select').options)
.filter(option => option.selected)
.map(option => option.value),
folderNames: Array.from(document.getElementById('folder-select').options)
.filter(option => option.selected)
.map(option => option.value)
};

if (!response.permissions || response.permissions.length <= 0) {
Expand All @@ -58,7 +64,7 @@ const addFolderRole = () => {
/**
* Adds an agent Role
*/
const addAgentRole = () => {
function addAgentRole() {
const roleName = document.getElementById('agentRoleName').value;
if (!roleName || roleName.length < 3) {
alert('Please enter a valid name for the role to be added');
Expand All @@ -67,8 +73,12 @@ const addAgentRole = () => {

const response = {
name: roleName,
agentNames: document.getElementById('agent-select').getValue(),
permissions: document.getElementById('agent-permission-select').getValue(),
agentNames: Array.from(document.getElementById('agent-select').options)
.filter(option => option.selected)
.map(option => option.value),
permissions: Array.from(document.getElementById('agent-permission-select').options)
.filter(option => option.selected)
.map(option => option.value)
};

if (!response.permissions || response.permissions.length <= 0) {
Expand Down Expand Up @@ -109,3 +119,21 @@ const sendPostRequest = (postUrl, json) => {

xhr.send(JSON.stringify(json));
};

document.addEventListener("DOMContentLoaded", () => {
document.querySelectorAll(".add-role-button").forEach((button) => {
button.addEventListener("click", (event) => {
const { action } = event.target.dataset;

window[action]();
});
});

document.querySelectorAll(".delete-role-form").forEach((form) => {
form.addEventListener("submit", (event) => {
if (!confirm(event.target.dataset.confirmDeleteText)) {
event.preventDefault();
}
});
})
});
4 changes: 4 additions & 0 deletions src/main/webapp/js/folders.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@ const renderFoldersAsOptions = (folders) => {
// enable submitting the form
document.getElementById('add-folder-role-button').removeAttribute('disabled');
};

document.addEventListener("DOMContentLoaded", () => {
getFolders();
});
12 changes: 12 additions & 0 deletions src/main/webapp/js/managesids.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,15 @@ function removeSid(roleType, index) {
request.setRequestHeader('Jenkins-Crumb', crumb.value);
request.send(formData);
}

document.addEventListener("DOMContentLoaded", () => {
document.querySelectorAll(".sid-action-button").forEach((button) => {
button.addEventListener("click", (event) => {
const data = event.target.dataset;
const index = parseInt(data.index);
const { roleType, action } = data;

window[action](roleType, index);
});
})
});
Loading