@@ -30,6 +30,7 @@ public function __construct() {
3030
3131 public function init () {
3232 add_filter ( 'gform_pre_render ' , array ( $ this , 'load_form_script ' ), 10 , 2 );
33+ add_action ( 'gform_register_init_scripts ' , array ( $ this , 'add_init_script ' ), 10 , 2 );
3334 }
3435
3536 public function load_form_script ( $ form , $ is_ajax_enabled ) {
@@ -42,25 +43,41 @@ public function load_form_script( $form, $is_ajax_enabled ) {
4243 return $ form ;
4344 }
4445
46+ public function add_init_script ( $ form ) {
47+ if ( ! $ this ->is_applicable_form ( $ form ) ) {
48+ return ;
49+ }
50+
51+ $ args = array ();
52+
53+ $ script = 'new ' . __CLASS__ . '( ' . json_encode ( $ args ) . ' ); ' ;
54+ $ slug = implode ( '_ ' , array ( strtolower ( __CLASS__ ) ) );
55+
56+ GFFormDisplay::add_init_script ( $ form ['id ' ], $ slug , GFFormDisplay::ON_PAGE_RENDER , $ script );
57+ }
58+
4559 public function output_script () {
4660 ?>
4761
48- <script type="text/javascript" defer>
62+ <script type="text/javascript">
63+ window.<?php echo __CLASS__ ; ?> = function() {
64+ var $ = jQuery;
4965
50- jQuery( function( $ ) {
5166 window.gform.addFilter( 'gplc_excluded_input_selectors', function( selectors ) {
5267 selectors.push( '.gw-disable-duplicates-disabled' );
5368 return selectors;
5469 });
5570
56- $inputs = $( '.gw-prevent-duplicates' ).find( 'input, select' );
57-
58- $inputs.change( function( event, selected ) {
59- gwDisableDuplicates( $( this ), $inputs, selected );
71+ // Bind events, use .on with delegation and always get fresh selectors for AJAX-refreshed fields
72+ $( '.gw-prevent-duplicates' ).on( 'change', 'input, select', function( event, selected ) {
73+ gwDisableDuplicates( $( this ), $( '.gw-prevent-duplicates' ).find( 'input, select' ), selected );
6074 } );
6175
76+ // Handle on-load
77+ $inputs = $( '.gw-prevent-duplicates' ).find( 'input, select' );
78+
6279 $inputs.each( function( event ) {
63- gwDisableDuplicates( $( this ), $inputs );
80+ gwDisableDuplicates( $( this ), $inputs.not('.gw-disable-duplicates-disabled') );
6481 } );
6582
6683 /**
@@ -96,7 +113,7 @@ function getChangedOptionElFromSelect( $select, selected ) {
96113
97114 /**
98115 * Handle multi select fields with GP Advanced Select enabled.
99- */
116+ */
100117 if ($select.siblings('.ts-wrapper').length) {
101118 const val = $select.val();
102119
@@ -136,7 +153,7 @@ function getChangedOptionElFromSelect( $select, selected ) {
136153 * @param {array} arr2
137154 *
138155 * @returns {string}
139- */
156+ */
140157 function getArrayDiff( arr1, arr2 ) {
141158 return arr1.filter( x => ! arr2.includes( x ) )[ 0 ];
142159 }
@@ -146,7 +163,6 @@ function findOptionByValue( $select, value ) {
146163 }
147164
148165 function gwDisableDuplicates( $elem, $group, selected ) {
149-
150166 // Some elements have a parent element (e.g. a <select>) that contains the actual elements (e.g. <option>) we want enable/disable.
151167 let $parent = $elem;
152168
@@ -203,7 +219,7 @@ function gwDisableDuplicates( $elem, $group, selected ) {
203219 }
204220
205221 }
206- } ) ;
222+ };
207223 </script>
208224
209225 <?php
0 commit comments