1/*global jQuery */ 2( function( $ ) { 3 4 const $document = $( document ); 5 6 const shareCart = { 7 init: function() { 8 this.shareButton = $( '.nhg-share-cart-btn' ); 9 this.popupCore = null; 10 this.copySuccessTimeout = null; 11 12 this.events(); 13 }, 14 15 ensurePopup: function() { 16 if ( this.popupCore ) { 17 return true; 18 } 19 20 if ( ! window.NHGPopup ) { 21 return false; 22 } 23 24 this.popupCore = new window.NHGPopup( { 25 wrapperId: 'nhg-cart-tools-share-popup-wrap', 26 wrapperClass: 'nhg-cart-tools-share-popup hidden', 27 contentId: 'nhg-cart-tools-share-popup-content', 28 } ); 29 30 return true; 31 }, 32 33 events: function() { 34 35 $document.on( 'click touch', '.nhg-share-cart-btn', function( e ) { 36 e.preventDefault(); 37 38 if ( ! shareCart.ensurePopup() ) { 39 return; 40 } 41 42 shareCart.shareButton = $( this ); 43 shareCart.shareButton.removeClass( 'nhg-share-cart-btn-added' ).addClass( 'nhg-share-cart-btn-adding' ); 44 45 $.ajax( { 46 type: 'POST', 47 url: woocommerce_params.ajax_url, 48 data: { 49 action: 'nhg_cart_share', 50 nonce: nhg_cart_share_vars.nonce, 51 }, 52 success: function( response ) { 53 if ( ! response || ! response.success || ! response.data || ! response.data.html ) { 54 return; 55 } 56 57 shareCart.shareButton.addClass( 'nhg-share-cart-btn-added' ); 58 shareCart.popupCore.setContent( response.data.html ); 59 shareCart.popupCore.open(); 60 }, 61 complete: function() { 62 shareCart.shareButton.removeClass( 'nhg-share-cart-btn-adding' ); 63 } 64 } ); 65 } ); 66 67 $document.on( 'click touch', '#nhg_cart_share_copy_url', function() { 68 shareCart.copyToClipboard( this ); 69 } ); 70 }, 71 72 showCopySuccess: function() { 73 const successElement = $( '.nhg-cart-tools-success' ); 74 successElement.removeClass( 'hidden' ); 75 76 if ( shareCart.copySuccessTimeout ) { 77 clearTimeout( shareCart.copySuccessTimeout ); 78 } 79 80 shareCart.copySuccessTimeout = setTimeout( function() { 81 successElement.addClass( 'hidden' ); 82 }, 3000 ); 83 }, 84 85 selectInputText: function( el ) { 86 if ( navigator.userAgent.match( /ipad|ipod|iphone/i ) ) { 87 const editable = el.contentEditable; 88 const readOnly = el.readOnly; 89 90 el.contentEditable = true; 91 el.readOnly = true; 92 93 const range = document.createRange(); 94 range.selectNodeContents( el ); 95 96 const selection = window.getSelection(); 97 selection.removeAllRanges(); 98 selection.addRange( range ); 99 el.setSelectionRange( 0, 999999 ); 100 101 el.contentEditable = editable; 102 el.readOnly = readOnly; 103 return; 104 } 105 106 el.select(); 107 }, 108 109 /** 110 * copy to clipboard 111 */ 112 copyToClipboard: function( el ) { 113 el = ( typeof el === 'string' ) ? document.querySelector( el ) : el; 114 if ( ! el ) { 115 return; 116 } 117 118 const copyText = el.value || ''; 119 if ( ! copyText.length ) { 120 return; 121 } 122 123 shareCart.selectInputText( el ); 124 125 if ( ! navigator.clipboard || 'function' !== typeof navigator.clipboard.writeText ) { 126 return; 127 } 128 129 navigator.clipboard.writeText( copyText ).then( function() { 130 shareCart.showCopySuccess(); 131 } ).catch( function() { 132 // Keep text selected for manual copy when clipboard API is unavailable or blocked. 133 } ); 134 }, 135 } 136 137 const emptyCart = { 138 popupCore: null, 139 140 init: function() { 141 $document.on( 'click', '.nhg-empty-cart-btn', function( e ) { 142 // Without the popup script the link empties the cart as before. 143 if ( ! window.NHGPopup ) { 144 return; 145 } 146 147 e.preventDefault(); 148 149 if ( ! emptyCart.popupCore ) { 150 emptyCart.popupCore = new window.NHGPopup( { 151 wrapperId: 'nhg-cart-tools-empty-popup-wrap', 152 wrapperClass: 'nhg-cart-tools-empty-popup hidden', 153 contentId: 'nhg-cart-tools-empty-popup-content', 154 } ); 155 } 156 157 emptyCart.popupCore.setContent( $( this ).siblings( '.nhg-empty-cart-confirm' ).html() ); 158 emptyCart.popupCore.open(); 159 } ); 160 }, 161 }; 162 163 $( function() { 164 shareCart.init(); 165 emptyCart.init(); 166 } ); 167 168} )( jQuery );
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.