d700ef25cec16be9404dbe0c970204612d4012ec
[odoo/odoo.git] / addons / website_sale / static / src / js / website_sale.js
1 $(document).ready(function () {
2 $('.oe_website_sale').each(function () {
3     var oe_website_sale = this;
4
5     var $shippingDifferent = $("select[name='shipping_id']", oe_website_sale);
6     $shippingDifferent.change(function (event) {
7         var value = +$shippingDifferent.val();
8         var data = $shippingDifferent.find("option:selected").data();
9         var $snipping = $(".js_shipping", oe_website_sale);
10         var $inputs = $snipping.find("input");
11         var $selects = $snipping.find("select");
12
13         $snipping.toggle(!!value);
14         $inputs.attr("readonly", value <= 0 ? null : "readonly" ).prop("readonly", value <= 0 ? null : "readonly" );
15         $selects.attr("disabled", value <= 0 ? null : "disabled" ).prop("disabled", value <= 0 ? null : "disabled" );
16
17         $inputs.each(function () {
18             $(this).val( data[$(this).attr("name")] || "" );
19         });
20     });
21
22     // change for css
23     $(oe_website_sale).on('mouseup touchend', '.js_publish', function (ev) {
24         $(ev.currentTarget).parents(".thumbnail").toggleClass("disabled");
25     });
26
27     $(oe_website_sale).on("change", ".oe_cart input.js_quantity", function () {
28         var $input = $(this);
29         var value = parseInt($input.val(), 10);
30         var line_id = parseInt($input.data('line-id'),10);
31         if (isNaN(value)) value = 0;
32         openerp.jsonRpc("/shop/cart/update_json", 'call', {
33             'line_id': line_id,
34             'product_id': parseInt($input.data('product-id'),10),
35             'set_qty': value})
36             .then(function (data) {
37                 if (!data.quantity) {
38                     location.reload();
39                     return;
40                 }
41                 var $q = $(".my_cart_quantity");
42                 $q.parent().parent().removeClass("hidden", !data.quantity);
43                 $q.html(data.cart_quantity).hide().fadeIn(600);
44
45                 $input.val(data.quantity);
46                 $('.js_quantity[data-line-id='+line_id+']').val(data.quantity).html(data.quantity);
47                 $("#cart_total").replaceWith(data['website_sale.total']);
48             });
49     });
50
51     // hack to add and rome from cart with json
52     $(oe_website_sale).on('click', 'a.js_add_cart_json', function (ev) {
53         ev.preventDefault();
54         var $link = $(ev.currentTarget);
55         var $input = $link.parent().parent().find("input");
56         var min = parseFloat($input.data("min") || 0);
57         var max = parseFloat($input.data("max") || Infinity);
58         var quantity = ($link.has(".fa-minus").length ? -1 : 1) + parseFloat($input.val(),10);
59         $input.val(quantity > min ? (quantity < max ? quantity : max) : min);
60         $('input[name="'+$input.attr("name")+'"]').val(quantity > min ? (quantity < max ? quantity : max) : min);
61         $input.change();
62         return false;
63     });
64
65     $('.oe_website_sale .a-submit, #comment .a-submit').off('click').on('click', function () {
66         $(this).closest('form').submit();
67     });
68     $('form.js_attributes input, form.js_attributes select', oe_website_sale).on('change', function () {
69         $(this).closest("form").submit();
70     });
71
72     // change price when they are variants
73     $('form.js_add_cart_json label', oe_website_sale).on('mouseup touchend', function (ev) {
74         var $label = $(this);
75         var $price = $label.parents("form:first").find(".oe_price .oe_currency_value");
76         if (!$price.data("price")) {
77             $price.data("price", parseFloat($price.text()));
78         }
79         var value = $price.data("price") + parseFloat($label.find(".badge span").text() || 0);
80         var dec = value % 1;
81         $price.html(value + (dec < 0.01 ? ".00" : (dec < 1 ? "0" : "") ));
82     });
83     // hightlight selected color
84     $('.css_attribute_color input', oe_website_sale).on('change', function (ev) {
85         $('.css_attribute_color').removeClass("active");
86         $('.css_attribute_color:has(input:checked)').addClass("active");
87     });
88
89     function price_to_str(price) {
90         price = Math.round(price * 100) / 100;
91         var dec = Math.round((price % 1) * 100);
92         return price + (dec ? '' : '.0') + (dec%10 ? '' : '0');
93     }
94
95     $(oe_website_sale).on('change', 'input.js_product_change', function (ev) {
96         var $parent = $(this).closest('.js_product');
97         $parent.find(".oe_default_price:first .oe_currency_value").html( price_to_str(+$(this).data('lst_price')) );
98         $parent.find(".oe_price:first .oe_currency_value").html(price_to_str(+$(this).data('price')) );
99
100         var $img = $(this).closest('tr.js_product, .oe_website_sale').find('span[data-oe-model^="product."][data-oe-type="image"] img:first, img.product_detail_img');
101         $img.attr("src", "/website/image/product.product/" + $(this).val() + "/image");
102     });
103
104     $(oe_website_sale).on('change', 'input.js_variant_change, select.js_variant_change', function (ev) {
105         var $ul = $(this).parents('ul.js_add_cart_variants:first');
106         var $parent = $ul.closest('.js_product');
107         var $product_id = $parent.find('input.product_id').first();
108         var $price = $parent.find(".oe_price:first .oe_currency_value");
109         var $default_price = $parent.find(".oe_default_price:first .oe_currency_value");
110         var variant_ids = $ul.data("attribute_value_ids");
111         var values = [];
112         $parent.find('input.js_variant_change:checked, select.js_variant_change').each(function () {
113             values.push(+$(this).val());
114         });
115
116         $parent.find("label").removeClass("text-muted css_not_available");
117
118         var product_id = false;
119         for (var k in variant_ids) {
120             if (_.isEmpty(_.difference(variant_ids[k][1], values))) {
121                 $price.html(price_to_str(variant_ids[k][2]));
122                 $default_price.html(price_to_str(variant_ids[k][3]));
123                 if (variant_ids[k][3]-variant_ids[k][2]>0.2) {
124                     $default_price.closest('.oe_website_sale').addClass("discount");
125                 } else {
126                     $default_price.closest('.oe_website_sale').removeClass("discount");
127                 }
128                 product_id = variant_ids[k][0];
129                 break;
130             }
131         }
132
133         if (product_id) {
134             var $img = $(this).closest('tr.js_product, .oe_website_sale').find('span[data-oe-model^="product."][data-oe-type="image"] img:first, img.product_detail_img');
135             $img.attr("src", "/website/image/product.product/" + product_id + "/image");
136             $img.parent().attr('data-oe-model', 'product.product').attr('data-oe-id', product_id)
137                 .data('oe-model', 'product.product').data('oe-id', product_id);
138         }
139
140         $parent.find("input.js_variant_change:radio, select.js_variant_change").each(function () {
141             var $input = $(this);
142             var id = +$input.val();
143             var values = [id];
144
145             $parent.find("ul:not(:has(input.js_variant_change[value='" + id + "'])) input.js_variant_change:checked, select").each(function () {
146                 values.push(+$(this).val());
147             });
148
149             for (var k in variant_ids) {
150                 if (!_.difference(values, variant_ids[k][1]).length) {
151                     return;
152                 }
153             }
154             $input.closest("label").addClass("css_not_available");
155             $input.find("option[value='" + id + "']").addClass("css_not_available");
156         });
157
158         if (product_id) {
159             $parent.removeClass("css_not_available");
160             $product_id.val(product_id);
161             $parent.find(".js_check_product").removeAttr("disabled");
162         } else {
163             $parent.addClass("css_not_available");
164             $product_id.val(0);
165             $parent.find(".js_check_product").attr("disabled", "disabled");
166         }
167     });
168     $('ul.js_add_cart_variants', oe_website_sale).each(function () {
169         $('input.js_variant_change, select.js_variant_change', this).first().trigger('change');
170     });
171
172     $(oe_website_sale).on('change', "select[name='country_id']", function () {
173         var $select = $("select[name='state_id']");
174         $select.find("option:not(:first)").hide();
175         var nb = $select.find("option[data-country_id="+($(this).val() || 0)+"]").show().size();
176         $select.parent().toggle(nb>1);
177     });
178     $(oe_website_sale).find("select[name='country_id']").change();
179
180     $(oe_website_sale).on('change', "select[name='shipping_country_id']", function () {
181         var $select = $("select[name='shipping_state_id']");
182         $select.find("option:not(:first)").hide();
183         var nb = $select.find("option[data-country_id="+($(this).val() || 0)+"]").show().size();
184         $select.parent().toggle(nb>1);
185     });
186     $(oe_website_sale).find("select[name='shipping_country_id']").change();
187 });
188 });