1docReady(function() { 2 var address = ''; 3var address_vars = ''; 4var street_no; 5var street_name; 6var city; 7var province; 8var post_code; 9var country; 10var address_line = ''; 11var addrenss_var_str = new Object(); 12var isDropdownNew = false; 13 14function checkPlaceAPI(request, callback) { 15 var autocompleteService2 = new google.maps.places.AutocompleteService(); 16 autocompleteService2.getPlacePredictions(request, function(results, status){ 17 if (status == google.maps.places.PlacesServiceStatus.REQUEST_DENIED) { 18 return callback( new Error('REQUEST_DENIED')); 19 } 20 if (status == google.maps.places.PlacesServiceStatus.OK) { 21 return callback(null, results); 22 } 23 callback( new Error('Service unavailable')); 24 }); 25} 26 27function PlaceApi2GetAutocompleteSuggestions(request, callback) { 28 google.maps.places.AutocompleteSuggestion.fetchAutocompleteSuggestions(request).then((response) => { 29 let foundResponce = [] 30 for (let suggestion of response.suggestions) { 31 const placePrediction = suggestion.placePrediction; 32 foundResponce.push({ 33 description: placePrediction.text.text, 34 place_id: placePrediction.placeId, 35 reference: 'newPlaceAPI', 36 structured_formatting: { 37 main_text: placePrediction.mainText.text, 38 } 39 }); 40 } 41 callback(null, foundResponce); 42 }); 43} 44 45function highlight(text, searchV) { 46 var reg = new RegExp(searchV, 'gi'); 47 return text.replace(reg, function(str){ 48 return '<strong>' + str + '</strong>' 49 }); 50} 51 52 async function createdDivDropDown(input, places, searchValue) { 53 const dropdown = document.createElement('div'); 54 dropdown.className = 'custom-dropdown'; 55 const parentEl = input.parentElement.parentElement; 56 const existingDropdown = parentEl.querySelector('.custom-dropdown'); 57 // Remove existing dropdown before creating a new one 58 if (existingDropdown) { 59 existingDropdown.remove(); 60 } 61 for (const place of places) { 62 const dropdownSelect = document.createElement('div'); 63 dropdownSelect.innerHTML = '<span class="icon-marker"></span>'+'<span class="inside">'+highlight(place.description, searchValue)+'</span>'; 64 dropdownSelect.addEventListener('click', async () => { 65 input.value = place.description; 66 await processPlaceNew(place); 67 dropdown.style.display = 'none'; 68 }); 69 dropdown.appendChild(dropdownSelect); 70 } 71 parentEl.appendChild(dropdown); 72 if (places.length > 0) isDropdownNew = true; 73 74 // Show dropdown when input focus 75 input.addEventListener('focus', () => { 76 const dropdown = parentEl.querySelector('.custom-dropdown'); 77 if (places.length > 0) { 78 dropdown.style.display = 'block'; 79 } 80 }); 81 82 // Hide dropdown when clicking outside 83 document.addEventListener('click', (event) => { 84 if (!input.contains(event.target)) { 85 dropdown.style.display = 'none'; 86 } 87 }); 88 } 89 90var initLeadForm = function() { 91 $('form').each(function() { 92 if(this.id == 'custom_first_step_free_home_lead_form' || this.id == 'custom_first_step_dream_home_lead_form') { 93 $(this).find('input[type="text"]').each(function() { 94 var options = { 95 componentRestrictions: {country: "ca"}, 96 }; 97 var autocomplete =''; 98 $(this).on('keyup', function() { 99 var inputElement = this; 100 if (this.value.length < smallAuto){ 101 google.maps.event.clearInstanceListeners(this); 102 $(".pac-container").remove(); 103 } else { 104 var request = { 105 input: this.value, 106 componentRestrictions: {country: 'ca'} 107 }; 108 checkPlaceAPI(request, function(err, results){ 109 if (err){ 110 if(request.componentRestrictions) { 111 if(request.componentRestrictions.country) { 112 request.includedRegionCodes = [request.componentRestrictions.country] 113 } 114 delete request.componentRestrictions; 115 } 116 PlaceApi2GetAutocompleteSuggestions(request, (err, results) => { 117 if (err) { 118 console.log(err); 119 } 120 createdDivDropDown(inputElement, results, request.input); 121 });
122 } else { 123 autocomplete = new google.maps.places.Autocomplete(inputElement, options); 124 attach_auto(this, autocomplete); 125 } 126 }); 127 } 128 }); 129 130 131 $(this).keyup(function(e) { 132 $(this).parents('form').find('.form-submit').removeAttr('disabled'); 133 if (e.which == 13) { 134 $(this).parents('form').submit(); 135 } 136 }); 137 }); 138 // on submission validate that we have a value 139 $(this).submit(function(e){ 140 var form = $(this); 141 e.preventDefault(); 142 // Disable button to prevent submission spamming, is re-enabled when the user types. 143 form.find('.form-submit').attr('disabled','disabled'); 144 var submission = $(this).find('input[type="text"]').val(); 145 if(submission ===''){ 146 leadWidgetError(form,'This field is required.'); 147 return; 148 } else { 149 // If we do not have address_vars set then check for a prediction from google otherwise display an error 150 if($('.address_vars').val() != '' || $("#edit-address_values").val()!=''){ 151 form.unbind('submit').submit(); 152 return; 153 } 154 var request2 = { 155 input: submission, 156 componentRestrictions: {country: 'ca'} 157 }; 158 checkPlaceAPI(request2, function(err, results){ 159 if (err){ 160 if (!isDropdownNew){ 161 leadWidgetError(form,'Unable to find location. Please try a different one.') 162 } else { 163 if(request2.componentRestrictions) { 164 if(request2.componentRestrictions.country) { 165 request2.includedRegionCodes = [request2.componentRestrictions.country] 166 } 167 delete request2.componentRestrictions; 168 } 169 PlaceApi2GetAutocompleteSuggestions(request2, (err2, results2) => { 170 if (err2) { 171 console.log(err2); 172 } 173 processPlaceNew(results2[0]).then(() => { 174 form.find('input[type="text"]').val(results2[0].description); 175 form.unbind('submit').submit(); 176 }); 177 }); 178 } 179 } else { 180 var autocompleteService = new google.maps.places.AutocompleteService(); 181 autocompleteService.getPlacePredictions(request2, function(predictions, status){ 182 // If no results from predictions then set error otherwise pick the first result and go to form 183 if(status === 'ZERO_RESULTS'){ 184 leadWidgetError(form,'Unable to find location. Please try a different one.') 185 } else { 186 // Get Basic Details of the first suggestion. 187 var placeDetails = new google.maps.places.PlacesService(document.createElement('div')); 188 placeDetails.getDetails({placeId:predictions[0].place_id,fields:['formatted_address','geometry']}, function(place, status){ 189 processPlace(place); 190 form.unbind('submit').submit(); 191 }); 192 }; 193 });
194 } 195 }); 196 }; 197 }); 198 }; 199 }); 200 }; 201 googleMapInit(function(){ 202 initLeadForm(); 203 }); 204 205function attach_auto(input, autocomplete) { 206 autocomplete.addListener('place_changed', function() { 207 var place = autocomplete.getPlace(); 208 if (!place.geometry) { 209 $(input).parents("form").submit(); 210 return; 211 } 212 var lat = place.geometry.location.lat(); 213 var lng = place.geometry.location.lng(); 214 $('.lat_lng_location').val(lat+','+lng); 215 if (place.address_components) { 216 processPlace(place); 217 $(input).parents("form").submit(); 218 } 219 }); 220}; 221 222//Function to set values before submission 223function set_address_form_fields_values(address_vars) { 224 $("input[id*='address_values']").val(JSON.stringify(address_vars)); 225}; 226 227 function setCookie(cname, cvalue, exdays) { 228 var d = new Date(); 229 d.setTime(d.getTime() + (exdays*24*60*60*1000)); 230 var expires = "expires="+d.toGMTString(); 231 document.cookie = cname + "=" + cvalue + "; " + expires; 232 }; 233 234 function getCookie(cname) { 235 var name = cname + "="; 236 var ca = document.cookie.split(';'); 237 for(var i=0; i<ca.length; i++) { 238 var c = ca[i].trim(); 239 if (c.indexOf(name) == 0) return c.substring(name.length,c.length); 240 } 241 return ""; 242 }; 243 244 function deleteCookie(name) { 245 setCookie(name,"",-1); 246 }; 247 248 function leadWidgetError(widget,message){ 249 if(!widget.hasClass('error')){ 250 widget.find('input[type="text"]').css({'border-color':'red'}).parents('.form-item').prepend('<div class="lead-widget-error">'+message+'</div>'); 251 widget.find('.lead-widget-error').css({'position':'absolute', 'background':'#fdbcbc', 'border':'1px solid red', 252 'padding': '0.5rem 0.3rem','font-size': '0.85rem','border-radius':'3px','bottom':'100%','left':'50%', 253 'transform':'translateX(-50%)','min-width':'200px','text-align':'center','line-height':'1','white-space':'nowrap'}); 254 }else{ 255 widget.find('.lead-widget-error').html(message); 256 } 257 widget.addClass('error'); 258 } 259 260 async function processPlaceNew(newPlace){ 261 const place = new google.maps.places.Place({ id: newPlace.place_id }); 262 await place.fetchFields({ fields: ["location", "displayName", "formattedAddress", "addressComponents"] }); 263 264 if (place.location) { 265 var lat = place.location.lat(); 266 var lng = place.location.lng(); 267 $('.lat_lng_location').val(lat+','+lng); 268 } 269 270 for(i in place.addressComponents){ 271 address_vars += place.addressComponents[i].types[0]+'='+place.addressComponents[i].shortText+'&'; 272 if (place.addressComponents[i].types[0] != undefined && place.addressComponents[i].types[0] == 'postal_code') { 273 $('.postal_code_1').val(place.addressComponents[i].shortText); 274 } 275 if(place.addressComponents[i].types[0] == 'street_number') { 276 street_no = place.addressComponents[i].shortText; 277 addrenss_var_str['street_no'] = street_no; 278 } 279 if(place.addressComponents[i].types[0] == 'route') { 280 street_name = place.addressComponents[i].shortText; 281 addrenss_var_str['street_name'] = street_name; 282 } 283 if(place.addressComponents[i].types[0] == 'locality') { 284 city = place.addressComponents[i].shortText; 285 addrenss_var_str['city'] = city; 286 } 287 if(place.addressComponents[i].types[0] == 'administrative_area_level_1') { 288 province = place.addressComponents[i].shortText; 289 addrenss_var_str['province'] = province; 290 } 291 if(place.addressComponents[i].types[0] == 'country') { 292 country = place.addressComponents[i].shortText; 293 addrenss_var_str['country'] = country; 294 } 295 if(place.addressComponents[i].types[0] == 'postal_code') { 296 post_code = place.addressComponents[i].shortText; 297 addrenss_var_str['post_code'] = post_code; 298 } 299 if(street_no != "" && street_no != undefined) { 300 address_line = street_no; 301 } 302 if(street_name != "" && street_name != undefined) { 303 address_line += ", "+street_name; 304 } 305 if(address_line != "") { 306 setCookie("address", address_line, 1); 307 } 308 if(city != "") { 309 setCookie("city", city, 1); 310 } 311 if(province != "") {
312 setCookie("province", province, 1); 313 } 314 if(post_code != "") { 315 setCookie("post_code", post_code, 1); 316 } 317 $("input[id*='address_vars']").val(address_vars); 318 } 319 set_address_form_fields_values(addrenss_var_str); 320 }; 321 322 // Common Function to process place into components 323 function processPlace(place){ 324 for(i in place.address_components){ 325 address_vars += place.address_components[i].types[0]+'='+place.address_components[i].short_name+'&'; 326 if (place.address_components[i].types[0] != undefined && place.address_components[i].types[0] == 'postal_code') { 327 $('.postal_code_1').val(place.address_components[i].short_name); 328 } 329 if(place.address_components[i].types[0] == 'street_number') { 330 street_no = place.address_components[i].short_name; 331 addrenss_var_str['street_no'] = street_no; 332 } 333 if(place.address_components[i].types[0] == 'route') { 334 street_name = place.address_components[i].short_name; 335 addrenss_var_str['street_name'] = street_name; 336 } 337 if(place.address_components[i].types[0] == 'locality') { 338 city = place.address_components[i].short_name; 339 addrenss_var_str['city'] = city; 340 } 341 if(place.address_components[i].types[0] == 'administrative_area_level_1') { 342 province = place.address_components[i].short_name; 343 addrenss_var_str['province'] = province; 344 } 345 if(place.address_components[i].types[0] == 'country') { 346 country = place.address_components[i].short_name; 347 addrenss_var_str['country'] = country; 348 } 349 if(place.address_components[i].types[0] == 'postal_code') { 350 post_code = place.address_components[i].short_name; 351 addrenss_var_str['post_code'] = post_code; 352 } 353 if(street_no != "" && street_no != undefined) { 354 address_line = street_no; 355 } 356 if(street_name != "" && street_name != undefined) { 357 address_line += ", "+street_name; 358 } 359 if(address_line != "") { 360 setCookie("address", address_line, 1); 361 } 362 if(city != "") { 363 setCookie("city", city, 1); 364 } 365 if(province != "") { 366 setCookie("province", province, 1); 367 } 368 if(post_code != "") { 369 setCookie("post_code", post_code, 1); 370 } 371 $("input[id*='address_vars']").val(address_vars); 372 } 373 set_address_form_fields_values(addrenss_var_str); 374 }; 375 window.addEventListener("message", function(e) { 376 if(e.data == 'custom form submit') { 377 setTimeout(function() { 378 var position = $('#customForm_get_form').offset().top - $('header').height() - 30; 379 window.scrollTo({top: position, behavior: 'smooth'}); 380 }, 1000); 381 } 382 }, false); 383});
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.