This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,227 @@
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
define([
'jquery',
'mage/template',
'underscore',
'jquery-ui-modules/widget',
'mage/validation'
], function ($, mageTemplate, _) {
'use strict';
$.widget('stemcell.administrativeUpdater', {
options: {
regionTemplate:
'<option value="<%- data.value %>" <% if (data.isSelected) { %>selected="selected"<% } %>>' +
'<%- data.title %>' +
'</option>',
isRegionRequired: true,
currentCity: null,
},
/**
*
* @private
*/
_create: function () {
// this._initCountryElement();
this.currentCityOption = this.options.currentCity;
this.regionTmpl = mageTemplate(this.options.regionTemplate);
// this._updateRegion(this.element.find('option:selected').val());
this.element.on('change', $.proxy(function (e) {
this.setOption = false;
this.currentCityOption = $(e.target).val();
console.log($(e.target).val());
$(this.options.subListId).val('');
this._updateRegion($(e.target).val());
}, this));
},
/**
*
* @private
*/
_initCountryElement: function () {
if (this.options.isMultipleCountriesAllowed) {
this.element.parents('div.field').show();
this.element.on('change', $.proxy(function (e) {
// clear region inputs on country change
$(this.options.subListId).val('');
this._updateRegion($(e.target).val());
}, this));
if (this.options.isCountryRequired) {
this.element.addClass('required-entry');
this.element.parents('div.field').addClass('required');
}
} else {
this.element.parents('div.field').hide();
}
},
/**
* Remove options from dropdown list
*
* @param {Object} selectElement - jQuery object for dropdown list
* @private
*/
_removeSelectOptions: function (selectElement) {
selectElement.find('option').each(function (index) {
if (index) {
$(this).remove();
}
});
},
/**
* Render dropdown list
* @param {Object} selectElement - jQuery object for dropdown list
* @param {String} key - region code
* @param {Object} value - region object
* @private
*/
_renderSelectOption: function (selectElement, key, value) {
selectElement.append($.proxy(function () {
var name = value.name.replace(/[!"#$%&'()*+,.\/:;<=>?@[\\\]^`{|}~]/g, '\\$&'),
tmplData,
tmpl;
if (value.code && $(name).is('span')) {
key = value.code;
value.name = $(name).text();
}
tmplData = {
value: value.code,
title: value.name,
isSelected: false
};
if (this.options.selectedValue === key) {
tmplData.isSelected = true;
}
tmpl = this.regionTmpl({
data: tmplData
});
return $(tmpl);
}, this));
},
/**
* Takes clearError callback function as first option
* If no form is passed as option, look up the closest form and call clearError method.
* @private
*/
_clearError: function () {
var args = ['clearError', this.options.subListId, this.options.regionInputId, this.options.postcodeId];
if (this.options.clearError && typeof this.options.clearError === 'function') {
this.options.clearError.call(this);
} else {
if (!this.options.form) {
this.options.form = this.element.closest('form').length ? $(this.element.closest('form')[0]) : null;
}
this.options.form = $(this.options.form);
this.options.form && this.options.form.data('validator') &&
this.options.form.validation.apply(this.options.form, _.compact(args));
// Clean up errors on region & zip fix
$(this.options.regionInputId).removeClass('mage-error').parent().find('[generated]').remove();
$(this.options.subListId).removeClass('mage-error').parent().find('[generated]').remove();
$(this.options.postcodeId).removeClass('mage-error').parent().find('[generated]').remove();
}
},
_getSubAdministrative: function(administrativeId) {
let data = [];
let self = this;
data.push({"name": "administrativeId", "value": administrativeId});
data.push({"name": "type", "value": this.options.type});
data.push({"name": "form_key", "value": $('input[name="form_key"]').val()});
return $.ajax({
url: self.options.requestUrl,
type: 'get',
async: false,
dataType: 'json',
context: self,
data: data
});
},
/**
* Update dropdown list based on the country selected
*
* @param {String} country - 2 uppercase letter for country code
* @private
*/
_updateRegion: function (country) {
// Clear validation error messages
var regionList = $(this.options.subListId),
regionInput = $(this.options.regionInputId),
label = regionList.parent().siblings('label'),
container = regionList.parents('div.field'),
regionsEntries,
regionId,
regionData;
this._clearError();
// this._checkRegionRequired(country);
$.when(this._getSubAdministrative(this.element.find('option:selected').val()))
.done(function (data) {
let administrative = data.data.administrative;
this._removeSelectOptions(regionList);
regionsEntries = _.pairs(administrative);
$.each(regionsEntries, $.proxy(function (key, value) {
regionId = value[0];
regionData = value[1];
this._renderSelectOption(regionList, regionId, regionData);
}, this));
if (this.currentRegionOption) {
regionList.val(this.currentRegionOption);
}
if (this.setOption) {
regionList.find('option').filter(function () {
return this.text === regionInput.val();
}).attr('selected', true);
}
})
// Add defaultvalue attribute to state/province select element
regionList.attr('defaultvalue', this.options.selectedValue);
this.options.form.find('[type="submit"]').removeAttr('disabled').show();
},
/**
* Check if the selected country has a mandatory region selection
*
* @param {String} country - Code of the country - 2 uppercase letter for country code
* @private
*/
_checkRegionRequired: function (country) {
var self = this;
this.options.isRegionRequired = false;
$.each(this.options.regionJson.config['regions_required'], function (index, elem) {
if (elem === country) {
self.options.isRegionRequired = true;
}
});
}
});
return $.stemcell.administrativeUpdater;
});