/* global FusionPageBuilderApp, fusionBuilderText */ var FusionPageBuilder = FusionPageBuilder || {}; FusionPageBuilder.options = FusionPageBuilder.options || {}; FusionPageBuilder.options.fusionLogics = { optionLogics: function ( $element ) { var self = this, $fusionLogics; $element = 'undefined' !== typeof $element && $element.length ? $element : this.$el; $fusionLogics = $element.find( '.fusion-builder-option-logics' ); if ( $fusionLogics.length ) { $fusionLogics.each( function() { self.optionLogicsInit( jQuery( this ) ); } ); } }, optionLogicsInit: function ( $element ) { var $optionsGrid = $element.find( '.options-grid' ), $addBtn = $element.find( '.fusion-builder-add-sortable-child' ), $fusionLogics = $optionsGrid.find( '.fusion-logics' ), $template = jQuery( '
  • ' + $element.find( '.fusion-logic-template' ).html() + '
  • ' ), $values = $optionsGrid.find( '.logic-values' ), updateValues; updateValues = function () { var options = []; $fusionLogics.children( 'li' ).each( function () { var option = {}, operator = jQuery( this ).find( '.fusion-sortable-operator' ); // operator. option.operator = operator.hasClass( 'and' ) ? 'and' : 'or'; // comparison. option.comparison = jQuery( this ).find( '.logic-comparison-selection' ).val(); // field. option.field = jQuery( this ).find( 'select.fusion-logic-choices' ).val(); // desired value. option.value = jQuery( this ).find( '.fusion-logic-option' ).val(); // additinals. if ( jQuery( this ).find( '.logic-additionals' ).length ) { option.additionals = jQuery( this ).find( '.fusion-logic-additionals-field' ).val(); } options.push( option ); } ); $values .val( FusionPageBuilderApp.base64Encode( JSON.stringify( options ) ) ) .trigger( 'change' ); }; // Init sortable $fusionLogics.sortable( { items: '.fusion-logic', tolerance: 'pointer', cursor: 'move', connectWith: '.fusion-logics', handle: '.fusion-logic-controller-head', axis: 'y' } ); // Bindings $fusionLogics.on( 'sortupdate', function () { updateValues(); } ); $fusionLogics.on( 'change keyup', 'input', function ( event ) { event.preventDefault(); updateValues(); } ); $fusionLogics.on( 'change', 'select.fusion-logic-option', function( event ) { event.preventDefault(); updateValues(); } ); $fusionLogics.on( 'change', 'select.fusion-logic-choices', function( event ) { var allChoices = $fusionLogics.closest( '.fusion-builder-option-logics' ).find( '.fusion-logics-all-choices' ).text(), selection = jQuery( this ).val(), selectionText = jQuery( this ).closest( 'select' ).find( 'option:selected' ).text(), $wrapper = jQuery( this ).closest( '.fusion-logic' ), $comparisons = '', $options = '', isSelected, currentChoice; event.preventDefault(); try { allChoices = JSON.parse( allChoices ); } catch ( e ) { allChoices = []; } $wrapper.find( 'h4.logic-title' ).text( selectionText ); currentChoice = allChoices.find( ( { id } ) => id === selection ); if ( 'object' === typeof currentChoice ) { if ( 'object' === typeof currentChoice.comparisons ) { jQuery.each( currentChoice.comparisons, function( comparisonValue, comparisonName ) { isSelected = 'equal' === comparisonValue ? 'active' : ''; $comparisons += '