|
| 1 | +/* global Option */ |
1 | 2 | /** @odoo-module **/
|
2 | 3 |
|
3 | 4 | import publicWidget from "@web/legacy/js/public/public_widget";
|
| 5 | +import {rpc} from "@web/core/network/rpc"; |
4 | 6 |
|
5 |
| -publicWidget.registry.WebsiteSale.prototype._changeCountry = function () { |
6 |
| - if (!$("#country_id").val()) { |
7 |
| - return; |
8 |
| - } |
9 |
| - return this.rpc("/shop/country_infos/" + $("#country_id").val(), { |
10 |
| - mode: $("#country_id").attr("mode"), |
11 |
| - }).then(function (data) { |
12 |
| - // Placeholder phone_code |
13 |
| - $("input[name='phone']").attr( |
14 |
| - "placeholder", |
15 |
| - data.phone_code !== 0 ? "+" + data.phone_code : "" |
16 |
| - ); |
| 7 | +publicWidget.registry.websiteSaleAddress = |
| 8 | + publicWidget.registry.websiteSaleAddress.extend({ |
| 9 | + /** |
| 10 | + * @private |
| 11 | + */ |
| 12 | + async _changeCountry(init = false) { |
| 13 | + const countryId = parseInt(this.addressForm.country_id.value); |
| 14 | + if (!countryId) { |
| 15 | + return; |
| 16 | + } |
17 | 17 |
|
18 |
| - // Populate states and display |
19 |
| - var selectStates = $("select[name='state_id']"); |
20 |
| - // Dont reload state at first loading (done in qweb) |
21 |
| - if ( |
22 |
| - selectStates.data("init") === 0 || |
23 |
| - selectStates.find("option").length === 1 |
24 |
| - ) { |
25 |
| - if (data.states.length || data.state_required) { |
26 |
| - selectStates.html(""); |
27 |
| - data.states.forEach((x) => { |
28 |
| - var opt = $("<option>") |
29 |
| - .text(x[1]) |
30 |
| - .attr("value", x[0]) |
31 |
| - .attr("data-code", x[2]); |
32 |
| - selectStates.append(opt); |
33 |
| - }); |
34 |
| - selectStates.parent("div").show(); |
| 18 | + const data = await rpc(`/shop/country_info/${parseInt(countryId)}`, { |
| 19 | + address_type: this.addressType, |
| 20 | + }); |
| 21 | + |
| 22 | + if (data.phone_code !== 0) { |
| 23 | + this.addressForm.phone.placeholder = "+" + data.phone_code; |
35 | 24 | } else {
|
36 |
| - selectStates.val("").parent("div").hide(); |
| 25 | + this.addressForm.phone.placeholder = ""; |
37 | 26 | }
|
38 |
| - selectStates.data("init", 0); |
39 |
| - } else { |
40 |
| - selectStates.data("init", 0); |
41 |
| - } |
42 | 27 |
|
43 |
| - // Manage fields order / visibility |
44 |
| - if (data.fields) { |
45 |
| - // [CUSTOM][DEL] Following 5 lines |
46 |
| - // if ($.inArray('zip', data.fields) > $.inArray('city', data.fields)){ |
47 |
| - // $(".div_zip").before($(".div_city")); |
48 |
| - // } else { |
49 |
| - // $(".div_zip").after($(".div_city")); |
50 |
| - // } |
51 |
| - // [CUSTOM][ADD] Following 5 lines: sort fields according to |
52 |
| - // online_address_format of the country |
53 |
| - var previous_field = $(".div_street"); |
54 |
| - data.fields.forEach((field) => { |
55 |
| - previous_field.after($(".div_" + field.split("_")[0])); |
56 |
| - previous_field = $(".div_" + field.split("_")[0]); |
| 28 | + // Populate states and display |
| 29 | + var selectStates = this.addressForm.state_id; |
| 30 | + if (!init || selectStates.options.length === 1) { |
| 31 | + // Dont reload state at first loading (done in qweb) |
| 32 | + if (data.states.length || data.state_required) { |
| 33 | + // Empty existing options, only keep the placeholder. |
| 34 | + selectStates.options.length = 1; |
| 35 | + |
| 36 | + // Create new options and append them to the select element |
| 37 | + data.states.forEach((state) => { |
| 38 | + const option = new Option(state[1], state[0]); |
| 39 | + // Used by localizations |
| 40 | + option.setAttribute("data-code", state[2]); |
| 41 | + selectStates.appendChild(option); |
| 42 | + }); |
| 43 | + this._showInput("state_id"); |
| 44 | + } else { |
| 45 | + this._hideInput("state_id"); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // Manage fields order / visibility |
| 50 | + if (data.fields) { |
| 51 | + // [CUSTOM][DEL] Following 5 lines |
| 52 | + // if (data.zip_before_city) { |
| 53 | + // this._getInputDiv('zip').after(this._getInputDiv('city')); |
| 54 | + // } else { |
| 55 | + // this._getInputDiv('zip').before(this._getInputDiv('city')); |
| 56 | + // } |
| 57 | + // [CUSTOM][ADD] Following 11 lines: sort fields according to |
| 58 | + // online_address_format of the country |
| 59 | + let prev = this._getInputDiv("street"); |
| 60 | + for (const fname of data.fields) { |
| 61 | + let key = fname.split("_")[0]; |
| 62 | + if (key === "state") key = "state_id"; |
| 63 | + if (key === "country") key = "country_id"; |
| 64 | + const el = this._getInputDiv(key); |
| 65 | + if (el) { |
| 66 | + prev.after(el); |
| 67 | + prev = el; |
| 68 | + } |
| 69 | + } |
| 70 | + // [CUSTOM][DEL] Following line |
| 71 | + // var all_fields = ['street', 'zip', 'city']; |
| 72 | + // [CUSTOM][ADD] street2 and state_code |
| 73 | + var all_fields = ["street", "street2", "zip", "city", "state_code"]; |
| 74 | + // [EDIT] Following 13 lines |
| 75 | + const toInputName = (fname) => { |
| 76 | + const key = fname.split("_")[0]; |
| 77 | + if (key === "state") return "state_id"; |
| 78 | + return key; |
| 79 | + }; |
| 80 | + all_fields.forEach((fname) => { |
| 81 | + const input = toInputName(fname); |
| 82 | + if (data.fields.includes(fname)) { |
| 83 | + this._showInput(input); |
| 84 | + } else { |
| 85 | + this._hideInput(input); |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + const required_fields = this.addressForm.querySelectorAll(":required"); |
| 91 | + required_fields.forEach((element) => { |
| 92 | + // Remove requirement on previously required fields |
| 93 | + if ( |
| 94 | + !data.required_fields.includes(element.name) && |
| 95 | + !this.requiredFields.includes(element.name) |
| 96 | + ) { |
| 97 | + this._markRequired(element.name, false); |
| 98 | + } |
57 | 99 | });
|
58 |
| - // [CUSTOM][DEL] Following line |
59 |
| - // var all_fields = ["street", "zip", "city", "country_name"]; // "state_code"]; |
60 |
| - // [CUSTOM][ADD] Following line: add street2 and state_code |
61 |
| - var all_fields = [ |
62 |
| - "street", |
63 |
| - "street2", |
64 |
| - "zip", |
65 |
| - "city", |
66 |
| - "country_name", |
67 |
| - "state_code", |
68 |
| - ]; |
69 |
| - all_fields.forEach((field) => { |
70 |
| - $(".checkout_autoformat .div_" + field.split("_")[0]).toggle( |
71 |
| - $.inArray(field, data.fields) >= 0 |
72 |
| - ); |
| 100 | + data.required_fields.forEach((fieldName) => { |
| 101 | + this._markRequired(fieldName, true); |
73 | 102 | });
|
74 |
| - } |
75 |
| - |
76 |
| - if ($("label[for='zip']").length) { |
77 |
| - $("label[for='zip']").toggleClass("label-optional", !data.zip_required); |
78 |
| - $("label[for='zip']") |
79 |
| - .get(0) |
80 |
| - .toggleAttribute("required", Boolean(data.zip_required)); |
81 |
| - } |
82 |
| - if ($("label[for='zip']").length) { |
83 |
| - $("label[for='state_id']").toggleClass( |
84 |
| - "label-optional", |
85 |
| - !data.state_required |
86 |
| - ); |
87 |
| - $("label[for='state_id']") |
88 |
| - .get(0) |
89 |
| - .toggleAttribute("required", Boolean(data.state_required)); |
90 |
| - } |
| 103 | + }, |
91 | 104 | });
|
92 |
| -}; |
|
0 commit comments