@@ -52,8 +52,8 @@ class FormStateManger {
5252 this . secondary_field_value_state = Array . from ( inputs ) . map ( input => {
5353 const selectedValues = [ ] ;
5454
55- for ( let i = 0 ; i < input . options . length ; i ++ ) {
56- const option = input . options [ i ] ;
55+ for ( const element of input . options ) {
56+ const option = element ;
5757 if ( option . selected && option . value !== "" ) {
5858 selectedValues . push ( option . value ) ;
5959 }
@@ -71,9 +71,7 @@ class FormStateManger {
7171 * @return {boolean } whether the value should be selected
7272 */
7373 isSecondaryFieldOptionValueSelected ( value , segmentIndex ) {
74- return this . secondary_field_value_state &&
75- this . secondary_field_value_state [ segmentIndex ] &&
76- this . secondary_field_value_state [ segmentIndex ] . includes ( value ) ;
74+ return this . secondary_field_value_state ?. [ segmentIndex ] ?. includes ( value ) ;
7775 }
7876
7977 /**
@@ -83,17 +81,16 @@ class FormStateManger {
8381 * @return {boolean } whether the value should be selected
8482 */
8583 isPrimaryValueSelected ( value ) {
86- return this . primary_field_values_state &&
87- this . primary_field_values_state . includes ( value )
84+ return this . primary_field_values_state ?. includes ( value )
8885 }
8986}
9087
9188
92- class DualCategoryChartCardBlockDefinition extends window . wagtailStreamField . blocks . StructBlockDefinition {
89+ class DualCategoryChartCardBlockDefinition extends globalThis . wagtailStreamField . blocks . StructBlockDefinition {
9390
94- static SELECTORS = {
95- SEGMENTS_CONTAINER : '[class=" dual-category-chart-card"] [data-streamfield-list- container] ' ,
96- SEGMENT_ITEM : 'dual-category-chart-card__segments' ,
91+ static SELECTORS = {
92+ SEGMENTS_CONTAINER : '. dual-category-chart-segments- container-form ' ,
93+ SEGMENT_ITEM : '. dual-category-chart-card__segments' ,
9794 SECONDARY_FIELD_VALUE : '[id$="secondary_field_value"]'
9895 } ;
9996
@@ -173,7 +170,7 @@ class DualCategoryChartCardBlockDefinition extends window.wagtailStreamField.blo
173170 if ( ! this . geography_type_field ) missingFields . push ( FIELD_SUFFIXES . GEOGRAPHY ) ;
174171 if ( ! this . secondary_category_field ) missingFields . push ( FIELD_SUFFIXES . SECONDARY_CATEGORY ) ;
175172
176- if ( missingFields . length > 0 ) {
173+ if ( missingFields . length > 0 ) {
177174 console . error ( `Category form fields not found: ${ missingFields . join ( ', ' ) } ` , {
178175 prefix,
179176 x_axis_field : this . x_axis_field ,
@@ -203,12 +200,11 @@ class DualCategoryChartCardBlockDefinition extends window.wagtailStreamField.blo
203200 const missingFields = [ ] ;
204201 if ( ! this . primary_field_values_inputs ) missingFields . push ( FIELD_SUFFIXES . PRIMARY_VALUES ) ;
205202 if ( ! this . secondary_field_value_inputs ) missingFields . push ( FIELD_SUFFIXES . SECONDARY_VALUES ) ;
206-
207203 if ( missingFields . length > 0 ) {
208204 console . error ( `Sub-category form fields not found: ${ missingFields . join ( ', ' ) } ` , {
209205 prefix,
210- primary_field_values : this . primary_field_values_inputs ,
211- secondary_field_value : this . primary_field_values_inputs ?. length || 0
206+ primary_field_values : this . primary_field_values_inputs ?. length || 0 ,
207+ secondary_field_value : this . secondary_field_value_inputs ?. length || 0 ,
212208 } ) ;
213209 }
214210 }
@@ -219,25 +215,25 @@ class DualCategoryChartCardBlockDefinition extends window.wagtailStreamField.blo
219215 * and restore saved state for existing content
220216 */
221217 initialiseFieldOptions ( ) {
222- if ( ! this . x_axis_field || ! this . secondary_category_field || ! this . secondary_category_choices ) {
218+ if ( ! this . x_axis_field || ! this . secondary_category_field || ! this . secondary_category_choices ) {
223219 return ;
224220 }
225221
226222 const { DEFAULT_OPTION } = DualCategoryChartCardBlockDefinition ;
227223
228- if ( this . x_axis_field && this . primary_field_values_inputs ) {
224+ if ( this . x_axis_field && this . primary_field_values_inputs ) {
229225 this . updatePrimaryFieldValueOptions ( this . x_axis_field . value ) ;
230226 }
231227
232- if ( this . secondary_category_field && this . secondary_field_value_inputs ) {
228+ if ( this . secondary_category_field && this . secondary_field_value_inputs ) {
233229 this . updateSecondaryFieldValueOptions ( this . secondary_category_field . value ) ;
234230 }
235231
236- if ( ! this . x_axis_field . value && this . primary_field_values_inputs ) {
232+ if ( ! this . x_axis_field . value && this . primary_field_values_inputs ) {
237233 this . primary_field_values_inputs . innerHTML = `<option value="${ DEFAULT_OPTION . VALUE } ">${ DEFAULT_OPTION . TEXT } </option>` ;
238234 }
239235
240- if ( ! this . secondary_category_field . value && this . secondary_field_value_inputs . length > 0 ) {
236+ if ( ! this . secondary_category_field . value && this . secondary_field_value_inputs . length > 0 ) {
241237 this . secondary_field_value_inputs . forEach ( input => {
242238 input . innerHTML = `<option value="${ DEFAULT_OPTION . VALUE } ">${ DEFAULT_OPTION . TEXT } </option>` ;
243239 } ) ;
@@ -325,10 +321,10 @@ class DualCategoryChartCardBlockDefinition extends window.wagtailStreamField.blo
325321 * @param prefix
326322 */
327323 observeSegmentBlocks ( prefix ) {
328- const { SELECTORS } = DualCategoryChartCardBlockDefinition ;
324+ const { SELECTORS } = DualCategoryChartCardBlockDefinition ;
329325 const segments_container = document . querySelector ( SELECTORS . SEGMENTS_CONTAINER )
330326
331- if ( ! segments_container ) {
327+ if ( ! segments_container ) {
332328 console . warn ( "Segments container not found - observer disabled" ) ;
333329 return ;
334330 }
@@ -337,8 +333,7 @@ class DualCategoryChartCardBlockDefinition extends window.wagtailStreamField.blo
337333 mutations . forEach ( ( mutation ) => {
338334 if ( mutation . type === "childList" ) {
339335 mutation . addedNodes . forEach ( node => {
340- if ( node . nodeType === 1 && node . classList . contains ( SELECTORS . SEGMENT_ITEM ) ) {
341-
336+ if ( node . nodeType === 1 && node . matches ( SELECTORS . SEGMENT_ITEM ) ) {
342337 this . stateManager . storeCurrentSelections ( this . secondary_field_value_inputs ) ;
343338
344339 this . setupSubCategoryFormFields ( prefix ) ;
@@ -374,22 +369,33 @@ class DualCategoryChartCardBlockDefinition extends window.wagtailStreamField.blo
374369 this . secondary_category_field . addEventListener ( "change" , ( evt ) => {
375370 this . stateManager . clearSecondaryFieldState ( ) ;
376371 this . updateSecondaryFieldValueOptions ( evt . target . value ) ;
372+
373+ // clear all segments
374+ document
375+ . querySelectorAll ( '.dual-category-chart-card__segments' )
376+ . forEach ( segments => {
377+ const section = segments . closest ( 'section[id$="-section"]' ) ;
378+ section
379+ ?. querySelector ( '[data-streamfield-action="DELETE"]' )
380+ ?. click ( ) ;
381+ } ) ;
377382 } ) ;
378383
379384 this . geography_type_field . addEventListener ( "change" , ( evt ) => {
380- if ( this . x_axis_field . value === FIELD_SUFFIXES . GEOGRAPHY_SUBCATEGORY_KEY ) {
385+ if ( this . x_axis_field . value === FIELD_SUFFIXES . GEOGRAPHY_SUBCATEGORY_KEY ) {
381386 this . stateManager . clearPrimaryFieldState ( ) ;
382387 this . updatePrimaryFieldValueOptions ( this . x_axis_field . value ) ;
383388 }
384389
385- if ( this . secondary_category_field . value === FIELD_SUFFIXES . GEOGRAPHY_SUBCATEGORY_KEY ) {
390+ if ( this . secondary_category_field . value === FIELD_SUFFIXES . GEOGRAPHY_SUBCATEGORY_KEY ) {
386391 this . stateManager . clearSecondaryFieldState ( ) ;
387392 this . updateSecondaryFieldValueOptions ( this . secondary_category_field . value ) ;
388393 }
389394 } ) ;
390395 }
391396
392397 render ( placeholder , prefix , initialState , initialError ) {
398+
393399 const block = super . render ( placeholder , prefix , initialState , initialError ) ;
394400
395401 // initialise form state
@@ -414,7 +420,7 @@ class DualCategoryChartCardBlockDefinition extends window.wagtailStreamField.blo
414420
415421}
416422
417- window . telepath . register (
423+ globalThis . telepath . register (
418424 'cms.dynamic_content.cards.DualCategoryChartCard' ,
419425 DualCategoryChartCardBlockDefinition ,
420426) ;
0 commit comments