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 ( ) ;
0 commit comments