Skip to content

Commit 38151a4

Browse files
committed
added custom icons
1 parent 4659e46 commit 38151a4

File tree

9 files changed

+68
-14
lines changed

9 files changed

+68
-14
lines changed

events/ui.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from pathlib import Path
2+
13
from flask import Blueprint, flash, redirect, render_template, request, url_for
24
from werkzeug.wrappers import Response
35

@@ -16,6 +18,16 @@
1618
@events_ui_bp.route("/create", methods=["GET", "POST"])
1719
@is_exec_wrapper
1820
def create(error: str | None = None) -> str | Response: # noqa: PLR0911
21+
"""Create a new event."""
22+
23+
# icons are all .svg files in the static/icons directory
24+
icons = [
25+
f.stem
26+
for f in Path("static/icons").iterdir()
27+
if f.is_file() and f.suffix == ".svg"
28+
]
29+
icons.sort()
30+
1931
# if getting, return the ui for creating an event
2032
if request.method == "GET":
2133
return render_template(
@@ -24,6 +36,7 @@ def create(error: str | None = None) -> str | Response: # noqa: PLR0911
2436
action="events_ui.create",
2537
method="POST",
2638
event=None,
39+
icons=icons,
2740
)
2841

2942
# if posting, create the event

static/icons/bbq.svg

Lines changed: 4 additions & 0 deletions
Loading

static/icons/docker.svg

Lines changed: 3 additions & 0 deletions
Loading

static/icons/fng.svg

Lines changed: 4 additions & 0 deletions
Loading

static/icons/milk.svg

Lines changed: 3 additions & 0 deletions
Loading

static/icons/wasd.svg

Lines changed: 3 additions & 0 deletions
Loading

static/js/event-management.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
// This script validates the form inputs before submission and updates fields if necessary
22

3-
document.addEventListener("DOMContentLoaded", function () {
3+
document.addEventListener("DOMContentLoaded", () => {
4+
// load icons from the select element
5+
const icons = Array.from(document.querySelectorAll("#icon-list option")).map(option => option.value.trim());
6+
47
// update icon preview
5-
// TODO: add custom uploads for icons
68
const iconInput = document.getElementById("icon");
79
const iconPreview = document.getElementById("icon-preview");
10+
const customIconPreview = document.getElementById("custom-icon-preview");
811

9-
iconInput.addEventListener("input", function () {
12+
iconInput.addEventListener("input", () => {
1013
if (iconInput.value.startsWith("ph-")) {
1114
// remove the "ph-" prefix if it exists
1215
iconInput.value = iconInput.value.substring(3);
@@ -15,21 +18,28 @@ document.addEventListener("DOMContentLoaded", function () {
1518
const newClass = "ph-" + iconInput.value.trim();
1619
iconPreview.className = "ph-bold " + newClass;
1720

18-
// check if the icon is valid
19-
isValid = window.getComputedStyle(iconPreview, "::before").content !== "none";
20-
if (!isValid) {
21-
// default to generic icon if invalid
22-
iconPreview.className = "ph-bold ph-phosphor-logo"
21+
if (window.getComputedStyle(iconPreview, "::before").content !== "none") {
22+
// if valid phosphor icon, show the icon preview and remove custom icon preview
23+
customIconPreview.classList.add("d-none");
24+
} else if (icons.includes(iconInput.value)) {
25+
// if the icon is one of the predefined icons, show it
26+
iconPreview.className += " d-none";
27+
customIconPreview.classList.remove("d-none");
28+
customIconPreview.src = `/static/icons/${iconInput.value}.svg`;
29+
} else {
30+
// show phosphor logo if invalid
31+
iconPreview.className = "ph-bold ph-phosphor-logo";
32+
customIconPreview.classList.add("d-none");
2333
}
2434
});
2535

2636
// icon validation
2737
iconInput.addEventListener("input", () => {
28-
// invalid if the iconPreview is phosphor logo
29-
if (iconInput.value === "" || !iconPreview.className.includes("ph-phosphor-logo")) {
38+
if (iconInput.value === "" || !iconPreview.className.includes("ph-phosphor-logo") || icons.includes(iconInput.value)) {
39+
// valid if empty, valid phosphor icon, or one of the predefined icons
3040
iconInput.setCustomValidity("");
3141
} else {
32-
iconInput.setCustomValidity("Please provide a valid Phosphor Icon name or one of: " + "{{ ', '.join(icons) }}");
42+
iconInput.setCustomValidity("Please provide a valid Phosphor Icon name or one of: " + icons.join(", "));
3343
}
3444
});
3545

@@ -162,7 +172,7 @@ document.addEventListener("DOMContentLoaded", function () {
162172

163173
// form validation
164174
const form = document.querySelector("form");
165-
form.addEventListener("submit", function (event) {
175+
form.addEventListener("submit", (event) => {
166176
if (!form.checkValidity()) {
167177
event.preventDefault();
168178
event.stopPropagation();

static/sass/custom/_form.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,13 @@
3737

3838
.was-validated .validation-spacer {
3939
display: block;
40+
}
41+
42+
// size form images accordingly
43+
44+
.form-image {
45+
width: 1rem;
46+
height: 1rem;
47+
// convert to white
48+
filter: brightness(0) invert(1);
4049
}

0 commit comments

Comments
 (0)