init
This commit is contained in:
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
@@ -0,0 +1,24 @@
|
||||
define([
|
||||
'jquery'
|
||||
], function ($) {
|
||||
'use strict';
|
||||
|
||||
return function (config, element) {
|
||||
var $book = $(element);
|
||||
var $tabs = $book.find('[data-address-tab]');
|
||||
var $panels = $book.find('[data-address-panel]');
|
||||
|
||||
$tabs.on('click', function () {
|
||||
var $tab = $(this);
|
||||
var target = $tab.attr('data-address-tab');
|
||||
|
||||
$tabs.removeClass('is-active').attr('aria-selected', 'false');
|
||||
$tab.addClass('is-active').attr('aria-selected', 'true');
|
||||
|
||||
$panels.removeClass('is-active').prop('hidden', true);
|
||||
$panels.filter('[data-address-panel="' + target + '"]')
|
||||
.addClass('is-active')
|
||||
.prop('hidden', false);
|
||||
});
|
||||
};
|
||||
});
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,136 @@
|
||||
define([
|
||||
'jquery'
|
||||
], function ($) {
|
||||
'use strict';
|
||||
|
||||
return function (customerData) {
|
||||
var initialized = false,
|
||||
initPromise,
|
||||
guardedMethods = [
|
||||
'reload',
|
||||
'set',
|
||||
'invalidate',
|
||||
'onAjaxComplete'
|
||||
],
|
||||
emptyArrayMethods = [
|
||||
'getExpiredSectionNames',
|
||||
'getExpiredKeys'
|
||||
];
|
||||
|
||||
if (!customerData || customerData._esunCustomerDataInitGuard) {
|
||||
return customerData;
|
||||
}
|
||||
|
||||
customerData._esunCustomerDataInitGuard = true;
|
||||
initPromise = typeof customerData.getInitCustomerData === 'function' ?
|
||||
customerData.getInitCustomerData() :
|
||||
null;
|
||||
|
||||
if (initPromise && typeof initPromise.done === 'function') {
|
||||
initPromise.done(function () {
|
||||
initialized = true;
|
||||
});
|
||||
} else {
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
function resolveDeferred(deferred, result) {
|
||||
if (result && typeof result.done === 'function') {
|
||||
result.done(function () {
|
||||
deferred.resolve.apply(deferred, arguments);
|
||||
});
|
||||
|
||||
if (typeof result.fail === 'function') {
|
||||
result.fail(function () {
|
||||
deferred.reject.apply(deferred, arguments);
|
||||
});
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
deferred.resolve(result);
|
||||
}
|
||||
|
||||
function runAfterInit(callback) {
|
||||
var deferred = $.Deferred();
|
||||
|
||||
if (initialized) {
|
||||
resolveDeferred(deferred, callback());
|
||||
|
||||
return deferred.promise();
|
||||
}
|
||||
|
||||
if (!initPromise || typeof initPromise.done !== 'function') {
|
||||
initialized = true;
|
||||
resolveDeferred(deferred, callback());
|
||||
|
||||
return deferred.promise();
|
||||
}
|
||||
|
||||
initPromise.done(function () {
|
||||
initialized = true;
|
||||
resolveDeferred(deferred, callback());
|
||||
});
|
||||
|
||||
if (typeof initPromise.fail === 'function') {
|
||||
initPromise.fail(function () {
|
||||
deferred.reject.apply(deferred, arguments);
|
||||
});
|
||||
}
|
||||
|
||||
return deferred.promise();
|
||||
}
|
||||
|
||||
guardedMethods.forEach(function (methodName) {
|
||||
var originalMethod = customerData[methodName];
|
||||
|
||||
if (typeof originalMethod !== 'function') {
|
||||
return;
|
||||
}
|
||||
|
||||
customerData[methodName] = function () {
|
||||
var context = this,
|
||||
args = arguments;
|
||||
|
||||
if (initialized) {
|
||||
return originalMethod.apply(context, args);
|
||||
}
|
||||
|
||||
return runAfterInit(function () {
|
||||
return originalMethod.apply(context, args);
|
||||
});
|
||||
};
|
||||
});
|
||||
|
||||
emptyArrayMethods.forEach(function (methodName) {
|
||||
var originalMethod = customerData[methodName];
|
||||
|
||||
if (typeof originalMethod !== 'function') {
|
||||
return;
|
||||
}
|
||||
|
||||
customerData[methodName] = function () {
|
||||
if (!initialized) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return originalMethod.apply(this, arguments);
|
||||
};
|
||||
});
|
||||
|
||||
if (typeof customerData.needReload === 'function') {
|
||||
customerData.needReload = function (originalMethod) {
|
||||
return function () {
|
||||
if (!initialized) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return originalMethod.apply(this, arguments);
|
||||
};
|
||||
}(customerData.needReload);
|
||||
}
|
||||
|
||||
return customerData;
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user