@@ -133,6 +133,7 @@ define([
133133 this . config = regions . config ;
134134 delete regions . config ;
135135 this . regions = regions ;
136+ this . sortedRegions = this . getSortedRegions ( ) ;
136137 this . disableAction = typeof disableAction === 'undefined' ? 'hide' : disableAction ;
137138 this . clearRegionValueOnDisable = typeof clearRegionValueOnDisable === 'undefined' ?
138139 false : clearRegionValueOnDisable ;
@@ -246,11 +247,13 @@ define([
246247 * Update.
247248 */
248249 update : function ( ) {
249- var option , region , def , regionId ;
250+ var option , selectElement , def , regionId , region ;
250251
251- if ( this . regions [ this . countryEl . value ] ) {
252+ selectElement = this . regionSelectEl ;
253+
254+ if ( this . sortedRegions [ this . countryEl . value ] ) {
252255 if ( this . lastCountryId != this . countryEl . value ) { //eslint-disable-line eqeqeq
253- def = this . regionSelectEl . getAttribute ( 'defaultValue' ) ;
256+ def = selectElement . getAttribute ( 'defaultValue' ) ;
254257
255258 if ( this . regionTextEl ) {
256259 if ( ! def ) {
@@ -259,26 +262,27 @@ define([
259262 this . regionTextEl . value = '' ;
260263 }
261264
262- this . regionSelectEl . options . length = 1 ;
265+ selectElement . options . length = 1 ;
263266
264- for ( regionId in this . regions [ this . countryEl . value ] ) { //eslint-disable-line guard-for-in
265- region = this . regions [ this . countryEl . value ] [ regionId ] ;
267+ this . sortedRegions [ this . countryEl . value ] . forEach ( function ( item ) {
268+ regionId = item [ 0 ] ;
269+ region = item [ 1 ] ;
266270
267271 option = document . createElement ( 'OPTION' ) ;
268272 option . value = regionId ;
269273 option . text = region . name . stripTags ( ) ;
270274 option . title = region . name ;
271275
272- if ( this . regionSelectEl . options . add ) {
273- this . regionSelectEl . options . add ( option ) ;
276+ if ( selectElement . options . add ) {
277+ selectElement . options . add ( option ) ;
274278 } else {
275- this . regionSelectEl . appendChild ( option ) ;
279+ selectElement . appendChild ( option ) ;
276280 }
277281
278282 if ( regionId == def || region . name . toLowerCase ( ) == def || region . code . toLowerCase ( ) == def ) { //eslint-disable-line
279- this . regionSelectEl . value = regionId ;
283+ selectElement . value = regionId ;
280284 }
281- }
285+ } ) ;
282286 }
283287
284288 if ( this . disableAction == 'hide' ) { //eslint-disable-line eqeqeq
@@ -340,6 +344,28 @@ define([
340344 display ? marks [ 0 ] . show ( ) : marks [ 0 ] . hide ( ) ;
341345 }
342346 }
347+ } ,
348+
349+ /**
350+ * Sort regions from JSON by name
351+ *
352+ * @returns {*[] }
353+ */
354+ getSortedRegions : function ( ) {
355+ var country , regionsEntries , regionsByCountry ;
356+
357+ regionsByCountry = [ ] ;
358+
359+ for ( country in this . regions ) { //eslint-disable-line guard-for-in
360+ regionsEntries = Object . entries ( this . regions [ country ] ) ;
361+ regionsEntries . sort ( function ( a , b ) {
362+ return a [ 1 ] . name > b [ 1 ] . name ? 1 : - 1 ;
363+ } ) ;
364+
365+ regionsByCountry [ country ] = regionsEntries ;
366+ }
367+
368+ return regionsByCountry ;
343369 }
344370 } ;
345371
0 commit comments