[FIX] website_sale: image doesn't change when the user select a product (for product...
[odoo/odoo.git] / addons / website_sale_options / static / src / js / website_sale.js
1 $(document).ready(function () {
2     $('.oe_website_sale #add_to_cart, .oe_website_sale #products_grid .a-submit')
3         .off('click')
4         .removeClass('a-submit')
5         .click(function (event) {
6             var $form = $(this).closest('form');
7             var quantity = parseFloat($form.find('input[name="add_qty"]').val() || 1);
8             var product_id = parseInt($form.find('input[type="hidden"][name="product_id"], input[type="radio"][name="product_id"]:checked').first().val(),10);
9             event.preventDefault();
10             openerp.jsonRpc("/shop/modal", 'call', {
11                     'product_id': product_id,
12                     kwargs: {
13                        context: openerp.website.get_context()
14                     },
15                 }).then(function (modal) {
16                     var $modal = $(modal);
17
18                     $modal.find('img:first').attr("src", "/website/image/product.product/" + product_id + "/image");
19
20                     $modal.appendTo($form)
21                         .modal()
22                         .on('hidden.bs.modal', function () {
23                             $(this).remove();
24                         });
25
26                     $modal.on('click', '.a-submit', function () {
27                         var $a = $(this);
28                         $form.ajaxSubmit({
29                             url:  '/shop/cart/update_option',
30                             success: function (quantity) {
31                                 if (!$a.hasClass('js_goto_shop')) {
32                                     window.location.href = window.location.href.replace(/shop([\/?].*)?$/, "shop/cart");
33                                 }
34                                 var $q = $(".my_cart_quantity");
35                                 $q.parent().parent().removeClass("hidden", !quantity);
36                                 $q.html(quantity).hide().fadeIn(600);
37                             }
38                         });
39                         $modal.modal('hide');
40                     });
41
42                     $modal.on("click", "a.js_add, a.js_remove", function (event) {
43                         event.preventDefault();
44                         var $parent = $(this).parents('.js_product:first');
45                         $parent.find("a.js_add, span.js_remove").toggleClass("hidden");
46                         $parent.find("input.js_optional_same_quantity").val( $(this).hasClass("js_add") ? 1 : 0 );
47                         var $remove = $parent.find(".js_remove");
48                     });
49
50                     $modal.on("change", "input.js_quantity", function (event) {
51                         var qty = parseFloat($(this).val());
52                         if (qty === 1) {
53                             $(".js_remove .js_items").addClass("hidden");
54                             $(".js_remove .js_item").removeClass("hidden");
55                         } else {
56                             $(".js_remove .js_items").removeClass("hidden").text($(".js_remove .js_items").text().replace(/[0-9.,]+/, qty));
57                             $(".js_remove .js_item").addClass("hidden");
58                         }
59                     });
60
61                     $modal.find('input[name="add_qty"]').val(quantity).change();
62                     $('ul.js_add_cart_variants').each(function () {
63                         $('input.js_variant_change, select.js_variant_change', this).first().trigger('change');
64                     });
65                 });
66             return false;
67         });
68
69 });