1/** 2 * Place Autocomplete and map marker helpers (modern + legacy fallback). 3 * 4 * @package FMEAACOC 5 */ 6(function (window) { 7 'use strict'; 8 var FMEAACOCPlaceUtils = window.FMEAACOCPlaceUtils || {}; 9 10 /** 11 * Normalize Place (New) fields to a legacy Autocomplete place shape. 12 * 13 * @param {google.maps.places.Place} place Place after fetchFields. 14 * @return {Object} Legacy-shaped place object. 15 */ 16 FMEAACOCPlaceUtils.normalizePlace = function (place) { 17 var addressComponents = []; 18 var components = place.addressComponents || []; 19 for (var i = 0; i < components.length; i++) { 20 addressComponents.push({ 21 long_name: components[i].longText || '', 22 short_name: components[i].shortText || '', 23 types: components[i].types || [], 24 }); 25 } 26 var displayName = ''; 27 if (place.displayName) { 28 displayName = 29 'string' === typeof place.displayName 30 ? place.displayName 31 : place.displayName.text || ''; 32 } 33 return { 34 address_components: addressComponents, 35 geometry: { 36 location: place.location || null, 37 viewport: place.viewport || null, 38 }, 39 name: displayName, 40 formatted_address: place.formattedAddress || '', 41 }; 42 }; 43 44 /** 45 * Fetch place fields and normalize to legacy shape. 46 * 47 * @param {google.maps.places.Place} place Place instance. 48 * @return {Promise<Object>} Normalized place. 49 */ 50 FMEAACOCPlaceUtils.fetchNormalizedPlace = function (place) { 51 return place 52 .fetchFields({ 53 fields: [ 54 'addressComponents', 55 'displayName', 56 'formattedAddress', 57 'location', 58 'viewport', 59 ], 60 }) 61 .then(function () { 62 return FMEAACOCPlaceUtils.normalizePlace(place); 63 }); 64 }; 65 66 /** 67 * Mount modern or legacy address autocomplete on address_1. 68 * 69 * @param {HTMLInputElement} input Native address_1 input. 70 * @param {Object} options Mount options. 71 * @return {Object|null} Autocomplete instance. 72 */ 73 FMEAACOCPlaceUtils.mountPlaceAutocomplete = function (input, options) { 74 if (!input || !window.google || !google.maps || !google.maps.places) { 75 return null; 76 } 77 options = options || {}; 78 FMEAACOCPlaceUtils.destroyPlaceAutocomplete(input); 79 var mapId = options.mapId || ''; 80 81 // Empty Map ID â legacy Autocomplete (existing customers). 82 if ( 83 FMEAACOCPlaceUtils.useLegacyApi(mapId) || 84 !google.maps.places.PlaceAutocompleteElement 85 ) { 86 return FMEAACOCPlaceUtils.mountLegacyAutocomplete(input, options); 87 } 88 89 // Map ID + block checkout â modern element with WC-like shell. 90 if (options.blockCheckout) { 91 return FMEAACOCPlaceUtils.mountBlockPlaceAutocomplete(input, options); 92 } 93 94 var el = new google.maps.places.PlaceAutocompleteElement(); 95 var wrapper = document.createElement('div'); 96 wrapper.className = 'fmeaacoc-place-autocomplete-wrap'; 97 el.className = 'fmeaacoc-place-autocomplete'; 98 el.style.colorScheme = 'light'; 99 el.style.backgroundColor = '#ffffff'; 100 el.style.color = '#2c3338'; 101 if (options.hideInputIcon) { 102 el.noInputIcon = true; 103 } 104 if (options.placeholder !== undefined && options.placeholder !== null) { 105 el.placeholder = options.placeholder; 106 } 107 if (options.country) { 108 el.includedRegionCodes = [String(options.country).toLowerCase()]; 109 } 110 input.classList.add('fmeaacoc-native-address-hidden'); 111 input.parentNode.insertBefore(wrapper, input.nextSibling); 112 wrapper.appendChild(el); 113 input._fmeaacocPlaceAutocomplete = el; 114 input._fmeaacocPlaceWrapper = wrapper; 115 input._fmeaacocIsLegacy = false; 116 FMEAACOCPlaceUtils.bindPlaceSelect(el, options); 117 return el; 118 }; 119 120 /** 121 * Destroy PlaceAutocomplete or legacy Autocomplete for an input. 122 * 123 * @param {HTMLInputElement} input Native address input. 124 * @return {void} 125 */ 126 FMEAACOCPlaceUtils.destroyPlaceAutocomplete = function (input) { 127 if (!input) { 128 return; 129 } 130 if (input._fmeaacocIsLegacy && input._fmeaacocPlaceAutocomplete) { 131 if (typeof input._fmeaacocPlaceAutocomplete.unbindAll === 'function') { 132 input._fmeaacocPlaceAutocomplete.unbindAll(); 133 } 134 if (google.maps.event && google.maps.event.clearInstanceListeners) { 135 google.maps.event.clearInstanceListeners(input); 136 } 137 } 138 if (input._fmeaacocPlaceWrapper && input._fmeaacocPlaceWrapper.parentNode) { 139 input._fmeaacocPlaceWrapper.parentNode.removeChild( 140 input._fmeaacocPlaceWrapper 141 ); 142 } 143 if (input._fmeaacocBlockField) { 144 input._fmeaacocBlockField.classList.remove( 145 'has-fmeaacoc-autocomplete', 146 'is-active', 147 'fmeaacoc-address-field-replaced' 148 ); 149 input._fmeaacocBlockField = null; 150 } 151 input._fmeaacocPlaceAutocomplete = null; 152 input._fmeaacocPlaceWrapper = null; 153 input._fmeaacocIsLegacy = false;
154 input.classList.remove( 155 'fmeaacoc-native-address-hidden', 156 'fmeaacoc-native-address-hidden--blocks' 157 ); 158 }; 159 160 /** 161 * Close Autocomplete suggestion UI (legacy pac + modern blur). 162 * 163 * @param {HTMLInputElement|null} input Optional address input. 164 * @return {void} 165 */ 166 FMEAACOCPlaceUtils.suppressAutocompleteDropdown = function (input) { 167 if (input && 'function' === typeof input.blur) { 168 input.blur(); 169 } 170 if ( 171 input && 172 input._fmeaacocPlaceAutocomplete && 173 !input._fmeaacocIsLegacy && 174 'function' === typeof input._fmeaacocPlaceAutocomplete.blur 175 ) { 176 input._fmeaacocPlaceAutocomplete.blur(); 177 } 178 var containers = document.querySelectorAll('.pac-container'); 179 for (var i = 0; i < containers.length; i++) { 180 containers[i].style.display = 'none'; 181 } 182 }; 183 184 /** 185 * Update country restriction on an autocomplete instance. 186 * 187 * @param {Object} placeAutocomplete Autocomplete instance. 188 * @param {string|null} country ISO country code or null. 189 * @return {void} 190 */ 191 FMEAACOCPlaceUtils.setCountryRestriction = function (placeAutocomplete, country) { 192 if (!placeAutocomplete) { 193 return; 194 } 195 if (typeof placeAutocomplete.setComponentRestrictions === 'function') { 196 placeAutocomplete.setComponentRestrictions( 197 country ? { country: country } : { country: [] } 198 ); 199 return; 200 } 201 placeAutocomplete.includedRegionCodes = country 202 ? [String(country).toLowerCase()] 203 : []; 204 }; 205 206 /** 207 * Build Map options including mapId or legacy styles. 208 * 209 * @param {Object} center LatLngLiteral center. 210 * @param {string} mapId Cloud Map ID. 211 * @param {Array|null} mapStyle Legacy JSON styles. 212 * @return {Object} Map constructor options. 213 */ 214 FMEAACOCPlaceUtils.buildMapOptions = function (center, mapId, mapStyle) { 215 var options = { center: center, zoom: 17 }; 216 if (mapId) { 217 options.mapId = mapId; 218 } else if (mapStyle && mapStyle.length) { 219 options.styles = mapStyle; 220 } 221 return options; 222 }; 223 224 /** 225 * Read mapId from localized data or settings object. 226 * 227 * @param {Object} settings Plugin settings. 228 * @param {Object} localized Localized fmeaacoc_data. 229 * @return {string} Map ID or empty string. 230 */ 231 FMEAACOCPlaceUtils.getMapId = function (settings, localized) { 232 settings = settings || {}; 233 localized = localized || {}; 234 if (localized.FMEAACOC_map_id) { 235 return String(localized.FMEAACOC_map_id); 236 } 237 if (settings.FMEAACOC_map_id) { 238 return String(settings.FMEAACOC_map_id); 239 } 240 return ''; 241 }; 242 243 /** 244 * Whether the map canvas should be initialized. 245 * 246 * @param {Object} settings Plugin settings. 247 * @return {boolean} True when map should render. 248 */ 249 FMEAACOCPlaceUtils.shouldShowMap = function (settings) { 250 settings = settings || {}; 251 return 'on' !== settings.FMEAACOC_Hide_Map; 252 }; 253 254 window.FMEAACOCPlaceUtils = FMEAACOCPlaceUtils; 255})(window);
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.