@@ -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
@@ -128,6 +125,9 @@ class DualCategoryChartCardBlockDefinition extends window.wagtailStreamField.blo
128125 primary_field_values_inputs = null ;
129126 secondary_field_value_inputs = null ;
130127
128+ // DOM prefix
129+ prefix = null
130+
131131 /**
132132 * @name setupDataScript
133133 * @description Retrieve JSON data from script tag containing all category data
@@ -173,7 +173,7 @@ class DualCategoryChartCardBlockDefinition extends window.wagtailStreamField.blo
173173 if ( ! this . geography_type_field ) missingFields . push ( FIELD_SUFFIXES . GEOGRAPHY ) ;
174174 if ( ! this . secondary_category_field ) missingFields . push ( FIELD_SUFFIXES . SECONDARY_CATEGORY ) ;
175175
176- if ( missingFields . length > 0 ) {
176+ if ( missingFields . length > 0 ) {
177177 console . error ( `Category form fields not found: ${ missingFields . join ( ', ' ) } ` , {
178178 prefix,
179179 x_axis_field : this . x_axis_field ,
@@ -203,12 +203,11 @@ class DualCategoryChartCardBlockDefinition extends window.wagtailStreamField.blo
203203 const missingFields = [ ] ;
204204 if ( ! this . primary_field_values_inputs ) missingFields . push ( FIELD_SUFFIXES . PRIMARY_VALUES ) ;
205205 if ( ! this . secondary_field_value_inputs ) missingFields . push ( FIELD_SUFFIXES . SECONDARY_VALUES ) ;
206-
207206 if ( missingFields . length > 0 ) {
208207 console . error ( `Sub-category form fields not found: ${ missingFields . join ( ', ' ) } ` , {
209208 prefix,
210- primary_field_values : this . primary_field_values_inputs ,
211- secondary_field_value : this . primary_field_values_inputs ?. length || 0
209+ primary_field_values : this . primary_field_values_inputs ?. length || 0 ,
210+ secondary_field_value : this . secondary_field_value_inputs ?. length || 0 ,
212211 } ) ;
213212 }
214213 }
@@ -219,25 +218,25 @@ class DualCategoryChartCardBlockDefinition extends window.wagtailStreamField.blo
219218 * and restore saved state for existing content
220219 */
221220 initialiseFieldOptions ( ) {
222- if ( ! this . x_axis_field || ! this . secondary_category_field || ! this . secondary_category_choices ) {
221+ if ( ! this . x_axis_field || ! this . secondary_category_field || ! this . secondary_category_choices ) {
223222 return ;
224223 }
225224
226225 const { DEFAULT_OPTION } = DualCategoryChartCardBlockDefinition ;
227226
228- if ( this . x_axis_field && this . primary_field_values_inputs ) {
227+ if ( this . x_axis_field && this . primary_field_values_inputs ) {
229228 this . updatePrimaryFieldValueOptions ( this . x_axis_field . value ) ;
230229 }
231230
232- if ( this . secondary_category_field && this . secondary_field_value_inputs ) {
231+ if ( this . secondary_category_field && this . secondary_field_value_inputs ) {
233232 this . updateSecondaryFieldValueOptions ( this . secondary_category_field . value ) ;
234233 }
235234
236- if ( ! this . x_axis_field . value && this . primary_field_values_inputs ) {
235+ if ( ! this . x_axis_field . value && this . primary_field_values_inputs ) {
237236 this . primary_field_values_inputs . innerHTML = `<option value="${ DEFAULT_OPTION . VALUE } ">${ DEFAULT_OPTION . TEXT } </option>` ;
238237 }
239238
240- if ( ! this . secondary_category_field . value && this . secondary_field_value_inputs . length > 0 ) {
239+ if ( ! this . secondary_category_field . value && this . secondary_field_value_inputs . length > 0 ) {
241240 this . secondary_field_value_inputs . forEach ( input => {
242241 input . innerHTML = `<option value="${ DEFAULT_OPTION . VALUE } ">${ DEFAULT_OPTION . TEXT } </option>` ;
243242 } ) ;
@@ -325,10 +324,10 @@ class DualCategoryChartCardBlockDefinition extends window.wagtailStreamField.blo
325324 * @param prefix
326325 */
327326 observeSegmentBlocks ( prefix ) {
328- const { SELECTORS } = DualCategoryChartCardBlockDefinition ;
327+ const { SELECTORS } = DualCategoryChartCardBlockDefinition ;
329328 const segments_container = document . querySelector ( SELECTORS . SEGMENTS_CONTAINER )
330329
331- if ( ! segments_container ) {
330+ if ( ! segments_container ) {
332331 console . warn ( "Segments container not found - observer disabled" ) ;
333332 return ;
334333 }
@@ -337,8 +336,8 @@ class DualCategoryChartCardBlockDefinition extends window.wagtailStreamField.blo
337336 mutations . forEach ( ( mutation ) => {
338337 if ( mutation . type === "childList" ) {
339338 mutation . addedNodes . forEach ( node => {
340- if ( node . nodeType === 1 && node . classList . contains ( SELECTORS . SEGMENT_ITEM ) ) {
341-
339+ if ( node . nodeType === 1 && node . matches ( SELECTORS . SEGMENT_ITEM ) ) {
340+ console . log ( "MUTATION:" , mutation )
342341 this . stateManager . storeCurrentSelections ( this . secondary_field_value_inputs ) ;
343342
344343 this . setupSubCategoryFormFields ( prefix ) ;
@@ -374,22 +373,34 @@ class DualCategoryChartCardBlockDefinition extends window.wagtailStreamField.blo
374373 this . secondary_category_field . addEventListener ( "change" , ( evt ) => {
375374 this . stateManager . clearSecondaryFieldState ( ) ;
376375 this . updateSecondaryFieldValueOptions ( evt . target . value ) ;
376+
377+ // clear all segments
378+ document
379+ . querySelectorAll ( '.dual-category-chart-card__segments' )
380+ . forEach ( segments => {
381+ const section = segments . closest ( 'section[id$="-section"]' ) ;
382+ section
383+ ?. querySelector ( '[data-streamfield-action="DELETE"]' )
384+ ?. click ( ) ;
385+ } ) ;
377386 } ) ;
378387
379388 this . geography_type_field . addEventListener ( "change" , ( evt ) => {
380- if ( this . x_axis_field . value === FIELD_SUFFIXES . GEOGRAPHY_SUBCATEGORY_KEY ) {
389+ if ( this . x_axis_field . value === FIELD_SUFFIXES . GEOGRAPHY_SUBCATEGORY_KEY ) {
381390 this . stateManager . clearPrimaryFieldState ( ) ;
382391 this . updatePrimaryFieldValueOptions ( this . x_axis_field . value ) ;
383392 }
384393
385- if ( this . secondary_category_field . value === FIELD_SUFFIXES . GEOGRAPHY_SUBCATEGORY_KEY ) {
394+ if ( this . secondary_category_field . value === FIELD_SUFFIXES . GEOGRAPHY_SUBCATEGORY_KEY ) {
386395 this . stateManager . clearSecondaryFieldState ( ) ;
387396 this . updateSecondaryFieldValueOptions ( this . secondary_category_field . value ) ;
388397 }
389398 } ) ;
390399 }
391400
392401 render ( placeholder , prefix , initialState , initialError ) {
402+ this . prefix = prefix
403+
393404 const block = super . render ( placeholder , prefix , initialState , initialError ) ;
394405
395406 // initialise form state
@@ -414,7 +425,7 @@ class DualCategoryChartCardBlockDefinition extends window.wagtailStreamField.blo
414425
415426}
416427
417- window . telepath . register (
428+ globalThis . telepath . register (
418429 'cms.dynamic_content.cards.DualCategoryChartCard' ,
419430 DualCategoryChartCardBlockDefinition ,
420431) ;
0 commit comments