{"version":3,"file":"../cart-update-220.min.js","sources":["cart-update-220.js"],"sourcesContent":["/**\n * Manage cart update actions.\n *\n * DEPENDS ON:\n * - jQuery // Interact with WooCommerce events\n */\n (function (root, factory) {\n\tif ( typeof define === 'function' && define.amd ) {\n\t\tdefine([], factory(root));\n\t} else if ( typeof exports === 'object' ) {\n\t\tmodule.exports = factory(root);\n\t} else {\n\t\troot.CartUpdate = factory(root);\n\t}\n})(typeof global !== 'undefined' ? global : this.window || this.global, function (root) {\n\n\t'use strict';\n\n\tvar $ = jQuery;\n\tvar _hasJQuery = ( $ != null );\n\n\tvar _hasInitialized = false;\n\tvar _publicMethods = {};\n\tvar _settings = {\n\t\tbodyClass: 'has-fc-cart-update',\n\n\t\twrapperSelector: '.fc-wrapper',\n\t\tsectionSelector: '.fc-cart-section',\n\t\tmessagesWrapperSelector: '.woocommerce-notices-wrapper',\n\n\t\tscrollToNoticesEnabled: 'yes',\n\n\t\tsectionVisibleStateFieldSelector: '.fc-section-visible-state[type=\"hidden\"]',\n\t\tsectionVisibleStateAttribute: 'data-section-visible',\n\n\t\tcartItemsListSelector: '.woocommerce-cart-form__contents tbody',\n\t\tcartItemNotRemovedSelector: '.woocommerce-cart-form__cart-item:not( .removed ):not( .undo )',\n\n\t\tuiProcessingClass: 'processing',\n\n\t\tshippingCalcMessagesWrapperSelector: '.fc-shipping-calculator-messages',\n\n\t\tupdateCartFragmentsNonce: '', // Value updated during runtime\n\t}\n\tvar _xhr = false;\n\tvar _fragments = {};\n\n\n\n\t/**\n\t * Global flags.\n\t */\n\n\twindow.can_update_cart = true;\n\n\n\n\t/**\n\t * METHODS\n\t */\n\n\n\n\n\t/**\n\t * Maybe change visibility status of cart sections.\n\t *\n\t * @param Event _event An unused `jQuery.Event` object.\n\t * @param Array data The updated checkout data.\n\t */\n\tvar maybeChangeSectionState = function( _event, data ) {\n\t\tvar sectionElements = document.querySelectorAll( _settings.sectionSelector );\n\t\tfor ( var i = 0; i < sectionElements.length; i++ ) {\n\t\t\tvar sectionElement = sectionElements[i];\n\n\t\t\t// Handle visibility state\n\t\t\tvar visibilityHiddenField = sectionElement.querySelector( _settings.sectionVisibleStateFieldSelector );\n\t\t\tif ( visibilityHiddenField && 'no' === visibilityHiddenField.value ) {\n\t\t\t\tsectionElement.setAttribute( _settings.sectionVisibleStateAttribute, visibilityHiddenField.value );\n\t\t\t}\n\t\t\telse {\n\t\t\t\tsectionElement.removeAttribute( _settings.sectionVisibleStateAttribute );\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Maybe enhance select fields with TomSelect.\n\t */\n\tvar maybeEnhanceFields = function() {\n\t\t// Bail if FCEnhancedSelect is not available\n\t\tif ( ! window.FCEnhancedSelect ) { return; }\n\n\t\tFCEnhancedSelect.enhanceFields();\n\t}\n\n\n\n\t/**\n\t * Maybe trigger the update cart event when the page visibility state changes.\n\t */\n\tvar maybeUpdateCartVisibilityChange = function() {\n\t\tif ( 'hidden' == document.visibilityState || 'visible' == document.visibilityState ) {\n\t\t\t$( document.body ).trigger( 'wc_fragment_refresh' );\n\t\t}\n\t};\n\tvar debouncedMaybeUpdateCartVisibilityChange = FCUtils.debounce( maybeUpdateCartVisibilityChange, 50 );\n\n\n\n\t/**\n\t * Trigger update cart fragments for use with event handlers.\n\t */\n\tvar triggerUpdateCartFragments = function( e ) {\n\t\t_publicMethods.updateCartFragments();\n\t};\n\n\n\n\t/**\n\t * Update the cart fragments.\n\t * @throws {TypeError} Throws TypeError when used directly with event handlers because events pass the Event object as the first parameter while this function expects the first parameter to be the extra data to be sent with the AJAX request.\n\t */\n\t_publicMethods.updateCartFragments = function( data ) {\n\t\t// Cancel existing update request\n\t\tif ( _xhr ) { _xhr.abort(); }\n\n\t\t// Bail if the cart is not updatable\n\t\tif ( ! window.can_update_cart ) { return; }\n\n\t\t// Add security\n\t\tdata = FCUtils.extendObject( data, {\n\t\t\tsecurity: _settings.updateCartFragmentsNonce,\n\t\t} );\n\n\t\t_xhr = $.ajax({\n\t\t\ttype:\t\t'POST',\n\t\t\turl:\t\tfcSettings.wcAjaxUrl.toString().replace( '%%endpoint%%', 'fc_pro_update_cart_fragments' ),\n\t\t\tdata:\t\tdata,\n\t\t\tsuccess:\tfunction( result ) {\n\n\t\t\t\t// Reload the page if requested\n\t\t\t\tif ( result && true === result.reload ) {\n\t\t\t\t\twindow.location.reload();\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\t// CHANGE: Set variables for current focused element\n\t\t\t\tFCUtils.setCurrentFocusedElementGlobalVariables();\n\n\t\t\t\t// Always update the fragments\n\t\t\t\tif ( result && result.fragments ) {\n\t\t\t\t\t// Try to remove select2 components from existing fields before replacing fragments\n\t\t\t\t\t$( 'select.country_select, select.state_select' ).each( function() {\n\t\t\t\t\t\tvar field = $( this );\n\t\t\t\t\t\tif ( field && false !== field.data( 'select2' ) && field.selectWoo && 'function' === typeof field.selectWoo ) {\n\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\tfield.select2( 'destroy' );\n\t\t\t\t\t\t\t} catch ( error ) {\n\t\t\t\t\t\t\t\ttry { field.selectWoo( 'destroy' ); }\n\t\t\t\t\t\t\t\tcatch ( error2 ) { /* Do nothing */ }\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\n\t\t\t\t\t$.each( result.fragments, function ( key, value ) {\n\t\t\t\t\t\t// CHANGE: Declare local variables needed for some checks before replacing the fragment\n\t\t\t\t\t\tvar fragmentToReplace = document.querySelector( key );\n\t\t\t\t\t\tvar replaceFragment = true;\n\n\t\t\t\t\t\t// CHANGE: Allow fragments to be replaced every time even when their contents are equal the existing elements in the DOM\n\t\t\t\t\t\tif ( value && -1 !== value.toString().indexOf( 'fc-fragment-always-replace' ) ) {\n\t\t\t\t\t\t\treplaceFragment = true;\n\t\t\t\t\t\t}\n\t\t\t\t\t\t\n\t\t\t\t\t\t// CHANGE: Allow fragments to be replaced every time even when their contents are equal the existing elements in the DOM\n\t\t\t\t\t\tif ( replaceFragment && ( ! _fragments || _fragments[ key ] !== value ) ) {\n\t\t\t\t\t\t\t// CHANGE: Log replaced fragment to console if debug mode is enabled.\n\t\t\t\t\t\t\tif ( fcSettings.debugMode ) {\n\t\t\t\t\t\t\t\tconsole.log( 'Replacing fragment: ' + key );\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t$( key ).replaceWith( value );\n\t\t\t\t\t\t}\n\t\t\t\t\t\t// CHANGE: Log skipped fragment to console if debug mode is enabled.\n\t\t\t\t\t\telse if ( fcSettings.debugMode ) {\n\t\t\t\t\t\t\tconsole.log( 'Skipping fragment: ' + key );\n\t\t\t\t\t\t}\n\t\t\t\t\t\t$( key ).unblock();\n\t\t\t\t\t} );\n\t\t\t\t\t_fragments = result.fragments;\n\t\t\t\t}\n\n\t\t\t\t// CHANGE: Re-set focus to the element with focus previously to updating fragments\n\t\t\t\tFCUtils.maybeRefocusElement( window.fcCurrentFocusedElement, window.fcCurrentFocusedElementValue );\n\n\t\t\t\t// Maybe scroll to notices\n\t\t\t\tvar messagesWrapper = document.querySelector( _settings.messagesWrapperSelector );\n\t\t\t\tif ( messagesWrapper && messagesWrapper.children.length > 0 && window.FCUtils && 'function' === typeof FCUtils.scrollToElement ) {\n\t\t\t\t\tFCUtils.scrollToElement( messagesWrapper );\n\t\t\t\t}\n\n\t\t\t\t$( document.body ).trigger( 'wc_fragments_refreshed' );\n\t\t\t\t$( document.body ).trigger( 'updated_cart_totals' );\n\t\t\t}\n\n\t\t});\n\t};\n\n\t\n\n\t/**\n\t * Use the same method that WooCommerce uses to block other parts of the checkout form while updating.\n\t * The UI is unblocked by the WooCommerce `checkout.js` script (which is replaced with a modified version but keeps the same behavior)\n\t * using the checkout fragment selector, then unblocking after the checkout update is completed.\n\t *\n\t * @param HTMLElement element Element to block the UI and show the loading indicator.\n\t */\n\t_publicMethods.blockUI = function( element ) {\n\t\t$( element ).addClass( _settings.uiProcessingClass ).block( {\n\t\t\tmessage: null,\n\t\t\toverlayCSS: {\n\t\t\t\tbackground: '#fff',\n\t\t\t\topacity: 0.6\n\t\t\t}\n\t\t} );\n\t}\n\n\t/**\n\t * Unblock UI element to be used again.\n\t *\n\t * @param HTMLElement element Element to get the UI unblocked.\n\t *\n\t * @see blockUI\n\t */\n\t_publicMethods.unblockUI = function( element ) {\n\t\t$( element ).removeClass( _settings.uiProcessingClass ).unblock();\n\t}\n\n\t/**\n\t * Add notices in the coupon code section.\n\t *\n\t * @param HTML content HTML content to be displayed.\n\t */\n\t _publicMethods.showShippingNotices = function( content ) {\n\t\tvar messagesWrapper = document.querySelector( _settings.shippingCalcMessagesWrapperSelector );\n\t\tif ( messagesWrapper ) {\n\t\t\tmessagesWrapper.innerHTML = content;\n\t\t}\n\t}\n\n\t/**\n\t * Remove all notices from the coupon code section.\n\t *\n\t * @param HTML content HTML content to be displayed.\n\t */\n\t_publicMethods.clearShippingNotices = function() {\n\t\tvar messagesWrapper = document.querySelector( _settings.shippingCalcMessagesWrapperSelector );\n\t\tif ( messagesWrapper ) {\n\t\t\tmessagesWrapper.innerHTML = '';\n\t\t}\n\t}\n\n\n\n\t/**\n\t * Initialize component and set related handlers.\n\t */\n\t_publicMethods.init = function( options ) {\n\t\tif ( _hasInitialized ) return;\n\n\t\t// Merge settings\n\t\t_settings = FCUtils.extendObject( _settings, options );\n\n\t\t// Add event listeners\n\t\tdocument.addEventListener( 'visibilitychange', debouncedMaybeUpdateCartVisibilityChange );\n\n\t\t// Add jQuery event listeners\n\t\tif ( _hasJQuery ) {\n\t\t\t$( document.body ).on( 'wc_fragment_refresh updated_wc_div updated_shipping_method', triggerUpdateCartFragments );\n\t\t\t\n\t\t\t// After cart fragments has been updated\n\t\t\t$( document.body ).on( 'wc_fragments_refreshed', maybeChangeSectionState );\n\t\t\t$( document.body ).on( 'wc_fragments_refreshed', maybeEnhanceFields );\n\t\t}\n\n\t\t// Add init class\n\t\tdocument.body.classList.add( _settings.bodyClass );\n\n\t\t// Update fragments\n\t\ttriggerUpdateCartFragments();\n\n\t\t_hasInitialized = true;\n\t};\n\n\n\n\t//\n\t// Public APIs\n\t//\n\treturn _publicMethods;\n\n});\n"],"names":["root","factory","define","amd","exports","module","CartUpdate","global","this","window","maybeChangeSectionState","_event","data","sectionElements","document","querySelectorAll","_settings","sectionSelector","i","length","sectionElement","visibilityHiddenField","querySelector","sectionVisibleStateFieldSelector","value","setAttribute","sectionVisibleStateAttribute","removeAttribute","maybeEnhanceFields","FCEnhancedSelect","enhanceFields","triggerUpdateCartFragments","e","_publicMethods","updateCartFragments","$","jQuery","_hasJQuery","_hasInitialized","bodyClass","wrapperSelector","messagesWrapperSelector","scrollToNoticesEnabled","cartItemsListSelector","cartItemNotRemovedSelector","uiProcessingClass","shippingCalcMessagesWrapperSelector","updateCartFragmentsNonce","_xhr","_fragments","debouncedMaybeUpdateCartVisibilityChange","can_update_cart","FCUtils","debounce","visibilityState","body","trigger","abort","extendObject","security","ajax","type","url","fcSettings","wcAjaxUrl","toString","replace","success","result","reload","location","setCurrentFocusedElementGlobalVariables","fragments","each","field","selectWoo","select2","error","error2","key","replaceFragment","indexOf","debugMode","console","log","replaceWith","unblock","maybeRefocusElement","fcCurrentFocusedElement","fcCurrentFocusedElementValue","messagesWrapper","children","scrollToElement","blockUI","element","addClass","block","message","overlayCSS","background","opacity","unblockUI","removeClass","showShippingNotices","content","innerHTML","clearShippingNotices","init","options","addEventListener","on","classList","add"],"mappings":"AAMC,CAAA,SAAWA,EAAMC,GACM,YAAlB,OAAOC,QAAyBA,OAAOC,IAC3CD,OAAO,GAAID,EAAY,CAAC,EACM,UAAnB,OAAOG,QAClBC,OAAOD,QAAUH,EAAY,EAE7BD,EAAKM,WAAaL,EAAY,CAE/B,EAAoB,aAAlB,OAAOM,OAAyBA,OAASC,KAAKC,QAAUD,KAAKD,OAAQ,SAAUP,GAEjF,aAsD8B,SAA1BU,EAAoCC,EAAQC,GAE/C,IADA,IAAIC,EAAkBC,SAASC,iBAAkBC,EAAUC,eAAgB,EACjEC,EAA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