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( $.extend( {}, term, { 137 _resultId: term.code, 138 id: term.city, 139 text: term.city, 140 title: [ term.country, term.region, term.city ].join( ', ' ), 141 } ) ) 142 } ); 143 } 144 145 return { results: terms }; 146 }, 147 cache: true 148 }, 149 resultsAdapter: $.fn.selectWoo.amd.require( 'edostavka/resultsAdapter' ), 150 templateSelection: function( item, container ) { 151 return item.id || item.text; 152 }, 153 templateResult: function( item, element ) { 154 155 if ( item.loading || ! item.region ) { 156 return item.text; 157 } 158 159 const region = [ item.region ]; 160 161 if( item.sub_region ) { 162 region.push( item.sub_region ); 163 } 164 165 const $container = $( '<div />', { 166 class: 'select2-result-item', 167 html: item.text + ' (' + region.join( ', ' ) + ')' 168 } ); 169 170 if( item.country ) { 171 $container.append( $( '<small />', { 172 style: 'color:#cbcbcb; display: block;', 173 text: [ item.text, item.region, item.country ].join( ', ' ) 174 } ) ); 175 } 176 177 return $container; 178 }, 179 tags: settings.allowTags, 180 insertTag: function( data, tag ) { 181 if( ! data.filter( function( item ) { 182 return item !== tag 183 } ).length ) { 184 tag.isCustom = true; 185 data.push( tag ) 186 } 187 }, 188 language: { 189 errorLoading: function() { 190 return wc_country_select_params.i18n_searching; 191 }, 192 inputTooLong: function( args ) { 193 var overChars = args.input.length - args.maximum; 194 195 if ( 1 === overChars ) { 196 return wc_country_select_params.i18n_input_too_long_1; 197 } 198 199 return wc_country_select_params.i18n_input_too_long_n.replace( '%qty%', overChars ); 200 }, 201 inputTooShort: function( args ) { 202 var remainingChars = args.minimum - args.input.length; 203 204 if ( 1 === remainingChars ) { 205 return wc_country_select_params.i18n_input_too_short_1; 206 } 207 208 return wc_country_select_params.i18n_input_too_short_n.replace( '%qty%', remainingChars ); 209 }, 210 loadingMore: function() { 211 return wc_country_select_params.i18n_load_more; 212 }, 213 maximumSelected: function( args ) { 214 if ( args.maximum === 1 ) { 215 return wc_country_select_params.i18n_selection_too_long_1; 216 } 217 218 return wc_country_select_params.i18n_selection_too_long_n.replace( '%qty%', args.maximum ); 219 }, 220 noResults: function() { 221 return wc_country_select_params.i18n_no_matches; 222 }, 223 searching: function() { 224 return wc_country_select_params.i18n_searching; 225 } 226 } 227 } ).addClass( 'enhanced' ); 228 229 //FIXME: пÑи вÑзове ÑобÑÑÐ¸Ñ change пÑоиÑÑ Ð¾Ð¸Ð´Ñ Ð±ÐµÑконеÑÐ½Ð°Ñ Ð¿ÐµÑезагÑÑзка ÑекаÑÑа 230 //$element.val( value ).trigger( 'change' ); 231 232 return $element; 233 } 234 235 } ); 236 237} )( 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.