PageSourceSearch

https://compbest.com.ua/vendor/widgets/ajax_cart.js

js compbest.com.ua collected 2026-10-05 11:12:49 UTC 17,114 bytes, 489 lines download raw bytes

1var AjaxCart = function (selector, skin, events) {
2    var me = this;
3    this.skin = skin || 'default';
4    var currentEvents = {
5        onChange: function () {
6            me.update();
7        },
8        onProductRemove: function (id, hash) {
9            var container = me.container.find('#product_' + hash);
10            if (!container.is('.j-remove-container')) {
11                container = container.parents('.j-remove-container:first');
12            }
13            if (me.products.length > 1) {
14                container.hide();
15            }
16            me.reloadHtml();
17            me.update();
18        },
19        onCouponChange: function (status, response) {
20            if (status !== this.STATUS_COUPON_REJECTED) {
21                me.reloadHtml();
22                me.update();
23            } else {
24                me.coupon_code.errorBox(response.message);
25            }
26        },
27        onReload : function () {
28            me.reloadHtml();
29        },
30        onBeforeProductAdd: function (id, type) {
31            if (me.Cart.getProductById(id, type) === null) {
32                me.loader.show();
33            }
34        },
35        onProductAdd: function () {
36            me.initialized = false;
37
38            me.reloadHtml();
39        },
40        onInit: function () {
41            me.init();
42        },
43        onError: function (errors) {
44            for (var i = 0, l = errors.length; i < l; ++i) {
45                var error = errors[i];
46//
47//            if(error.code == Cart.MAX_QUANTITY_REACHED) {
48//                $('.j-cart-item-' + error.additional.id).errorBox('Максимальное количество: ' + error.additional.quantity, 2500);
49//
50//            }
51            }
52        }
53    };
54
55    Cart.AJAX_LINK = GLOBAL.URI_PREFIX + Cart.AJAX_LINK.substr(1);
56    this.Cart = new Cart(AjaxCart.useSession, currentEvents, GLOBAL.currency.id, AjaxCart.id);
57
58    this.Cart.attachEventHandlers(events);
59
60    this.container = $(selector);
61
62    this.initialized = false;
63    this.interval = null;
64    this.products = [];
65    this.init();
66};
67
68AjaxCart.selector = "#cart";
69AjaxCart.skin = "default";
70AjaxCart.quantity_mask = "{quantity} {products} {on}";
71AjaxCart.total_sum_wrapper = "";
72AjaxCart.product_sum_wrapper = "";
73AjaxCart.remove_product_on_zero = false;
74AjaxCart.events = {};
75
76AjaxCart.instance = null;
77AjaxCart.useSession = true;
78AjaxCart.id = undefined;
79/**
80 *
81 * @returns {AjaxCart}
82 */
83AjaxCart.getInstance = function () {
84    if (this.instance === null) {
85        this.instance = new AjaxCart(this.selector, this.skin, this.events);
86    }
87
88    return this.instance;
89};
90
91
92AjaxCart.prototype = {
93    init: function () {
94        var me = this;
95
96//        var interval = setInterval(function () {
97//            if (me.Cart.initialized) {
98//        clearInterval(interval);
99        me.loader = me.container.find('.j-cart-loader').hide();
100        me.total_quantity = me.container.find('.j-total-quantity');
101        me.total_p_sum = me.container.find('.j-total-p-sum');
102        me.discount_percent = me.container.find('.j-discount-percent');
103        me.discount_value = me.container.find('.j-discount-value');
104        me.total_sum = me.container.find('.j-total-sum');
105
106        me.coupon_code = me.container.find(".j-coupon-input");
107
108        me.coupon_link = me.container.find('.j-coupon-add');
109        me.coupon_none = me.container.find('.j-coupon-add-container');
110        me.coupon_field = me.container.find('.j-coupon-add-form');
111        me.coupon_remove = me.container.find('.j-coupon-remove');
112        me.coupon_submit = me.container.find('.j-coupon-submit');
113        me.coupon_cancel = me.container.find('.j-coupon-cancel');
114        me.coupon_discount_value = me.container.find('.j-coupon-discount-value');
115        me.coupon_discount_tooltip = me.container.find('.j-coupon-discount-tooltip');
116
117        me.initialized = true;
118        me.initProducts();
119
120
121        me.coupon_link.off('click').on('click', function () {
122            me.coupon_none.hide();
123            me.coupon_field.show();
124            me.coupon_code.focus();
125
126            return false;
127        });
128
129        me.coupon_remove.off('click').on('click', function () {
130
131            if (confirm("Удалить купон?")) {
132                me.setCouponCode("", me.skin);
133                me.coupon_none.show();
134                me.coupon_field.hide();
135            }
136
137            return false;
138        });
139
140        me.coupon_cancel.off('click').on('click', function () {
141            me.coupon_field.val('');
142            me.coupon_none.show();
143            me.coupon_field.hide();
144
145            return false;
146        });
147
148        var coupon_code_change = function () {
149            me.coupon_code.errorBox();
150            if ($.trim(me.coupon_code.val()) !== "") {
151                me.setCouponCode(me.coupon_code.val(), me.skin);
152            }
153
154            return false;
155        };
156
157        me.coupon_code.off('change, keydown').on('keydown', function (e) {
158            if (e.which == 13) {
159                coupon_code_change();
160                return false;
161            }
162
163            return true;
164        });
165        me.coupon_submit.off('click').on('click', coupon_code_change);
166        me.initialized = true;
167
168        me.initManagerMarkup();
169        me.initCustomDeliveryPrice();
170        me.initProducts();
171    },
172
173    initCustomDeliveryPrice: function () {
174        var me = this;
175
176        this.delivery_price = this.container.find('.j-delivery-price');
177        this.custom_delivery_price_edit = this.container.find('.j-custom-delivery-price-edit');
178        this.custom_delivery_price_container = this.container.find('.j-custom-delivery-price-container');
179        this.custom_delivery_price_input = this.custom_delivery_price_container.find('.j-custom-delivery-price-input');
180        this.custom_delivery_price_cancel = this.custom_delivery_price_container.find('.j-custom-delivery-price-cancel');
181
182        this.custom_delivery_price_edit.off('click').on('click', function () {
183            me.Cart.setCustomDeliveryPrice(me.custom_delivery_price_input.val());
184            me.delivery_price.hide();
185            me.custom_delivery_price_edit.hide();
186            me.custom_delivery_price_container.show();
187            me.custom_delivery_price_input.focus();
188            return false;
189        });
190
191        this.custom_delivery_price_input.off('change').on('change', function () {
192            me.Cart.setCustomDeliveryPrice($(this).val());
193        });
194
195        this.custom_delivery_price_cancel.off('click').on('click', function () {
196            me.Cart.setCustomDeliveryPrice(-1);
197
198            me.delivery_price.show();
199            me.custom_delivery_price_edit.show();
200            me.custom_delivery_price_container.hide();
201        });
202    },
203
204    initManagerMarkup: function () {
205        var me = this;
206        this.markup_add = this.container.find('.j-markup-add');
207        this.markup_name = this.container.find('.j-markup-name');
208        this.markup_price = this.container.find('.j-markup-price');
209        this.markup_price_container = this.container.find('.j-markup-price-container');
210
211        this.markup_name.off('change').on('change', function () {
212            me.updateMarkup();
213        });
214
215        this.markup_price.numberMask({type: 'float', allowNegative: true}).off('change').on('change', function () {
216            me.updateMarkup();
217        });
218
219        this.markup_add.off('click').on('click', function () {
220            me.markup_add.hide();
221            me.markup_name.show().focus();
222            me.markup_price_container.show();
223            return false;
224        });
225    },
226
227    updateMarkup: function () {
228        var me = this;
229        this.Cart.setMarkup(this.markup_name.val(), this.markup_price.val(), function () {
230            me.markup_name.val(me.Cart.manager_markup.markup_name);
231            me.markup_price.val(me.Cart.manager_markup.markup_price);
232        });
233    },
234
235    updateObject: function (object) {
236        var product = this.getProductByHash(object.hash);
237        if (product) {
238            if (object.quantity.is(':input')) {
239                object.quantity.val(product.quantity);
240            } else {
241                object.quantity.html(product.quantity);
242            }
243
244            object.sum.html(this.priceFormat(product.price * product.quantity, AjaxCart.product_sum_wrapper));
245            object.sum_old.html(this.priceFormat(product.cprice.initial_price * product.quantity, AjaxCart.product_sum_wrapper));
246            if (product.quantity == 1 && !AjaxCart.remove_product_on_zero) {
247                object.decrease.addClass('__disabled');
248            } else {
249                object.decrease.removeClass('__disabled');
250            }
251            if (product.quantity == product.max_quantity) {
252                object.increase.addClass('__disabled');
253            } else {
254                object.increase.removeClass('__disabled');
255            }
256        }
257    },
258
259    reloadHtml: function () {
260        if (this.container.length !== 0) {
261            var me = this;
262            this.Cart.ajax('render/' + this.skin, {}, function (status, response) {
263                me.loader.hide();
264                if (status === 'OK') {
265                    me.container.empty().append($(response.html).html());
266                    me.init();
267                    me.Cart.trigger('onReloadHtml');
268                }
269            });
270        }
271    },
272
273    /**
274     * @param {Object} events
275     */
276    attachEventHandlers: function (events) {
277        this.Cart.attachEventHandlers(events);
278    },
279
280    /**
281     * @param {String} eventName
282     * @param {Function} callback
283     */
284    attachEventHandler: function (eventName, callback) {
285        this.Cart.attachEventHandler(eventName, callback);
286    },
287
288    trigger : function (fName) {
289        return this.Cart.trigger.apply(this.Cart, arguments);
290    },
291
292    initProducts: function () {
293        var me = this;
294        this.products = [];
295
296
297        this.container.find('.j-cart-product').each(function () {
298            var obj = $(this),
299                hash = obj.attr('id').substr("product_".length),
300                remove = obj.find('.j-remove-p'),
301                sum = obj.find('.j-sum-p'),
302                sum_old = obj.find('.j-sum-old-p'),
303                decrease = obj.find('.j-decrease-p'),
304                increase = obj.find('.j-increase-p'),
305                quantity = obj.find('.j-quantity-p'),
306                quantity_reached = obj.find('.j-quantity-reached-message');
307
308            var product_object = {
309                hash: hash,
310                container: obj,
311                sum: sum,
312                sum_old: sum_old,
313                remove: remove,
314                decrease: decrease,
315                increase: increase,
316                quantity: quantity
317            };
318
319            me.products.push(product_object);
320
321            remove.
322                off('click.AjaxCart').
323                on('click.AjaxCart', function () {
324                    if (me.trigger('onBeforeProductRemove', product_object) !== false) {
325                        me.removeProductByHash(hash);
326                        me.trigger('onProductChange', hash);
327                    }
328                    return false;
329                });
330            decrease.
331                off('click.AjaxCart').
332                on('click.AjaxCart', function () {
333                    quantity_reached.hide().stop();
334                    var c_quantity = quantity.val() * 1;//me.getProductByHash(hash).quantity;
335
336                    if (--c_quantity > 0) {
337                        me.Cart.setProductQuantityByHash(hash, c_quantity);
338                        me.trigger('onProductChange', hash, c_quantity);
339//                        me.updateObject(product_object);
340                        me.update();
341                    } else if (AjaxCart.remove_product_on_zero && me.trigger('onBeforeProductRemove', product_object) !== false) {
342                        me.removeProductByHash(hash);
343                        me.trigger('onProductChange', hash);
344                    }
345
346                    return false;
347                });
348
349            increase.
350                off('click.AjaxCart').
351                on('click.AjaxCart', function () {
352                    quantity_reached.hide().stop();
353                    var c_quantity = quantity.val() * 1,
354                        product = me.getProductByHash(hash);//me.getProductByHash(hash).quantity;
355
356                    if (product) {
357                        //alert(me.getProductByHash(hash).max_quantity);
358                        if (++c_quantity <= product.max_quantity) {
359                            me.Cart.setProductQuantityByHash(hash, c_quantity);
360//                        me.updateObject(product_object);
361                            me.update();
362                            me.trigger('onProductChange', hash, c_quantity);
363                        } else {
364                            quantity_reached.stop().show().delay(3000).fadeOut('fast');
365                        }
366                    }
367                    return false;
368                });
369
370            quantity.
371                numberMask({type: 'int'}).
372                off('change.AjaxCart').
373                on('change.AjaxCart', function () {
374                    quantity_reached.hide().stop();
375                    var quantity = $(this).val() * 1,
376                        product = me.getProductByHash(hash);
377
378                    if ($(this).val() === 'more') {
379                        var input = $(this).parents('.select').hide().siblings('input').show();
380
381                        quantity = +input.data('start-quantity');
382
383                        input.val(quantity);
384                        me.Cart.setProductQuantityByHash(hash, quantity);
385                        me.update();
386
387                        return false;
388                    }
389
390                    if (product) {
391                        if (quantity <= 0) {
392                            quantity = 1;
393                        } else if (quantity > product.max_quantity) {
394                            quantity_reached.show().stop().delay(3000).fadeOut('fast');
395                            quantity = product.max_quantity;
396                        }
397                        me.Cart.setProductQuantityByHash(hash, quantity);
398                        me.trigger('onProductChange', hash, quantity);
399                        me.update();
400                    }
401
402
403//                    me.updateObject(product_object);
404
405                    return false;
406                });
407
408            me.updateObject(product_object);
409
410        });
411
412    },
413
414    update: function () {
415        var products = this.products,
416            total = this.Cart.total,
417            discount = this.Cart.discount,
418            coupon_data = this.Cart.coupon_data;
419
420        var total_p_sum = 0;
421
422        //AjaxCart.quantity_mask
423        var quantity_text = str_replace (
424            ['{quantity}', '{products}', '{on}'],
425            [total.quantity, getCountText(total.quantity), l10n("на")],
426            AjaxCart.quantity_mask
427        );
428
429        this.total_quantity.html(quantity_text);
430        this.discount_percent.html(discount.discount_percent);
431        this.discount_value.html(discount.discount_value > 0 ? '&ndash; ' + this.priceFormat(discount.discount_value) : this.priceFormat(0));
432        this.total_sum.html(this.priceFormat(total.sum, AjaxCart.total_sum_wrapper));
433
434        for (var i = 0, length = products.length; i < length; ++i) {
435            var product = products[i],
436                data = this.getProductByHash(product.hash);
437            if (data) {
438                total_p_sum += data.quantity * data.price;
439                this.updateObject(product);
440            }
441        }
442
443        this.total_p_sum.html(this.priceFormat(total_p_sum, AjaxCart.product_sum_wrapper));
444        this.coupon_discount_value.html(coupon_data.sum > 0 ? '&ndash; ' + this.priceFormat(coupon_data.sum, AjaxCart.product_sum_wrapper) : this.priceFormat(0));
445        coupon_data.showTooltip ? this.coupon_discount_tooltip.show() : this.coupon_discount_tooltip.hide();
446    },
447
448    priceFormat: function (price, abbr_wrapper) {
449        abbr_wrapper = abbr_wrapper || '';
450        return currency.wrap_html(number_format(price, GLOBAL.round_to, '.', ' '), abbr_wrapper);
451    },
452
453    appendProduct: function (product, related) {
454        this.Cart.appendProduct(product, related);
455    },
456
457    appendProducts: function (products) {
458        this.Cart.appendProducts(products);
459    },
460
461    getProductByHash: function (hash) {
462        return this.Cart.getProductByHash(hash);
463    },
464
465    getProductById: function (id) {
466        return this.Cart.getProductById(id);
467    },
468
469    removeProductByHash: function (hash) {
470        this.Cart.removeProductByHash(hash);
471    },
472
473    setCouponCode: function (code, skin) {
474        this.Cart.setCouponCode(code, skin);
475    },
476
477    getProductObjByHash: function (hash) {
478        for (var i = 0, l = this.products.length; i < l; ++i) {
479            var product = this.products[i];
480            if (product.hash == hash) {
481                return product;
482            }
483        }
484
485        return null;
486    }
487
488
489};

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.