| 1 |
;(function($){ |
| 2 |
"use strict"; |
| 3 |
|
| 4 |
var $body = $('body'), |
| 5 |
$popup = $('.htcompare-popup'); |
| 6 |
|
| 7 |
// Add product in compare table |
| 8 |
$body.on('click', 'a.htcompare-btn', function (e) { |
| 9 |
var $this = $(this), |
| 10 |
id = $this.data('product_id'), |
| 11 |
addedText = $this.data('added-text'); |
| 12 |
|
| 13 |
if( evercompare.popup === 'yes' ){ |
| 14 |
e.preventDefault(); |
| 15 |
if ( $this.hasClass('added') ) { |
| 16 |
$body.find('.htcompare-popup').addClass('open'); |
| 17 |
return true; |
| 18 |
} |
| 19 |
}else{ |
| 20 |
if ( $this.hasClass('added') ) return true; |
| 21 |
} |
| 22 |
|
| 23 |
e.preventDefault(); |
| 24 |
|
| 25 |
$this.addClass('loading'); |
| 26 |
|
| 27 |
$.ajax({ |
| 28 |
url: evercompare.ajaxurl, |
| 29 |
data: { |
| 30 |
action: 'ever_compare_add_to_compare', |
| 31 |
id: id, |
| 32 |
}, |
| 33 |
dataType: 'json', |
| 34 |
method: 'GET', |
| 35 |
success: function ( response ) { |
| 36 |
if ( response.table ) { |
| 37 |
updateCompareData( response ); |
| 38 |
$popup.addClass('open'); |
| 39 |
} else { |
| 40 |
console.log( 'Something wrong loading compare data' ); |
| 41 |
} |
| 42 |
}, |
| 43 |
error: function ( data ) { |
| 44 |
console.log('Something wrong with AJAX response.'); |
| 45 |
}, |
| 46 |
complete: function () { |
| 47 |
$this.removeClass('loading').addClass('added'); |
| 48 |
$this.html('<span class="htcompare-btn-text">'+addedText+'</span>'); |
| 49 |
}, |
| 50 |
}); |
| 51 |
|
| 52 |
}); |
| 53 |
|
| 54 |
// Remove data from compare table |
| 55 |
$body.on('click', 'a.htcompare-remove', function (e) { |
| 56 |
var $table = $('.htcompare-table'); |
| 57 |
|
| 58 |
e.preventDefault(); |
| 59 |
var $this = $(this), |
| 60 |
id = $this.data('product_id'); |
| 61 |
|
| 62 |
$table.addClass('loading'); |
| 63 |
$this.addClass('loading'); |
| 64 |
|
| 65 |
jQuery.ajax({ |
| 66 |
url: evercompare.ajaxurl, |
| 67 |
data: { |
| 68 |
action: 'ever_compare_remove_from_compare', |
| 69 |
id: id, |
| 70 |
}, |
| 71 |
dataType: 'json', |
| 72 |
method: 'GET', |
| 73 |
success: function (response) { |
| 74 |
if (response.table) { |
| 75 |
updateCompareData(response); |
| 76 |
} else { |
| 77 |
console.log( 'Something wrong loading compare data' ); |
| 78 |
} |
| 79 |
}, |
| 80 |
error: function (data) { |
| 81 |
console.log('Something wrong with AJAX response.'); |
| 82 |
}, |
| 83 |
complete: function () { |
| 84 |
$table.removeClass('loading'); |
| 85 |
$this.addClass('loading'); |
| 86 |
}, |
| 87 |
}); |
| 88 |
|
| 89 |
}); |
| 90 |
|
| 91 |
// Update table HTML |
| 92 |
function updateCompareData( data ) { |
| 93 |
if ( $('.htcompare-table').length > 0 ) { |
| 94 |
$('.htcompare-table').replaceWith( data.table ); |
| 95 |
$('.evercompare-copy-link').on('click',function(e){ |
| 96 |
evercompareCopyToClipboard( $(this).closest('.ever-compare-shareable-link').find('.evercompare-share-link') , this ); |
| 97 |
}); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
// Close popup |
| 102 |
$body.on('click','.htcompare-popup-close', function(e){ |
| 103 |
$popup.removeClass('open'); |
| 104 |
}); |
| 105 |
|
| 106 |
// Copy Shareable link |
| 107 |
$('.evercompare-copy-link').on('click',function(e){ |
| 108 |
evercompareCopyToClipboard( $(this).closest('.ever-compare-shareable-link').find('.evercompare-share-link') , this ); |
| 109 |
}); |
| 110 |
function evercompareCopyToClipboard( element, button ) { |
| 111 |
var $tempdata = $("<input>"); |
| 112 |
$("body").append($tempdata); |
| 113 |
$tempdata.val($(element).text()).select(); |
| 114 |
document.execCommand("copy"); |
| 115 |
$tempdata.remove(); |
| 116 |
$(button).text( $(button).data('copytext') ); |
| 117 |
setTimeout(function() { |
| 118 |
$( button ).text( $(button).data('btntext') ); |
| 119 |
}, 1000); |
| 120 |
} |
| 121 |
|
| 122 |
})(jQuery); |