| 1 |
;(function($){ |
| 2 |
"use strict"; |
| 3 |
|
| 4 |
var $body = $('body'), |
| 5 |
$popup = $('.htcompare-popup'); |
| 6 |
|
| 7 |
$body.append(`<div id="htcompare-error-modal" class="htcompare-error-modal" style="display:none"> |
| 8 |
<div class="htcompare-error-modal-content"> |
| 9 |
<button class="htcompare-error-modal-close">×</button> |
| 10 |
<div class="htcompare-error-modal-body"></div> |
| 11 |
</div> |
| 12 |
</div>`); |
| 13 |
const $htcompareErrorModal = $("#htcompare-error-modal"); |
| 14 |
$('.htcompare-error-modal-close').on('click', function() { |
| 15 |
$htcompareErrorModal.css('display', 'none'); |
| 16 |
}) |
| 17 |
$(window).on('click', function(e) { |
| 18 |
if(e.target == $htcompareErrorModal[0]) { |
| 19 |
$htcompareErrorModal.css('display', 'none'); |
| 20 |
} |
| 21 |
}) |
| 22 |
$('a.htcompare-btn').each(function() { |
| 23 |
const $this = $(this); |
| 24 |
if($this.hasClass('added')) { |
| 25 |
$this.html('<span class="htcompare-btn-text">'+$this.data('added-text')+'</span>'); |
| 26 |
} |
| 27 |
}); |
| 28 |
|
| 29 |
// Notification Markup |
| 30 |
const notificationMarkup = `<div class="htcompare-notification"> |
| 31 |
<div class="htcompare-notification-text">${evercompare.option_data.success_added_notification_text}</div> |
| 32 |
<span class="htcompare-notification-close">close</span> |
| 33 |
</div>` |
| 34 |
// Insert Notification Markup in body & notification close method if notification is enabled |
| 35 |
if(evercompare.option_data.enable_success_notification === 'on') { |
| 36 |
$body.append(notificationMarkup); |
| 37 |
$body.on('click', '.htcompare-notification-close', function() { |
| 38 |
$body.find('.htcompare-notification').removeClass('open'); |
| 39 |
}); |
| 40 |
} |
| 41 |
// Notification Show Function |
| 42 |
const ShowNotification = (message) => { |
| 43 |
if(evercompare.option_data.enable_success_notification === 'on') { |
| 44 |
$body.find('.htcompare-notification-text').html(message); |
| 45 |
$body.find('.htcompare-notification').addClass('open'); |
| 46 |
if(+evercompare.option_data.removed_notification_after > -1) { |
| 47 |
setTimeout(function() { |
| 48 |
$body.find('.htcompare-notification').removeClass('open'); |
| 49 |
}, +evercompare.option_data.removed_notification_after * 1000) |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
// Add product in compare table |
| 55 |
$body.on('click', 'a.htcompare-btn', function (e) { |
| 56 |
var $this = $(this), |
| 57 |
id = $this.data('product_id'), |
| 58 |
addedText = $this.data('added-text'), |
| 59 |
success_message = evercompare.option_data.success_added_notification_text.replace('{product_name}', $this.data('product_title')); |
| 60 |
|
| 61 |
if( evercompare.popup === 'yes' && evercompare.option_data.remove_on_click === 'off' ){ |
| 62 |
e.preventDefault(); |
| 63 |
if ($this.hasClass('added') ) { |
| 64 |
$body.find('.htcompare-popup').addClass('open'); |
| 65 |
return true; |
| 66 |
} |
| 67 |
}else{ |
| 68 |
if ( $this.hasClass('added') ) return true; |
| 69 |
} |
| 70 |
|
| 71 |
e.preventDefault(); |
| 72 |
|
| 73 |
$this.addClass('loading'); |
| 74 |
|
| 75 |
$.ajax({ |
| 76 |
url: evercompare.ajaxurl, |
| 77 |
data: { |
| 78 |
action: 'ever_compare_add_to_compare', |
| 79 |
nonce: evercompare.nonce, |
| 80 |
id: id, |
| 81 |
}, |
| 82 |
dataType: 'json', |
| 83 |
method: 'GET', |
| 84 |
success: function ( response ) { |
| 85 |
const $products = typeof response.products === 'array' ? response.products : Object.values(response.products); |
| 86 |
if ( response.table && $products.indexOf(id.toString()) >= 0 ) { |
| 87 |
updateCompareData( response ); |
| 88 |
$popup.addClass('open'); |
| 89 |
} else { |
| 90 |
$('.htcompare-error-modal-body').html(response.limitReached) |
| 91 |
$htcompareErrorModal.css('display', 'flex'); |
| 92 |
console.log( 'Something wrong loading compare data' ); |
| 93 |
} |
| 94 |
$body.find('.htcompare-counter').html( response.count ); |
| 95 |
}, |
| 96 |
error: function ( data ) { |
| 97 |
console.log('Something wrong with AJAX response.'); |
| 98 |
}, |
| 99 |
complete: function (res) { |
| 100 |
$this.removeClass('loading'); |
| 101 |
const $products = typeof res.responseJSON.products === 'array' ? res.responseJSON.products : Object.values(res.responseJSON.products); |
| 102 |
if($products.indexOf(id.toString()) >= 0) { |
| 103 |
$this.addClass('added'); |
| 104 |
$this.html('<span class="htcompare-btn-text">'+addedText+'</span>'); |
| 105 |
} |
| 106 |
ShowNotification(success_message); |
| 107 |
}, |
| 108 |
}); |
| 109 |
|
| 110 |
}); |
| 111 |
|
| 112 |
if(evercompare.option_data.remove_on_click && evercompare.option_data.remove_on_click === 'on') { |
| 113 |
$body.on('click', 'a.htcompare-btn.added', function (e) { |
| 114 |
e.preventDefault(); |
| 115 |
var $this = $(this), |
| 116 |
id = $this.data('product_id'), |
| 117 |
success_message = evercompare.option_data.success_removed_notification_text.replace('{product_name}', $this.data('product_title')); |
| 118 |
$this.addClass('loading'); |
| 119 |
jQuery.ajax({ |
| 120 |
url: evercompare.ajaxurl, |
| 121 |
data: { |
| 122 |
action: 'ever_compare_remove_from_compare', |
| 123 |
nonce: evercompare.nonce, |
| 124 |
id: id, |
| 125 |
}, |
| 126 |
dataType: 'json', |
| 127 |
method: 'GET', |
| 128 |
success: function (response) { |
| 129 |
if (response) { |
| 130 |
$body.find('.htcompare-counter').html( response.count ); |
| 131 |
} else { |
| 132 |
console.log( 'Something wrong loading compare data' ); |
| 133 |
} |
| 134 |
}, |
| 135 |
error: function (data) { |
| 136 |
console.log('Something wrong with AJAX response.'); |
| 137 |
}, |
| 138 |
complete: function () { |
| 139 |
$this.removeClass('loading added').html('<span class="htcompare-btn-text">'+$this.data('text')+'</span>'); |
| 140 |
ShowNotification(success_message); |
| 141 |
}, |
| 142 |
}); |
| 143 |
}); |
| 144 |
} |
| 145 |
|
| 146 |
// Remove data from compare table |
| 147 |
$body.on('click', 'a.htcompare-remove', function (e) { |
| 148 |
var $table = $('.htcompare-table'); |
| 149 |
|
| 150 |
e.preventDefault(); |
| 151 |
var $this = $(this), |
| 152 |
id = $this.data('product_id'), |
| 153 |
success_message = evercompare.option_data.success_removed_notification_text.replace('{product_name}', $this.data('product_title')); |
| 154 |
|
| 155 |
$table.addClass('loading'); |
| 156 |
$this.addClass('loading'); |
| 157 |
|
| 158 |
jQuery.ajax({ |
| 159 |
url: evercompare.ajaxurl, |
| 160 |
data: { |
| 161 |
action: 'ever_compare_remove_from_compare', |
| 162 |
nonce: evercompare.nonce, |
| 163 |
id: id, |
| 164 |
}, |
| 165 |
dataType: 'json', |
| 166 |
method: 'GET', |
| 167 |
success: function (response) { |
| 168 |
if (response.table) { |
| 169 |
updateCompareData(response); |
| 170 |
} else { |
| 171 |
console.log( 'Something wrong loading compare data' ); |
| 172 |
} |
| 173 |
$('a.htcompare-btn').each(function() { |
| 174 |
if($(this).data('product_id') === id) { |
| 175 |
$(this).removeClass('added'); |
| 176 |
$(this).html('<span class="htcompare-btn-text">'+$(this).data('text')+'</span>'); |
| 177 |
} |
| 178 |
}) |
| 179 |
$body.find('.htcompare-counter').html( response.count ); |
| 180 |
}, |
| 181 |
error: function (data) { |
| 182 |
console.log('Something wrong with AJAX response.'); |
| 183 |
}, |
| 184 |
complete: function () { |
| 185 |
$table.removeClass('loading'); |
| 186 |
$this.addClass('loading'); |
| 187 |
ShowNotification(success_message); |
| 188 |
}, |
| 189 |
}); |
| 190 |
|
| 191 |
}); |
| 192 |
|
| 193 |
// Update table HTML |
| 194 |
function updateCompareData( data ) { |
| 195 |
if ( $('.htcompare-table').length > 0 ) { |
| 196 |
$('.htcompare-table').replaceWith( data.table ); |
| 197 |
$('.evercompare-copy-link').on('click',function(e){ |
| 198 |
evercompareCopyToClipboard( $(this).closest('.ever-compare-shareable-link').find('.evercompare-share-link') , this ); |
| 199 |
}); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
// Close popup |
| 204 |
$body.on('click','.htcompare-popup-close', function(e){ |
| 205 |
$(this).parent('.htcompare-popup.open').removeClass('open'); |
| 206 |
$popup.removeClass('open'); |
| 207 |
}); |
| 208 |
|
| 209 |
// Copy Shareable link |
| 210 |
$('.evercompare-copy-link').on('click',function(e){ |
| 211 |
evercompareCopyToClipboard( $(this).closest('.ever-compare-shareable-link').find('.evercompare-share-link') , this ); |
| 212 |
}); |
| 213 |
function evercompareCopyToClipboard( element, button ) { |
| 214 |
var $tempdata = $("<input>"); |
| 215 |
$("body").append($tempdata); |
| 216 |
$tempdata.val($(element).text()).select(); |
| 217 |
document.execCommand("copy"); |
| 218 |
$tempdata.remove(); |
| 219 |
$(button).text( $(button).data('copytext') ); |
| 220 |
setTimeout(function() { |
| 221 |
$( button ).text( $(button).data('btntext') ); |
| 222 |
}, 1000); |
| 223 |
} |
| 224 |
|
| 225 |
})(jQuery); |