1( function( $ ) { 2 'use strict'; 3 4 /** 5 * @typedef {Object} RedPartsSputnikWishlistButtonState 6 * @property {string} label 7 * @property {string} icon 8 */ 9 10 /** 11 * @typedef {Object} RedPartsSputnikWishlistButtonDef 12 * @property {RedPartsSputnikWishlistButtonState} added 13 */ 14 15 /** 16 * @typedef {Object} RedPartsSputnikWishlistVars 17 * @property {string} ajaxUrl 18 * @property {string} pageUrl 19 * @property {string} nonceAdd 20 * @property {string} nonceRemove 21 * @property {RedPartsSputnikWishlistButtonDef} button 22 */ 23 24 /** 25 * @member {RedPartsSputnikWishlistVars} redPartsSputnikWishlistVars 26 */ 27 28 /** 29 * @type {RedPartsSputnikWishlistVars} 30 */ 31 const vars = window.redPartsSputnikWishlistVars; 32 // noinspection DuplicatedCode 33 const wishlist = window.redPartsSputnikWishlist = { 34 queue: ( function() { 35 const d = $.Deferred(); 36 37 d.resolve(); 38 39 return d.promise(); 40 }() ), 41 ajax: function( ajaxBuilder ) { 42 /** @type {XMLHttpRequest} */ 43 let xhr; 44 let aborted = false; 45 46 const abort = function() { 47 aborted = true; 48 49 if ( xhr ) { 50 xhr.abort(); 51 } 52 }; 53 54 wishlist.queue = wishlist.queue.then( function() { 55 const d = $.Deferred(); 56 57 if ( aborted ) { 58 d.resolve(); 59 return; 60 } 61 62 xhr = ajaxBuilder().always( function() { 63 d.resolve(); 64 } ); 65 66 return d.promise(); 67 } ); 68 69 return abort; 70 }, 71 add: function( productId, success ) { 72 success = success || function() {}; 73 74 return wishlist.ajax( function() { 75 return $.ajax( { 76 type: 'post', 77 url: vars.ajaxUrl, 78 dataType: 'json', 79 data: { 80 action: 'redparts_sputnik_wishlist_add', 81 nonce: vars.nonceAdd, 82 product_id: productId, 83 }, 84 success: function( response ) { 85 success( response ); 86 87 if ( response.success ) { 88 Object.keys( response.fragments ).forEach( function( selector ) { 89 $( selector ).replaceWith( response.fragments[ selector ] ); 90 } ); 91 } 92 }, 93 } ); 94 } ); 95 }, 96 remove: function( productId, success ) { 97 success = success || function() {}; 98 99 return wishlist.ajax( function() { 100 return $.ajax( { 101 type: 'post', 102 url: vars.ajaxUrl, 103 dataType: 'json', 104 data: { 105 action: 'redparts_sputnik_wishlist_remove', 106 nonce: vars.nonceRemove, 107 product_id: productId, 108 }, 109 success: function( response ) { 110 success( response ); 111 112 if ( response.success ) { 113 Object.keys( response.fragments ).forEach( function( selector ) { 114 $( selector ).replaceWith( response.fragments[ selector ] ); 115 } ); 116 } 117 }, 118 } ); 119 } ); 120 }, 121 /** 122 * @param {MouseEvent} event 123 */ 124 onAddClick: function( event ) { 125 if ( ! event.cancelable ) { 126 return; 127 } 128 129 const button = $( this ); 130 const productId = button.data( 'product-id' ); 131 132 if ( ! button.is( '.th-wishlist-add--added' ) ) { 133 event.preventDefault(); 134 } 135 136 if ( button.is( '.th-wishlist-add--added' ) ) { 137 138 } else if ( button.is( '.th-wishlist-add--loading' ) ) { 139 const abort = button.data( 'redparts-wishlist-abort' ); 140 141 if ( abort ) { 142 abort(); 143 } 144 145 button.removeClass( 'th-wishlist-add--loading' ); 146 } else { 147 const abort = wishlist.add( productId, function() { 148 button.removeClass( 'th-wishlist-add--loading' ); 149 button.addClass( 'th-wishlist-add--added' ); 150 button.find( '.th-wishlist-add__label' ).text( vars.button.added.label ); 151 button.find( '.th-wishlist-add__icon' ).html( vars.button.added.icon ); 152 button.attr( 'href', vars.pageUrl ); 153 } ); 154 155 button.addClass( 'th-wishlist-add--loading' ); 156 button.data( 'redparts-wishlist-abort', abort ); 157 } 158 }, 159 /** 160 * @param {MouseEvent} event 161 */ 162 onRemoveClick: function( event ) { 163 if ( ! event.cancelable ) { 164 return; 165 } 166 167 event.preventDefault(); 168 169 const button = $( this ); 170 const productId = button.data( 'product-id' ); 171 172 if ( button.is( '.th-button--loading' ) ) { 173 return; 174 } 175 176 button.addClass( 'th-button--loading' ); 177 178 wishlist.remove( productId, function() { 179 button.removeClass( 'th-button--loading' ); 180 } ); 181 }, 182 }; 183 184 $( document ).on( 'click', '.th-wishlist-add', wishlist.onAddClick ); 185 $( document ).on( 'click', '.th-wishlist-remove', wishlist.onRemoveClick ); 186}( 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.