(function( $j ) { 'use strict'; var alopeyk = alopeyk || { wcshm : {} }; alopeyk.wcshm.public = {}; /*========================================== = Defining Variables = ==========================================*/ alopeyk.wcshm.public.vars = { common : { info : window.awcshm, activeClass : 'active', loadingClass : 'loading', disabledClass : 'disabled', alopeykPrefix : 'awcshm-', paymentMethodInput : 'input[name="payment_method"]', }, maps : { selector : '.map-canvas', mapContainerClass : 'map-container', destinationLatInput : '#destination_latitude, #_shipping_address_latitude', destinationLngInput : '#destination_longitude, #_shipping_address_longitude', destinationCityInput : '#destination_city, #_shipping_address_location_city', destinationAddressInput : '#destination_address, #_shipping_address_location', destinationNumberInput : '#destination_number, #_shipping_address_number', destinationUnitInput : '#destination_unit, #_shipping_address_unit', shippingAddress1Input : '#shipping_address_1, #_shipping_address_1', shippingAddress2Input : '#shipping_address_2, #_shipping_address_2', billingAddress1Input : '#billing_address_1, #_billing_address_1', billingAddress2Input : '#billing_address_2, #_billing_address_2', shipToDifferentAddressInput : 'input[name="ship_to_different_address"]', editAddressButton : 'a.edit_address:eq(1)', destinationLocatorMapId : 'destination-locator-map', destinationLocatorMapClass : 'map-canvas destination-locator-map', destinationLocatorCtaClass : 'destination-locator-cta', mapMarkerIconClass : 'map-marker-icon', destinationLocatorInputWrapperId : 'destination-locator-input-wrapper', destinationLocatorInputWrapperClass : 'destination-locator-input-wrapper', destinationLocatorAutocompleteResultsClass : 'destination-locator-autocomplete-results', destinationLocatorAutocompleteResultClass : 'destination-locator-autocomplete-result', destinationLocatorHiddenableInput : '.hide-parent-row', autocompletePlaceholderDataAttr : 'autocomplete-placeholder', autocompleteKeyupTimeout : null, positionKeyupTimeout : null, autoCompleteKeyupDelay : 500, positionKeyupDelay : 500, defaultZoom : 15, maxZoom : 17, defaultCenter : { lat : 35.744989, lng : 51.375284 }, cedarMapJsLib : 'https://api.cedarmaps.com/cedarmaps.js/v1.8.0/cedarmaps.js', cedarMapCssLib : 'https://api.cedarmaps.com/cedarmaps.js/v1.8.0/cedarmaps.css', cedarMapTilesSource : 'https://alopeyk.api.cedarmaps.com/v1/tiles/cedarmaps.streets.json?access_token={{TOKEN}}', }, }; /*================================== = Prototypes = ==================================*/ /*========================================== = Defining Functions = ==========================================*/ alopeyk.wcshm.public.fn = { addPrefix : function ( classes ) { var prefixedClasses = [], classesArray = classes.split ( ' ' ); for ( var i = 0; i < classesArray.length; i++ ) { prefixedClasses.push ( alopeyk.wcshm.public.vars.common.alopeykPrefix + classesArray[ i ] ) } return prefixedClasses.join ( ' ' ); }, translate : function ( term ) { var translation = alopeyk.wcshm.public.vars.common.info.translations[ term ]; return translation ? translation : term; }, injectScript : function ( src, callback ) { if ( ! $j( '[src="' + src + '"]' ).length ) { var s, t; s = document.createElement ( 'script' ); s.type = 'text/javascript'; s.async = true; s.src = src; s.onload = callback ? callback : false; t = document.getElementsByTagName ( 'script' )[ 0 ]; t.parentNode.insertBefore ( s, t ); } }, injectStylesheet : function ( href ) { if ( ! $j( '[href="' + href + '"]' ).length ) { var s, t; s = document.createElement ( 'link' ); s.type = 'text/css'; s.rel = "stylesheet"; s.href = href; t = document.getElementsByTagName ( 'link' )[ 0 ]; t.parentNode.insertBefore ( s, t ); } }, loadCedarMaps : function () { window[ 'alopeykHandleMapsPublic' ] = alopeyk.wcshm.public.fn.handleMaps; if ( typeof alopeykHandleMapsPublic === 'function' ) { if ( typeof window.L != 'undefined' ) { window.cedarMapIsLoading = false; alopeyk.wcshm.public.fn.handleMaps(); } else if ( window.cedarMapIsLoading ) { alopeyk.wcshm.public.vars.loadingMapInterval = setInterval ( function () { if ( window.L ) { window.cedarMapIsLoading = false; clearInterval ( alopeyk.wcshm.public.vars.loadingMapInterval ); alopeyk.wcshm.public.fn.handleMaps(); } }, 500 ); } else { window.cedarMapIsLoading = true; alopeyk.wcshm.public.fn.injectScript ( alopeyk.wcshm.public.vars.maps.cedarMapJsLib, function () { alopeyk.wcshm.public.fn.injectScript ( alopeyk.wcshm.public.vars.common.info.leaflet_gesture_handling.js, alopeykHandleMapsPublic ); }); alopeyk.wcshm.public.fn.injectStylesheet ( alopeyk.wcshm.public.vars.maps.cedarMapCssLib ); alopeyk.wcshm.public.fn.injectStylesheet ( alopeyk.wcshm.public.vars.common.info.leaflet_gesture_handling.css ); } } }, handleMaps : function () { $j( document ).trigger ( 'alopeyk:public:map:loaded' ); }, initMaps : function () { $j( document ).on ( 'alopeyk:public:map:loaded', function () { var destinationLatInput = $j( alopeyk.wcshm.public.vars.maps.destinationLatInput ), destinationLngInput = $j( alopeyk.wcshm.public.vars.maps.destinationLngInput ), destinationCityInput = $j( alopeyk.wcshm.public.vars.maps.destinationCityInput ), destinationAddressInput = $j( alopeyk.wcshm.public.vars.maps.destinationAddressInput ), destinationNumberInput = $j( alopeyk.wcshm.public.vars.maps.destinationNumberInput ), destinationUnitInput = $j( alopeyk.wcshm.public.vars.maps.destinationUnitInput ), shippingAddress1Input = $j( alopeyk.wcshm.public.vars.maps.shippingAddress1Input ), shippingAddress2Input = $j( alopeyk.wcshm.public.vars.maps.shippingAddress2Input ), billingAddress1Input = $j( alopeyk.wcshm.public.vars.maps.billingAddress1Input ), billingAddress2Input = $j( alopeyk.wcshm.public.vars.maps.billingAddress2Input ), shipToDifferentAddressInput = $j( alopeyk.wcshm.public.vars.maps.shipToDifferentAddressInput ), editAddressButton = $j( alopeyk.wcshm.public.vars.maps.editAddressButton ); if ( destinationLatInput.length && destinationLngInput.length && destinationCityInput.length && destinationAddressInput.length ) { alopeyk.wcshm.public.fn.initDestinationLocator( destinationLatInput, destinationLngInput, destinationCityInput, destinationAddressInput, destinationNumberInput, destinationUnitInput, shippingAddress1Input, shippingAddress2Input, billingAddress1Input, billingAddress2Input, shipToDifferentAddressInput, editAddressButton ); } }); alopeyk.wcshm.public.fn.loadCedarMaps(); }, setCurrentGeolocation : function ( forceRelocation ) { var destinationMap = alopeyk.wcshm.public.vars.common.destinationMap, map = destinationMap ? destinationMap.mapObject : null, destinationLatInput = $j( alopeyk.wcshm.public.vars.maps.destinationLatInput ), destinationLngInput = $j( alopeyk.wcshm.public.vars.maps.destinationLngInput ); if ( navigator.geolocation && map && destinationLatInput.length && destinationLngInput.length && ( forceRelocation || ( ! destinationLatInput.val().length && ! destinationLngInput.val().length ) || ( destinationLatInput.val() == alopeyk.wcshm.public.vars.maps.defaultCenter.lat && destinationngInput.val() == alopeyk.wcshm.public.vars.maps.defaultCenter.lng ) ) ) { navigator.geolocation.getCurrentPosition ( function ( position ) { if ( alopeyk.wcshm.public.vars.maps.fetchAddressConnection ) { alopeyk.wcshm.public.vars.maps.fetchAddressConnection.abort(); } map.setView ({ lat : position.coords.latitude, lng : position.coords.longitude }); alopeyk.wcshm.public.vars.maps.defaultCenter.lat = position.coords.latitude; alopeyk.wcshm.public.vars.maps.defaultCenter.lng = position.coords.longitude; destinationLatInput.val ( position.coords.latitude ); destinationLngInput.val ( position.coords.longitude ).trigger ( 'change' ); }); } }, initDestinationLocator : function ( destinationLatInput, destinationLngInput, destinationCityInput, destinationAddressInput, destinationNumberInput, destinationUnitInput, shippingAddress1Input, shippingAddress2Input, billingAddress1Input, billingAddress2Input, shipToDifferentAddressInput, editAddressButton ) { var initialize = function () { var mapMarkerImageUrl = alopeyk.wcshm.public.vars.common.info.alopeyk.wcshm.map.marker, mapCanvas = $j( '
' ).attr ({ id : alopeyk.wcshm.public.fn.addPrefix ( alopeyk.wcshm.public.vars.maps.destinationLocatorMapId ), class : alopeyk.wcshm.public.fn.addPrefix ( alopeyk.wcshm.public.vars.maps.destinationLocatorMapClass ), }), destinationAutocompleteInput = destinationAddressInput.clone().attr ({ id : destinationAddressInput.attr ( 'id' ) + '_autocomplete', class : destinationAddressInput.attr ( 'class' ), placeholder : destinationAddressInput.data ( alopeyk.wcshm.public.vars.maps.autocompletePlaceholderDataAttr ), name : '', style : '', type : 'text', spellcheck : 'false', autocapitalize : 'off', autocorrect : 'off', autocomplete : 'off' }).removeClass ( alopeyk.wcshm.public.vars.common.disabledClass ), destinationAutocompleteInputWrapper = $j( '
' ).attr ({ id : alopeyk.wcshm.public.fn.addPrefix ( alopeyk.wcshm.public.vars.maps.destinationLocatorInputWrapperId ), class : alopeyk.wcshm.public.fn.addPrefix ( alopeyk.wcshm.public.vars.maps.destinationLocatorInputWrapperClass ), }), autoCompleteList = $j( '