Skip to content

Commit f83eff7

Browse files
committed
Simplify the interaction with the color picker
1 parent d20ee99 commit f83eff7

File tree

5 files changed

+52
-95
lines changed

5 files changed

+52
-95
lines changed

ts/WoltLabSuite/Core/Component/Icon/Badge.ts

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,25 @@
77
* @since 6.3
88
*/
99

10-
export class IconBadge {
11-
#iconContainer: HTMLElement;
12-
#colorInput?: HTMLInputElement;
13-
#backgroundColorInput?: HTMLInputElement;
14-
15-
constructor(iconFieldId: string, colorFieldId?: string, backgroundColorFieldId?: string) {
16-
this.#iconContainer = document.getElementById(`${iconFieldId}_icon`)!;
17-
this.#iconContainer.classList.add("iconBadge");
18-
19-
const observer = new MutationObserver((mutationsList) => {
20-
for (const mutation of mutationsList) {
21-
if (mutation.type === "attributes" && mutation.attributeName === "value") {
22-
this.#updateIcon();
23-
}
24-
}
10+
export function setup(iconFieldId: string, colorFieldId: string, backgroundColorFieldId: string) {
11+
const container = document.getElementById(`${iconFieldId}_icon`)!;
12+
container.classList.add("iconBadge");
13+
14+
if (colorFieldId !== "") {
15+
const colorInput = document.getElementById(colorFieldId) as HTMLInputElement;
16+
colorInput.addEventListener("color-picker:submit", () => {
17+
container.style.setProperty("color", colorInput.value);
2518
});
2619

27-
if (colorFieldId) {
28-
this.#colorInput = document.getElementById(colorFieldId) as HTMLInputElement;
29-
30-
observer.observe(this.#colorInput, {
31-
attributes: true,
32-
attributeFilter: ["value"],
33-
});
34-
}
35-
36-
if (backgroundColorFieldId) {
37-
this.#backgroundColorInput = document.getElementById(backgroundColorFieldId) as HTMLInputElement;
38-
39-
observer.observe(this.#backgroundColorInput, {
40-
attributes: true,
41-
attributeFilter: ["value"],
42-
});
43-
}
44-
45-
this.#updateIcon();
20+
container.style.setProperty("color", colorInput.value);
4621
}
4722

48-
#updateIcon(): void {
49-
this.#iconContainer.style.color = this.#colorInput?.value || "";
50-
this.#iconContainer.style.backgroundColor = this.#backgroundColorInput?.value || "";
23+
if (backgroundColorFieldId !== "") {
24+
const backgroundColorInput = document.getElementById(backgroundColorFieldId) as HTMLInputElement;
25+
backgroundColorInput.addEventListener("color-picker:submit", () => {
26+
container.style.setProperty("background-color", backgroundColorInput.value);
27+
});
28+
29+
container.style.setProperty("background-color", backgroundColorInput.value);
5130
}
5231
}

ts/WoltLabSuite/Core/Ui/Color/Picker.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ class UiColorPicker implements DialogCallbackObject {
131131
<div class="colorPickerComparison">
132132
<small>${Language.get("wcf.style.colorPicker.new")}</small>
133133
<div class="colorPickerColorNew">
134-
<span style="background-color: ${this.input.value}"></span>
134+
<span style="background-color: ${this.input.value}; flex: 1 auto"></span>
135135
</div>
136136
<div class="colorPickerColorOld">
137-
<span style="background-color: ${this.input.value}"></span>
137+
<span style="background-color: ${this.input.value}; flex: 1 auto"></span>
138138
</div>
139139
<small>${Language.get("wcf.style.colorPicker.current")}</small>
140140
</div>
@@ -354,16 +354,15 @@ class UiColorPicker implements DialogCallbackObject {
354354
const colorString = ColorUtil.rgbaToString(color);
355355

356356
this.oldColor!.style.backgroundColor = colorString;
357-
// The change in value via `this.input.value = colorString;` cannot be detected by a MutationObserver.
358-
this.input.setAttribute("value", colorString);
357+
this.input.value = colorString;
359358

360-
if (!(this.element instanceof HTMLButtonElement)) {
361-
const span = this.element.querySelector("span");
362-
if (span) {
363-
span.style.backgroundColor = colorString;
364-
} else {
365-
this.element.style.backgroundColor = colorString;
366-
}
359+
const event = new CustomEvent<void>("color-picker:submit");
360+
this.input.dispatchEvent(event);
361+
362+
const span = this.element.querySelector("span")
363+
if (span !== null) {
364+
span.classList.add("colorPickerButton__color");
365+
span.style.setProperty("background-color", colorString);
367366
}
368367

369368
UiDialog.close(this);

wcfsetup/install/files/acp/templates/trophyAdd.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
{unsafe:$form->getHtml()}
2121

2222
<script data-relocate="true">
23-
require(['WoltLabSuite/Core/Component/Icon/Badge'], ({ IconBadge }) => {
24-
new IconBadge('iconName', 'iconColor', 'badgeColor');
23+
require(["WoltLabSuite/Core/Component/Icon/Badge"], ({ setup }) => {
24+
setup("iconName", "iconColor", "badgeColor");
2525
});
2626
</script>
2727

wcfsetup/install/files/js/WoltLabSuite/Core/Component/Icon/Badge.js

Lines changed: 15 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Color/Picker.js

Lines changed: 9 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)