PageSourceSearch

https://origamitea.ru/wp-content/plugins/woocommerce-edostavka/assets/js/frontend/city-select.js?ver=2.2.5.3

js origamitea.ru collected 2026-10-04 23:42:39 UTC 6,987 bytes, 245 lines download raw bytes

1( function( $ ) {
2    'use strict'
3
4	$( document ).ready( function () {
5
6		$.fn.selectWoo.amd.define( 'edostavka/resultsAdapter', [ 'select2/utils', 'select2/results' ], function ( Utils, ResultsList ) {
7
8			const ResultAdapter = function( decorated, $element, options, dataAdapter ) {
9				return decorated.call( this, $element, options, dataAdapter );
10			}
11
12			ResultAdapter.prototype.setClasses = function () {
13				var self = this;
14
15				this.data.current( function (selected) {
16
17					const customerLocation = self.options.get('customerLocation');
18
19					const selectedIds = customerLocation && customerLocation.city_code ? [customerLocation.city_code.toString()] : $.map(selected, function (s) {
20						return s.id.toString();
21					});
22
23					var $options = self.$results.find('.select2-results__option[data-selected]');
24
25					$options.each(function () {
26						var $option = $(this);
27
28						var item = $.data(this, 'data');
29
30						var id = customerLocation && customerLocation.city_code && item?.code ? item.code.toString() : item?.id.toString();
31
32						if ( ( item?.element != null && item?.element.selected ) ||
33							( item?.element == null && $.inArray(id, selectedIds) > -1)) {
34							$option.attr('data-selected', 'true');
35						} else {
36							$option.attr('data-selected', 'false');
37						}
38					});
39
40				} );
41			}
42
43			return Utils.Decorate( ResultsList, ResultAdapter );
44
45		} );
46
47		$.fn.WCEdostavkaSelectCity = function( options ) {
48			return this.each( function() {
49				( new $.WCEdostavkaSelectCity( $( this ), options ) );
50			} );
51		}
52
53		$.WCEdostavkaSelectCity = function( $element, options ) {
54			const settings = $.extend( {
55				placeholder: '',
56				defaultValue: '',
57				countryCode: '',
58				minimumInputLength: 2,
59				placeholderOption: 'first',
60				allowTags: false,
61				ajaxUrl: false,
62				customerLocation: {}
63			}, options || {} );
64
65			var xhr           = false,
66				input_name    = $element.attr( 'name' ),
67				input_id      = $element.attr('id'),
68				input_classes = $element.attr('data-input-classes'),
69				value         = $element.val() || settings.defaultValue,
70				placeholder   = $element.attr( 'placeholder' ) || $element.attr( 'data-placeholder' ) || settings.placeholder,
71				$newelement;
72
73			if ( $element.is( 'input' ) ) {
74				$newelement = $( '<select />' )
75					.prop( 'id', input_id )
76					.prop( 'name', input_name )
77					.data( 'placeholder', placeholder )
78					.attr( 'data-input-classes', input_classes )
79					.addClass( input_classes );
80				$element.replaceWith( $newelement );
81				$element = $newelement;
82			}
83
84			if( value && settings.customerLocation && settings.customerLocation.country_code && settings.countryCode !== settings.customerLocation.country_code ) {
85				value = '';
86			}
87
88			$element.empty();
89
90			if( value ) {
91				$element.append(
92					$( '<option />' )
93						.prop( 'value', value )
94						.text( value )
95				);
96			}
97
98			$element.selectWoo( {
99				placeholder: settings.placeholder,
100				minimumInputLength: settings.minimumInputLength,
101				placeholderOption: settings.placeholderOption,
102				width: '100%',
103				customerLocation: settings.customerLocation,
104				ajax: {
105					url: settings.ajaxUrl,
106					method: 'POST',
107					dataType: 'json',
108					delay: 250,
109					data: function( params ) {
110						return {
111							action: 'edostavka_get_location_cities',
112							country_codes: settings.countryCode,
113							city: params.term || value,
114							size: 10
115						}
116					},
117					transport: function( xhr_params, success, failure ) {
118
119						if ( xhr ) {
120							xhr.abort();
121						}
122
123						xhr = $.ajax( xhr_params );
124						xhr.then( success );
125						xhr.fail( failure );
126
127						return xhr;
128					},
129					processResults: function ( data, params ) {
130						var terms = [];
131
132						if ( data.success && data.data ) {
133							data.data.filter( function( item ) {
134								return settings.countryCode == item.country_code
135							} ).map( function( term ) {
136								terms.push( {
137									_resultId: term.code,
138									id:   term.city,
139									text: term.city,
140									title: [ term.country, term.region, term.city ].join( ', ' ),
141									code: term.code,
142									region: term.region,
143									region_code: term.region_code,
144									sub_region: term.sub_region || null,
145									country: term.country,
146									country_code: term.country_code,
147									longitude: term.longitude,
148									latitude: term.latitude
149								} );
150							} );
151						}
152
153						return { results: terms };
154					},
155					cache: true
156				},
157				resultsAdapter: $.fn.selectWoo.amd.require( 'edostavka/resultsAdapter' ),
158				templateSelection: function( item, container ) {
159					return item.id || item.text;
160				},
161				templateResult: function( item, element ) {
162
163					if ( item.loading || ! item.region ) {
164						return item.text;
165					}
166
167					const region = [ item.region ];
168
169					if( item.sub_region ) {
170						region.push( item.sub_region );
171					}
172
173					const $container = $( '<div />', {
174						class: 'select2-result-item',
175						html: item.text + ' (' + region.join( ', ' ) + ')'
176					} );
177
178					if( item.country ) {
179						$container.append( $( '<small />', {
180							style: 'color:#cbcbcb; display: block;',
181							text: [ item.text, item.region, item.country ].join( ', ' )
182						} ) );
183					}
184
185					return $container;
186				},
187				tags: settings.allowTags,
188				insertTag: function( data, tag ) {
189					if( ! data.filter( function( item ) {
190						return item !== tag
191					} ).length ) {
192						tag.isCustom = true;
193						data.push( tag )
194					}
195				},
196				language: {
197					errorLoading: function() {
198						return wc_country_select_params.i18n_searching;
199					},
200					inputTooLong: function( args ) {
201						var overChars = args.input.length - args.maximum;
202
203						if ( 1 === overChars ) {
204							return wc_country_select_params.i18n_input_too_long_1;
205						}
206
207						return wc_country_select_params.i18n_input_too_long_n.replace( '%qty%', overChars );
208					},
209					inputTooShort: function( args ) {
210						var remainingChars = args.minimum - args.input.length;
211
212						if ( 1 === remainingChars ) {
213							return wc_country_select_params.i18n_input_too_short_1;
214						}
215
216						return wc_country_select_params.i18n_input_too_short_n.replace( '%qty%', remainingChars );
217					},
218					loadingMore: function() {
219						return wc_country_select_params.i18n_load_more;
220					},
221					maximumSelected: function( args ) {
222						if ( args.maximum === 1 ) {
223							return wc_country_select_params.i18n_selection_too_long_1;
224						}
225
226						return wc_country_select_params.i18n_selection_too_long_n.replace( '%qty%', args.maximum );
227					},
228					noResults: function() {
229						return wc_country_select_params.i18n_no_matches;
230					},
231					searching: function() {
232						return wc_country_select_params.i18n_searching;
233					}
234				}
235			} ).addClass( 'enhanced' );
236
237			//FIXME: при вызове события change происхоидт бесконечная перезагрузка чекаута
238			//$element.val( value ).trigger( 'change' );
239
240			return $element;
241		}
242
243	} );
244
245} )( jQuery );

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.