Skip to content

Commit b7632cf

Browse files
Tsvetan StoychevTsvetan Stoychev
authored andcommitted
Improve monitoring settings validation
1 parent b48bde1 commit b7632cf

14 files changed

Lines changed: 1041 additions & 177 deletions

File tree

checklist.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ Acceptance criteria:
1919

2020
## 2. Resolve the unused consent mode setting
2121

22-
- [ ] Define the intended behavior for `explicit`, `implicit`, `cookie_banner`,
23-
and `gdpr_banner` modes.
22+
- [ ] Define the intended behavior for `explicit`, `implicit`, and `cookie_popup`
23+
modes.
2424
- [ ] Decide whether each mode has distinct runtime behavior.
2525
- [ ] Implement the defined behavior, or remove modes that do not represent real
2626
behavior and keep a single consent-enabled switch.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.basicrum-field-control {
2+
display: inline-flex;
3+
align-items: center;
4+
gap: 6px;
5+
max-width: 100%;
6+
}
7+
8+
.basicrum-field-control .regular-text {
9+
min-width: 0;
10+
}
11+
12+
.basicrum-field-error-icon {
13+
color: #d63638;
14+
flex: 0 0 auto;
15+
}
16+
17+
.basicrum-field-error-icon[hidden] {
18+
display: none;
19+
}
20+
21+
.form-table p.basicrum-field-error-message {
22+
color: #d63638;
23+
font-weight: 600;
24+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
( function() {
2+
'use strict';
3+
4+
document.addEventListener( 'DOMContentLoaded', function() {
5+
var enabled = document.getElementById( 'basicrum_enabled' );
6+
var form = enabled ? enabled.closest( 'form' ) : null;
7+
var fieldIds = [ 'basicrum_beacon_url', 'basicrum_brum_site_id' ];
8+
var requiredFields = fieldIds.map( function( fieldId ) {
9+
return document.getElementById( fieldId );
10+
} ).filter( Boolean );
11+
var dependentFields = form ? Array.from( form.querySelectorAll( 'input, select, textarea' ) ).filter( function( field ) {
12+
return field !== enabled && 'hidden' !== field.type && 0 === field.name.indexOf( 'basicrum_settings[' );
13+
} ) : [];
14+
var preservedValues = form ? Array.from( form.querySelectorAll( '.basicrum-disabled-setting-value' ) ) : [];
15+
16+
if ( ! enabled || ! form || ! requiredFields.length ) {
17+
return;
18+
}
19+
20+
function setInvalidState( field, isInvalid ) {
21+
var row = field.closest( 'tr' );
22+
var error = document.getElementById( field.id + '_error' );
23+
var errorIcon = document.getElementById( field.id + '_error_icon' );
24+
25+
field.setAttribute( 'aria-invalid', isInvalid ? 'true' : 'false' );
26+
27+
if ( row ) {
28+
row.classList.toggle( 'form-invalid', isInvalid );
29+
}
30+
31+
if ( error ) {
32+
error.hidden = ! isInvalid;
33+
}
34+
35+
if ( errorIcon ) {
36+
errorIcon.hidden = ! isInvalid;
37+
}
38+
}
39+
40+
function syncField( field, showInvalid ) {
41+
var isRequired = enabled.checked;
42+
var isEmpty = '' === field.value.trim();
43+
var row = field.closest( 'tr' );
44+
45+
field.required = isRequired;
46+
field.setAttribute( 'aria-required', isRequired ? 'true' : 'false' );
47+
field.setCustomValidity( isRequired && isEmpty ? field.dataset.requiredMessage : '' );
48+
49+
field.classList.toggle( 'form-required', isRequired );
50+
51+
setInvalidState( field, isRequired && isEmpty && showInvalid );
52+
}
53+
54+
function syncAvailability() {
55+
var isEnabled = enabled.checked;
56+
57+
dependentFields.forEach( function( field ) {
58+
field.disabled = ! isEnabled;
59+
field.setAttribute( 'aria-disabled', isEnabled ? 'false' : 'true' );
60+
} );
61+
62+
preservedValues.forEach( function( field ) {
63+
field.disabled = isEnabled;
64+
} );
65+
}
66+
67+
syncAvailability();
68+
69+
requiredFields.forEach( function( field ) {
70+
var showInvalid = 'true' === field.getAttribute( 'aria-invalid' );
71+
72+
syncField( field, showInvalid );
73+
74+
field.addEventListener( 'invalid', function() {
75+
syncField( field, true );
76+
} );
77+
78+
field.addEventListener( 'input', function() {
79+
syncField( field, 'true' === field.getAttribute( 'aria-invalid' ) );
80+
} );
81+
} );
82+
83+
enabled.addEventListener( 'change', function() {
84+
syncAvailability();
85+
86+
requiredFields.forEach( function( field ) {
87+
syncField( field, false );
88+
} );
89+
} );
90+
91+
form.addEventListener( 'submit', function() {
92+
if ( enabled.checked ) {
93+
return;
94+
}
95+
96+
preservedValues.forEach( function( field ) {
97+
field.disabled = true;
98+
} );
99+
100+
dependentFields.forEach( function( field ) {
101+
field.disabled = false;
102+
} );
103+
} );
104+
} );
105+
}() );
-164 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)