Skip to content

Commit d4b81c4

Browse files
committed
internal/relui: fix log viewing errors
Wait for DOMContentLoaded before trying to add listeners, as some of the data may not be in the dom yet when many workflows are visible. If the page has already loaded, register them immediately. Update eslint to require function expression declarations in javascript. Remove inline JavaScript from the workflow form. Registering event listeners in JavaScript instead of in HTML attributes is preferable for several reasons: - Separation of JavaScript logic from view - Avoids polluting the global namespace - Allows attaching multiple listeners to elements - Easier code re-use Updates golang/go#53382 Change-Id: I3dfefd1212482448eb2ff23aa77f89ead57823d8 Reviewed-on: https://go-review.googlesource.com/c/build/+/412678 Run-TryBot: Alex Rakoczy <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent cef0fbf commit d4b81c4

File tree

3 files changed

+81
-46
lines changed

3 files changed

+81
-46
lines changed

cmd/relui/.eslintrc.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ overrides:
1515
- eslint:recommended
1616
- plugin:prettier/recommended
1717
rules:
18-
max-len: off
18+
func-style:
19+
- error
20+
- "expression"
21+
prettier/prettier:
22+
- error
23+
- printWidth: 120
1924
valid-jsdoc:
2025
- error
2126
- requireParamType: false

internal/relui/static/site.js

Lines changed: 69 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,58 +14,83 @@
1414
*
1515
* @param {string} selector - css selector for target element
1616
*/
17-
function registerTaskListExpandListeners(selector) {
17+
const registerTaskListExpandListeners = (selector) => {
1818
document.querySelectorAll(selector).forEach((element) => {
1919
element.addEventListener("click", (e) => {
2020
e.stopPropagation();
2121
element.classList.toggle("TaskList-expanded");
2222
element.nextElementSibling.classList.toggle("TaskList-expanded");
2323
});
2424
});
25-
}
25+
};
2626

27-
registerTaskListExpandListeners(".TaskList-expandableItem");
28-
})();
27+
/**
28+
* addSliceRow creates and appends a row to a slice parameter
29+
* for filling in an element.
30+
*
31+
* @param {HTMLElement} container - the container element
32+
* @param {string} paramName - the parameter name
33+
* @param {string} element - the element tag to create
34+
* @param {string} inputType - the type attribute if element is "input"
35+
* @param {string} paramExample - an example value for the parameter
36+
*/
37+
const addSliceRow = (container, paramName, element, inputType, paramExample) => {
38+
/*
39+
Create an input element, a button to remove it, group them in a "parameterRow" div:
2940
30-
/* eslint-disable no-unused-vars */
31-
/**
32-
* addSliceRow creates and appends a row to a slice parameter
33-
* for filling in an element.
34-
*
35-
* @param {HTMLElement} container - the container element
36-
* @param {string} paramName - the parameter name
37-
* @param {string} element - the element tag to create
38-
* @param {string} inputType - the type attribute if element is "input"
39-
* @param {string} paramExample - an example value for the parameter
40-
*/
41-
function addSliceRow(container, paramName, element, inputType, paramExample) {
42-
/*
43-
Create an input element, a button to remove it, group them in a "parameterRow" div:
41+
<div class="NewWorkflow-parameterRow">
42+
<input name="workflow.params.{{$p.Name}}" placeholder="{{paramExample}}" />
43+
<button title="Remove this row from the slice." onclick="/ * Remove this row. * /">-</button>
44+
</div>
45+
*/
46+
const input = document.createElement(element);
47+
input.name = "workflow.params." + paramName;
48+
if (element === "input") {
49+
input.type = inputType;
50+
}
51+
input.placeholder = paramExample;
52+
const removeButton = document.createElement("button");
53+
removeButton.title = "Remove this row from the slice.";
54+
removeButton.addEventListener("click", (e) => {
55+
e.preventDefault();
56+
container.removeChild(div);
57+
});
58+
removeButton.appendChild(document.createTextNode("-"));
59+
const div = document.createElement("div");
60+
div.className = "NewWorkflow-parameterRow";
61+
div.appendChild(input);
62+
div.appendChild(removeButton);
4463

45-
<div class="NewWorkflow-parameterRow">
46-
<input name="workflow.params.{{$p.Name}}" placeholder="{{paramExample}}" />
47-
<button title="Remove this row from the slice." onclick="/ * Remove this row. * /">-</button>
48-
</div>
49-
*/
50-
const input = document.createElement(element);
51-
input.name = "workflow.params." + paramName;
52-
if (element === "input") {
53-
input.type = inputType;
54-
}
55-
input.placeholder = paramExample;
56-
const removeButton = document.createElement("button");
57-
removeButton.title = "Remove this row from the slice.";
58-
removeButton.addEventListener("click", (e) => {
59-
e.preventDefault();
60-
container.removeChild(div);
61-
});
62-
removeButton.appendChild(document.createTextNode("-"));
63-
const div = document.createElement("div");
64-
div.className = "NewWorkflow-parameterRow";
65-
div.appendChild(input);
66-
div.appendChild(removeButton);
64+
// Add the "parameterRow" div to the document.
65+
container.appendChild(div);
66+
};
6767

68-
// Add the "parameterRow" div to the document.
69-
container.appendChild(div);
70-
}
71-
/* eslint-enable no-unused-vars */
68+
/** addSliceRowListener registers listeners for addSliceRow.
69+
*
70+
* @param {string} selector - elements to add click listener for addSliceRow.
71+
*/
72+
const addSliceRowListener = (selector) => {
73+
document.querySelectorAll(selector).forEach((element) => {
74+
element.addEventListener("click", (e) => {
75+
e.stopPropagation();
76+
addSliceRow(
77+
element.parentElement.parentElement,
78+
element.dataset.paramname,
79+
element.dataset.element,
80+
element.dataset.inputtype,
81+
element.dataset.paramexample
82+
);
83+
});
84+
});
85+
};
86+
87+
const registerListeners = () => {
88+
registerTaskListExpandListeners(".TaskList-expandableItem");
89+
addSliceRowListener(".NewWorkflow-addSliceRowButton");
90+
};
91+
if (document.readyState === "loading") {
92+
document.addEventListener("DOMContentLoaded", registerListeners);
93+
} else {
94+
registerListeners();
95+
}
96+
})();

internal/relui/templates/new_workflow.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ <h2>New Go Release</h2>
3737
<div class="NewWorkflow-parameter NewWorkflow-parameter--slice">
3838
<div class="NewWorkflow-parameterRow">
3939
<label title="{{$p.Doc}}">{{$p.Name}}</label>
40-
<button title="Increment the slice length." onclick="event.preventDefault(); addSliceRow(this.parentElement.parentElement, '{{$p.Name}}', '{{$p.HTMLElement}}', '{{$p.HTMLInputType}}', '{{$p.Example}}');">+</button>
40+
<button class="NewWorkflow-addSliceRowButton" title="Increment the slice length."
41+
type="button"
42+
data-ParamName="{{$p.Name}}"
43+
data-Element="{{$p.HTMLElement}}"
44+
data-InputType="{{$p.HTMLInputType}}"
45+
data-ParamExample="{{$p.Example}}">+</button>
4146
</div>
4247
</div>
4348
{{else}}

0 commit comments

Comments
 (0)