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 } : (?: [ 0 1 ] \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+ } ) ;
0 commit comments