@@ -30,6 +30,7 @@ public function __construct() {
30
30
31
31
public function init () {
32
32
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 );
33
34
}
34
35
35
36
public function load_form_script ( $ form , $ is_ajax_enabled ) {
@@ -42,25 +43,41 @@ public function load_form_script( $form, $is_ajax_enabled ) {
42
43
return $ form ;
43
44
}
44
45
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
+
45
59
public function output_script () {
46
60
?>
47
61
48
- <script type="text/javascript" defer>
62
+ <script type="text/javascript">
63
+ window.<?php echo __CLASS__ ; ?> = function() {
64
+ var $ = jQuery;
49
65
50
- jQuery( function( $ ) {
51
66
window.gform.addFilter( 'gplc_excluded_input_selectors', function( selectors ) {
52
67
selectors.push( '.gw-disable-duplicates-disabled' );
53
68
return selectors;
54
69
});
55
70
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 );
60
74
} );
61
75
76
+ // Handle on-load
77
+ $inputs = $( '.gw-prevent-duplicates' ).find( 'input, select' );
78
+
62
79
$inputs.each( function( event ) {
63
- gwDisableDuplicates( $( this ), $inputs );
80
+ gwDisableDuplicates( $( this ), $inputs.not('.gw-disable-duplicates-disabled') );
64
81
} );
65
82
66
83
/**
@@ -96,7 +113,7 @@ function getChangedOptionElFromSelect( $select, selected ) {
96
113
97
114
/**
98
115
* Handle multi select fields with GP Advanced Select enabled.
99
- */
116
+ */
100
117
if ($select.siblings('.ts-wrapper').length) {
101
118
const val = $select.val();
102
119
@@ -136,7 +153,7 @@ function getChangedOptionElFromSelect( $select, selected ) {
136
153
* @param {array} arr2
137
154
*
138
155
* @returns {string}
139
- */
156
+ */
140
157
function getArrayDiff( arr1, arr2 ) {
141
158
return arr1.filter( x => ! arr2.includes( x ) )[ 0 ];
142
159
}
@@ -146,7 +163,6 @@ function findOptionByValue( $select, value ) {
146
163
}
147
164
148
165
function gwDisableDuplicates( $elem, $group, selected ) {
149
-
150
166
// Some elements have a parent element (e.g. a <select>) that contains the actual elements (e.g. <option>) we want enable/disable.
151
167
let $parent = $elem;
152
168
@@ -203,7 +219,7 @@ function gwDisableDuplicates( $elem, $group, selected ) {
203
219
}
204
220
205
221
}
206
- } ) ;
222
+ };
207
223
</script>
208
224
209
225
<?php
0 commit comments