PageSourceSearch

https://wondergreener.com/wp-content/plugins/ry-wc-city-select/style/js/city-select.js?ver=1.1.2

js wondergreener.com collected 2026-10-08 11:38:56 UTC 8,282 bytes, 203 lines download raw bytes

1jQuery(function ($) {
2    // ry_wc_city_select_params is required to continue, ensure the object exists
3    // wc_country_select_params is used for selectWoo texts. This one is added by WC
4    if (typeof wc_country_select_params === 'undefined' || typeof ry_wc_city_select_params === 'undefined') {
5        return false;
6    }
7
8    // Select2 Enhancement if it exists
9    if ($().selectWoo) {
10        var getEnhancedSelectFormatString = function () {
11            return {
12                'language': {
13                    errorLoading: function () {
14                        // Workaround for https://github.com/select2/select2/issues/4355 instead of i18n_ajax_error.
15                        return wc_country_select_params.i18n_searching;
16                    },
17                    inputTooLong: function (args) {
18                        var overChars = args.input.length - args.maximum;
19
20                        if (1 === overChars) {
21                            return wc_country_select_params.i18n_input_too_long_1;
22                        }
23                        return wc_country_select_params.i18n_input_too_long_n.replace('%qty%', overChars);
24                    },
25                    inputTooShort: function (args) {
26                        var remainingChars = args.minimum - args.input.length;
27
28                        if (1 === remainingChars) {
29                            return wc_country_select_params.i18n_input_too_short_1;
30                        }
31                        return wc_country_select_params.i18n_input_too_short_n.replace('%qty%', remainingChars);
32                    },
33                    loadingMore: function () {
34                        return wc_country_select_params.i18n_load_more;
35                    },
36                    maximumSelected: function (args) {
37                        if (args.maximum === 1) {
38                            return wc_country_select_params.i18n_selection_too_long_1;
39                        }
40                        return wc_country_select_params.i18n_selection_too_long_n.replace('%qty%', args.maximum);
41                    },
42                    noResults: function () {
43                        return wc_country_select_params.i18n_no_matches;
44                    },
45                    searching: function () {
46                        return wc_country_select_params.i18n_searching;
47                    }
48                }
49            };
50        };
51
52        var wc_city_select_select2 = function () {
53            $('select.city_select:visible').each(function () {
54                var select2_args = $.extend({
55                    placeholder: $(this).attr('data-placeholder') || $(this).attr('placeholder') || '',
56                    width: '100%'
57                }, getEnhancedSelectFormatString());
58
59                $(this)
60                    .on('select2:select', function () {
61                        $(this).focus(); // Maintain focus after select https://github.com/select2/select2/issues/4384
62                    })
63                    .selectWoo(select2_args);
64            });
65        };
66
67        wc_city_select_select2();
68
69        $(document.body).bind('city_to_select', function () {
70            wc_city_select_select2();
71        });
72    }
73
74    /* City select boxes */
75    $(document.body).on('country_to_state_changing', function (e, country, $container) {
76        var $statebox = $container.find('#billing_state, #shipping_state, #calc_shipping_state'),
77            state = $statebox.val();
78        $(document.body).trigger('state_changing', [country, state, $container]);
79    });
80
81    $(document.body).on('change', 'select.state_select, #calc_shipping_state', function () {
82        var $container = $(this).closest('.form-row').parent(),
83            country = $container.find('#billing_country, #shipping_country, #calc_shipping_country').val(),
84            state = $(this).val();
85
86        $(document.body).trigger('state_changing', [country, state, $container]);
87    });
88
89    $(document.body).on('state_changing', function (e, country, state, $container) {
90        var $citybox = $container.find('#billing_city, #shipping_city, #calc_shipping_city');
91
92        if (ry_wc_city_select_params.cities[country]) {
93            /* if the country has no states */
94            if (state) {
95                if (ry_wc_city_select_params.cities[country][state]) {
96                    cityToSelect($citybox, ry_wc_city_select_params.cities[country][state]);
97                } else {
98                    cityToInput($citybox);
99                }
100            } else {
101                disableCity($citybox);
102            }
103        } else {
104            cityToInput($citybox);
105        }
106    });
107
108    $(document.body).on('change', 'select.city_select', function () {
109        var $container = $(this).closest('.form-row').parent(),
110            $city = $container.find('#billing_city, #shipping_city, #calc_shipping_city'),
111            postcode = $city.find(':selected').data('postcode');
112
113        if (postcode !== undefined) {
114            $container.find('#billing_postcode, #shipping_postcode, #calc_shipping_postcode').val(postcode);
115        } else {
116            $container.find('#billing_postcode, #shipping_postcode, #calc_shipping_postcode').val('');
117        }
118    });
119
120    /* Ajax replaces .cart_totals (child of .cart-collaterals) on shipping calculator */
121    if ($('.cart-collaterals').length && $('#calc_shipping_state').length) {
122        var calc_observer = new MutationObserver(function () {
123            $('#calc_shipping_state').change();
124        });
125        calc_observer.observe(document.querySelector('.cart-collaterals'), { childList: true });
126    }
127
128    function cityToInput($citybox) {
129        if ($citybox.is('input')) {
130            $citybox.prop('disabled', false);
131            return;
132        }
133
134        var input_name = $citybox.attr('name'),
135            input_id = $citybox.attr('id'),
136            placeholder = $citybox.attr('placeholder'),
137            $newcity = $('<input type="text" />')
138                .prop('id', input_id)
139                .prop('name', input_name)
140                .prop('placeholder', placeholder)
141                .addClass('input-text');
142
143        $citybox.parent().find('.select2-container').remove();
144        $citybox.replaceWith($newcity);
145
146        $('#' + input_id).closest('.form-row').parent()
147            .find('#billing_postcode, #shipping_postcode, #calc_shipping_postcode')
148            .val('');
149    }
150
151    function disableCity($citybox) {
152        $citybox.val('').change();
153        $citybox.prop('disabled', true);
154    }
155
156    function cityToSelect($citybox, current_cities) {
157        var value = $citybox.val();
158
159        if ($citybox.is('input')) {
160            var input_name = $citybox.attr('name'),
161                input_id = $citybox.attr('id'),
162                placeholder = $citybox.attr('placeholder'),
163                $newcity = $('<select></select>')
164                    .prop('id', input_id)
165                    .prop('name', input_name)
166                    .data('placeholder', placeholder)
167                    .addClass('city_select');
168
169            $citybox.replaceWith($newcity);
170            $citybox = $('#' + input_id);
171        } else {
172            $citybox.prop('disabled', false);
173        }
174
175        var $defaultOption = $('<option></option>')
176            .attr('value', '')
177            .text(ry_wc_city_select_params.i18n_select_city_text);
178        $citybox.empty().append($defaultOption);
179
180        for (var index in current_cities) {
181            if (current_cities.hasOwnProperty(index)) {
182                var $option = $('<option></option>');
183                if (current_cities[index] instanceof Array) {
184                    var cityName = current_cities[index][0];
185                    $option.attr('data-postcode', current_cities[index][1]);
186                } else {
187                    var cityName = current_cities[index];
188                }
189                $option.prop('value', cityName)
190                    .text(cityName);
191                $citybox.append($option);
192            }
193        }
194
195        if ($('option[value="' + value + '"]', $citybox).length) {
196            $citybox.val(value).change();
197        } else {
198            $citybox.val('').change();
199        }
200
201        $(document.body).trigger('city_to_select');
202    }
203});

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.