Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gw-prevent-duplicate-selections.php: Fixed an issue with duplicate selections logic did not work with some dynamically populated data. #766

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 52 additions & 12 deletions gravity-forms/gw-prevent-duplicate-selections.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function __construct() {

public function init() {
add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 );
add_action( 'gform_register_init_scripts', array( $this, 'add_init_script' ), 10, 2 );
}

public function load_form_script( $form, $is_ajax_enabled ) {
Expand All @@ -42,25 +43,65 @@ public function load_form_script( $form, $is_ajax_enabled ) {
return $form;
}

public function add_init_script( $form ) {
if ( ! $this->is_applicable_form( $form ) ) {
return;
}

$args = array();

$script = 'new ' . __CLASS__ . '( ' . json_encode( $args ) . ' );';
$slug = implode( '_', array( strtolower( __CLASS__ ) ) );

GFFormDisplay::add_init_script( $form['id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );
}

public function output_script() {
?>

<script type="text/javascript" defer>
<script type="text/javascript">
window.<?php echo __CLASS__; ?> = function() {
var $ = jQuery;

$( document ).on( 'gppa_updated_batch_fields', function( event, formId, fieldIds ) {
$( gwDisableDuplicates );
})

jQuery( function( $ ) {
window.gform.addFilter( 'gplc_excluded_input_selectors', function( selectors ) {
selectors.push( '.gw-disable-duplicates-disabled' );
return selectors;
});

$inputs = $( '.gw-prevent-duplicates' ).find( 'input, select' );

$inputs.change( function( event, selected ) {
gwDisableDuplicates( $( this ), $inputs, selected );
// Bind events, use .on with delegation and always get fresh selectors for AJAX-refreshed fields
$( '.gw-prevent-duplicates' ).on( 'change', 'input, select', function( event, selected ) {
gwDisableDuplicates( $( this ), $( '.gw-prevent-duplicates' ).find( 'input, select' ), selected );
} );

// Event listener for post conditional logic
$(document).on('gform_post_conditional_logic', function(event, formId, fields, isInit){
// Check each field affected by conditional logic
$.each(fields, function(index, fieldId){
var $field = $('#field_' + formId + '_' + fieldId);
var $inputs = $field.find('input, select');

// Check if the field contains input/select elements and if has been hidden or is now visible
if($inputs.length > 0 && $field.is(':hidden') ){
// Field has been hidden
$('.gw-disable-duplicates-disabled').removeClass('gw-disable-duplicates-disabled gf-default-disabled').prop('disabled', false);
gwDisableDuplicates($inputs, $('.gw-prevent-duplicates').find('input, select').not($field.find('input, select')));
}
if($inputs.length > 0 && ! $field.is(':hidden') ){
// Field has been shown
gwDisableDuplicates($inputs, $('.gw-prevent-duplicates').find('input, select'));
}
});
});

// Handle on-load
$inputs = $( '.gw-prevent-duplicates' ).find( 'input, select' );

$inputs.each( function( event ) {
gwDisableDuplicates( $( this ), $inputs );
gwDisableDuplicates( $( this ), $inputs.not('.gw-disable-duplicates-disabled') );
} );

/**
Expand Down Expand Up @@ -96,7 +137,7 @@ function getChangedOptionElFromSelect( $select, selected ) {

/**
* Handle multi select fields with GP Advanced Select enabled.
*/
*/
if ($select.siblings('.ts-wrapper').length) {
const val = $select.val();

Expand Down Expand Up @@ -136,7 +177,7 @@ function getChangedOptionElFromSelect( $select, selected ) {
* @param {array} arr2
*
* @returns {string}
*/
*/
function getArrayDiff( arr1, arr2 ) {
return arr1.filter( x => ! arr2.includes( x ) )[ 0 ];
}
Expand All @@ -146,11 +187,10 @@ function findOptionByValue( $select, value ) {
}

function gwDisableDuplicates( $elem, $group, selected ) {

// Some elements have a parent element (e.g. a <select>) that contains the actual elements (e.g. <option>) we want enable/disable.
let $parent = $elem;

if ( $elem.is( 'select' ) ) {
if ( $elem.is( 'select' ) && !$elem.is(':hidden') ) {
$elem = getChangedOptionElFromSelect( $elem, selected );

// Note: This prevents selects from working with other field types.
Expand Down Expand Up @@ -203,7 +243,7 @@ function gwDisableDuplicates( $elem, $group, selected ) {
}

}
} );
};
</script>

<?php
Expand Down