PluginProbe
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder / 51.1.78
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder v51.1.78
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.78, at includes/wishlist/assets/script.js

188 lines 6.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 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 = settings.labels?.add || '';
25 const labelAdded = 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
33 const $label = $button.find('.king-addons-wishlist-button__label');
34 if ($label.length) {
35 $label.text(isAdded ? labelAdded : labelDefault);
36 }
37 };
38
39 const updateCounters = (count) => {
40 $('.king-addons-wishlist-counter').each(function () {
41 const $counter = $(this);
42 $counter.attr('data-count', count);
43 $counter.text(count);
44 });
45 };
46
47 const handleToggle = ($button) => {
48 const productId = parseInt($button.data('product-id'), 10);
49 const wishlistId = $button.data('wishlist-id') || '';
50 const variationId = getVariationId($button);
51
52 if (!productId) {
53 return;
54 }
55
56 $button.prop('disabled', true);
57
58 $.post(settings.ajaxUrl, {
59 action: 'king_addons_wishlist_toggle',
60 nonce: settings.nonce,
61 product_id: productId,
62 variation_id: variationId,
63 wishlist_id: wishlistId,
64 })
65 .done((response) => {
66 if (response.success && response.data) {
67 updateButtonState($button, response.data.state);
68 if (response.data.count !== undefined) {
69 updateCounters(response.data.count);
70 }
71
72 if (response.data.state === 'default' && $button.hasClass('king-addons-wishlist-button--table-remove')) {
73 $button.closest('tr').remove();
74 }
75 } else if (response?.data?.message) {
76 alert(response.data.message);
77 } else {
78 alert(settings.labels?.error || 'Error');
79 }
80 })
81 .fail((xhr) => {
82 if (xhr?.responseJSON?.data?.message) {
83 alert(xhr.responseJSON.data.message);
84 } else {
85 alert(settings.labels?.error || 'Error');
86 }
87 })
88 .always(() => {
89 $button.prop('disabled', false);
90 });
91 };
92
93 $(document).on('click', '.king-addons-wishlist-button', function (event) {
94 event.preventDefault();
95 handleToggle($(this));
96 });
97
98 // Notes functionality (Pro)
99 const handleNoteSave = ($container) => {
100 const productId = parseInt($container.data('product-id'), 10);
101 const variationId = parseInt($container.data('variation-id'), 10) || 0;
102 const wishlistId = $container.data('wishlist-id') || '';
103 const note = $container.find('.king-addons-wishlist-notes__input').val().trim();
104
105 const $saveBtn = $container.find('.king-addons-wishlist-notes__save');
106 $saveBtn.prop('disabled', true).text('...');
107
108 $.post(settings.ajaxUrl, {
109 action: 'king_addons_wishlist_update_note',
110 nonce: settings.nonce,
111 product_id: productId,
112 variation_id: variationId,
113 wishlist_id: wishlistId,
114 note: note,
115 })
116 .done((response) => {
117 if (response.success) {
118 const $display = $container.find('.king-addons-wishlist-notes__display');
119 const $form = $container.find('.king-addons-wishlist-notes__form');
120 const $text = $container.find('.king-addons-wishlist-notes__text');
121
122 $text.text(note);
123
124 if (note) {
125 $display.show();
126 $form.hide();
127 } else {
128 $display.hide();
129 $form.show();
130 }
131 } else if (response?.data?.message) {
132 alert(response.data.message);
133 }
134 })
135 .fail((xhr) => {
136 if (xhr?.responseJSON?.data?.message) {
137 alert(xhr.responseJSON.data.message);
138 } else {
139 alert(settings.labels?.error || 'Error');
140 }
141 })
142 .always(() => {
143 $saveBtn.prop('disabled', false).text('Save');
144 });
145 };
146
147 // Edit note button click
148 $(document).on('click', '.king-addons-wishlist-notes__edit-btn', function (event) {
149 event.preventDefault();
150 const $container = $(this).closest('.king-addons-wishlist-notes');
151 $container.find('.king-addons-wishlist-notes__display').hide();
152 $container.find('.king-addons-wishlist-notes__form').show();
153 $container.find('.king-addons-wishlist-notes__input').focus();
154 });
155
156 // Save note button click
157 $(document).on('click', '.king-addons-wishlist-notes__save', function (event) {
158 event.preventDefault();
159 const $container = $(this).closest('.king-addons-wishlist-notes');
160 handleNoteSave($container);
161 });
162
163 // Cancel note edit
164 $(document).on('click', '.king-addons-wishlist-notes__cancel', function (event) {
165 event.preventDefault();
166 const $container = $(this).closest('.king-addons-wishlist-notes');
167 const $display = $container.find('.king-addons-wishlist-notes__display');
168 const $form = $container.find('.king-addons-wishlist-notes__form');
169 const originalNote = $display.find('.king-addons-wishlist-notes__text').text();
170
171 $container.find('.king-addons-wishlist-notes__input').val(originalNote);
172 $display.show();
173 $form.hide();
174 });
175
176 // Submit on Enter key
177 $(document).on('keydown', '.king-addons-wishlist-notes__input', function (event) {
178 if (event.key === 'Enter' && !event.shiftKey) {
179 event.preventDefault();
180 const $container = $(this).closest('.king-addons-wishlist-notes');
181 handleNoteSave($container);
182 }
183 });
184 })(jQuery);
185
186
187
188