init
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
define([
|
||||
'jquery',
|
||||
'mage/utils/wrapper',
|
||||
'Magento_Customer/js/customer-data',
|
||||
'uiRegistry'
|
||||
], function ($, wrapper, customerData, registry) {
|
||||
'use strict';
|
||||
|
||||
var directoryData = customerData.get('directory-data');
|
||||
|
||||
/**
|
||||
* Resolve a region from customer-data or the checkout provider dictionary.
|
||||
*
|
||||
* The provider dictionary is rendered with the checkout page and is
|
||||
* available before the asynchronous customer-data section has loaded.
|
||||
*
|
||||
* @param {Object} addressData
|
||||
* @returns {Object|null}
|
||||
*/
|
||||
function getRegion(addressData) {
|
||||
var countries = directoryData() || {},
|
||||
country = countries[addressData.country_id],
|
||||
provider,
|
||||
options,
|
||||
option;
|
||||
|
||||
if (country && country.regions) {
|
||||
option = country.regions[addressData.region_id];
|
||||
|
||||
if (option) {
|
||||
return {
|
||||
code: option.code,
|
||||
name: option.name
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
provider = registry.get('checkoutProvider');
|
||||
options = provider && provider.dictionaries && provider.dictionaries.region_id;
|
||||
|
||||
if (!options && provider && typeof provider.get === 'function') {
|
||||
options = provider.get('dictionaries.region_id');
|
||||
}
|
||||
|
||||
if (!Array.isArray(options)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
options.some(function (item) {
|
||||
if (String(item.value) === String(addressData.region_id) &&
|
||||
(!item.country_id || item.country_id === addressData.country_id)
|
||||
) {
|
||||
option = item;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
return option ? {
|
||||
code: addressData.region_code || '',
|
||||
name: option.label || option.title
|
||||
} : null;
|
||||
}
|
||||
|
||||
return function (addressConverter) {
|
||||
addressConverter.formAddressDataToQuoteAddress = wrapper.wrapSuper(
|
||||
addressConverter.formAddressDataToQuoteAddress,
|
||||
function (formData) {
|
||||
var addressData = $.extend(true, {}, formData),
|
||||
region;
|
||||
|
||||
if (addressData.region_id && !addressData.region) {
|
||||
region = getRegion(addressData);
|
||||
|
||||
if (region) {
|
||||
addressData.region = region.name;
|
||||
addressData.region_code = region.code;
|
||||
}
|
||||
}
|
||||
|
||||
return this._super(addressData);
|
||||
}
|
||||
);
|
||||
|
||||
return addressConverter;
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,79 @@
|
||||
define([], function () {
|
||||
'use strict';
|
||||
|
||||
return function (Region) {
|
||||
return Region.extend({
|
||||
/**
|
||||
* Keep the text region value in sync with the selected region ID.
|
||||
*
|
||||
* Magento normally resolves the name later from the asynchronous
|
||||
* directory-data customer section. When that section has not
|
||||
* finished loading, a newly-created checkout address has a valid
|
||||
* regionId but an empty region label, so its address card omits the
|
||||
* state until the page is reloaded.
|
||||
*
|
||||
* @param {*} value
|
||||
* @returns {Object}
|
||||
*/
|
||||
onUpdate: function (value) {
|
||||
var result = this._super();
|
||||
|
||||
this.syncRegionName(value);
|
||||
|
||||
return result;
|
||||
},
|
||||
|
||||
/**
|
||||
* Synchronize addresses restored before their country regions have
|
||||
* finished loading (most visible when editing a second address).
|
||||
*
|
||||
* @param {Array} options
|
||||
* @returns {Object}
|
||||
*/
|
||||
setOptions: function (options) {
|
||||
var result = this._super(options);
|
||||
|
||||
this.syncRegionName(this.value());
|
||||
|
||||
return result;
|
||||
},
|
||||
|
||||
/**
|
||||
* Copy the selected option label to the text region data scope.
|
||||
*
|
||||
* @param {*} value
|
||||
*/
|
||||
syncRegionName: function (value) {
|
||||
var option = this.getOption(value),
|
||||
regionScope = this.customEntry;
|
||||
|
||||
if (!regionScope && this.dataScope) {
|
||||
regionScope = this.dataScope.replace(/region_id$/, 'region');
|
||||
}
|
||||
|
||||
if (option && regionScope && this.source) {
|
||||
this.source.set(regionScope, option.label);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Restore the region selector when switching from a country whose
|
||||
* regions are hidden to one (such as US) that has region options.
|
||||
*/
|
||||
hideRegion: function (option) {
|
||||
var hasVisibleRegions;
|
||||
|
||||
if (!option) {
|
||||
return;
|
||||
}
|
||||
|
||||
hasVisibleRegions = option.is_region_visible !== false && this.options().length > 1;
|
||||
this.setVisible(hasVisibleRegions);
|
||||
|
||||
if (this.customEntry) {
|
||||
this.toggleInput(!hasVisibleRegions && option.is_region_visible !== false);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user