@@ -133,6 +133,7 @@ define([
133
133
this . config = regions . config ;
134
134
delete regions . config ;
135
135
this . regions = regions ;
136
+ this . sortedRegions = this . getSortedRegions ( ) ;
136
137
this . disableAction = typeof disableAction === 'undefined' ? 'hide' : disableAction ;
137
138
this . clearRegionValueOnDisable = typeof clearRegionValueOnDisable === 'undefined' ?
138
139
false : clearRegionValueOnDisable ;
@@ -246,11 +247,13 @@ define([
246
247
* Update.
247
248
*/
248
249
update : function ( ) {
249
- var option , region , def , regionId ;
250
+ var option , selectElement , def , regionId , region ;
250
251
251
- if ( this . regions [ this . countryEl . value ] ) {
252
+ selectElement = this . regionSelectEl ;
253
+
254
+ if ( this . sortedRegions [ this . countryEl . value ] ) {
252
255
if ( this . lastCountryId != this . countryEl . value ) { //eslint-disable-line eqeqeq
253
- def = this . regionSelectEl . getAttribute ( 'defaultValue' ) ;
256
+ def = selectElement . getAttribute ( 'defaultValue' ) ;
254
257
255
258
if ( this . regionTextEl ) {
256
259
if ( ! def ) {
@@ -259,26 +262,27 @@ define([
259
262
this . regionTextEl . value = '' ;
260
263
}
261
264
262
- this . regionSelectEl . options . length = 1 ;
265
+ selectElement . options . length = 1 ;
263
266
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 ] ;
266
270
267
271
option = document . createElement ( 'OPTION' ) ;
268
272
option . value = regionId ;
269
273
option . text = region . name . stripTags ( ) ;
270
274
option . title = region . name ;
271
275
272
- if ( this . regionSelectEl . options . add ) {
273
- this . regionSelectEl . options . add ( option ) ;
276
+ if ( selectElement . options . add ) {
277
+ selectElement . options . add ( option ) ;
274
278
} else {
275
- this . regionSelectEl . appendChild ( option ) ;
279
+ selectElement . appendChild ( option ) ;
276
280
}
277
281
278
282
if ( regionId == def || region . name . toLowerCase ( ) == def || region . code . toLowerCase ( ) == def ) { //eslint-disable-line
279
- this . regionSelectEl . value = regionId ;
283
+ selectElement . value = regionId ;
280
284
}
281
- }
285
+ } ) ;
282
286
}
283
287
284
288
if ( this . disableAction == 'hide' ) { //eslint-disable-line eqeqeq
@@ -340,6 +344,28 @@ define([
340
344
display ? marks [ 0 ] . show ( ) : marks [ 0 ] . hide ( ) ;
341
345
}
342
346
}
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 ;
343
369
}
344
370
} ;
345
371
0 commit comments