PluginProbe
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder / 51.1.87
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder v51.1.87
51.1.87 51.1.86 51.1.84 51.1.85 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 All 41 releases
king-addons / includes / wishlist / assets / script.js

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

230 lines 8.4 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 if (response.data.page_html) {
112 $('.king-addons-wishlist-page').each(function () {
113 const $next = $(response.data.page_html);
114 $(this).replaceWith($next);
115 });
116 }
117 } else if (response.data && response.data.message) {
118 alert(response.data.message);
119 } else {
120 alert(settings.labels?.error || 'Error');
121 }
122 })
123 .fail((xhr) => {
124 if (xhr?.responseJSON?.data?.message) {
125 alert(xhr.responseJSON.data.message);
126 } else {
127 alert(settings.labels?.error || 'Error');
128 }
129 })
130 .always(() => {
131 $button.prop('disabled', false);
132 });
133 };
134
135 $(document).on('click', '.king-addons-wishlist-button', function (event) {
136 event.preventDefault();
137 handleToggle($(this));
138 });
139
140 // Notes functionality (Pro)
141 const handleNoteSave = ($container) => {
142 const productId = parseInt($container.data('product-id'), 10);
143 const variationId = parseInt($container.data('variation-id'), 10) || 0;
144 const wishlistId = $container.data('wishlist-id') || '';
145 const note = $container.find('.king-addons-wishlist-notes__input').val().trim();
146
147 const $saveBtn = $container.find('.king-addons-wishlist-notes__save');
148 $saveBtn.prop('disabled', true).text('...');
149
150 $.post(settings.ajaxUrl, {
151 action: 'king_addons_wishlist_update_note',
152 nonce: settings.nonce,
153 product_id: productId,
154 variation_id: variationId,
155 wishlist_id: wishlistId,
156 note: note,
157 })
158 .done((response) => {
159 if (response.success) {
160 const $display = $container.find('.king-addons-wishlist-notes__display');
161 const $form = $container.find('.king-addons-wishlist-notes__form');
162 const $text = $container.find('.king-addons-wishlist-notes__text');
163
164 $text.text(note);
165
166 if (note) {
167 $display.show();
168 $form.hide();
169 } else {
170 $display.hide();
171 $form.show();
172 }
173 } else if (response?.data?.message) {
174 alert(response.data.message);
175 }
176 })
177 .fail((xhr) => {
178 if (xhr?.responseJSON?.data?.message) {
179 alert(xhr.responseJSON.data.message);
180 } else {
181 alert(settings.labels?.error || 'Error');
182 }
183 })
184 .always(() => {
185 $saveBtn.prop('disabled', false).text('Save');
186 });
187 };
188
189 // Edit note button click
190 $(document).on('click', '.king-addons-wishlist-notes__edit-btn', function (event) {
191 event.preventDefault();
192 const $container = $(this).closest('.king-addons-wishlist-notes');
193 $container.find('.king-addons-wishlist-notes__display').hide();
194 $container.find('.king-addons-wishlist-notes__form').show();
195 $container.find('.king-addons-wishlist-notes__input').focus();
196 });
197
198 // Save note button click
199 $(document).on('click', '.king-addons-wishlist-notes__save', function (event) {
200 event.preventDefault();
201 const $container = $(this).closest('.king-addons-wishlist-notes');
202 handleNoteSave($container);
203 });
204
205 // Cancel note edit
206 $(document).on('click', '.king-addons-wishlist-notes__cancel', function (event) {
207 event.preventDefault();
208 const $container = $(this).closest('.king-addons-wishlist-notes');
209 const $display = $container.find('.king-addons-wishlist-notes__display');
210 const $form = $container.find('.king-addons-wishlist-notes__form');
211 const originalNote = $display.find('.king-addons-wishlist-notes__text').text();
212
213 $container.find('.king-addons-wishlist-notes__input').val(originalNote);
214 $display.show();
215 $form.hide();
216 });
217
218 // Submit on Enter key
219 $(document).on('keydown', '.king-addons-wishlist-notes__input', function (event) {
220 if (event.key === 'Enter' && !event.shiftKey) {
221 event.preventDefault();
222 const $container = $(this).closest('.king-addons-wishlist-notes');
223 handleNoteSave($container);
224 }
225 });
226 })(jQuery);
227
228
229
230