/* global gtc_frontend_params */ jQuery( function ( $ ) { 'use strict' ; if ( typeof gtc_frontend_params === 'undefined' ) { return false ; } var GTC_Frontend = { init : function ( ) { // Show variation data. $( document ).on( 'show_variation' , this.onFoundVariation ) ; $( document ).on( 'hide_variation' , this.onResetVariation ) ; // Handles the gift card predefined button. $( document ).on( 'click' , '.gtc-gift-card-predefined-button' , this.handle_gift_card_predefined_button ) ; // Pagination for active gift cards. $( document ).on( 'click' , '.gtc-myaccount-active-gift-card-table .gtc_pagination' , this.active_gift_card_pagination ) ; // Pagination for transaction logs. $( document ).on( 'click' , '.gtc-myaccount-activity-table .gtc_pagination' , this.transaction_logs_pagination ) ; // Toggle the checkout gift card usage. $( document ).on( 'click' , '.gtc-checkout-gift-card-usage-link' , this.toggle_checkout_gift_card_usage ) ; // Handles the cart gift card code usage. $( document ).on( 'click' , '.gtc-cart-gift-card-usage-btn' , this.handle_cart_gift_card_usage ) ; // Handles the cart remove gift card code usage. $( document ).on( 'click' , '.gtc-cart-remove-gift-card-code' , this.handle_cart_remove_gift_card_code ) ; // Handles the checkout remove gift card code. $( document ).on( 'click' , '.gtc-checkout-gift-card-usage-btn' , this.handle_checkout_gift_card_usage ) ; // Handles the checkout gift card code usage. $( document ).on( 'click' , '.gtc-checkout-remove-gift-card-code' , this.handle_checkout_remove_gift_card_code ) ; // Toggle the checkout wallet usage. $( document ).on( 'click' , '.gtc-checkout-wallet-usage-link' , this.toggle_checkout_wallet_fund_usage ) ; // Handles the cart wallet usage. $( document ).on( 'click' , '.gtc-cart-wallet-usage-btn' , this.handle_cart_wallet_fund_usage ) ; // Handles the cart remove wallet usage. $( document ).on( 'click' , '.gtc-cart-remove-wallet-fund' , this.handle_cart_remove_wallet_fund ) ; // Handles the checkout remove wallet fund. $( document ).on( 'click' , '.gtc-checkout-wallet-usage-btn' , this.handle_checkout_wallet_fund_usage ) ; // Handles the checkout wallet usage. $( document ).on( 'click' , '.gtc-checkout-remove-wallet-fund' , this.handle_checkout_remove_wallet_fund ) ; //Update the cart when updating shipping. $( document.body ).on( 'updated_shipping_method' , this.updated_shipping_method ) ; // Handles the gift card enable. $( document ).on( 'change' , '.gtc-gift-card-enable' , this.handle_product_gift_card_enable ) ; // Validate the message count. $( document ).on( 'keyup , change' , '.gtc-gift-card-sender-message' , this.validate_message_count ) ; // Handles the gift card design item. $( document ).on( 'click' , '.gtc-gift-card-design-wrapper-select' , this.handle_design_selection ) ; // Handles the gift card popup design item. $( document ).on( 'click' , '.gtc-gift-card-popup-design-wrapper-item' , this.handle_design_popup_selection ) ; // Handles the gift card popup design category items. $( document ).on( 'click' , '.gtc-popup-design-category-menu' , this.handle_design_popup_category_items ) ; // Preview the virtual gift card. $( document ).on( 'click' , '.gtc-virtual-gift-card-design-preview' , this.preview_virtual_gift_card ) ; // Preview the product gift card. $( document ).on( 'click' , '.gtc-product-gift-card-design-preview' , this.preview_product_gift_card ) ; // Handles the form. $( document ).on( 'click' , '.gtc-form-btn' , this.handle_form ) ; this.trigger_on_page_load( ) ; } , trigger_on_page_load : function () { GTC_Frontend.product_gift_card_enable( '.gtc-gift-card-enable' ) ; GTC_Frontend.initialize_date_picker() ; } , onFoundVariation : function ( evt , variation , purchasable ) { GTC_Frontend.onResetVariation() ; if ( variation.gtc_gift_cards ) { $( '.variations_form' ).find( '.woocommerce-variation-add-to-cart' ).before( variation.gtc_gift_cards ) ; GTC_Frontend.product_gift_card_enable( '.gtc-gift-card-enable' ) ; GTC_Frontend.initialize_date_picker() ; $( document.body ).trigger( 'gtc-enhanced-lightcase' ) ; } } , onResetVariation : function ( evt ) { if ( $( '.variations_form' ).find( '.gtc-product-gift-card-summary' ).length ) { $( '.variations_form' ).find( '.gtc-product-gift-card-summary' ).remove() ; } } , initialize_date_picker : function () { if ( $( '.gtc_datepicker' ).length ) { $( '.gtc_datepicker' ).on( 'change' , function ( ) { if ( $( this ).val( ) === '' ) { $( this ).next().next( ".gtc_alter_datepicker_value" ).val( '' ) ; } } ) ; $( '.gtc_datepicker' ).each( function ( ) { $( this ).datepicker( { altField : $( this ).next( ".gtc-alter-datepicker-value" ) , altFormat : 'yy-mm-dd' , changeMonth : true , changeYear : true , minDate : 0 } ) ; } ) ; } } , validate_message_count : function ( event ) { var $this = $( event.currentTarget ) , message = $( $this ) , message_count = message.val().length , message_wrapper = $( $this ).closest( '.gtc-gift-card-field-row' ) , total_message_count = message.data( 'max' ) ; if ( message_count <= total_message_count ) { message_wrapper.find( '.gtc-gift-card-sender-message-count' ).html( total_message_count - message_count ) ; } else { message.val( message.val().substring( 0 , total_message_count ) ) ; } return true ; } , handle_gift_card_predefined_button : function ( event ) { event.preventDefault( ) ; var $this = $( event.currentTarget ) , wrapper = $( $this ).closest( '.gtc-gift-card-amount-wrapper' ) , fund = $( $this ).data( 'amount' ) ; wrapper.find( '.gtc-gift-card-predefined-button' ).removeClass( 'gtc-active' ) ; $( $this ).addClass( 'gtc-active' ) ; console.log("test => " + fund); wrapper.find( '#gtc_gift_card_amount' ).val( fund ) ; } , active_gift_card_pagination : function ( event ) { event.preventDefault( ) ; var $this = $( event.currentTarget ) , table = $this.closest( 'table.gtc-myaccount-active-gift-card-table' ) , table_body = table.find( 'tbody' ) , page_number = $this.data( 'page' ) ; GTC_Frontend.block( table_body ) ; var data = ( { action : 'gtc_active_gift_card_pagination' , page_number : page_number , gtc_security : gtc_frontend_params.pagination_nonce , } ) ; $.post( gtc_frontend_params.ajax_url , data , function ( res ) { if ( true === res.success ) { table_body.html( res.data.html ) ; GTC_Frontend.handle_pagination( table , page_number ) ; } else { alert( res.data.error ) ; } GTC_Frontend.unblock( table_body ) ; } ) ; } , transaction_logs_pagination : function ( event ) { event.preventDefault( ) ; var $this = $( event.currentTarget ) , table = $this.closest( 'table.gtc-myaccount-activity-table' ) , table_body = table.find( 'tbody' ) , page_number = $this.data( 'page' ) ; GTC_Frontend.block( table_body ) ; var data = ( { action : 'gtc_transaction_logs_pagination' , page_number : page_number , gtc_security : gtc_frontend_params.pagination_nonce , } ) ; $.post( gtc_frontend_params.ajax_url , data , function ( res ) { if ( true === res.success ) { table_body.html( res.data.html ) ; GTC_Frontend.handle_pagination( table , page_number ) ; } else { alert( res.data.error ) ; } GTC_Frontend.unblock( table_body ) ; } ) ; } , handle_pagination : function ( table , page_number ) { table.find( '.gtc_pagination' ).removeClass( 'current' ) ; table.find( '.gtc_pagination_' + page_number ).addClass( 'current' ) ; var next_page = page_number ; if ( page_number > 1 ) { next_page = page_number - 1 ; } var last_page = table.find( '.gtc_last_pagination' ).data( 'page' ) ; if ( page_number < last_page ) { last_page = page_number + 1 ; } table.find( '.gtc_next_pagination' ).data( 'page' , last_page ).attr( 'title' , last_page ) ; table.find( '.gtc_prev_pagination' ).data( 'page' , next_page ).attr( 'title' , next_page ) ; } , toggle_checkout_gift_card_usage : function ( ) { $( '.gtc-checkout-gift-card-usage-content' ).slideToggle( 400 , function () { $( '.gtc-checkout-gift-card-usage-content' ).find( ':input:eq(0)' ).focus() ; } ) ; return false ; } , handle_cart_gift_card_usage : function ( event ) { event.preventDefault( ) ; var $this = $( event.currentTarget ) , wrapper = $( $this ).closest( '.gtc-cart-gift-card-usage-wrapper' ) ; GTC_Frontend.block( wrapper ) ; var data = ( { action : 'gtc_apply_gift_card_code' , code : wrapper.find( '.gtc-cart-gift-card-code' ).val() , gtc_security : gtc_frontend_params.gift_card_usage_nonce , } ) ; $.post( gtc_frontend_params.ajax_url , data , function ( res ) { $( document.body ).trigger( 'wc_update_cart' ) ; GTC_Frontend.unblock( wrapper ) ; } ) ; } , handle_cart_remove_gift_card_code : function ( event ) { event.preventDefault( ) ; var $this = $( event.currentTarget ) , wrapper = $( $this ).closest( '.cart_totals' ) ; GTC_Frontend.block( wrapper ) ; var data = ( { action : 'gtc_remove_gift_card_code' , code : $( $this ).data( 'code' ) , gtc_security : gtc_frontend_params.gift_card_usage_nonce , } ) ; $.post( gtc_frontend_params.ajax_url , data , function ( res ) { $( document.body ).trigger( 'wc_update_cart' ) ; GTC_Frontend.unblock( wrapper ) ; } ) ; } , handle_checkout_gift_card_usage : function ( event ) { event.preventDefault( ) ; var $this = $( event.currentTarget ) , wrapper = $( $this ).closest( '.gtc-checkout-gift-card-usage-wrapper' ) ; GTC_Frontend.block( wrapper ) ; var data = ( { action : 'gtc_apply_gift_card_code' , code : wrapper.find( '.gtc-checkout-gift-card-code' ).val() , gtc_security : gtc_frontend_params.gift_card_usage_nonce , } ) ; $.post( gtc_frontend_params.ajax_url , data , function ( res ) { window.location.reload( true ) ; } ) ; } , handle_checkout_remove_gift_card_code : function ( event ) { event.preventDefault( ) ; var $this = $( event.currentTarget ) , wrapper = $( $this ).parents( '.woocommerce-checkout-review-order' ) ; GTC_Frontend.block( wrapper ) ; var data = ( { action : 'gtc_remove_gift_card_code' , code : $( $this ).data( 'code' ) , gtc_security : gtc_frontend_params.gift_card_usage_nonce , } ) ; $.post( gtc_frontend_params.ajax_url , data , function ( res ) { window.location.reload( true ) ; } ) ; } , toggle_checkout_wallet_fund_usage : function ( ) { $( '.gtc-checkout-wallet-usage-content' ).slideToggle( 400 , function () { $( '.gtc-checkout-wallet-usage-content' ).find( ':input:eq(0)' ).focus() ; } ) ; return false ; } , handle_cart_wallet_fund_usage : function ( event ) { event.preventDefault( ) ; var $this = $( event.currentTarget ) , wrapper = $( $this ).closest( '.gtc-cart-wallet-usage-wrapper' ) ; GTC_Frontend.block( wrapper ) ; var data = ( { action : 'gtc_apply_wallet_fund' , fund : wrapper.find( '.gtc-cart-wallet-usage-amount' ).val() , gtc_security : gtc_frontend_params.wallet_usage_nonce , } ) ; $.post( gtc_frontend_params.ajax_url , data , function ( res ) { $( document.body ).trigger( 'wc_update_cart' ) ; GTC_Frontend.unblock( wrapper ) ; } ) ; } , handle_cart_remove_wallet_fund : function ( event ) { event.preventDefault( ) ; var $this = $( event.currentTarget ) , wrapper = $( $this ).closest( '.cart_totals' ) ; GTC_Frontend.block( wrapper ) ; var data = ( { action : 'gtc_remove_wallet_fund' , gtc_security : gtc_frontend_params.wallet_usage_nonce , } ) ; $.post( gtc_frontend_params.ajax_url , data , function ( res ) { $( document.body ).trigger( 'wc_update_cart' ) ; GTC_Frontend.unblock( wrapper ) ; } ) ; } , handle_checkout_wallet_fund_usage : function ( event ) { event.preventDefault( ) ; var $this = $( event.currentTarget ) , wrapper = $( $this ).closest( '.gtc-checkout-wallet-usage-wrapper' ) ; GTC_Frontend.block( wrapper ) ; var data = ( { action : 'gtc_apply_wallet_fund' , fund : wrapper.find( '.gtc-checkout-wallet-usage-amount' ).val() , gtc_security : gtc_frontend_params.wallet_usage_nonce , } ) ; $.post( gtc_frontend_params.ajax_url , data , function ( res ) { window.location.reload( true ) ; } ) ; } , handle_checkout_remove_wallet_fund : function ( event ) { event.preventDefault( ) ; var $this = $( event.currentTarget ) , wrapper = $( $this ).parents( '.woocommerce-checkout-review-order' ) ; GTC_Frontend.block( wrapper ) ; var data = ( { action : 'gtc_remove_wallet_fund' , gtc_security : gtc_frontend_params.wallet_usage_nonce , } ) ; $.post( gtc_frontend_params.ajax_url , data , function ( res ) { window.location.reload( true ) ; } ) ; } , updated_shipping_method : function () { $( document.body ).trigger( 'wc_update_cart' ) ; } , handle_product_gift_card_enable : function ( event ) { event.preventDefault( ) ; var $this = $( event.currentTarget ) ; GTC_Frontend.product_gift_card_enable( $this ) ; } , product_gift_card_enable : function ( $this ) { var wrapper = $( $this ).closest( '.gtc-product-gift-card-summary' ) ; if ( $( $this ).is( ':checked' ) ) { wrapper.find( '.gtc-product-gift-card-wrapper' ).show() ; } else { wrapper.find( '.gtc-product-gift-card-wrapper' ).hide() ; } } , handle_design_selection : function ( event ) { event.preventDefault( ) ; var $this = $( event.currentTarget ) ; $( '.gtc-gift-card-design-wrapper-item' ).removeClass( 'gtc_current' ) ; $( $this ).addClass( 'gtc_current' ) ; $( '.gtc-gift-card-design-id' ).val( $( $this ).data( 'design-id' ) ) ; } , handle_design_popup_selection : function ( event ) { event.preventDefault( ) ; var $this = $( event.currentTarget ) , popup_content = $( '.gtc-gift-card-popup-designs-wrapper-content' ) , design_wrapper = $( '.gtc-gift-card-designs-wrapper-content' ) , items = design_wrapper.find( '.gtc-gift-card-design-wrapper-item' ) , product_id = design_wrapper.find( '.gtc-design-wrapper-product-id' ).val() , current_item = $( $this ).data( 'design-id' ) ; GTC_Frontend.block( popup_content ) ; var data = ( { action : 'gtc_select_popup_design' , design_id : current_item , product_id : product_id , gtc_security : gtc_frontend_params.popup_design_nonce , } ) ; $.post( gtc_frontend_params.ajax_url , data , function ( res ) { if ( true === res.success ) { items.removeClass( 'gtc_current' ) ; if ( $( '.gtc-gift-card-design-wrapper-item-' + current_item ).length ) { $( '.gtc-gift-card-design-wrapper-item-' + current_item ).remove() ; } else { $( '.gtc-gift-card-design-wrapper-select:last' ).remove() ; } $( '.gtc-gift-card-design-id' ).val( current_item ) ; design_wrapper.find( '.gtc-gift-card-design-items' ).prepend( res.data.item ) ; lightcase.close() ; } else { alert( res.data.error ) ; } GTC_Frontend.unblock( popup_content ) ; } ) ; } , handle_design_popup_category_items : function ( event ) { event.preventDefault( ) ; var $this = $( event.currentTarget ) , popup_modal = $( '.gtc-gift-card-popup-designs-wrapper' ) , popup_content = popup_modal.find( '.gtc-gift-card-popup-designs-wrapper-content' ) , items = popup_modal.find( '.gtc-gift-card-popup-design-items' ) , current_item = $( $this ).data( 'category-id' ) ; GTC_Frontend.block( popup_content ) ; var data = ( { action : 'gtc_category_designs' , category_id : current_item , gtc_security : gtc_frontend_params.popup_design_nonce , } ) ; $.post( gtc_frontend_params.ajax_url , data , function ( res ) { if ( true === res.success ) { items.html( res.data.items ) ; $( '.gtc-popup-design-category-menu' ).removeClass( 'gtc_current' ) ; $( $this ).addClass( 'gtc_current' ) ; } else { alert( res.data.error ) ; } GTC_Frontend.unblock( popup_content ) ; } ) ; } , preview_virtual_gift_card : function ( event ) { event.preventDefault( ) ; var $this = $( event.currentTarget ) , wrapper = $this.closest( '.gtc-gift-card-design-preivew-content' ) ; GTC_Frontend.block( wrapper ) ; var data = ( { action : 'gtc_virtual_gift_card_design_preview_data' , amount : wrapper.find( '#gtc_design_gift_card_amount' ).val() , design_id : $( '.gtc-gift-card-design-id' ).val() , product_id : $( '.gtc-design-wrapper-product-id' ).val() , sender_name : $( '.gtc-gift-card-sender-name' ).val() , sender_message : $( '.gtc-gift-card-sender-message' ).val() , gtc_security : gtc_frontend_params.preview_nonce , } ) ; $.post( gtc_frontend_params.ajax_url , data , function ( res ) { if ( true === res.success ) { $( '#gtc-popup-gift-card-design-preview-modal' ).html( res.data.html ) ; $( document.body ).trigger( 'gtc-enhanced-lightcase' ) ; wrapper.find( '.gtc-popup-gift-card' ).trigger( 'click' ) ; } else { alert( res.data.error ) ; } GTC_Frontend.unblock( wrapper ) ; } ) ; } , preview_product_gift_card : function ( event ) { event.preventDefault( ) ; var $this = $( event.currentTarget ) , wrapper = $this.closest( '.gtc-gift-card-design-preivew-content' ) ; GTC_Frontend.block( wrapper ) ; var data = ( { action : 'gtc_product_gift_card_design_preview_data' , amount : wrapper.find( '#gtc_design_gift_card_amount' ).val() , design_id : $( '.gtc-gift-card-design-id' ).val() , product_id : $( '#gtc_design_gift_card_id' ).val() , sender_name : $( '.gtc-gift-card-sender-name' ).val() , sender_message : $( '.gtc-gift-card-sender-message' ).val() , gtc_security : gtc_frontend_params.preview_nonce , } ) ; $.post( gtc_frontend_params.ajax_url , data , function ( res ) { if ( true === res.success ) { $( '#gtc-popup-gift-card-design-preview-modal' ).html( res.data.html ) ; $( document.body ).trigger( 'gtc-enhanced-lightcase' ) ; wrapper.find( '.gtc-popup-gift-card' ).trigger( 'click' ) ; } else { alert( res.data.error ) ; } GTC_Frontend.unblock( wrapper ) ; } ) ; } , handle_form : function ( event ) { var $this = $( event.currentTarget ) , wrapper = $this.closest( '.gtc-form-wrapper' ) ; GTC_Frontend.block( wrapper ) ; return true ; } , block : function ( id ) { if ( !GTC_Frontend.is_blocked( id ) ) { $( id ).addClass( 'processing' ).block( { message : null , overlayCSS : { background : '#fff' , opacity : 0.7 } } ) ; } } , unblock : function ( id ) { $( id ).removeClass( 'processing' ).unblock() ; } , is_blocked : function ( id ) { return $( id ).is( '.processing' ) || $( id ).parents( '.processing' ).length ; } } ; GTC_Frontend.init( ) ; } ) ;