1class SmartPhoneFieldFree { 2 constructor( options ) { 3 this.options = options; 4 this.init(); 5 } 6 7 init() { 8 this.intiSmartPhoneFieldFree(); 9 } 10 11 intiSmartPhoneFieldFree() { 12 if (typeof intlTelInput == 'undefined') { 13 return; 14 } 15 const input = document.querySelector(this.options.inputId); 16 17 const iti = window.intlTelInput(input, this.configuration()); 18 19 input.addEventListener('keypress', function(e) { 20 21 var charCode = e.which ? e.which : e.keyCode; 22 if (String.fromCharCode(charCode).match(/[^0-9+]/g)) { 23 e.preventDefault(); 24 } 25 }); 26 27 this.addCountryCodeInputHandler( input, iti ); 28 29 input.addEventListener('blur', (e) => { 30 this.validateNumber(input, iti); 31 }); 32 33 input.addEventListener('keyup', (e) => { 34 this.formatValidation( input, iti ); 35 }); 36 } 37 38 configuration() { 39 let field_id = `input_${this.options.fieldId}`; 40 41 var config = { 42 initialCountry: this.options.defaultCountry, 43 formatOnDisplay: false, 44 formatAsYouType: false, 45 fixDropdownWidth: true, 46 hiddenInput: function(telInputName) { 47 return { 48 phone: field_id 49 }; 50 }, 51 useFullscreenPopup: false 52 }; 53 54 if(this.options.countrySearch) { 55 config.countrySearch = true; 56 } 57 58 if (this.options.flag == "flagcode") { 59 config.nationalMode = false; 60 config.autoHideDialCode = false; 61 } else if (this.options.flag == "flagdial" || this.options.flag == "flagwithcode") { 62 config.nationalMode = false; 63 config.separateDialCode = true; 64 } else { 65 config.nationalMode = true; 66 } 67 68 if( this.options.exIn == 'ex_only') { 69 config.onlyCountries = this.options.countries.split(','); 70 } 71 72 if( this.options.exIn == 'pre_only') { 73 config.excludeCountries = this.options.countries.split(','); 74 } 75 76 if( this.options.autoIp ){ 77 this.detectIPAddress( config ); 78 } 79 80 if( this.options.placeholder ) { 81 config.autoPlaceholder = 'off'; 82 } 83 84 config = gform.applyFilters( 'gform_spf_options_pre_init', config, this.options.formId, this.options.fieldId); 85 86 return config; 87 } 88 89 detectIPAddress(config) { 90 var api_url = "https://ipinfo.io"; 91 config.initialCountry = "auto"; 92 config.geoIpLookup = function (success, failure) { 93 jQuery.get(api_url, function () {}, "jsonp").always( 94 function (resp) { 95 var countryCode = 96 resp && resp.country ? resp.country : ""; 97 success(countryCode); 98 } 99 ); 100 }; 101 } 102 103 validateNumber( input, iti ) { 104 const isValid = iti.isValidNumber(); 105 106 let errorMsg = input.parentNode.parentNode.querySelector(".error-msg"), 107 validMsg = input.parentNode.parentNode.querySelector(".valid-msg"); 108 109 if( input.value ) { 110 if( isValid ) { 111 errorMsg.classList.add('hide'); 112 validMsg.classList.remove('hide'); 113 } else { 114 validMsg.classList.add('hide'); 115 errorMsg.classList.remove('hide'); 116 } 117 } else { 118 validMsg.classList.add('hide'); 119 errorMsg.classList.add('hide'); 120 } 121 } 122 123 formatValidation( input, iti ) { 124 const isValid = iti.isValidNumber(); 125 126 let errorMsg = input.parentNode.parentNode.querySelector(".error-msg"), 127 validMsg = input.parentNode.parentNode.querySelector(".valid-msg"); 128 129 if( input.value ) { 130 if( isValid ) { 131 errorMsg.classList.add('hide'); 132 validMsg.classList.remove('hide'); 133 } else { 134 validMsg.classList.add('hide'); 135 errorMsg.classList.add('hide'); 136 } 137 } else { 138 validMsg.classList.add('hide'); 139 errorMsg.classList.add('hide'); 140 } 141 } 142 143 addCountryCodeInputHandler( inputElement, iti ) { 144 145 if( this.options.flag !== 'flagcode' ) return; 146 147 const handleCountryChange = (event) => { 148 const currentCountryData = iti.getSelectedCountryData(); 149 const currentCode = `+${currentCountryData.dialCode}`; 150 151 this.updateCountryCodeHandler(event.currentTarget, currentCode); 152 } 153 154 inputElement.addEventListener('keydown', handleCountryChange); 155 inputElement.addEventListener('input', handleCountryChange); 156 inputElement.addEventListener('countrychange', handleCountryChange); 157 } 158 159 updateCountryCodeHandler( input, currentCode ) { 160 let value = input.value; 161 162 if( currentCode && '+undefined' === currentCode || ['','+'].includes(value) ){ 163 return; 164 } 165 166 if (!value.startsWith(currentCode)) { 167 value = value.replace(/\+/g, ''); 168 input.value = currentCode + value; 169 } 170 } 171}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.