Debian Codegraph @

addons/website_sale/static/src/js/website_sale_utils.js

raw symbols SCIP
1 import { markup } from '@odoo/owl';
2 import { browser } from '@web/core/browser/browser';
3 import { _t } from '@web/core/l10n/translation';
4 import { setElementContent } from '@web/core/utils/html';
5
6 function animateClone($cart, $elem, offsetTop, offsetLeft) {
7 if (!$cart.length) {
8 return Promise.resolve();
9 }
10 $cart.removeClass('d-none').find('.o_animate_blink').addClass('o_red_highlight o_shadow_animation').delay(500).queue(function () {
11 $(this).removeClass("o_shadow_animation").dequeue();
12 }).delay(2000).queue(function () {
13 $(this).removeClass("o_red_highlight").dequeue();
14 });
15 return new Promise(function (resolve, reject) {
16 if(!$elem) resolve();
17 var $imgtodrag = $elem.find('img').eq(0);
18 if ($imgtodrag.length) {
19 var $imgclone = $imgtodrag.clone()
20 .offset({
21 top: $imgtodrag.offset().top,
22 left: $imgtodrag.offset().left
23 })
24 .removeClass()
25 .addClass('o_website_sale_animate')
26 .appendTo(document.body)
27 .css({
28 // Keep the same size on cloned img.
29 width: $imgtodrag.width(),
30 height: $imgtodrag.height(),
31 })
32 .animate({
33 top: $cart.offset().top + offsetTop,
34 left: $cart.offset().left + offsetLeft,
35 width: 75,
36 height: 75,
37 }, 500);
38
39 $imgclone.animate({
40 width: 0,
41 height: 0,
42 }, function () {
43 resolve();
44 $(this).detach();
45 });
46 } else {
47 resolve();
48 }
49 });
50 }
51
52 /**
53 * Returns the closest product form to a given element if exists.
54 * Required for product pages with full-width or no images where the "Add to cart" button can be
55 * outside of the form.
56 *
57 * @param { HTMLElement } element - Reference to an HTML element in the DOM.
58 * @returns { HTMLFormElement|undefined }
59 */
60 function getClosestProductForm(element){
61 return element.closest('form') ?? element.closest('.js_product')?.querySelector('form');
62 }
63
64 /**
65 * Updates both navbar cart
66 * @param {Object} data
67 * @return {void}
68 */
69 function updateCartNavBar(data) {
70 browser.sessionStorage.setItem('website_sale_cart_quantity', data.cart_quantity);
71 // Mobile and Desktop elements have to be updated.
72 const cartQuantityElements = document.querySelectorAll('.my_cart_quantity');
73 for(const cartQuantityElement of cartQuantityElements) {
74 if (data.cart_quantity === 0) {
75 cartQuantityElement.classList.add('d-none');
76 } else {
77 const cartIconElement = document.querySelector('li.o_wsale_my_cart');
78 cartIconElement.classList.remove('d-none');
79 cartQuantityElement.classList.remove('d-none');
80 cartQuantityElement.classList.add('o_mycart_zoom_animation');
81 setTimeout(() => {
82 cartQuantityElement.textContent = data.cart_quantity;
83 cartQuantityElement.classList.remove('o_mycart_zoom_animation');
84 }, 300);
85 }
86 }
87
88 $(".js_cart_lines").first().before(data['website_sale.cart_lines']).end().remove();
89
90 updateCartSummary(data);
91
92 // Adjust the cart's left column width to accommodate the cart summary (right column). The left
93 // column of an empty cart initially takes the full width, but adding products (e.g. via quick
94 // reorder) enables the cart summary on the right.
95 document.querySelector('.oe_cart').classList.toggle('col-lg-7', !!data.cart_quantity);
96
97 if (data.cart_ready) {
98 document.querySelector("a[name='website_sale_main_button']")?.classList.remove('disabled');
99 } else {
100 document.querySelector("a[name='website_sale_main_button']")?.classList.add('disabled');
101 }
102 }
103
104 /**
105 * Update the cart summary.
106 *
107 * @param {Object} data
108 * @return {void}
109 */
110 function updateCartSummary(data) {
111 if (data['website_sale.shorter_cart_summary']) {
112 const shorterCartSummaryEl = document.querySelector('.o_wsale_shorter_cart_summary');
113 setElementContent(shorterCartSummaryEl, markup(data['website_sale.shorter_cart_summary']));
114 }
115 if (data['website_sale.total']) {
116 document.querySelectorAll('div.o_cart_total').forEach(
117 div => div.innerHTML = data['website_sale.total']
118 );
119 }
120 }
121
122 /**
123 * Update the quick reorder side panel.
124 *
125 * @param {Object} data
126 * @return {void}
127 */
128 function updateQuickReorderSidebar(data) {
129 const quickReorderButton = document.getElementById('quick_reorder_button');
130 document.querySelectorAll('.o_wsale_quick_reorder_line_group').forEach(el => el.remove());
131 if (data['website_sale.quick_reorder_history'].trim()) {
132 document.querySelector('#quick_reorder_sidebar .offcanvas-body').insertAdjacentHTML(
133 'afterbegin', data['website_sale.quick_reorder_history']
134 );
135 quickReorderButton.removeAttribute('disabled');
136 quickReorderButton.parentElement.title = "";
137 } else {
138 quickReorderButton.click();
139 quickReorderButton.setAttribute('disabled', 'true');
140 quickReorderButton.parentElement.title = _t("No previous products available for reorder.");
141 }
142 }
143
144 /**
145 * Displays `message` in an alert box at the top of the page if it's a
146 * non-empty string.
147 *
148 * @param {string | null} message
149 */
150 function showWarning(message) {
151 if (!message) {
152 return;
153 }
154 var $page = $('.oe_website_sale');
155 var cart_alert = $page.children('#data_warning');
156 if (!cart_alert.length) {
157 cart_alert = $(
158 '<div class="alert alert-danger alert-dismissible" role="alert" id="data_warning">' +
159 '<button type="button" class="btn-close" data-bs-dismiss="alert"></button> ' +
160 '<span></span>' +
161 '</div>').prependTo($page);
162 }
163 cart_alert.children('span:last-child').text(message);
164 }
165
166 /**
167 * Return the selected attribute values from the given container.
168 *
169 * @param {Element} container the container to look into
170 */
171 function getSelectedAttributeValues(container) {
172 return Array.from(container.querySelectorAll(
173 'input.js_variant_change:checked, select.js_variant_change'
174 )).map(el => parseInt(el.value));
175 }
176
177 export default {
178 animateClone: animateClone,
179 getClosestProductForm: getClosestProductForm,
180 updateCartNavBar: updateCartNavBar,
181 showWarning: showWarning,
182 getSelectedAttributeValues: getSelectedAttributeValues,
183 updateQuickReorderSidebar: updateQuickReorderSidebar,
184 };