PluginProbe
Ever Compare – Products Compare Plugin for WooCommerce / 1.2.5
Ever Compare – Products Compare Plugin for WooCommerce v1.2.5
1.3.7 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 trunk 1.0.0 1.0.1 1.0.2 All 31 releases
ever-compare / assets / js / frontend.js

frontend.js in Ever Compare – Products Compare Plugin for WooCommerce 1.2.5, at assets/js/frontend.js

159 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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">&times;</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 // Add product in compare table
30 $body.on('click', 'a.htcompare-btn', function (e) {
31 var $this = $(this),
32 id = $this.data('product_id'),
33 addedText = $this.data('added-text');
34
35 if( evercompare.popup === 'yes' ){
36 e.preventDefault();
37 if ( $this.hasClass('added') ) {
38 $body.find('.htcompare-popup').addClass('open');
39 return true;
40 }
41 }else{
42 if ( $this.hasClass('added') ) return true;
43 }
44
45 e.preventDefault();
46
47 $this.addClass('loading');
48
49 $.ajax({
50 url: evercompare.ajaxurl,
51 data: {
52 action: 'ever_compare_add_to_compare',
53 id: id,
54 },
55 dataType: 'json',
56 method: 'GET',
57 success: function ( response ) {
58 const $products = typeof response.products === 'array' ? response.products : Object.values(response.products);
59 if ( response.table && $products.indexOf(id.toString()) >= 0 ) {
60 updateCompareData( response );
61 $popup.addClass('open');
62 } else {
63 $('.htcompare-error-modal-body').html(response.limitReached)
64 $htcompareErrorModal.css('display', 'flex');
65 console.log( 'Something wrong loading compare data' );
66 }
67 $body.find('.htcompare-counter').html( response.count );
68 },
69 error: function ( data ) {
70 console.log('Something wrong with AJAX response.');
71 },
72 complete: function (res) {
73 $this.removeClass('loading');
74 const $products = typeof res.responseJSON.products === 'array' ? res.responseJSON.products : Object.values(res.responseJSON.products);
75 if($products.indexOf(id.toString()) >= 0) {
76 $this.addClass('added');
77 $this.html('<span class="htcompare-btn-text">'+addedText+'</span>');
78 }
79 },
80 });
81
82 });
83
84 // Remove data from compare table
85 $body.on('click', 'a.htcompare-remove', function (e) {
86 var $table = $('.htcompare-table');
87
88 e.preventDefault();
89 var $this = $(this),
90 id = $this.data('product_id');
91
92 $table.addClass('loading');
93 $this.addClass('loading');
94
95 jQuery.ajax({
96 url: evercompare.ajaxurl,
97 data: {
98 action: 'ever_compare_remove_from_compare',
99 id: id,
100 },
101 dataType: 'json',
102 method: 'GET',
103 success: function (response) {
104 if (response.table) {
105 updateCompareData(response);
106 } else {
107 console.log( 'Something wrong loading compare data' );
108 }
109 $('a.htcompare-btn').each(function() {
110 if($(this).data('product_id') === id) {
111 $(this).removeClass('added');
112 $(this).html('<span class="htcompare-btn-text">'+$(this).data('text')+'</span>');
113 }
114 })
115 $body.find('.htcompare-counter').html( response.count );
116 },
117 error: function (data) {
118 console.log('Something wrong with AJAX response.');
119 },
120 complete: function () {
121 $table.removeClass('loading');
122 $this.addClass('loading');
123 },
124 });
125
126 });
127
128 // Update table HTML
129 function updateCompareData( data ) {
130 if ( $('.htcompare-table').length > 0 ) {
131 $('.htcompare-table').replaceWith( data.table );
132 $('.evercompare-copy-link').on('click',function(e){
133 evercompareCopyToClipboard( $(this).closest('.ever-compare-shareable-link').find('.evercompare-share-link') , this );
134 });
135 }
136 }
137
138 // Close popup
139 $body.on('click','.htcompare-popup-close', function(e){
140 $popup.removeClass('open');
141 });
142
143 // Copy Shareable link
144 $('.evercompare-copy-link').on('click',function(e){
145 evercompareCopyToClipboard( $(this).closest('.ever-compare-shareable-link').find('.evercompare-share-link') , this );
146 });
147 function evercompareCopyToClipboard( element, button ) {
148 var $tempdata = $("<input>");
149 $("body").append($tempdata);
150 $tempdata.val($(element).text()).select();
151 document.execCommand("copy");
152 $tempdata.remove();
153 $(button).text( $(button).data('copytext') );
154 setTimeout(function() {
155 $( button ).text( $(button).data('btntext') );
156 }, 1000);
157 }
158
159 })(jQuery);