| 1 |
import RepeaterRowView from './fields-repeater-row'; |
| 2 |
|
| 3 |
export default class extends elementor.modules.controls.Repeater { |
| 4 |
className() { |
| 5 |
let classes = super.className(); |
| 6 |
|
| 7 |
classes += ' elementor-control-type-repeater'; |
| 8 |
|
| 9 |
return classes; |
| 10 |
} |
| 11 |
|
| 12 |
getChildView() { |
| 13 |
return RepeaterRowView; |
| 14 |
} |
| 15 |
|
| 16 |
initialize( ...args ) { |
| 17 |
super.initialize( ...args ); |
| 18 |
|
| 19 |
const formFields = this.container.settings.get( 'form_fields' ); |
| 20 |
|
| 21 |
this |
| 22 |
.listenTo( formFields, 'change', ( model ) => this.onFormFieldChange( model ) ) |
| 23 |
.listenTo( formFields, 'remove', ( model ) => this.onFormFieldRemove( model ) ); |
| 24 |
} |
| 25 |
|
| 26 |
getFirstChild() { |
| 27 |
return this.children.findByModel( this.collection.models[ 0 ] ); |
| 28 |
} |
| 29 |
|
| 30 |
lockFirstStep() { |
| 31 |
const firstChild = this.getFirstChild(); |
| 32 |
|
| 33 |
if ( 'step' !== firstChild.model.get( 'field_type' ) ) { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
const stepFields = this.collection.where( { field_type: 'step' } ); |
| 38 |
|
| 39 |
if ( 1 < stepFields.length ) { |
| 40 |
firstChild.toggleFieldTypeControl( false ); |
| 41 |
|
| 42 |
firstChild.toggleTools( false ); |
| 43 |
} |
| 44 |
|
| 45 |
firstChild.toggleSort( false ); |
| 46 |
} |
| 47 |
|
| 48 |
onFormFieldChange( model ) { |
| 49 |
const fieldType = model.changed.field_type; |
| 50 |
|
| 51 |
if ( ! fieldType || ( 'step' !== fieldType && 'step' !== model._previousAttributes.field_type ) ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
const isStep = 'step' === fieldType; |
| 56 |
|
| 57 |
this.children.findByModel( model ).toggleStepField( isStep ); |
| 58 |
|
| 59 |
this.onStepFieldChanged( isStep ); |
| 60 |
} |
| 61 |
|
| 62 |
onFormFieldRemove( model ) { |
| 63 |
if ( 'step' === model.get( 'field_type' ) ) { |
| 64 |
this.onStepFieldChanged( false ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
onStepFieldChanged( isStep ) { |
| 69 |
if ( isStep ) { |
| 70 |
this.lockFirstStep(); |
| 71 |
|
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
const stepFields = this.collection.where( { field_type: 'step' } ); |
| 76 |
|
| 77 |
if ( stepFields.length > 1 ) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
const firstChild = this.getFirstChild(); |
| 82 |
|
| 83 |
if ( 1 === stepFields.length ) { |
| 84 |
firstChild.toggleTools( true ); |
| 85 |
|
| 86 |
firstChild.toggleFieldTypeControl( true ); |
| 87 |
|
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
firstChild.toggleSort( true ); |
| 92 |
} |
| 93 |
|
| 94 |
onAddChild( childView ) { |
| 95 |
super.onAddChild( childView ); |
| 96 |
|
| 97 |
if ( 'step' === childView.model.get( 'field_type' ) ) { |
| 98 |
this.lockFirstStep(); |
| 99 |
|
| 100 |
childView.toggleStepField( true ); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|