PluginProbe
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder / 51.1.83
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder v51.1.83
51.1.83 51.1.82 51.1.81 51.1.79 51.1.78 51.1.77 51.1.76 51.1.74 51.1.75 51.1.65 51.1.64 51.1.63 trunk 51.1.14 51.1.2 51.1.35 51.1.36 51.1.37 51.1.38 51.1.39 51.1.44 51.1.45 51.1.46 51.1.47 51.1.49 All 37 releases
king-addons / includes / wishlist / assets / script.js

script.js in King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder 51.1.83, at includes/wishlist/assets/script.js

224 lines 8.1 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 const settings = window.kingAddonsWishlist || {};
5
6 const getVariationId = ($button) => {
7 const explicit = parseInt($button.data('variation-id'), 10);
8 if (explicit) {
9 return explicit;
10 }
11
12 const $form = $button.closest('form.variations_form');
13 if ($form.length) {
14 const picked = parseInt($form.find('input.variation_id').val(), 10);
15 if (!isNaN(picked) && picked > 0) {
16 return picked;
17 }
18 }
19
20 return 0;
21 };
22
23 const updateButtonState = ($button, state) => {
24 const labelDefault = $button.attr('data-label-default') || settings.labels?.add || '';
25 const labelAdded = $button.attr('data-label-added') || settings.labels?.added || '';
26 const isAdded = state === 'added';
27
28 $button.attr('data-state', isAdded ? 'added' : 'default');
29 $button.attr('aria-pressed', isAdded ? 'true' : 'false');
30 $button.toggleClass('king-addons-wishlist-button--added', isAdded);
31 $button.toggleClass('king-addons-wishlist-button--default', !isAdded);
32 $button.attr('aria-label', isAdded ? labelAdded : labelDefault);
33
34 const $label = $button.find('.king-addons-wishlist-button__label');
35 if ($label.length) {
36 $label.text(isAdded ? labelAdded : labelDefault);
37 }
38 };
39
40 const updateCounters = (count) => {
41 $('.king-addons-wishlist-counter').each(function () {
42 const $counter = $(this);
43 $counter.attr('data-count', count);
44 $counter.text(count);
45 });
46 };
47
48 const restoreEmptyRows = () => {
49 $('.king-addons-wishlist-table tbody').each(function () {
50 const $tbody = $(this);
51 if ($tbody.find('.king-addons-wishlist-table__row').length) {
52 return;
53 }
54 if ($tbody.find('.king-addons-wishlist-table__empty').length) {
55 return;
56 }
57 const cols = $tbody.closest('table').find('thead th').length || 1;
58 $tbody.append(
59 '<tr><td colspan="' + cols + '" class="king-addons-wishlist-table__empty">' +
60 (settings.labels?.empty || 'No products in wishlist yet.') +
61 '</td></tr>'
62 );
63 });
64 };
65
66 const syncButtons = (productId, variationId, state) => {
67 $('.king-addons-wishlist-button').each(function () {
68 const $btn = $(this);
69 const pid = parseInt($btn.attr('data-product-id'), 10);
70 const vid = parseInt($btn.attr('data-variation-id'), 10) || 0;
71 if (pid !== productId || vid !== variationId) {
72 return;
73 }
74 if ($btn.hasClass('king-addons-wishlist-button--table-remove')) {
75 if (state === 'default') {
76 $btn.closest('tr').remove();
77 }
78 return;
79 }
80 updateButtonState($btn, state);
81 });
82 if (state === 'default') {
83 restoreEmptyRows();
84 }
85 };
86
87 const handleToggle = ($button) => {
88 const productId = parseInt($button.data('product-id'), 10);
89 const wishlistId = $button.data('wishlist-id') || '';
90 const variationId = getVariationId($button);
91
92 if (!productId) {
93 return;
94 }
95
96 $button.prop('disabled', true);
97
98 $.post(settings.ajaxUrl, {
99 action: 'king_addons_wishlist_toggle',
100 nonce: settings.nonce,
101 product_id: productId,
102 variation_id: variationId,
103 wishlist_id: wishlistId,
104 })
105 .done((response) => {
106 if (response.success && response.data) {
107 syncButtons(productId, variationId, response.data.state);
108 if (response.data.count !== undefined) {
109 updateCounters(response.data.count);
110 }
111 } else if (response?.data?.message) {
112 alert(response.data.message);
113 } else {
114 alert(settings.labels?.error || 'Error');
115 }
116 })
117 .fail((xhr) => {
118 if (xhr?.responseJSON?.data?.message) {
119 alert(xhr.responseJSON.data.message);
120 } else {
121 alert(settings.labels?.error || 'Error');
122 }
123 })
124 .always(() => {
125 $button.prop('disabled', false);
126 });
127 };
128
129 $(document).on('click', '.king-addons-wishlist-button', function (event) {
130 event.preventDefault();
131 handleToggle($(this));
132 });
133
134 // Notes functionality (Pro)
135 const handleNoteSave = ($container) => {
136 const productId = parseInt($container.data('product-id'), 10);
137 const variationId = parseInt($container.data('variation-id'), 10) || 0;
138 const wishlistId = $container.data('wishlist-id') || '';
139 const note = $container.find('.king-addons-wishlist-notes__input').val().trim();
140
141 const $saveBtn = $container.find('.king-addons-wishlist-notes__save');
142 $saveBtn.prop('disabled', true).text('...');
143
144 $.post(settings.ajaxUrl, {
145 action: 'king_addons_wishlist_update_note',
146 nonce: settings.nonce,
147 product_id: productId,
148 variation_id: variationId,
149 wishlist_id: wishlistId,
150 note: note,
151 })
152 .done((response) => {
153 if (response.success) {
154 const $display = $container.find('.king-addons-wishlist-notes__display');
155 const $form = $container.find('.king-addons-wishlist-notes__form');
156 const $text = $container.find('.king-addons-wishlist-notes__text');
157
158 $text.text(note);
159
160 if (note) {
161 $display.show();
162 $form.hide();
163 } else {
164 $display.hide();
165 $form.show();
166 }
167 } else if (response?.data?.message) {
168 alert(response.data.message);
169 }
170 })
171 .fail((xhr) => {
172 if (xhr?.responseJSON?.data?.message) {
173 alert(xhr.responseJSON.data.message);
174 } else {
175 alert(settings.labels?.error || 'Error');
176 }
177 })
178 .always(() => {
179 $saveBtn.prop('disabled', false).text('Save');
180 });
181 };
182
183 // Edit note button click
184 $(document).on('click', '.king-addons-wishlist-notes__edit-btn', function (event) {
185 event.preventDefault();
186 const $container = $(this).closest('.king-addons-wishlist-notes');
187 $container.find('.king-addons-wishlist-notes__display').hide();
188 $container.find('.king-addons-wishlist-notes__form').show();
189 $container.find('.king-addons-wishlist-notes__input').focus();
190 });
191
192 // Save note button click
193 $(document).on('click', '.king-addons-wishlist-notes__save', function (event) {
194 event.preventDefault();
195 const $container = $(this).closest('.king-addons-wishlist-notes');
196 handleNoteSave($container);
197 });
198
199 // Cancel note edit
200 $(document).on('click', '.king-addons-wishlist-notes__cancel', function (event) {
201 event.preventDefault();
202 const $container = $(this).closest('.king-addons-wishlist-notes');
203 const $display = $container.find('.king-addons-wishlist-notes__display');
204 const $form = $container.find('.king-addons-wishlist-notes__form');
205 const originalNote = $display.find('.king-addons-wishlist-notes__text').text();
206
207 $container.find('.king-addons-wishlist-notes__input').val(originalNote);
208 $display.show();
209 $form.hide();
210 });
211
212 // Submit on Enter key
213 $(document).on('keydown', '.king-addons-wishlist-notes__input', function (event) {
214 if (event.key === 'Enter' && !event.shiftKey) {
215 event.preventDefault();
216 const $container = $(this).closest('.king-addons-wishlist-notes');
217 handleNoteSave($container);
218 }
219 });
220 })(jQuery);
221
222
223
224