ajax-submission.js
1 month ago
ajax-submission.min.js
1 month ago
everest-forms-file-upload.js
4 months ago
everest-forms-file-upload.min.js
4 months ago
everest-forms-survey-polls-quiz.js
1 year ago
everest-forms-survey-polls-quiz.min.js
2 years ago
everest-forms.js
4 months ago
everest-forms.min.js
4 months ago
evf-form-preview.js
1 year ago
evf-form-preview.min.js
1 year ago
text-limit.js
1 year ago
text-limit.min.js
1 year ago
everest-forms.js
1162 lines
| 1 | /* eslint-disable max-len */ |
| 2 | /* global everest_forms_params */ |
| 3 | jQuery( function ( $ ) { |
| 4 | 'use strict'; |
| 5 | |
| 6 | // everest_forms_params is required to continue, ensure the object exists. |
| 7 | if ( typeof everest_forms_params === 'undefined' ) { |
| 8 | return false; |
| 9 | } |
| 10 | |
| 11 | var getEnhancedSelectFormatString = function() { |
| 12 | return { |
| 13 | 'language': { |
| 14 | noResults: function() { |
| 15 | return everest_forms_params.i18n_no_matches; |
| 16 | } |
| 17 | } |
| 18 | }; |
| 19 | }; |
| 20 | |
| 21 | var everest_forms = { |
| 22 | $everest_form: $( 'form.everest-form' ), |
| 23 | init: function() { |
| 24 | const everstFormsInitializations = () => { |
| 25 | this.init_inputMask(); |
| 26 | this.init_mailcheck(); |
| 27 | this.init_datepicker(); |
| 28 | this.init_datedropdown(); |
| 29 | this.load_validation(); |
| 30 | this.submission_scroll(); |
| 31 | this.randomize_elements(); |
| 32 | this.init_enhanced_select(); |
| 33 | this.checkUncheckAllcheckbox(); |
| 34 | this.validateMinimumWordLength(); |
| 35 | this.validateMinimumcharacterLength(); |
| 36 | this.loadPhoneField(); |
| 37 | this.loadCountryFlags(); |
| 38 | this.ratingInit(); |
| 39 | this.FormSubmissionWaitingTime(); |
| 40 | this.popUpMessage(); |
| 41 | }; |
| 42 | |
| 43 | // Initial setup |
| 44 | everstFormsInitializations(); |
| 45 | |
| 46 | // Reinitialize functions on Elementor popup show. |
| 47 | $(document).on('elementor/popup/show', everstFormsInitializations); |
| 48 | |
| 49 | // Inline validation |
| 50 | this.$everest_form.on('input validate change', '.input-text, select, input:checkbox, input:radio', this.validate_field); |
| 51 | |
| 52 | // Add or remove active class on focus |
| 53 | this.$everest_form.on('focusin', '.input-text, select, input[type="checkbox"], input[type="radio"]', function() { |
| 54 | $(this).addClass('everest-forms-field-active'); |
| 55 | }).on('focusout', '.input-text, select, input[type="checkbox"], input[type="radio"]', function() { |
| 56 | $(this).removeClass('everest-forms-field-active'); |
| 57 | }); |
| 58 | |
| 59 | // Scroll to first error on submit |
| 60 | this.$everest_form.on('submit', function() { |
| 61 | everest_forms.onSubmitErrorScroll(); |
| 62 | }); |
| 63 | |
| 64 | // Trigger custom event |
| 65 | $(document.body).trigger('everest_forms_loaded'); |
| 66 | }, |
| 67 | init_inputMask: function() { |
| 68 | // Only load if jQuery inputMask library exists. |
| 69 | if ( typeof $.fn.inputmask !== 'undefined' ) { |
| 70 | $( '.evf-masked-input' ).inputmask(); |
| 71 | } |
| 72 | }, |
| 73 | init_mailcheck: function() { |
| 74 | // Only load if Mailcheck library exists and enabled. |
| 75 | if ( typeof $.fn.mailcheck === 'undefined' || ! everest_forms_params.mailcheck_enabled ) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | // Setup default domains for Mailcheck. |
| 80 | if ( everest_forms_params.mailcheck_domains.length > 0 ) { |
| 81 | Mailcheck.defaultDomains = Mailcheck.defaultDomains.concat( everest_forms_params.mailcheck_domains ); |
| 82 | } |
| 83 | |
| 84 | // Setup default top level domains for Mailcheck. |
| 85 | if ( everest_forms_params.mailcheck_toplevel_domains.length > 0 ) { |
| 86 | Mailcheck.defaultTopLevelDomains = Mailcheck.defaultTopLevelDomains.concat( everest_forms_params.mailcheck_toplevel_domains ); |
| 87 | } |
| 88 | |
| 89 | // Mailcheck suggestion. |
| 90 | $( document ).on( 'blur', '.evf-field-email input', function() { |
| 91 | var $el = $( this ), |
| 92 | id = $el.attr( 'id' ); |
| 93 | |
| 94 | $el.mailcheck( { |
| 95 | suggested: function( el, suggestion ) { |
| 96 | $( '#' + id + '_suggestion' ).remove(); |
| 97 | var escapedSuggestion = $('<div>').text(suggestion.full).html(); |
| 98 | var suggestion_msg = everest_forms_params.i18n_messages_email_suggestion.replace( '{suggestion}', '<a href="#" class="mailcheck-suggestion" data-id="' + id + '" title="' + everest_forms_params.i18n_messages_email_suggestion_title + '">' + escapedSuggestion + '</a>' ); |
| 99 | if( el.parents( 'span.input-wrapper' ).length ) { |
| 100 | $( el ).parents( 'span.input-wrapper' ).after( '<label class="evf-error mailcheck-error" id="' + id + '_suggestion">' + suggestion_msg + '</label>' ); |
| 101 | }else { |
| 102 | $( el ).after( '<label class="evf-error mailcheck-error" id="' + id + '_suggestion">' + suggestion_msg + '</label>' ); |
| 103 | } |
| 104 | }, |
| 105 | empty: function() { |
| 106 | $( '#' + id + '_suggestion' ).remove(); |
| 107 | }, |
| 108 | } ); |
| 109 | } ); |
| 110 | |
| 111 | // Apply Mailcheck suggestion. |
| 112 | $( document ).on( 'click', '.evf-field-email .mailcheck-suggestion', function( e ) { |
| 113 | var $el = $( this ), |
| 114 | id = $el.attr( 'data-id' ); |
| 115 | e.preventDefault(); |
| 116 | $( '#' + id ).val( $el.text() ); |
| 117 | $el.parent().remove(); |
| 118 | } ); |
| 119 | }, |
| 120 | init_datepicker: function () { |
| 121 | |
| 122 | // Exclude flatpicker in oxygen builder. |
| 123 | if (window.location.search.includes('ct_builder=true')) { |
| 124 | return; |
| 125 | } |
| 126 | var evfDateField = $( '.evf-field-date-time' ); |
| 127 | if ( evfDateField.length && evfDateField.find( '.flatpickr-field' ).length ) { |
| 128 | evfDateField.find( '.flatpickr-field' ).each( function () { |
| 129 | var timeInterval = 5, |
| 130 | inputData = $( this ).data(), |
| 131 | disableDates = []; |
| 132 | |
| 133 | var minDateRange = ''; |
| 134 | if( inputData.minDateRange ) { |
| 135 | minDateRange = 'today'; |
| 136 | if( 'today' === inputData.minDateRange ) { |
| 137 | minDateRange = minDateRange |
| 138 | } else if ( /^\s*[-+]?\d+\s*d/i.test( inputData.minDateRange ) ) { |
| 139 | minDateRange = inputData.minDateRange.match( /^\s*[-+]?\d+\s*d/i )[0].replace( 'd', '' ); |
| 140 | minDateRange = new Date().fp_incr( minDateRange ); |
| 141 | } else { |
| 142 | minDateRange = 'today'; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | var maxDateRange = ''; |
| 147 | if( inputData.maxDateRange ) { |
| 148 | maxDateRange = 'today'; |
| 149 | if( 'today' === inputData.maxDateRange ) { |
| 150 | maxDateRange = maxDateRange |
| 151 | } else if ( /^\s*[-+]?\d+\s*d/i.test( inputData.maxDateRange ) ) { |
| 152 | maxDateRange = inputData.maxDateRange.match( /^\s*[-+]?\d+\s*d/i )[0].replace( 'd', '' ); |
| 153 | maxDateRange = new Date().fp_incr( maxDateRange ); |
| 154 | } else { |
| 155 | maxDateRange = ''; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // Extract list of disabled dates. |
| 160 | if ( inputData.disableDates ) { |
| 161 | disableDates = inputData.disableDates.split( ',' ); |
| 162 | } |
| 163 | |
| 164 | var pastDisableDate = ''; |
| 165 | if (inputData.pastDisableDate) { |
| 166 | if (inputData.dateFormat === 'Y-m-d') { |
| 167 | pastDisableDate = inputData.pastDisableDate; |
| 168 | } else { |
| 169 | const parsed = flatpickr.parseDate(inputData.pastDisableDate, inputData.dateFormat); |
| 170 | if (parsed) { |
| 171 | pastDisableDate = flatpickr.formatDate(parsed, 'Y-m-d'); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | switch( inputData.dateTime ) { |
| 177 | case 'date': |
| 178 | // Apply flatpicker to field. |
| 179 | $( this ).flatpickr({ |
| 180 | disableMobile : true, |
| 181 | mode : inputData.mode, |
| 182 | minDate : minDateRange ? minDateRange : ( inputData.minDate ? inputData.minDate : pastDisableDate ), |
| 183 | maxDate : maxDateRange ? maxDateRange : inputData.maxDate, |
| 184 | dateFormat : inputData.dateFormat, |
| 185 | disable : disableDates, |
| 186 | }); |
| 187 | break; |
| 188 | case 'time': |
| 189 | if ( undefined !== inputData.timeInterval ) { |
| 190 | timeInterval = parseInt( inputData.timeInterval, 10 ); |
| 191 | } |
| 192 | |
| 193 | // Apply flatpicker to field. |
| 194 | $( this ).flatpickr({ |
| 195 | enableTime : true, |
| 196 | noCalendar : true, |
| 197 | minuteIncrement : timeInterval, |
| 198 | dateFormat : inputData.dateFormat, |
| 199 | disableMobile : true, |
| 200 | time_24hr : inputData.dateFormat.includes('H:i') |
| 201 | }); |
| 202 | break; |
| 203 | case 'date-time': |
| 204 | if ( undefined !== inputData.timeInterval ) { |
| 205 | timeInterval = parseInt( inputData.timeInterval, 10 ); |
| 206 | } |
| 207 | |
| 208 | // Apply flatpicker to field. |
| 209 | $( this ).flatpickr({ |
| 210 | enableTime : true, |
| 211 | noCalendar : false, |
| 212 | disableMobile : true, |
| 213 | mode : inputData.mode, |
| 214 | minDate : minDateRange ? minDateRange : ( inputData.minDate ? inputData.minDate : pastDisableDate ), |
| 215 | maxDate : maxDateRange ? maxDateRange : inputData.maxDate, |
| 216 | minuteIncrement : timeInterval, |
| 217 | dateFormat : inputData.dateFormat, |
| 218 | time_24hr : inputData.dateFormat.includes( 'H:i' ), |
| 219 | disable : disableDates, |
| 220 | }); |
| 221 | break; |
| 222 | default: |
| 223 | } |
| 224 | } ); |
| 225 | } |
| 226 | // $(document).find(".evf-field-date-time input").on('change', function (event) { |
| 227 | // var slotBooking = $(this).data('slot-booking'), |
| 228 | // targetLabel = $(this).parent(), |
| 229 | // errorLabel = $(this).parent().find('label.evf-error'); |
| 230 | // if(slotBooking === 1) { |
| 231 | // var dataTimeValue = $(this).val(), |
| 232 | // dateFormat = $(this).data('date-format'), |
| 233 | // dateTimeFormat = $(this).data('date-time'), |
| 234 | // mode = $(this).data('mode'), |
| 235 | // form_id = $(this).data('form-id'), |
| 236 | // time_interval = $(this).data('time-interval'), |
| 237 | // data = {'action':'everest_forms_slot_booking', 'data-time-value':dataTimeValue, 'data-format': dateFormat, 'data-time-format': dateTimeFormat, 'mode': mode, 'form-id': form_id, 'time-interval':time_interval, 'security': everest_forms_params.everest_forms_slot_booking}; |
| 238 | |
| 239 | // $.ajax({ |
| 240 | // url:everest_forms_params.ajax_url, |
| 241 | // data: data, |
| 242 | // type: 'POST', |
| 243 | // beforeSend: function () { |
| 244 | // var submitButton = $(document).find('.evf-submit-container button'); |
| 245 | // $(submitButton).prop('disabled', true); |
| 246 | // }, |
| 247 | // success: function (res) { |
| 248 | // if($(errorLabel).length) { |
| 249 | // $(errorLabel).remove(); |
| 250 | // } |
| 251 | // if(res.success === true) { |
| 252 | // var message = res.data.message; |
| 253 | // $(targetLabel).append('<label class="evf-error">'+message+'</label>'); |
| 254 | // var submitButton = $(document).find('.evf-submit-container button'); |
| 255 | // $(submitButton).prop('disabled', true); |
| 256 | // } else { |
| 257 | // var submitButton = $(document).find('.evf-submit-container button'); |
| 258 | // $(submitButton).prop('disabled', false); |
| 259 | // } |
| 260 | // } |
| 261 | // }); |
| 262 | // } |
| 263 | // }) |
| 264 | |
| 265 | }, |
| 266 | init_datedropdown: function () { |
| 267 | //Dropdown logic here |
| 268 | $( '.date-dropdown-field' ).each( function() { |
| 269 | var $this = $ (this ); |
| 270 | $this.hide(); |
| 271 | everest_forms.change_minutes( $this ); |
| 272 | }); |
| 273 | |
| 274 | $( 'body' ).on( 'change', '.date-time-container [id*=hour-select]', function() { |
| 275 | var $this = $( this ).siblings( 'input.date-dropdown-field' ); |
| 276 | everest_forms.change_minutes( $this ); |
| 277 | }) |
| 278 | |
| 279 | $( 'body' ).on( 'change', '.date-time-container [id*=-select]', function() { |
| 280 | var $this = $( this ).siblings( 'input.date-dropdown-field' ); |
| 281 | $this.val( everest_forms.format_dropdown_date( $this ) ); |
| 282 | }) |
| 283 | }, |
| 284 | change_minutes: function( $this ) { |
| 285 | //Changes minutes as per hours selected, Time limit option. |
| 286 | var id = $this.attr( 'id' ); |
| 287 | if( typeof( $this.siblings( '#minute-select-' + id ).attr( 'id' ) ) != 'undefined' ) { |
| 288 | var max_hour = $this.attr( 'data-max-hour' ); |
| 289 | var min_hour = $this.attr( 'data-min-hour' ); |
| 290 | var max_minute = $this.attr( 'data-max-minute' ); |
| 291 | var min_minute = $this.attr( 'data-min-minute' ); |
| 292 | if( typeof( min_hour ) == 'undefined' || min_hour == '' ) { |
| 293 | min_hour = 0; |
| 294 | } |
| 295 | if( typeof (max_hour ) == 'undefined' || max_hour == '' ) { |
| 296 | min_hour = 23; |
| 297 | } |
| 298 | if( typeof( min_minute ) == 'undefined' || min_minute == '' ) { |
| 299 | min_minute = 0; |
| 300 | } |
| 301 | if( typeof( max_minute ) == 'undefined' || max_minute == '' ) { |
| 302 | max_minute = 59; |
| 303 | } |
| 304 | var options = ''; |
| 305 | for( var i = 0; i<= 59; i++ ) { |
| 306 | if( $this.siblings( '#hour-select-' + id ).val() == min_hour && i < min_minute ) { |
| 307 | continue; |
| 308 | } |
| 309 | if( $this.siblings( '#hour-select-' + id ).val() == max_hour && i > max_minute ) { |
| 310 | break; |
| 311 | } |
| 312 | options += '<option value = "' + i + '"> ' + ( ( i< 10 ) ? '0' + i : i ) + '</option>'; |
| 313 | } |
| 314 | |
| 315 | $this.siblings( '#minute-select-'+id ).html( options ); |
| 316 | $this.siblings( '#minute-select-'+id ).attr('value', $this.siblings( '#minute-select-'+id ).find('option:first').val()); |
| 317 | } |
| 318 | $this.val( everest_forms.format_dropdown_date( $this ) ); |
| 319 | }, |
| 320 | format_dropdown_date: function ( $this ) { |
| 321 | var id = $this.attr( 'id' ); |
| 322 | var selectd_date = { |
| 323 | selected_year: $this.siblings( '#year-select-' + id ).val(), |
| 324 | selected_month: $this.siblings( '#month-select-' + id ).val(), |
| 325 | selected_day: $this.siblings( '#day-select-' + id ).val(), |
| 326 | selected_hour: $this.siblings( '#hour-select-' + id ).val(), |
| 327 | selected_minute: $this.siblings( '#minute-select-' + id ).val() |
| 328 | } |
| 329 | var setting = { |
| 330 | date_format: $this.attr( 'data-date-format' ), |
| 331 | date_time: $this.attr( 'data-date-time' ) |
| 332 | } |
| 333 | var list_months = [ |
| 334 | 'January', |
| 335 | 'Febuary', |
| 336 | 'March', |
| 337 | 'April', |
| 338 | 'May', |
| 339 | 'June', |
| 340 | 'July', |
| 341 | 'August', |
| 342 | 'September', |
| 343 | 'October', |
| 344 | 'November', |
| 345 | 'December' |
| 346 | ]; |
| 347 | var formatted_date = ''; |
| 348 | if( setting.date_time == 'date' || setting.date_time == 'date-time' ) { |
| 349 | selectd_date.selected_day = (selectd_date.selected_day < 10 ) ? '0' + selectd_date.selected_day : selectd_date.selected_day; |
| 350 | if ( setting.date_format.match( /F j, Y/ ) ) { |
| 351 | formatted_date = list_months[ parseInt( selectd_date.selected_month ) - 1 ] + ' ' + selectd_date.selected_day + ', ' + selectd_date.selected_year; |
| 352 | } else { |
| 353 | selectd_date.selected_month = (selectd_date.selected_month < 10 ) ? '0' + selectd_date.selected_month : selectd_date.selected_month; |
| 354 | if(setting.date_format.match( /Y-m-d/ ) ) { |
| 355 | formatted_date = selectd_date.selected_year + '-' + selectd_date.selected_month + '-' + selectd_date.selected_day; |
| 356 | } else if ( setting.date_format.match( /m\/d\/Y/ ) ) { |
| 357 | formatted_date = selectd_date.selected_month + '/' + selectd_date.selected_day + '/' + selectd_date.selected_year; |
| 358 | } else { |
| 359 | formatted_date = selectd_date.selected_day + '/' + selectd_date.selected_month + '/' + selectd_date.selected_year; |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | if( setting.date_time == 'time' || setting.date_time == 'date-time' ) { |
| 364 | selectd_date.selected_minute = ( selectd_date.selected_minute < 10) ? '0' + selectd_date.selected_minute : selectd_date.selected_minute; |
| 365 | if( setting.date_format.match( /H:i/ ) ) { |
| 366 | selectd_date.selected_hour = ( selectd_date.selected_hour < 10 ) ? '0' + selectd_date.selected_hour : selectd_date.selected_hour; |
| 367 | formatted_date += ' ' + selectd_date.selected_hour + ":" + selectd_date.selected_minute; |
| 368 | } else { |
| 369 | var period = 'PM'; |
| 370 | if( selectd_date.selected_hour < 12 ) { |
| 371 | period = 'AM' |
| 372 | if( selectd_date.selected_hour == 0 ){ |
| 373 | selectd_date.selected_hour = 12; |
| 374 | } |
| 375 | } else if ( selectd_date.selected_hour > 12 ) { |
| 376 | selectd_date.selected_hour = selectd_date.selected_hour - 12; |
| 377 | } |
| 378 | |
| 379 | formatted_date += ' ' + selectd_date.selected_hour + ":" + selectd_date.selected_minute + ' ' + period; |
| 380 | } |
| 381 | } |
| 382 | return formatted_date.trim(); |
| 383 | }, |
| 384 | load_validation: function() { |
| 385 | if ( typeof $.fn.validate === 'undefined' ) { |
| 386 | return false; |
| 387 | } |
| 388 | |
| 389 | // Prepend URL field contents with http:// if user input doesn't contain a schema. |
| 390 | $( '.evf-field-url input[type=url]' ).on( 'change', function () { |
| 391 | var url = $( this ).val(); |
| 392 | if ( ! url ) { |
| 393 | return false; |
| 394 | } |
| 395 | if ( url.substr( 0, 7 ) !== 'http://' && url.substr( 0, 8 ) !== 'https://' ) { |
| 396 | $( this ).val( 'http://' + url ); |
| 397 | } |
| 398 | }); |
| 399 | |
| 400 | // Validator messages. |
| 401 | $.extend( $.validator.messages, { |
| 402 | required: everest_forms_params.i18n_messages_required, |
| 403 | url: everest_forms_params.i18n_messages_url, |
| 404 | email: everest_forms_params.i18n_messages_email, |
| 405 | number: everest_forms_params.i18n_messages_number |
| 406 | }); |
| 407 | |
| 408 | // Validate email addresses. |
| 409 | $.validator.methods.email = function( value, element ) { |
| 410 | /* https://stackoverflow.com/questions/2855865/jquery-validate-e-mail-address-regex */ |
| 411 | var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i); |
| 412 | return this.optional( element ) || pattern.test( value ); |
| 413 | }; |
| 414 | |
| 415 | // Validate confirmations. |
| 416 | $.validator.addMethod( 'confirm', function( value, element, param ) { |
| 417 | return $.validator.methods.equalTo.call( this, value, element, param ); |
| 418 | }, everest_forms_params.i18n_messages_confirm ); |
| 419 | |
| 420 | // Validate checkbox choice limit. |
| 421 | $.validator.addMethod( 'check-limit', function( value, element ) { |
| 422 | var $ul = $( element ).closest( 'ul' ), |
| 423 | $checked = $ul.find( 'input[type="checkbox"]:checked' ), |
| 424 | choiceLimit = parseInt( $ul.attr( 'data-choice-limit' ) || 0, 10 ); |
| 425 | |
| 426 | if ( 0 === choiceLimit ) { |
| 427 | return true; |
| 428 | } |
| 429 | |
| 430 | return $checked.length <= choiceLimit; |
| 431 | }, function( params, element ) { |
| 432 | var choiceLimit = parseInt( $( element ).closest( 'ul' ).attr( 'data-choice-limit' ) || 0, 10 ); |
| 433 | return everest_forms_params.i18n_messages_check_limit.replace( '{#}', choiceLimit ); |
| 434 | } ); |
| 435 | |
| 436 | $.validator.addMethod( 'phone-field', function( value, element ) { |
| 437 | if ( value.match( /[^\d()\-+\s]/ ) ) { |
| 438 | return false; |
| 439 | } |
| 440 | return this.optional( element ) || value.replace( /[^\d]/g, '' ).length > 0; |
| 441 | }, everest_forms_params.i18n_messages_phone ); |
| 442 | |
| 443 | // Validate Smart Phone Field. |
| 444 | if ( 'undefined' !== typeof $.fn.intlTelInput ) { |
| 445 | $.validator.addMethod( 'smart-phone-field', function( value, element ) { |
| 446 | if ( value.match( /[^\d()\-+\s]/ ) ) { |
| 447 | return false; |
| 448 | } |
| 449 | return this.optional( element ) || $( element ).intlTelInput( 'isValidNumber' ); |
| 450 | }, everest_forms_params.i18n_messages_phone ); |
| 451 | } |
| 452 | |
| 453 | this.$everest_form.each( function() { |
| 454 | var $this = $( this ); |
| 455 | |
| 456 | // List messages to show for required fields. Use name of the field as key. |
| 457 | var error_messages = {}; |
| 458 | $( '.evf-field' ).each( function() { |
| 459 | var form_id = $( this ).closest( 'form' ).data( 'formid' ); |
| 460 | var field_id = $( this ).data( 'field-id' ); |
| 461 | var error_message = $( this ).data( 'required-field-message' ); |
| 462 | var key = 'everest_forms[form_fields][' + field_id + ']'; // Name of the input field is used as a key. |
| 463 | |
| 464 | if ( $( this ).is( '.evf-field-payment-single' ) ) { |
| 465 | if ( ! $( this ).find('.evf-payment-price').is( '.evf-payment-user-input' ) ) { |
| 466 | $( this ).find('.evf-payment-price').attr( 'required', false ); |
| 467 | error_message = null; |
| 468 | } |
| 469 | } else if ( $( this ).is( '.evf-field-checkbox, .evf-field-payment-checkbox' ) ) { |
| 470 | key = key + '[]'; |
| 471 | } else if ( $( this ).is( '.evf-field-image-upload' ) ) { |
| 472 | key = 'evf_' + form_id + '_' + field_id; |
| 473 | } else if ( $( this ).is( '.evf-field-signature' ) ) { |
| 474 | key = 'everest_forms[form_fields][' + field_id + '][signature_image]'; |
| 475 | } else if ( $( this ).is( '.evf-field-phone' ) ) { |
| 476 | key = key + '[phone_field]'; |
| 477 | } else if ( $( this ).is( '.evf-field-email' ) || $( this ).is( '.evf-field-password' ) ) { |
| 478 | // For when the confirm is disabled. |
| 479 | key = 'everest_forms[form_fields][' + field_id + ']'; |
| 480 | error_messages[ key ] = { |
| 481 | required: error_message, // Set message using 'required' key to avoid conflicts with other validations. |
| 482 | }; |
| 483 | |
| 484 | // For when the confirm is enabled. |
| 485 | key = 'everest_forms[form_fields][' + field_id + '][primary]'; |
| 486 | error_messages[ key ] = { |
| 487 | required: error_message, // Set message using 'required' key to avoid conflicts with other validations. |
| 488 | }; |
| 489 | key = 'everest_forms[form_fields][' + field_id + '][secondary]'; |
| 490 | error_messages[ key ] = { |
| 491 | required: error_message, // Set message using 'required' key to avoid conflicts with other validations. |
| 492 | }; |
| 493 | error_message = null; |
| 494 | } else if ( $( this ).is( '.evf-field-address' ) ) { |
| 495 | var sub_field_error_messages = { |
| 496 | 'address1': $( this ).data( 'required-field-message-address1' ), |
| 497 | 'city' : $( this ).data( 'required-field-message-city' ), |
| 498 | 'state' : $( this ).data( 'required-field-message-state' ), |
| 499 | 'postal' : $( this ).data( 'required-field-message-postal' ), |
| 500 | 'country' : $( this ).data( 'required-field-message-country' ), |
| 501 | } |
| 502 | |
| 503 | var sub_field_types = Object.keys( sub_field_error_messages ); |
| 504 | for ( var i = 0; i < sub_field_types.length; i++ ) { |
| 505 | var sub_field_type = sub_field_types[i], |
| 506 | error_message = sub_field_error_messages[ sub_field_types[i] ]; |
| 507 | |
| 508 | key = 'everest_forms[form_fields][' + field_id + '][' + sub_field_type + ']'; |
| 509 | error_messages[ key ] = { |
| 510 | required: error_message, // Set message using 'required' key to avoid conflicts with other validations. |
| 511 | }; |
| 512 | } |
| 513 | error_message = null; |
| 514 | } else if ( $( this ).is( '.evf-field-likert' ) ) { |
| 515 | var row_keys = $( this ).data( 'row-keys' ); |
| 516 | var sub_field_error_messages = {}; |
| 517 | |
| 518 | if ( row_keys && Array.isArray( row_keys ) ) { |
| 519 | for ( var i = 0; i < row_keys.length; i++ ) { |
| 520 | var row_key = row_keys[i]; |
| 521 | sub_field_error_messages[ row_key ] = $( this ).data( 'required-field-message-' + row_key ); |
| 522 | } |
| 523 | for ( var i = 0; i < row_keys.length; i++ ) { |
| 524 | error_message = sub_field_error_messages[ row_keys[i] ]; |
| 525 | key = 'everest_forms[form_fields][' + field_id + '][' + row_keys[i] + ']'; |
| 526 | error_messages[ key ] = { |
| 527 | required: error_message, // Set message using 'required' key to avoid conflicts with other validations. |
| 528 | }; |
| 529 | } |
| 530 | } |
| 531 | error_message = null; |
| 532 | } else if ( $( this ).is( '.evf-field-file-upload' ) ) { |
| 533 | key = 'everest_forms_' + form_id + '_' + field_id; |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * Check if the error message has been already set (null value in error_message variable |
| 538 | * should indicate that the message has already been set). |
| 539 | */ |
| 540 | if ( error_message ) { |
| 541 | error_messages[ key ] = { |
| 542 | required: error_message, // Set message using 'required' key to avoid conflicts with other validations. |
| 543 | }; |
| 544 | } |
| 545 | }); |
| 546 | |
| 547 | $this.validate({ |
| 548 | messages: error_messages, |
| 549 | ignore: ':hidden:not(.evf-enhanced-select)', |
| 550 | errorClass: 'evf-error', |
| 551 | validClass: 'evf-valid', |
| 552 | errorPlacement: function( error, element ) { |
| 553 | if ( element.closest( '.evf-field' ).is( '.evf-field-privacy-policy' ) ) { |
| 554 | element.closest( '.evf-field' ).append( error ); |
| 555 | } else if ( element.closest( '.evf-field' ).is( '.evf-field-range-slider' ) ) { |
| 556 | if ( element.closest( '.evf-field' ).find( '.evf-field-description' ).length ) { |
| 557 | element.closest( '.evf-field' ).find( '.evf-field-description' ).before( error ); |
| 558 | } else { |
| 559 | element.closest( '.evf-field' ).append( error ); |
| 560 | } |
| 561 | } else if ( element.closest( '.evf-field' ).is( '.evf-field-scale-rating' ) ) { |
| 562 | element.closest( '.evf-field' ).find( '.everest-forms-field-scale-rating' ).after( error ); |
| 563 | } else if ( 'radio' === element.attr( 'type' ) || 'checkbox' === element.attr( 'type' ) ) { |
| 564 | if ( element.hasClass( 'everest-forms-likert-field-option' ) ) { |
| 565 | element.closest( 'tr' ).children( 'th' ).append( error ); |
| 566 | } else { |
| 567 | element.closest( '.evf-field-checkbox' ).find( 'label.evf-error' ).remove(); |
| 568 | element.parent().parent().parent().append( error ); |
| 569 | } |
| 570 | } else if ( element.is( 'select' ) && element.attr( 'class' ).match( /date-month|date-day|date-year/ ) ) { |
| 571 | if ( element.parent().find( 'label.evf-error:visible' ).length === 0 ) { |
| 572 | element.parent().find( 'select:last' ).after( error ); |
| 573 | } |
| 574 | } else if ( element.is( 'select' ) && element.hasClass( 'evf-enhanced-select' ) ) { |
| 575 | if ( element.parent().find( 'label.evf-error:visible' ).length === 0 ) { |
| 576 | element.parent().find( '.select2' ).after( error ); |
| 577 | } |
| 578 | } else if ( element.hasClass( 'evf-smart-phone-field' ) || element.hasClass( 'everest-forms-field-password-primary' ) || element.hasClass( 'everest-forms-field-password-secondary' ) ) { |
| 579 | if( element.parents('span.input-wrapper').length ) { |
| 580 | element.parents('span.input-wrapper').after( error ); |
| 581 | } else { |
| 582 | element.parent().after( error ); |
| 583 | } |
| 584 | } else { |
| 585 | if( element.parents('span.input-wrapper').length ) { |
| 586 | element.parents('span.input-wrapper').after( error ); |
| 587 | } else { |
| 588 | error.insertAfter( element ); |
| 589 | } |
| 590 | } |
| 591 | }, |
| 592 | highlight: function( element, errorClass, validClass ) { |
| 593 | var $element = $( element ), |
| 594 | $parent = $element.closest( '.form-row' ), |
| 595 | inputName = $element.attr( 'name' ); |
| 596 | |
| 597 | if ( $element.attr( 'type' ) === 'radio' || $element.attr( 'type' ) === 'checkbox' ) { |
| 598 | $parent.find( 'input[name="' + inputName + '"]' ).addClass( errorClass ).removeClass( validClass ); |
| 599 | } else { |
| 600 | $element.addClass( errorClass ).removeClass( validClass ); |
| 601 | } |
| 602 | |
| 603 | $parent.removeClass( 'everest-forms-validated' ).addClass( 'everest-forms-invalid evf-has-error' ); |
| 604 | }, |
| 605 | unhighlight: function( element, errorClass, validClass ) { |
| 606 | var $element = $( element ), |
| 607 | $parent = $element.closest( '.form-row' ), |
| 608 | inputName = $element.attr( 'name' ); |
| 609 | |
| 610 | if ( $element.attr( 'type' ) === 'radio' || $element.attr( 'type' ) === 'checkbox' ) { |
| 611 | $parent.find( 'input[name="' + inputName + '"]' ).addClass( validClass ).removeClass( errorClass ); |
| 612 | } else { |
| 613 | $element.addClass( validClass ).removeClass( errorClass ); |
| 614 | } |
| 615 | |
| 616 | $parent.removeClass( 'evf-has-error' ); |
| 617 | }, |
| 618 | submitHandler: function( form ) { |
| 619 | var $form = $( form ), |
| 620 | $submit = $form.find( '.evf-submit' ), |
| 621 | processText = $submit.data( 'process-text' ); |
| 622 | var recaptchaID = $submit.get( 0 ).recaptchaID; |
| 623 | var razorpayForms = $form.find( "[data-gateway='razorpay']" ); |
| 624 | var stripeForms = $form.find( "[data-gateway*='stripe']" ); |
| 625 | |
| 626 | // Process form. |
| 627 | if ( processText ) { |
| 628 | $submit.text( processText ).prop( 'disabled', true ); |
| 629 | } |
| 630 | |
| 631 | if ( recaptchaID === 0 ) { |
| 632 | grecaptcha.execute( recaptchaID ); |
| 633 | return false; |
| 634 | } |
| 635 | |
| 636 | if( razorpayForms.length > 0 ){ |
| 637 | return; |
| 638 | } |
| 639 | |
| 640 | if ( 1 !== $form.data( 'ajax_submission' ) || (stripeForms.length < 0 && 0 !== stripeForms.children.length) ) { |
| 641 | form.submit(); |
| 642 | } else { |
| 643 | return; |
| 644 | } |
| 645 | }, |
| 646 | onkeyup: function( element, event ) { |
| 647 | // This code is copied from JQuery Validate 'onkeyup' method with only one change: 'everest-forms-novalidate-onkeyup' class check. |
| 648 | var excludedKeys = [ 16, 17, 18, 20, 35, 36, 37, 38, 39, 40, 45, 144, 225 ]; |
| 649 | |
| 650 | // Disable onkeyup validation for some elements (e.g. remote calls). |
| 651 | if ( $( element ).hasClass( 'everest-forms-novalidate-onkeyup' ) ) { |
| 652 | return; |
| 653 | } |
| 654 | |
| 655 | if ( 9 === event.which && '' === this.elementValue( element ) || -1 !== $.inArray( event.keyCode, excludedKeys ) ) { |
| 656 | return; |
| 657 | } else if ( element.name in this.submitted || element.name in this.invalid ) { |
| 658 | this.element( element ); |
| 659 | } |
| 660 | }, |
| 661 | onfocusout: function( element ) { |
| 662 | // This code is copied from JQuery Validate 'onfocusout' method with only one change: 'everest-forms-novalidate-onkeyup' class check. |
| 663 | var validate = false; |
| 664 | |
| 665 | // Empty value error handling for elements with onkeyup validation disabled. |
| 666 | if ( $( element ).hasClass( 'everest-forms-novalidate-onkeyup' ) && ! element.value ) { |
| 667 | validate = true; |
| 668 | } |
| 669 | |
| 670 | if ( ! this.checkable( element ) && ( element.name in this.submitted || ! this.optional( element ) ) ) { |
| 671 | validate = true; |
| 672 | } |
| 673 | |
| 674 | if ( validate ) { |
| 675 | this.element( element ); |
| 676 | } |
| 677 | }, |
| 678 | onclick: function( element ) { |
| 679 | var validate = false, |
| 680 | type = ( element || {} ).type, |
| 681 | $el = $( element ); |
| 682 | |
| 683 | if ( 'checkbox' === type ) { |
| 684 | $el.closest( '.evf-field-checkbox' ).find( 'label.evf-error' ).remove(); |
| 685 | validate = true; |
| 686 | } else if ( ! 'select-multiple' === type ) { |
| 687 | $( element ).valid(); |
| 688 | } |
| 689 | |
| 690 | if ( validate ) { |
| 691 | this.element( element ); |
| 692 | } |
| 693 | } |
| 694 | }); |
| 695 | }); |
| 696 | }, |
| 697 | validate_field: function ( e ) { |
| 698 | var $this = $( this ), |
| 699 | $body = $( 'body' ), |
| 700 | $parent = $this.closest( '.form-row' ), |
| 701 | validated = true, |
| 702 | validate_required = $parent.is( '.validate-required' ), |
| 703 | validate_email = $parent.is( '.validate-email' ), |
| 704 | event_type = e.type; |
| 705 | |
| 706 | if ( $body.hasClass( 'everest-forms-is-offline' ) ) { |
| 707 | $parent.removeClass( 'everest-forms-invalid everest-forms-invalid-required-field everest-forms-invalid-email everest-forms-validated' ); |
| 708 | } else if ( $parent.hasClass( 'evf-field-address' ) || $parent.hasClass( 'evf-field-payment-single' ) || $( 'body' ).hasClass( 'everest-forms-is-offline' ) ) { |
| 709 | if ( 0 === $parent.find( 'input.evf-error' ).length ) { |
| 710 | $parent.removeClass( 'everest-forms-invalid everest-forms-invalid-required-field everest-forms-invalid-email' ).addClass( 'everest-forms-validated' ); |
| 711 | } |
| 712 | } else { |
| 713 | if ( 'input' === event_type ) { |
| 714 | $parent.removeClass( 'everest-forms-invalid everest-forms-invalid-required-field everest-forms-invalid-email everest-forms-validated' ); |
| 715 | } |
| 716 | |
| 717 | if ( 'validate' === event_type || 'change' === event_type ) { |
| 718 | if ( validate_required ) { |
| 719 | if ( $this.hasClass( 'everest-forms-likert-field-option' ) ) { |
| 720 | if ( $parent.find( 'input.evf-error' ).length > 0 ) { |
| 721 | $parent.removeClass( 'everest-forms-validated' ).addClass( 'everest-forms-invalid everest-forms-invalid-required-field' ); |
| 722 | validated = false; |
| 723 | } |
| 724 | } else if ( 'checkbox' === $this.attr( 'type' ) && 0 === $parent.find( 'input:checked' ).length ) { |
| 725 | $parent.removeClass( 'everest-forms-validated' ).addClass( 'everest-forms-invalid everest-forms-invalid-required-field' ); |
| 726 | validated = false; |
| 727 | } else if ( '' === $this.val() ) { |
| 728 | $parent.removeClass( 'everest-forms-validated' ).addClass( 'everest-forms-invalid everest-forms-invalid-required-field' ); |
| 729 | validated = false; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | if ( validate_email ) { |
| 734 | if ( $this.val() ) { |
| 735 | /* https://stackoverflow.com/questions/2855865/jquery-validate-e-mail-address-regex */ |
| 736 | var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i); // eslint-disable-line max-len |
| 737 | |
| 738 | if ( ! pattern.test( $this.val() ) ) { |
| 739 | $parent.removeClass( 'everest-forms-validated' ).addClass( 'everest-forms-invalid everest-forms-invalid-email' ); |
| 740 | validated = false; |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | if ( validated ) { |
| 745 | $parent.removeClass( 'everest-forms-invalid everest-forms-invalid-required-field everest-forms-invalid-email' ).addClass( 'everest-forms-validated' ); |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | }, |
| 750 | submission_scroll: function(){ |
| 751 | if ( $( 'div.everest-forms-submission-scroll' ).length ) { |
| 752 | $( 'html,body' ).animate( { |
| 753 | scrollTop: ( $( 'div.everest-forms-submission-scroll' ).offset().top ) - 100 |
| 754 | }, 1000 ); |
| 755 | } |
| 756 | }, |
| 757 | |
| 758 | /** |
| 759 | * Focus on first error on submit. |
| 760 | * |
| 761 | * @since 3.0.5 |
| 762 | */ |
| 763 | onSubmitErrorScroll: function() { |
| 764 | if ($('.everest-forms-invalid').length) { |
| 765 | const offset = $('.everest-forms-invalid').eq(0).offset(); |
| 766 | if (offset) { |
| 767 | $('html,body').animate({ |
| 768 | scrollTop: offset.top - 100 |
| 769 | }, 1000); |
| 770 | } |
| 771 | } |
| 772 | }, |
| 773 | randomize_elements: function() { |
| 774 | $( '.everest-forms-randomize' ).each( function() { |
| 775 | var $list = $( this ), |
| 776 | $listItems = $list.children(); |
| 777 | |
| 778 | while ( $listItems.length ) { |
| 779 | $list.append( $listItems.splice( Math.floor( Math.random() * $listItems.length ), 1 )[0] ); |
| 780 | } |
| 781 | } ); |
| 782 | }, |
| 783 | init_enhanced_select: function() { |
| 784 | // Only continue if SelectWoo library exists. |
| 785 | try { |
| 786 | $( document.body ).on( 'evf-frontend-enhanced-select-init', function() { |
| 787 | if ( 'undefined' !== typeof $.fn.selectWoo ) { |
| 788 | $( 'select.evf-enhanced-select:visible' ).filter( ':not(.evf-enhanced)' ).each( function() { |
| 789 | var select2_args = $.extend({ |
| 790 | minimumResultsForSearch: 10 < $( this ).find( 'option' ).length ? 10 : null, |
| 791 | placeholder: $( this ).attr( 'placeholder' ) || '', |
| 792 | allowClear: $( this ).prop( 'multiple' ) ? false : true, |
| 793 | }, getEnhancedSelectFormatString() ); |
| 794 | |
| 795 | $( this ).selectWoo( select2_args ).addClass( 'evf-enhanced' ); |
| 796 | }); |
| 797 | } |
| 798 | }).trigger( 'evf-frontend-enhanced-select-init' ); |
| 799 | } catch( err ) { |
| 800 | // If select2 failed (conflict?) log the error but don't stop other scripts breaking. |
| 801 | window.console.log( err ); |
| 802 | } |
| 803 | }, |
| 804 | checkUncheckAllcheckbox: function () { |
| 805 | // To check and uncheck all the option in checkbox. |
| 806 | var all_select_all_chk = $('form.everest-form').find('.evf-field, .evf-field-checkbox, .form-row').find('ul').find('li.evf-select-all-checkbox-li').find('#evfCheckAll'); |
| 807 | if (all_select_all_chk.length){ |
| 808 | |
| 809 | all_select_all_chk.each(function () { |
| 810 | var $this = $(this); |
| 811 | |
| 812 | $this.on('click', function () { |
| 813 | if($(this).prop("checked") == true){ |
| 814 | $this.parent().parent().find('li').find('input:checkbox').not($this).prop('checked', true); |
| 815 | }else if($(this).prop("checked") == false){ |
| 816 | $this.parent().parent().find('li').find('input:checkbox').not($this).prop('checked', false); |
| 817 | } |
| 818 | }); |
| 819 | |
| 820 | $this.parent().parent().find('li').find('input:checkbox').not($this).on('change', function () { |
| 821 | var checked = ($this.parent().parent().find('li').find('input:checkbox:checked').not($this)).length; |
| 822 | var chck_len = ($this.parent().parent().find('li').find('input:checkbox').not($this)).length; |
| 823 | |
| 824 | if (checked === chck_len) { |
| 825 | $this.prop('checked', true); |
| 826 | } else if (checked < chck_len) { |
| 827 | $this.prop('checked', false); |
| 828 | } |
| 829 | }); |
| 830 | }); |
| 831 | } |
| 832 | }, |
| 833 | validateMinimumWordLength: function() { |
| 834 | Array.prototype.slice.call( document.querySelectorAll( '.everest-forms-min-words-length-enabled' ) ).map( function( event ) { |
| 835 | if (!jQuery(event).is(':visible')){ |
| 836 | return; |
| 837 | } |
| 838 | var minWords = parseInt( event.dataset.textMinLength, 10 ) || 0; |
| 839 | |
| 840 | // Add the custom validation method. |
| 841 | jQuery.validator.addMethod( 'minWordLength', |
| 842 | function(value, element, params) { |
| 843 | var wordsCount = value.trim().split( /\s+/ ).length; |
| 844 | return wordsCount >= params[0]; |
| 845 | }, everest_forms_params.il8n_min_word_length_err_msg |
| 846 | ); |
| 847 | |
| 848 | jQuery( '#'+event.id ).each( function() { |
| 849 | jQuery( this ).rules( 'add', { minWordLength: [minWords] }); |
| 850 | }); |
| 851 | |
| 852 | } ); |
| 853 | }, |
| 854 | validateMinimumcharacterLength: function() { |
| 855 | Array.prototype.slice.call( document.querySelectorAll( '.everest-forms-min-characters-length-enabled' ) ).map( function( event ) { |
| 856 | // Skips the validation for hidden fields. |
| 857 | if (!jQuery(event).is(':visible')){ |
| 858 | return; |
| 859 | } |
| 860 | |
| 861 | var minCharacters = parseInt( event.dataset.textMinLength, 10 ) || 0; |
| 862 | |
| 863 | // Add the custom validation method. |
| 864 | jQuery.validator.addMethod( 'minCharacterLength', |
| 865 | function(value, element, params) { |
| 866 | var charCount = value.length; |
| 867 | return charCount >= params[0]; |
| 868 | }, everest_forms_params.il8n_min_character_length_err_msg |
| 869 | ); |
| 870 | |
| 871 | jQuery( '#'+event.id ).each( function() { |
| 872 | jQuery( this ).rules( 'add', { minCharacterLength: [minCharacters] }); |
| 873 | }); |
| 874 | |
| 875 | } ); |
| 876 | }, |
| 877 | /** |
| 878 | * Load phone field. |
| 879 | * |
| 880 | * @since 1.2.9 |
| 881 | */ |
| 882 | loadPhoneField: function() { |
| 883 | var inputOptions = {}; |
| 884 | |
| 885 | // Only continue if intlTelInput library exists. |
| 886 | if ( typeof $.fn.intlTelInput === 'undefined' ) { |
| 887 | return false; |
| 888 | } |
| 889 | |
| 890 | |
| 891 | // Determine the country by IP if storing user details is enabled. |
| 892 | if ( 'yes' !== everest_forms_params.disable_user_details ) { |
| 893 | inputOptions.geoIpLookup = everest_forms.currentIpToCountry; |
| 894 | } |
| 895 | |
| 896 | // Try an alternative solution if storing user details is disabled. |
| 897 | if ( 'yes' === everest_forms_params.disable_user_details ) { |
| 898 | var lang = this.getFirstBrowserLanguage(), |
| 899 | countryCode = lang.indexOf( '-' ) > -1 ? lang.split( '-' ).pop() : ''; |
| 900 | } |
| 901 | |
| 902 | // Make sure the library recognizes browser country code to avoid console error. |
| 903 | if ( countryCode ) { |
| 904 | var countryData = window.intlTelInputGlobals.getCountryData(); |
| 905 | |
| 906 | countryData = countryData.filter( function( country ) { |
| 907 | return country.iso2 === countryCode.toLowerCase(); |
| 908 | } ); |
| 909 | countryCode = countryData.length ? countryCode : ''; |
| 910 | } |
| 911 | |
| 912 | // Set default country. |
| 913 | inputOptions.initialCountry = 'yes' === everest_forms_params.disable_user_details && countryCode ? countryCode : 'auto'; |
| 914 | |
| 915 | inputOptions.onlyCountries = |
| 916 | everest_forms_params.evf_smart_phone_allowed_countries; |
| 917 | inputOptions.preferredCountries = []; |
| 918 | |
| 919 | $( '.evf-smart-phone-field' ).each( function( i, el ) { |
| 920 | var $el = $( el ); |
| 921 | |
| 922 | const rect = $el[0].getBoundingClientRect(); |
| 923 | |
| 924 | if (rect.width === 0 || rect.height === 0) { |
| 925 | return; |
| 926 | } |
| 927 | |
| 928 | |
| 929 | // Hidden input allows to include country code into submitted data. |
| 930 | inputOptions.hiddenInput = $el.closest( '.evf-field-phone' ).data( 'field-id' ); |
| 931 | inputOptions.utilsScript = everest_forms_params.plugin_url + 'assets/js/intlTelInput/utils.js'; |
| 932 | $el.intlTelInput( inputOptions ); |
| 933 | |
| 934 | // Change name of the phone field. |
| 935 | var field_name = $el.attr( 'name' ), |
| 936 | field_new_name = field_name + '[phone_field]'; |
| 937 | |
| 938 | $el.attr( 'name', field_new_name ); |
| 939 | $el.blur( function() { |
| 940 | if ( $el.intlTelInput( 'isValidNumber' ) ) { |
| 941 | $el.siblings( 'input[type="hidden"]' ).val( $el.intlTelInput( 'getNumber' ) ); |
| 942 | } |
| 943 | } ); |
| 944 | } ); |
| 945 | }, |
| 946 | /** |
| 947 | * Asynchronously fetches country code using current IP |
| 948 | * and executes a callback with the relevant country code. |
| 949 | * |
| 950 | * @since 1.2.9 |
| 951 | * |
| 952 | * @param {Function} callback Executes once the fetch is completed. |
| 953 | */ |
| 954 | currentIpToCountry: function( callback ) { |
| 955 | $.get( 'https://ipapi.co/json' ).always( function( resp ) { |
| 956 | var countryCode = ( resp && resp.country ) ? resp.country : ''; |
| 957 | |
| 958 | if ( ! countryCode ) { |
| 959 | var lang = everest_forms_pro.getFirstBrowserLanguage(); |
| 960 | countryCode = lang.indexOf( '-' ) > -1 ? lang.split( '-' ).pop() : ''; |
| 961 | } |
| 962 | |
| 963 | callback( countryCode ); |
| 964 | } ); |
| 965 | }, |
| 966 | loadCountryFlags: function() { |
| 967 | // Only continue if SelectWoo library exists. |
| 968 | if ( 'undefined' !== typeof $.fn.selectWoo ) { |
| 969 | $.fn.selectWoo.amd.define( 'evfCountrySelectionAdapter', [ |
| 970 | 'select2/utils', |
| 971 | 'select2/selection/single', |
| 972 | ], function ( Utils, SingleSelection ) { |
| 973 | var adapter = SingleSelection; |
| 974 | adapter.prototype.update = function ( data ) { |
| 975 | if ( 0 === data.length ) { |
| 976 | this.clear(); |
| 977 | return; |
| 978 | } |
| 979 | var selection = data[0]; |
| 980 | var $rendered = this.$selection.find( '.select2-selection__rendered' ); |
| 981 | var formatted = this.display( selection, $rendered ); |
| 982 | $rendered.empty().append( formatted ); |
| 983 | $rendered.prop( 'title', selection.title || selection.text ); |
| 984 | }; |
| 985 | return adapter; |
| 986 | } ); |
| 987 | |
| 988 | $( 'select.evf-country-flag-selector:visible' ).each( function() { |
| 989 | var select2_args = $.extend({ |
| 990 | placeholder: $( this ).attr( 'placeholder' ) || '', |
| 991 | selectionAdapter: $.fn.selectWoo.amd.require( 'evfCountrySelectionAdapter' ), |
| 992 | templateResult: everest_forms.getFormattedCountryFlags, |
| 993 | templateSelection: everest_forms.getFormattedCountryFlags, |
| 994 | }, getEnhancedSelectFormatString() ); |
| 995 | |
| 996 | $( this ).selectWoo( select2_args ); |
| 997 | }); |
| 998 | } |
| 999 | }, |
| 1000 | |
| 1001 | /** |
| 1002 | * Get formatted country flags. |
| 1003 | * |
| 1004 | * @param {object} country Country object. |
| 1005 | */ |
| 1006 | getFormattedCountryFlags: function( country ) { |
| 1007 | if ( ! country.id ) { |
| 1008 | return country.text; |
| 1009 | } |
| 1010 | return $( '<div class="iti__flag-box"><div class="iti__flag iti__' + country.id.toLowerCase() + '"></div></div><span class="iti__country-name">' + country.text + '</span>' ); |
| 1011 | }, |
| 1012 | |
| 1013 | FormSubmissionWaitingTime: function() { |
| 1014 | $(document).ready(function() { |
| 1015 | var ajax_submission = $('.everest-form').data('ajax_submission'); |
| 1016 | |
| 1017 | if ($('#evf_submission_start_time').length<0) { |
| 1018 | return ''; |
| 1019 | } |
| 1020 | |
| 1021 | $('#evf_submission_start_time').val(Date.now()); |
| 1022 | |
| 1023 | var startTimer = function() { |
| 1024 | var display = $('#evf_submission_duration'); |
| 1025 | if (display.length) { |
| 1026 | var duration = parseInt(display.data('duration'), 10); |
| 1027 | var timer = duration; |
| 1028 | var interval = setInterval(function() { |
| 1029 | display.text(timer); |
| 1030 | if (--timer < 0) { |
| 1031 | clearInterval(interval); |
| 1032 | display.parent().remove(); |
| 1033 | } |
| 1034 | }, 1000); |
| 1035 | } |
| 1036 | }; |
| 1037 | |
| 1038 | if (ajax_submission === 1) { |
| 1039 | // Create a MutationObserver to handle dynamic content. |
| 1040 | var observer = new MutationObserver(function(mutations) { |
| 1041 | mutations.forEach(function(mutation) { |
| 1042 | if (mutation.addedNodes.length > 0) { |
| 1043 | $(mutation.addedNodes).each(function() { |
| 1044 | if ($('#evf_submission_duration').length) { |
| 1045 | startTimer(); |
| 1046 | observer.disconnect(); |
| 1047 | } |
| 1048 | }); |
| 1049 | } |
| 1050 | }); |
| 1051 | }); |
| 1052 | |
| 1053 | var targetNode = document.body; |
| 1054 | var config = { childList: true, subtree: true }; |
| 1055 | observer.observe(targetNode, config); |
| 1056 | } else { |
| 1057 | startTimer(); |
| 1058 | } |
| 1059 | }); |
| 1060 | }, |
| 1061 | |
| 1062 | popUpMessage: function() { |
| 1063 | var $isPopup = $('.everest-form').is('[data-message_location]'); |
| 1064 | var $isFormStateHidden = $('.everest-form').data('form_state_type'); |
| 1065 | |
| 1066 | if('hide' === $isFormStateHidden) { |
| 1067 | $('.evf-frontend-row, .evf-submit-container ').hide(); |
| 1068 | } |
| 1069 | if ( ! $isPopup ) { |
| 1070 | return; |
| 1071 | } |
| 1072 | |
| 1073 | var $message = $('.everest-form').data('message'); |
| 1074 | |
| 1075 | $('body').css('overflow', 'hidden'); |
| 1076 | |
| 1077 | var popupHTML = ` |
| 1078 | <div class="everest-forms-popup-overlay"> |
| 1079 | <div class="everest-forms-popup"> |
| 1080 | <div class="everest-forms-popup-close"> |
| 1081 | <svg width="14" height="14" viewBox="0 0 14 14" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 1082 | <path d="M7 6.06668L1.26666 0.333344L0.333328 1.20001L6.06666 6.93334L0.333328 12.6L1.26666 13.5333L7 7.86668L12.6667 13.5333L13.6 12.6L7.93333 6.93334L13.6667 1.33334L12.7333 0.466677L7 6.06668Z" fill="#383838"/> |
| 1083 | </svg> |
| 1084 | </div> |
| 1085 | <img src="${ everest_forms_params.evf_checked_image_url }" alt="Checked Logo" class="everest-forms-popup-success-logo"> |
| 1086 | <p class="everest-forms-popup-success-text">${ everest_forms_params.i18n_evf_success_text }</p> |
| 1087 | <p>${ $('<div>').text($message).html() }</p> |
| 1088 | </div> |
| 1089 | </div> |
| 1090 | `; |
| 1091 | |
| 1092 | $('body').append(popupHTML); |
| 1093 | |
| 1094 | $('.everest-forms-popup-close').on('click', function() { |
| 1095 | $('.everest-forms-popup-overlay').fadeOut(200, function() { |
| 1096 | $(this).remove(); |
| 1097 | $('body').css('overflow', ''); |
| 1098 | }); |
| 1099 | }); |
| 1100 | }, |
| 1101 | |
| 1102 | getFirstBrowserLanguage: function() { |
| 1103 | var nav = window.navigator, |
| 1104 | browserLanguagePropertyKeys = [ 'language', 'browserLanguage', 'systemLanguage', 'userLanguage' ], |
| 1105 | i, |
| 1106 | language; |
| 1107 | |
| 1108 | // Support for HTML 5.1 "navigator.languages". |
| 1109 | if ( Array.isArray( nav.languages ) ) { |
| 1110 | for ( i = 0; i < nav.languages.length; i++ ) { |
| 1111 | language = nav.languages[ i ]; |
| 1112 | if ( language && language.length ) { |
| 1113 | return language; |
| 1114 | } |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | // Support for other well known properties in browsers. |
| 1119 | for ( i = 0; i < browserLanguagePropertyKeys.length; i++ ) { |
| 1120 | language = nav[ browserLanguagePropertyKeys[ i ] ]; |
| 1121 | if ( language && language.length ) { |
| 1122 | return language; |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | return ''; |
| 1127 | }, |
| 1128 | |
| 1129 | ratingInit:function(){ |
| 1130 | // Rating field: hover effect. |
| 1131 | $( '.everest-forms-field-rating' ).hover( |
| 1132 | function() { |
| 1133 | $( this ).parent().find( '.everest-forms-field-rating' ).removeClass( 'selected hover' ); |
| 1134 | $( this ).prevAll().addBack().addClass( 'hover' ); |
| 1135 | }, |
| 1136 | function() { |
| 1137 | $( this ).parent().find( '.everest-forms-field-rating' ).removeClass( 'selected hover' ); |
| 1138 | $( this ).parent().find( 'input:checked' ).parent().prevAll().addBack().addClass( 'selected' ); |
| 1139 | } |
| 1140 | ); |
| 1141 | |
| 1142 | // Rating field: toggle. |
| 1143 | $( document ).on( 'change', '.everest-forms-field-rating input', function() { |
| 1144 | var $this = $( this ), |
| 1145 | $wrap = $this.closest( '.everest-forms-field-rating-container' ), |
| 1146 | $items = $wrap.find( '.everest-forms-field-rating' ); |
| 1147 | |
| 1148 | $items.removeClass( 'hover selected' ); |
| 1149 | $this.parent().prevAll().addBack().addClass( 'selected' ); |
| 1150 | } ); |
| 1151 | |
| 1152 | // Rating field: preselect the selected rating. |
| 1153 | $( document ).ready( function () { |
| 1154 | $( '.everest-forms-field-rating input:checked' ).trigger( 'change' ); |
| 1155 | } ); |
| 1156 | } |
| 1157 | }; |
| 1158 | |
| 1159 | everest_forms.init(); |
| 1160 | |
| 1161 | }); |
| 1162 |