|
| 1 | +odoo.define('website_sale_require_state.requireState', function (require) { |
| 2 | +'use strict'; |
| 3 | + |
| 4 | + |
| 5 | +var publicWidget = require('web.public.widget'); |
| 6 | +require('website_sale.website_sale'); |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +publicWidget.registry.WebsiteSale.include({ |
| 11 | + |
| 12 | + /** |
| 13 | + * @override |
| 14 | + * @private |
| 15 | + */ |
| 16 | + _changeCountry: function () { |
| 17 | + if (!$("#country_id").val()) { |
| 18 | + return; |
| 19 | + } |
| 20 | + this._rpc({ |
| 21 | + route: "/shop/country_infos/" + $("#country_id").val(), |
| 22 | + params: { |
| 23 | + mode: 'shipping', |
| 24 | + }, |
| 25 | + }).then(function (data) { |
| 26 | + // placeholder phone_code |
| 27 | + //$("input[name='phone']").attr('placeholder', data.phone_code !== 0 ? '+'+ data.phone_code : ''); |
| 28 | + |
| 29 | + // populate states and display |
| 30 | + var selectStates = $("select[name='state_id']"); |
| 31 | + // dont reload state at first loading (done in qweb) |
| 32 | + if (selectStates.data('init') === 0 || selectStates.find('option').length === 1) { |
| 33 | + if (data.states.length) { |
| 34 | + selectStates.html(''); |
| 35 | + selectStates.append('<option value="">Select...</option>'); |
| 36 | + _.each(data.states, function (x) { |
| 37 | + var opt = $('<option>').text(x[1]) |
| 38 | + .attr('value', x[0]) |
| 39 | + .attr('data-code', x[2]); |
| 40 | + selectStates.append(opt); |
| 41 | + }); |
| 42 | + selectStates.parent('div').show(); |
| 43 | + } else { |
| 44 | + selectStates.val('').parent('div').hide(); |
| 45 | + } |
| 46 | + selectStates.data('init', 0); |
| 47 | + } else { |
| 48 | + selectStates.data('init', 0); |
| 49 | + } |
| 50 | + |
| 51 | + // manage fields order / visibility |
| 52 | + if (data.fields) { |
| 53 | + if ($.inArray('zip', data.fields) > $.inArray('city', data.fields)) { |
| 54 | + $(".div_zip").before($(".div_city")); |
| 55 | + } else { |
| 56 | + $(".div_zip").after($(".div_city")); |
| 57 | + } |
| 58 | + var all_fields = ["street", "zip", "city", "country_name"]; // "state_code"]; |
| 59 | + _.each(all_fields, function (field) { |
| 60 | + $(".checkout_autoformat .div_" + field.split('_')[0]).toggle($.inArray(field, data.fields) >= 0); |
| 61 | + }); |
| 62 | + } |
| 63 | + }); |
| 64 | + }, |
| 65 | +}) |
| 66 | +}); |
0 commit comments