Skip to content

Commit 563c9a3

Browse files
Extract inline script blocks and event handlers from jelly files (#399)
1 parent 253d41a commit 563c9a3

File tree

14 files changed

+190
-174
lines changed

14 files changed

+190
-174
lines changed

src/main/resources/jenkins/plugins/foldericon/CustomFolderIcon/config.jelly

+3-10
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,16 @@ SOFTWARE.
3333
<f:entry title="${%IconPreview}" help="${descriptor.getHelpFile('upload')}">
3434
<div>
3535
<div id="custom-icon-cropper"><!-- set in initCustomIcon() --></div>
36-
<f:file id="custom-icon-upload" accept="image/*" onchange="setCustomIconFile(this.files[0])"
37-
clazz="custom-icon jenkins-file-upload" />
36+
<f:file id="custom-icon-upload" accept="image/*" clazz="custom-icon jenkins-file-upload" />
3837
<st:nbsp />
39-
<input type="button" value="${%Apply}"
40-
onclick="doUploadCustomIcon('${it.getUrl()}', '${%UploadSuccess}', '${%UploadFailed}')" />
38+
<input id="custom-icon-upload-apply" type="button" value="${%Apply}" />
4139
</div>
4240
</f:entry>
4341
<j:if test="${not empty customicons}">
4442
<f:advanced title="${%AvailableIcons}${not empty customicons ? ' (' + customicons.size() + ')' : ''}">
4543
<j:forEach var="icon" items="${customicons}">
4644
<a tooltip="${icon}">
47-
<img class="custom-icon-selection" src="${rootURL}/userContent/customFolderIcons/${icon}"
48-
onclick="setCustomIcon(this.src)" />
45+
<img id="custom-icon-preview-${icon}" class="custom-icon-selection" src="${rootURL}/userContent/customFolderIcons/${icon}" />
4946
</a>
5047
<st:nbsp />
5148
</j:forEach>
@@ -54,8 +51,4 @@ SOFTWARE.
5451
<f:entry field="foldericon">
5552
<f:textbox id="custom-icon-name" value="${instance.foldericon}" clazz="custom-icon-name" />
5653
</f:entry>
57-
<script>
58-
// init the preview
59-
window.addEventListener("DOMContentLoaded", initCustomIcon());
60-
</script>
6154
</j:jelly>

src/main/resources/jenkins/plugins/foldericon/CustomFolderIcon/config.properties

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,4 @@
2222

2323
IconPreview=Icon preview
2424
Apply=Apply
25-
AvailableIcons=Show all available icons
26-
UploadSuccess=Image uploaded as
27-
UploadFailed=Image uploaded failed:
25+
AvailableIcons=Show all available icons

src/main/resources/jenkins/plugins/foldericon/CustomFolderIconConfiguration/global.jelly

+1-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ SOFTWARE.
4343
</j:forEach>
4444
</f:advanced>
4545
<f:entry help="${descriptor.getHelpFile('cleanup')}">
46-
<input id="custom-icon-cleanup" type="button" value="${%CleanupIcons}"
47-
onclick="doCustomIconCleanup('${%CleanupSuccess}', '${%CleanupFailed}')" />
46+
<input id="custom-icon-cleanup" type="button" value="${%CleanupIcons}" />
4847
</f:entry>
4948
</j:if>
5049
</f:section>

src/main/resources/jenkins/plugins/foldericon/CustomFolderIconConfiguration/global.properties

-2
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,3 @@ AvailableIcons=Show all available icons
2424
CleanupIcons=Cleanup unused icons
2525
IconCount=Number of icons:
2626
DiskUsage=Disk usage of icons:
27-
CleanupSuccess=Successfully removed all unused icon images
28-
CleanupFailed=Failed to removed unused icon images:

src/main/resources/jenkins/plugins/foldericon/EmojiFolderIcon/config.jelly

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ SOFTWARE.
3333
</f:entry>
3434
<f:advanced title="${%AvailableIcons}${not empty emojis ? ' (' + emojis.size() + ')' : ''}">
3535
<j:forEach var="icon" items="${emojis}">
36-
<a id="select-emoji-${icon.key}" class="emoji-icon-selection" onclick="setEmojiIcon('${icon.key}')">
36+
<a id="select-emoji-${icon.key}" class="emoji-icon-selection">
3737
<l:icon id="emoji-icon-${icon.key}" src="${icon.value}" title="${icon.key}" tooltip="${icon.key}"
3838
class="icon-md" />
3939
</a>

src/main/resources/jenkins/plugins/foldericon/FontAwesomeFolderIcon/config.jelly

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ SOFTWARE.
4444
<b>${style.name().substring(0, 1)}${style.name().substring(1).toLowerCase()} Icons</b>
4545
<br />
4646
<j:forEach var="icon" items="${filtered}">
47-
<a id="select-fontawesome-${icon.key}" class="fontawesome-icon-selection"
48-
onclick="setFontAwesomeIcon('${icon.key}')">
47+
<a id="select-fontawesome-${icon.key}" class="fontawesome-icon-selection">
4948
<l:icon id="fontawesome-icon-${icon.key}" src="${icon.value}" title="${icon.key}"
5049
tooltip="${icon.key}"
5150
class="icon-md" />

src/main/resources/jenkins/plugins/foldericon/IoniconFolderIcon/config.jelly

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ SOFTWARE.
3333
</f:entry>
3434
<f:advanced title="${%AvailableIcons}${not empty ionicons ? ' (' + ionicons.size() + ')' : ''}">
3535
<j:forEach var="icon" items="${ionicons}">
36-
<a id="select-ionicon-${icon.key}" class="ionicon-icon-selection" onclick="setIoniconIcon('${icon.key}')">
36+
<a id="select-ionicon-${icon.key}" class="ionicon-icon-selection">
3737
<l:icon id="ionicon-icon-${icon.key}" src="${icon.value}" title="${icon.key}" tooltip="${icon.key}"
3838
class="icon-md" />
3939
</a>

src/main/resources/jenkins/plugins/foldericon/OpenSourceFolderIcon/config.jelly

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ SOFTWARE.
3333
</f:entry>
3434
<f:advanced title="${%AvailableIcons}${not empty openSourceIcons ? ' (' + openSourceIcons.size() + ')' : ''}">
3535
<j:forEach var="icon" items="${openSourceIcons}">
36-
<a id="select-oss-${icon.key}" class="oss-icon-selection" onclick="setOpenSourceIcon('${icon.key}')">
36+
<a id="select-oss-${icon.key}" class="oss-icon-selection">
3737
<l:icon id="oss-icon-${icon.key}" src="${icon.value}" title="${icon.key}" tooltip="${icon.key}"
3838
class="icon-md" />
3939
</a>

src/main/webapp/js/custom-icon-config.js

+80-69
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
* THE SOFTWARE.
2323
*/
2424

25+
// global instance
2526
let croppie
2627

2728
/**
28-
* Initialization of preview image.
29-
*
29+
* Initialization of croppie and preview image.
3030
*/
31-
function initCustomIcon() {
31+
window.addEventListener("DOMContentLoaded", () => {
3232
let preview = document.getElementById("custom-icon-name").getAttribute("value");
3333
let url;
3434
if (preview == null || preview === "") {
@@ -53,89 +53,100 @@ function initCustomIcon() {
5353
} catch (e) {
5454
// NOP
5555
}
56-
}
56+
57+
return false;
58+
});
5759

5860
/**
5961
* Set an icon for cropping / preview.
60-
*
61-
* @param {string} url The icon url.
6262
*/
63-
function setCustomIcon(url) {
64-
// load icon image
65-
croppie.bind({
66-
url: url,
67-
zoom: 1
68-
});
63+
Behaviour.specify("[id^=\"custom-icon-preview-\"]", "CustomIconPreviewSelection", 0, element => {
64+
element.onclick = () => {
65+
let url = element.src;
6966

70-
// reset the name in the upload input element
71-
document.getElementById("custom-icon-upload").value = "";
67+
// load icon image
68+
croppie.bind({
69+
url: url,
70+
zoom: 1
71+
});
7272

73-
// set the file name - in case you don't crop / upload the image again it will simply be re-used that way
74-
let paths = url.split("/");
75-
let icon = paths[paths.length - 1];
73+
// reset the name in the upload input element
74+
document.getElementById("custom-icon-upload").value = "";
7675

77-
let iconName = document.getElementById("custom-icon-name")
78-
iconName.setAttribute("value", icon);
79-
iconName.dispatchEvent(new Event("input"));
80-
}
76+
// set the file name - in case you don't crop / upload the image again it will simply be re-used that way
77+
let paths = url.split("/");
78+
let icon = paths[paths.length - 1];
79+
80+
let iconName = document.getElementById("custom-icon-name")
81+
iconName.setAttribute("value", icon);
82+
iconName.dispatchEvent(new Event("input"));
83+
84+
return false;
85+
}
86+
});
8187

8288

8389
/**
8490
* Set a file for cropping / preview.
85-
*
86-
* @param {Blob} file The file input.
8791
*/
88-
function setCustomIconFile(file) {
89-
// read file input
90-
let reader = new FileReader();
91-
reader.onload = function (ev) {
92-
croppie.bind({
93-
url: ev.target.result,
94-
zoom: 1
95-
});
92+
Behaviour.specify("[id^=\"custom-icon-upload\"]", "CustomIconPreview", 0, element => {
93+
element.onchange = () => {
94+
// read file input
95+
let reader = new FileReader();
96+
reader.onload = event => {
97+
croppie.bind({
98+
url: event.target.result,
99+
zoom: 1
100+
});
101+
}
102+
let file = element.files[0];
103+
reader.readAsDataURL(file);
104+
105+
return false;
96106
}
97-
reader.readAsDataURL(file);
98-
}
107+
});
99108

100109
/**
101110
* Upload the cropped icon.
102-
*
103-
* @param {string} jobURL - The current job url.
104-
* @param {string} successMessage - The success message.
105-
* @param {string} errorMessage - The error message.
106111
*/
107-
function doUploadCustomIcon(jobURL, successMessage, errorMessage) {
108-
// get the icon blob
109-
croppie.result("blob").then(blob => {
110-
let formData = new FormData();
111-
formData.append("file", blob);
112-
return formData;
113-
}).then(formData => {
114-
// upload the icon
115-
fetch(rootURL + "/" + jobURL + "descriptorByName/jenkins.plugins.foldericon.CustomFolderIcon/uploadIcon", {
116-
method: "post",
117-
headers: crumb.wrap({}),
118-
body: formData
119-
}).then(rsp => {
120-
rsp.text().then(text => {
121-
let cropper = document.getElementById("custom-icon-cropper")
122-
if (rsp.ok) {
123-
let iconName = document.getElementById("custom-icon-name")
124-
iconName.setAttribute("value", text);
125-
iconName.dispatchEvent(new Event("input"));
126-
hoverNotification(successMessage + " " + text, cropper);
127-
} else {
128-
let error = text.substring(text.lastIndexOf("<title>") + 7, text.lastIndexOf("</title>"))
129-
hoverNotification(errorMessage + " " + error, cropper);
130-
}
112+
Behaviour.specify("[id^=\"custom-icon-upload-apply\"]", "CustomIconUpload", 0, element => {
113+
element.onclick = () => {
114+
// get the icon blob
115+
croppie.result("blob").then(blob => {
116+
let formData = new FormData();
117+
formData.append("file", blob);
118+
return formData;
119+
}).then(formData => {
120+
// upload the icon
121+
let jobUrl = window.location.href.substring(0, window.location.href.lastIndexOf('/'));
122+
123+
fetch(jobUrl + "/descriptorByName/jenkins.plugins.foldericon.CustomFolderIcon/uploadIcon", {
124+
method: "post",
125+
headers: crumb.wrap({}),
126+
body: formData
127+
}).then(response => {
128+
response.text().then(text => {
129+
let cropper = document.getElementById("custom-icon-cropper")
130+
if (response.ok) {
131+
let iconName = document.getElementById("custom-icon-name")
132+
iconName.setAttribute("value", text);
133+
iconName.dispatchEvent(new Event("input"));
134+
hoverNotification("Image uploaded as " + text, cropper);
135+
} else {
136+
let error = text.substring(text.lastIndexOf("<title>") + 7, text.lastIndexOf("</title>"))
137+
hoverNotification("Image uploaded failed: " + error, cropper);
138+
}
139+
}).catch(error => {
140+
console.error(error);
141+
});
131142
}).catch(error => {
132143
console.error(error);
133144
});
134-
}).catch(error => {
135-
console.error(error);
136-
});
137-
}
138-
).catch(error => {
139-
console.error(error);
140-
});
141-
}
145+
}
146+
).catch(error => {
147+
console.error(error);
148+
});
149+
150+
return false;
151+
}
152+
});

src/main/webapp/js/custom-icon-global.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,22 @@
2424

2525
/**
2626
* Cleanup unused icons.
27-
*
28-
* @param {string} successMessage - The success message.
29-
* @param {string} errorMessage - The error message.
3027
*/
31-
function doCustomIconCleanup(successMessage, errorMessage) {
32-
fetch(rootURL + "/descriptor/jenkins.plugins.foldericon.CustomFolderIconConfiguration/cleanup", {
33-
method: "post",
34-
headers: crumb.wrap({}),
35-
}).then(rsp => {
36-
let button = document.getElementById("custom-icon-cleanup")
37-
if (rsp.ok) {
38-
hoverNotification(successMessage, button.parentNode.parentNode);
39-
} else {
40-
hoverNotification(errorMessage + " " + rsp.status + " - " + rsp.statusText, button.parentNode.parentNode);
41-
}
42-
}).catch(error => {
43-
console.error(error);
44-
});
45-
}
28+
Behaviour.specify("[id^=\"custom-icon-cleanup\"]", "CustomIconCleanup", 0, element => {
29+
element.onclick = () => {
30+
fetch(rootURL + "/descriptor/jenkins.plugins.foldericon.CustomFolderIconConfiguration/cleanup", {
31+
method: "post",
32+
headers: crumb.wrap({}),
33+
}).then(response => {
34+
if (response.ok) {
35+
hoverNotification("Successfully removed all unused icon images", element.parentNode.parentNode);
36+
} else {
37+
hoverNotification("Failed to remove unused icon images: " + response.status + " - " + response.statusText, element.parentNode.parentNode);
38+
}
39+
}).catch(error => {
40+
console.error(error);
41+
});
42+
43+
return false;
44+
}
45+
});

src/main/webapp/js/emoji-config.js

+20-17
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,28 @@
2424

2525
/**
2626
* Select the icon for preview.
27-
*
28-
* @param {string} icon - The icon name.
29-
*
3027
*/
31-
function setEmojiIcon(icon) {
32-
if (icon == null || icon === "") {
33-
return;
34-
}
28+
Behaviour.specify("[id^=\"select-emoji-\"]", "EmojiPreviewSelection", 0, element => {
29+
element.onclick = () => {
30+
let icon = element.id.replace("select-emoji-", "");
3531

36-
let iconName = document.getElementById("emoji-icon-name");
37-
iconName.setAttribute("value", icon);
38-
iconName.dispatchEvent(new Event("input"));
32+
if (icon == null || icon === "") {
33+
return;
34+
}
3935

40-
let oldPreview = document.getElementById("emoji-preview");
41-
let selectedIcon = document.getElementById("emoji-icon-" + icon);
36+
let iconName = document.getElementById("emoji-icon-name");
37+
iconName.setAttribute("value", icon);
38+
iconName.dispatchEvent(new Event("input"));
4239

43-
let newPreview = selectedIcon.cloneNode(true);
44-
newPreview.id = "emoji-preview";
45-
newPreview.classList.replace("icon-md", "icon-xlg");
40+
let oldPreview = document.getElementById("emoji-preview");
41+
let selectedIcon = document.getElementById("emoji-icon-" + icon);
4642

47-
oldPreview.parentElement.replaceChild(newPreview, oldPreview);
48-
}
43+
let newPreview = selectedIcon.cloneNode(true);
44+
newPreview.id = "emoji-preview";
45+
newPreview.classList.replace("icon-md", "icon-xlg");
46+
47+
oldPreview.parentElement.replaceChild(newPreview, oldPreview);
48+
49+
return false;
50+
}
51+
});

0 commit comments

Comments
 (0)