Skip to content

Commit fb9bb11

Browse files
committed
added javascript previews
1 parent 9057f65 commit fb9bb11

File tree

2 files changed

+114
-5
lines changed

2 files changed

+114
-5
lines changed

static/js/event-management.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// This script validates the form inputs before submission and updates fields if necessary
2+
3+
document.addEventListener("DOMContentLoaded", function () {
4+
// update icon preview
5+
// TODO: add custom uploads for icons
6+
const iconInput = document.getElementById("icon");
7+
const iconPreview = document.getElementById("icon-preview");
8+
9+
iconInput.addEventListener("input", function () {
10+
if (iconInput.value.startsWith("ph-")) {
11+
// remove the "ph-" prefix if it exists
12+
iconInput.value = iconInput.value.substring(3);
13+
}
14+
15+
const newClass = "ph-" + iconInput.value.trim();
16+
iconPreview.className = "ph-bold " + newClass;
17+
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"
23+
}
24+
});
25+
26+
// update colour preview
27+
const colourPicker = document.getElementById("color_colour");
28+
const colourText = document.getElementById("text_colour");
29+
30+
function syncColourInputs(fromText) {
31+
if (fromText) {
32+
colourPicker.value = "#" + colourText.value;
33+
}
34+
else {
35+
colourText.value = colourPicker.value.substring(1); // remove the '#' character
36+
}
37+
}
38+
39+
colourPicker.addEventListener("input", () => syncColourInputs(false));
40+
colourText.addEventListener("input", () => syncColourInputs(true));
41+
42+
// update duration/end time
43+
const endTimeInput = document.getElementById("end_time");
44+
const durationInput = document.getElementById("duration");
45+
const startTimeInput = document.getElementById("start_time");
46+
47+
function formatDateTimeInput(input) {
48+
// format the input value to YYYY-MM-DDTHH:MM
49+
const pad = (num) => num.toString().padStart(2, "0");
50+
const year = input.getFullYear();
51+
const month = pad(input.getMonth() + 1); // months are zero-indexed
52+
const day = pad(input.getDate());
53+
const hours = pad(input.getHours());
54+
const minutes = pad(input.getMinutes());
55+
return `${year}-${month}-${day}T${hours}:${minutes}`;
56+
}
57+
58+
function updateDuration() {
59+
if (startTimeInput.value && endTimeInput.value) {
60+
const startTime = new Date(startTimeInput.value);
61+
const endTime = new Date(endTimeInput.value);
62+
63+
let duration = endTime - startTime;
64+
if (duration < 0) {
65+
duration = 0;
66+
}
67+
// convert duration into DD:HH:MM format
68+
const days = Math.floor(duration / (1000 * 60 * 60 * 24));
69+
const hours = Math.floor((duration % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
70+
const minutes = Math.floor((duration % (1000 * 60 * 60)) / (1000 * 60));
71+
72+
// prepend 0s if necessary
73+
const formattedDuration = [
74+
days.toString().padStart(2, '0'),
75+
hours.toString().padStart(2, '0'),
76+
minutes.toString().padStart(2, '0')
77+
].join(':');
78+
console.log(`Duration: ${formattedDuration}`);
79+
durationInput.value = formattedDuration;
80+
}
81+
}
82+
83+
function updateEndTime() {
84+
if (startTimeInput.value && durationInput.value) {
85+
// confirm that the duration is in DD:HH:MM format
86+
if (/^\d{1,2}:(?:[01]\d|2[0-3]):[0-5]\d$/.test(durationInput.value)) {
87+
const [days, hours, minutes] = durationInput.value.split(':').map(Number);
88+
const startTime = new Date(startTimeInput.value);
89+
90+
// calculate end time
91+
startTime.setDate(startTime.getDate() + days);
92+
startTime.setHours(startTime.getHours() + hours);
93+
startTime.setMinutes(startTime.getMinutes() + minutes);
94+
95+
// update end time input
96+
endTimeInput.value = formatDateTimeInput(startTime);
97+
}
98+
}
99+
}
100+
101+
startTimeInput.addEventListener("input", () => {
102+
updateDuration();
103+
updateEndTime();
104+
});
105+
durationInput.addEventListener("input", () => updateEndTime());
106+
endTimeInput.addEventListener("input", () => updateDuration());
107+
});

templates/events/form.html

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
{% block title %}Create Event{% endblock %}
44

5+
{% block header %}
6+
<script src="{{ url_for('static', filename='js/event-management.js') }}"></script>
7+
{% endblock %}
8+
59
{% block content %}
610
<div class="container text-center my-4">
711
<h1>Create Event</h1>
@@ -33,6 +37,7 @@ <h1>Create Event</h1>
3337
</div>
3438

3539
<!-- location -->
40+
<!-- TODO: maybe update location url?-->
3641
<div class="input-group col-12">
3742
<div class="input-group-text">
3843
<i class="ph-bold ph-map-pin"></i>
@@ -58,11 +63,11 @@ <h1>Create Event</h1>
5863
</div>
5964
</div>
6065

66+
<!-- TODO: allow for custom icons -->
6167
<div class="col-md-4">
6268
<div class="input-group">
63-
<!-- make this update -->
6469
<div class="input-group-text">
65-
<i class="ph-bold ph-phosphor-logo"></i>
70+
<i class="ph-bold ph-phosphor-logo" id="icon-preview"></i>
6671
</div>
6772
<div class="form-floating">
6873
<input type="text" name="icon" id="icon" class="form-control" placeholder="ph-calendar">
@@ -71,9 +76,6 @@ <h1>Create Event</h1>
7176
</div>
7277
</div>
7378

74-
<!-- have text input match color input and vice versa -->
75-
<!-- pre-input of # -->
76-
<!-- dropdown of pre-made colours -->
7779
<div class="col-md-4">
7880
<div class="input-group">
7981
<div class="input-group-text">

0 commit comments

Comments
 (0)