forked from jenkinsci/design-library-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.js
More file actions
85 lines (75 loc) · 2.92 KB
/
sample.js
File metadata and controls
85 lines (75 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
document.addEventListener("DOMContentLoaded", () => {
const url = document.querySelector("head").dataset.rooturl;
document.querySelectorAll(".callback-button").forEach((element) => {
let callback = element.dataset.callback;
element.onclick = () => {
if (
callback &&
window[callback] &&
typeof window[callback] === "function"
) {
window[callback]();
} else {
console.warn(`Callback function ${callback} is not defined`);
}
};
});
document.querySelectorAll(".sample-remote").forEach((element) => {
const uiComponentName = element.dataset.componentName;
const fileName = element.dataset.sample;
const executable = element.dataset.executable;
// On the inputs page the preview markup link adds a hash to the url which breaks the regex extraction
const fullUrl = `${url}/plugin/design-library/${uiComponentName}/${fileName}`;
fetch(fullUrl)
.then((response) => response.text())
.then((text) => {
element.innerText = text;
Prism.highlightElement(element);
function setPrismBackgroundVariable() {
const computedStyle = window.getComputedStyle(element.parentElement);
const background = computedStyle.getPropertyValue("background");
document.documentElement.style.setProperty(prismVariable, background);
}
// This is for the copy clipboard section which doesn't use prism
// We need to match the colour
const prismVariable = "--prism-background";
if (
!getComputedStyle(document.documentElement).getPropertyValue(
prismVariable,
)
) {
setPrismBackgroundVariable();
if (window.isSystemRespectingTheme) {
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", () => {
// If done immediately while appearance is changing from light to dark sometimes the wrong value is retrieved
// A slight delay fixes this
setTimeout(() => setPrismBackgroundVariable(), 50);
});
}
}
const codeWrapper = element.closest(".jdl-component-code");
if (codeWrapper) {
const copyButton = codeWrapper.querySelector(
".copy-button, .jenkins-copy-button",
);
copyButton.setAttribute("text", text);
}
});
if (executable === "true") {
const script = document.createElement("script"); // create a script DOM node
script.src = fullUrl; // set its src to the provided URL
document.head.appendChild(script);
}
});
document
.querySelectorAll(".jdl-component-code__expander")
.forEach((expander) => {
expander.addEventListener("click", () => {
expander
.closest(".jdl-component-code")
.classList.toggle("jdl-component-code--minimized");
});
});
});