PageSourceSearch

https://www.professioneled.com/modules/relatedproducts/views/js/related_products.js

js professioneled.com collected 2026-09-26 11:04:46 UTC 11,331 bytes, 345 lines download raw bytes

1/**
2 * NOTICE OF LICENSE
3 *
4 * This source file is subject to the Open Software License (OSL 3.0)
5 * that is bundled with this package in the file LICENSE.txt.
6 * It is also available through the world-wide-web at this URL:
7 * http://opensource.org/licenses/osl-3.0.php
8 *
9 * @author    FMM Modules
10 * @copyright Copyright 2021 © FMM Modules
11 * @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
12 */
13
14$(function() {
15    var selected_products = $(".relproductcheckbox:checked");
16    if (selected_products.length > 0) {
17        selected_products.each(function(e) {
18            var button = $(this).closest(".rp_select_button").get(0);
19            addRelatedProducts($(this).data("price"), button, false);
20        });
21    } else {
22        $("#relProductsPriceBlock").hide();
23    }
24
25    if (rp_view == "grid") {
26        initCarousel();
27
28        if (
29            (ps_version === 0 && current_page == "orderopc") ||
30            current_page == "order"
31        ) {
32            //if cart updates - ps 1.6
33            $(
34                ".cart_quantity_delete, .cart_quantity_input, .cart_quantity_down, .cart_quantity_up"
35            ).on("click change", function() {
36                setTimeout(function() {
37                    initCarousel();
38                }, 600);
39            });
40        }
41    }
42});
43
44$(document).on("click", ".add_related_products", addRelatedToCart);
45
46function addRelatedProducts(related_price, button, checked, key) {
47    var _checkBox = $(button).children("input.input");
48    if (ps_version === 0) {
49        _checkBox = $(button).find("input");
50    }
51    if (_checkBox.prop("checked")) {
52        _checkBox.removeAttr("checked");
53    } else {
54        _checkBox.prop("checked", true);
55    }
56    if (typeof checked === "undefined" || typeof checked === "null") {
57        checked = true;
58    }
59
60    if (checked) {
61        _checkBox.attr("checked", _checkBox.is(":checked"));
62    }
63    if (_checkBox.prop("checked")) {
64        _checkBox.closest(".item").find(".image_overlay").addClass("selected");
65        $(button).removeClass("btn-info").addClass("btn-success");
66    } else {
67        _checkBox.closest(".item").find(".image_overlay").removeClass("selected");
68        $(button).removeClass("btn-success").addClass("btn-info");
69    }
70
71    related_price = parseFloat(related_price);
72    var objRp = $("#related_base_price" + key);
73    pPrice = objRp.val();
74    pPrice = parseFloat(pPrice);
75    if (_checkBox.prop("checked")) {
76        related_price = ps_round(pPrice + related_price);
77        _checkBox.parent().addClass("rel_product_checked");
78    } else {
79        related_price = ps_round(pPrice - related_price);
80        related_price = related_price < 0.0 ? 0.0 : related_price;
81        _checkBox.parent().removeClass("rel_product_checked");
82    }
83
84    if (related_price > 0.0) {
85        $("#relProductsPriceBlock" + key).show();
86    } else {
87        $("#relProductsPriceBlock" + key).hide();
88    }
89    objRp.val(related_price);
90    objRp
91        .parent()
92        .find("span")
93        .text(currency_sign + related_price);
94}
95
96function addRelatedToCart() {
97    var selected_products = $(".relproductcheckbox:checked");
98    if (selected_products.length <= 0) {
99        alert(unselectedmessage);
100    } else {
101        $(".add_related_products").attr("disabled", true);
102        var success = [];
103        selected_products.each(function(e) {
104            var item = $(this);
105            e = e + 1;
106            setTimeout(function() {
107                item
108                    .closest(".item")
109                    .find(".image_overlay")
110                    .toggleClass(function() {
111                        if ($(this).hasClass("selected")) {
112                            $(this).removeClass("selected");
113                            return "rp_loader";
114                        }
115                    });
116            }, 500 * e);
117
118            if (ps_version) {
119                success.push(
120                    ajaxCustomCart(item.val(), item.data("ipa"),prestashop.urls.pages.cart)
121                );
122            } else {
123                if (selected_products.length > e) {
124                    success.push(ajaxCustomCart(item.val(), item.data("ipa"), cart_link));
125                }
126                //ajaxCart.add(item.val(), item.data('ipa'), true, null, 1, null);
127            }
128            setTimeout(function() {
129                item
130                    .closest(".item")
131                    .find(".image_overlay")
132                    .toggleClass(function() {
133                        if ($(this).hasClass("rp_loader")) {
134                            $(this).removeClass("rp_loader");
135                            return "selected";
136                        }
137                    });
138
139                if (selected_products.length === e) {
140                    if (ps_version) {
141                        if (success.length && $.inArray(false, success) === -1) {
142                            prestashop.emit("updateCart", {
143                                reason: {
144                                    idProduct: item.val(),
145                                    idProductAttribute: item.data("ipa"),
146                                    idCustomization: 0,
147                                    linkAction: "add-to-cart",
148                                },
149                                resp: {},
150                            });
151                            //location.reload();
152                        }
153                    } else {
154                        ajaxCart.add(item.val(), item.data("ipa"), true, null, 1, null);
155                        if (current_page == "orderopc" || current_page == "order") {
156                            $(".layer_cart_cart")
157                                .find(".continue")
158                                .on("click", function() {
159                                    location.reload(true);
160                                });
161                        }
162                    }
163                }
164            }, 800 * e);
165        });
166        $(".add_related_products").removeAttr("disabled");
167    }
168}
169
170function ajaxCustomCart(idProduct, idCombination, actionUrl) {
171    if (parseInt(idProduct)) {
172        var jsonData = {
173            type: "POST",
174            headers: { "cache-control": "no-cache" },
175            url: actionUrl,
176            async: false,
177            cache: false,
178            dataType: "json",
179            data: {
180                add: 1,
181                qty: 1,
182                ajax: true,
183                action: "update",
184                controller: "cart",
185                token: static_token,
186                id_product: idProduct,
187                ipa: parseInt(idCombination) && idCombination != null ?
188                    parseInt(idCombination) :
189                    0,
190            },
191            success: function(response) {
192                if (typeof response !== "undefined" && response) {
193                    if (response.errors) {
194                        alert(response.errors);
195                    }
196                }
197            },
198            error: function(XMLHttpRequest, textStatus, errorThrown) {
199                alert(textStatus + "<br>" + errorThrown);
200            },
201        };
202        //send the ajax request to the server
203        var xhr = $.ajax(jsonData);
204        if (
205            typeof xhr !== "undefined" &&
206            xhr &&
207            typeof xhr.responseJSON !== "undefined" &&
208            xhr.responseJSON
209        ) {
210            if (xhr.responseJSON.success) {
211                return true;
212            } else {
213                return false;
214            }
215        } else {
216            return false;
217        }
218    }
219}
220
221function initCarousel() {
222    var owl = $(".owl-related-products-block");
223    owl.owlCarousel({
224        // Most important owl features
225        items: 4,
226        itemsCustom: false,
227        itemsDesktop: [1199, 4],
228        itemsDesktopSmall: [980, 3],
229        itemsTablet: [768, 2],
230        itemsTabletSmall: false,
231        itemsMobile: [479, 1],
232        singleItem: false,
233        itemsScaleUp: false,
234        //Basic Speeds
235        slideSpeed: 200,
236        paginationSpeed: 800,
237        rewindSpeed: 1000,
238        //Autoplay
239        autoPlay: false,
240        stopOnHover: false,
241        // Navigation
242        navigation: true,
243        navigationText: ["prev", "next"],
244        navigationText: false,
245        rewindNav: true,
246        scrollPerPage: false,
247        // Responsive
248        responsive: true,
249        responsiveRefreshRate: 200,
250        responsiveBaseWidth: window,
251        //Mouse Events
252        dragBeforeAnimFinish: true,
253        mouseDrag: true,
254        touchDrag: true,
255    });
256}
257
258function ps_round(value, places) {
259    if (typeof roundMode === "undefined") {
260        roundMode = 2;
261    }
262    if (typeof places === "undefined") {
263        places = 2;
264    }
265
266    var method = roundMode;
267
268    if (method === 0) {
269        return ceilf(value, places);
270    } else if (method === 1) {
271        return floorf(value, places);
272    } else if (method === 2) {
273        return ps_round_half_up(value, places);
274    } else if (method == 3 || method == 4 || method == 5) {
275        // From PHP Math.c
276        var precision_places = 14 - Math.floor(ps_log10(Math.abs(value)));
277        var f1 = Math.pow(10, Math.abs(places));
278
279        if (precision_places > places && precision_places - places < 15) {
280            var f2 = Math.pow(10, Math.abs(precision_places));
281            if (precision_places >= 0) {
282                tmp_value = value * f2;
283            } else {
284                tmp_value = value / f2;
285            }
286
287            tmp_value = ps_round_helper(tmp_value, roundMode);
288
289            /* now correctly move the decimal point */
290            f2 = Math.pow(10, Math.abs(places - precision_places));
291            /* because places < precision_places */
292            tmp_value /= f2;
293        } else {
294            /* adjust the value */
295            if (places >= 0) tmp_value = value * f1;
296            else tmp_value = value / f1;
297
298            if (Math.abs(tmp_value) >= 1e15) return value;
299        }
300
301        tmp_value = ps_round_helper(tmp_value, roundMode);
302        if (places > 0) {
303            tmp_value = tmp_value / f1;
304        } else {
305            tmp_value = tmp_value * f1;
306        }
307        return tmp_value;
308    }
309}
310
311function ps_round_helper(value, mode) {
312    // From PHP Math.c
313    if (value >= 0.0) {
314        tmp_value = Math.floor(value + 0.5);
315        if (
316            (mode == 3 && value == -0.5 + tmp_value) ||
317            (mode == 4 && value == 0.5 + 2 * Math.floor(tmp_value / 2.0)) ||
318            (mode == 5 && value == 0.5 + 2 * Math.floor(tmp_value / 2.0) - 1.0)
319        )
320            tmp_value -= 1.0;
321    } else {
322        tmp_value = Math.ceil(value - 0.5);
323        if (
324            (mode == 3 && value == 0.5 + tmp_value) ||
325            (mode == 4 && value == -0.5 + 2 * Math.ceil(tmp_value / 2.0)) ||
326            (mode == 5 && value == -0.5 + 2 * Math.ceil(tmp_value / 2.0) + 1.0)
327        )
328            tmp_value += 1.0;
329    }
330    return tmp_value;
331}
332
333function ps_round_half_up(value, precision) {
334    var mul = Math.pow(10, precision);
335    var val = value * mul;
336
337    var next_digit = Math.floor(val * 10) - 10 * Math.floor(val);
338    if (next_digit >= 5) {
339        val = Math.ceil(val);
340    } else {
341        val = Math.floor(val);
342    }
343
344    return val / mul;
345}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.