PageSourceSearch

https://tsz-sp.ru/tools/js/cart.js

js tsz-sp.ru collected 2026-10-09 01:03:22 UTC 4,098 bytes, 139 lines download raw bytes

1$(document).ready(function () {
2
3    var cart = {};
4    cart.products = [];
5
6    if (!localStorage.getItem('cart')) {
7        localStorage.setItem('cart', JSON.stringify(cart));
8    }
9
10
11    $(document).on('click', '.js-buy-btn', function (e) {
12        e.preventDefault();
13        var product = {};
14        product.id = $(this).data('prod-id');
15        product.art = $(this).data('prod-art');
16        product.name = $(this).data('prod-name');
17        product.quantity = parseInt($(this).data('prod-amount') || 1);
18        addToCart(product, $(this));
19        changeNum();
20        updateCart();
21    });
22
23    function addToCart(product, btn) {
24        var cart = JSON.parse(localStorage.getItem('cart'));
25
26        var isUnique = cart.products.filter(item => item.id === product.id)
27        if (isUnique.length === 0) {
28            cart.products.push(product);
29            localStorage.setItem('cart', JSON.stringify(cart));
30        }
31        btn.attr('href', '/cart/');
32        btn.find('span').html('В заказе');
33    }
34
35
36    $(document).on('click', '.js-del-btn', function (e) {
37        e.preventDefault(e);
38        console.log('click del btn')
39        var productId = $(this).data('prod-id');
40        delFromCart(productId);
41        changeNum();
42        updateCart();
43    });
44
45
46    function delFromCart(productId) {
47        var cart = JSON.parse(localStorage.getItem('cart'));
48        cart.products = cart.products.filter(product => product.id !== productId);
49        localStorage.setItem('cart', JSON.stringify(cart));
50    }
51
52
53    (function () {
54
55        $(document).on('click', '.js-quant-plus', function (e) {
56            e.preventDefault();
57            let productId = $(this).data('prod-id');
58
59            changeItemQuantityByBtn(productId, 1);
60            setValues();
61        });
62
63        $(document).on('click', '.js-quant-minus', function (e) {
64            e.preventDefault();
65            let productId = $(this).data('prod-id');
66
67            changeItemQuantityByBtn(productId, -1);
68            setValues();
69        });
70
71        $(document).on('change', '.js-quant-input', function (e) {
72            e.preventDefault();
73            let productId = $(this).data('prod-id');
74            let value = $(this).val();
75            changeItemQuantityByInput(productId, value, $(this));
76            setValues();
77            calculateTotalPrice();
78        });
79
80    })();
81
82    function changeItemQuantityByBtn(productId, changes) {
83        var cart = JSON.parse(localStorage.getItem('cart'));
84        let elem = cart.products.find(product => product.id === productId);
85
86        let set = parseInt(elem.quantity) + changes;
87
88        if (set > 0){
89            cart.products = cart.products.filter(product => product.id !== productId);
90            elem.quantity = set;
91            cart.products.push(elem);
92            localStorage.setItem('cart', JSON.stringify(cart));
93        }
94    }
95
96    function changeItemQuantityByInput(productId, value, input) {
97        let cart = JSON.parse(localStorage.getItem('cart'));
98        let elem = cart.products.find(product => product.id === productId);
99        let filtered = cart.products.filter(product => product.id !== productId);
100        cart.products = filtered;
101
102        if (value >= 1) {
103            elem.quantity = value;
104        } else {
105            elem.quantity = 1;
106            input.val(elem.quantity);
107        }
108
109            cart.products.push(elem);
110            localStorage.setItem('cart', JSON.stringify(cart));
111        }
112
113    function changeNum() {
114        var cart = JSON.parse(localStorage.getItem('cart'));
115        $('.js-cart-num span').html(cart.products.length);
116    }
117
118    changeNum();
119
120    (function () {
121        let $btn = $('.js-buy-btn');
122        var cart = JSON.parse(localStorage.getItem('cart'));
123
124        $btn.each(function () {
125            let isUnique = cart.products.filter(product => product.id === $(this).data('prod-id'));
126
127            if (isUnique.length !== 0) {
128                $(this).attr('href', '/cart/');
129                $(this).find('span').html('В заказе');
130            }
131
132        })
133
134    })();
135
136});
137
138
139

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.