PageSourceSearch

https://francis.co.uk/wp-content/themes/twentyseventeen/assets/js/jquery.flagstrap.js

js francis.co.uk collected 2026-10-03 18:02:43 UTC 14,793 bytes, 457 lines download raw bytes

1/*
2 *  FlagStrap - v1.0
3 *  A lightwieght jQuery plugin for creating Bootstrap 3 compatible country select boxes with flags.
4 *  http://www.blazeworx.com/flagstrap
5 *
6 *  Made by Alex Carter
7 *  Under MIT License
8 */
9(function ($) {
10
11    var defaults = {
12        buttonSize: "btn-md",
13        buttonType: "btn-default",
14        labelMargin: "10px",
15        scrollable: true,
16        scrollableHeight: "250px",
17        placeholder: {
18            value: '',
19            text: 'Please select country'
20        }
21    };
22
23    var countries = {
24        "AF": "Afghanistan",
25        "AL": "Albania",
26        "DZ": "Algeria",
27        "AS": "American Samoa",
28        "AD": "Andorra",
29        "AO": "Angola",
30        "AI": "Anguilla",
31        "AG": "Antigua and Barbuda",
32        "AR": "Argentina",
33        "AM": "Armenia",
34        "AW": "Aruba",
35        "AU": "Australia",
36        "AT": "Austria",
37        "AZ": "Azerbaijan",
38        "BS": "Bahamas",
39        "BH": "Bahrain",
40        "BD": "Bangladesh",
41        "BB": "Barbados",
42        "BY": "Belarus",
43        "BE": "Belgium",
44        "BZ": "Belize",
45        "BJ": "Benin",
46        "BM": "Bermuda",
47        "BT": "Bhutan",
48        "BO": "Bolivia, Plurinational State of",
49        "BA": "Bosnia and Herzegovina",
50        "BW": "Botswana",
51        "BV": "Bouvet Island",
52        "BR": "Brazil",
53        "IO": "British Indian Ocean Territory",
54        "BN": "Brunei Darussalam",
55        "BG": "Bulgaria",
56        "BF": "Burkina Faso",
57        "BI": "Burundi",
58        "KH": "Cambodia",
59        "CM": "Cameroon",
60        "CA": "Canada",
61        "CV": "Cape Verde",
62        "KY": "Cayman Islands",
63        "CF": "Central African Republic",
64        "TD": "Chad",
65        "CL": "Chile",
66        "CN": "China",
67        "CO": "Colombia",
68        "KM": "Comoros",
69        "CG": "Congo",
70        "CD": "Congo, the Democratic Republic of the",
71        "CK": "Cook Islands",
72        "CR": "Costa Rica",
73        "CI": "C" + "ô" + "te d'Ivoire",
74        "HR": "Croatia",
75        "CU": "Cuba",
76        "CW": "Cura" + "ç" + "ao",
77        "CY": "Cyprus",
78        "CZ": "Czech Republic",
79        "DK": "Denmark",
80        "DJ": "Djibouti",
81        "DM": "Dominica",
82        "DO": "Dominican Republic",
83        "EC": "Ecuador",
84        "EG": "Egypt",
85        "SV": "El Salvador",
86        "GQ": "Equatorial Guinea",
87        "ER": "Eritrea",
88        "EE": "Estonia",
89        "ET": "Ethiopia",
90        "FK": "Falkland Islands (Malvinas)",
91        "FO": "Faroe Islands",
92        "FJ": "Fiji",
93        "FI": "Finland",
94        "FR": "France",
95        "GF": "French Guiana",
96        "PF": "French Polynesia",
97        "TF": "French Southern Territories",
98        "GA": "Gabon",
99        "GM": "Gambia",
100        "GE": "Georgia",
101        "DE": "Germany",
102        "GH": "Ghana",
103        "GI": "Gibraltar",
104        "GR": "Greece",
105        "GL": "Greenland",
106        "GD": "Grenada",
107        "GP": "Guadeloupe",
108        "GU": "Guam",
109        "GT": "Guatemala",
110        "GG": "Guernsey",
111        "GN": "Guinea",
112        "GW": "Guinea-Bissau",
113        "GY": "Guyana",
114        "HT": "Haiti",
115        "HM": "Heard Island and McDonald Islands",
116        "VA": "Holy See (Vatican City State)",
117        "HN": "Honduras",
118        "HK": "Hong Kong",
119        "HU": "Hungary",
120        "IS": "Iceland",
121        "IN": "India",
122        "ID": "Indonesia",
123        "IR": "Iran, Islamic Republic of",
124        "IQ": "Iraq",
125        "IE": "Ireland",
126        "IM": "Isle of Man",
127        "IL": "Israel",
128        "IT": "Italy",
129        "JM": "Jamaica",
130        "JP": "Japan",
131        "JE": "Jersey",
132        "JO": "Jordan",
133        "KZ": "Kazakhstan",
134        "KE": "Kenya",
135        "KI": "Kiribati",
136        "KP": "Korea, Democratic People's Republic of",
137        "KR": "Korea, Republic of",
138        "KW": "Kuwait",
139        "KG": "Kyrgyzstan",
140        "LA": "Lao People's Democratic Republic",
141        "LV": "Latvia",
142        "LB": "Lebanon",
143        "LS": "Lesotho",
144        "LR": "Liberia",
145        "LY": "Libya",
146        "LI": "Liechtenstein",
147        "LT": "Lithuania",
148        "LU": "Luxembourg",
149        "MO": "Macao",
150        "MK": "Macedonia, the former Yugoslav Republic of",
151        "MG": "Madagascar",
152        "MW": "Malawi",
153        "MY": "Malaysia",
154        "MV": "Maldives",
155        "ML": "Mali",
156        "MT": "Malta",
157        "MH": "Marshall Islands",
158        "MQ": "Martinique",
159        "MR": "Mauritania",
160        "MU": "Mauritius",
161        "YT": "Mayotte",
162        "MX": "Mexico",
163        "FM": "Micronesia, Federated States of",
164        "MD": "Moldova, Republic of",
165        "MC": "Monaco",
166        "MN": "Mongolia",
167        "ME": "Montenegro",
168        "MS": "Montserrat",
169        "MA": "Morocco",
170        "MZ": "Mozambique",
171        "MM": "Myanmar",
172        "NA": "Namibia",
173        "NR": "Nauru",
174        "NP": "Nepal",
175        "NL": "Netherlands",
176        "NC": "New Caledonia",
177        "NZ": "New Zealand",
178        "NI": "Nicaragua",
179        "NE": "Niger",
180        "NG": "Nigeria",
181        "NU": "Niue",
182        "NF": "Norfolk Island",
183        "MP": "Northern Mariana Islands",
184        "NO": "Norway",
185        "OM": "Oman",
186        "PK": "Pakistan",
187        "PW": "Palau",
188        "PS": "Palestinian Territory, Occupied",
189        "PA": "Panama",
190        "PG": "Papua New Guinea",
191        "PY": "Paraguay",
192        "PE": "Peru",
193        "PH": "Philippines",
194        "PN": "Pitcairn",
195        "PL": "Poland",
196        "PT": "Portugal",
197        "PR": "Puerto Rico",
198        "QA": "Qatar",
199        "RE": "R" + "é" + "union",
200        "RO": "Romania",
201        "RU": "Russian Federation",
202        "RW": "Rwanda",
203        "SH": "Saint Helena, Ascension and Tristan da Cunha",
204        "KN": "Saint Kitts and Nevis",
205        "LC": "Saint Lucia",
206        "MF": "Saint Martin (French part)",
207        "PM": "Saint Pierre and Miquelon",
208        "VC": "Saint Vincent and the Grenadines",
209        "WS": "Samoa",
210        "SM": "San Marino",
211        "ST": "Sao Tome and Principe",
212        "SA": "Saudi Arabia",
213        "SN": "Senegal",
214        "RS": "Serbia",
215        "SC": "Seychelles",
216        "SL": "Sierra Leone",
217        "SG": "Singapore",
218        "SX": "Sint Maarten (Dutch part)",
219        "SK": "Slovakia",
220        "SI": "Slovenia",
221        "SB": "Solomon Islands",
222        "SO": "Somalia",
223        "ZA": "South Africa",
224        "GS": "South Georgia and the South Sandwich Islands",
225        "SS": "South Sudan",
226        "ES": "Spain",
227        "LK": "Sri Lanka",
228        "SD": "Sudan",
229        "SR": "Suriname",
230        "SZ": "Swaziland",
231        "SE": "Sweden",
232        "CH": "Switzerland",
233        "SY": "Syrian Arab Republic",
234        "TW": "Taiwan, Province of China",
235        "TJ": "Tajikistan",
236        "TZ": "Tanzania, United Republic of",
237        "TH": "Thailand",
238        "TL": "Timor-Leste",
239        "TG": "Togo",
240        "TK": "Tokelau",
241        "TO": "Tonga",
242        "TT": "Trinidad and Tobago",
243        "TN": "Tunisia",
244        "TR": "Turkey",
245        "TM": "Turkmenistan",
246        "TC": "Turks and Caicos Islands",
247        "TV": "Tuvalu",
248        "UG": "Uganda",
249        "UA": "Ukraine",
250        "AE": "United Arab Emirates",
251        "GB": "United Kingdom",
252        "US": "United States",
253        "UM": "United States Minor Outlying Islands",
254        "UY": "Uruguay",
255        "UZ": "Uzbekistan",
256        "VU": "Vanuatu",
257        "VE": "Venezuela, Bolivarian Republic of",
258        "VN": "Viet Nam",
259        "VG": "Virgin Islands, British",
260        "VI": "Virgin Islands, U.S.",
261        "WF": "Wallis and Futuna",
262        "EH": "Western Sahara",
263        "YE": "Yemen",
264        "ZM": "Zambia",
265        "ZW": "Zimbabwe"
266    };
267
268    $.flagStrap = function (element, options, i) {
269
270        var plugin = this;
271
272        var uniqueId = generateId(8);
273
274        plugin.countries = {};
275        plugin.selected = {value: null, text: null};
276        plugin.settings = {inputName: 'country-' + uniqueId};
277
278        var $container = $(element);
279        var htmlSelectId = 'flagstrap-' + uniqueId;
280        var htmlSelect = '#' + htmlSelectId;
281
282        plugin.init = function () {
283
284            // Merge in global settings then merge in individual settings via data attributes
285            plugin.countries = countries;
286
287            // Initialize Settings, priority: defaults, init options, data attributes
288            plugin.countries = countries;
289            plugin.settings = $.extend({}, defaults, options, $container.data());
290
291            if (undefined !== plugin.settings.countries) {
292                plugin.countries = plugin.settings.countries;
293            }
294
295            if (undefined !== plugin.settings.inputId) {
296                htmlSelectId = plugin.settings.inputId;
297                htmlSelect = '#' + htmlSelectId;
298            }
299
300            // Build HTML Select, Construct the drop down button, Assemble the drop down list items element and insert
301            $container
302                .addClass('flagstrap')
303                .append(buildHtmlSelect)
304                .append(buildDropDownButton)
305                .append(buildDropDownButtonItemList);
306
307            // Check to see if the onSelect callback method is assigned / callable, bind the change event for broadcast
308            if (plugin.settings.onSelect !== undefined && plugin.settings.onSelect instanceof Function) {
309                $(htmlSelect).change(function (event) {
310                    var element = this;
311                    options.onSelect($(element).val(), element);
312                });
313            }
314
315            // Hide the actual HTML select
316            $(htmlSelect).hide();
317
318        };
319
320        var buildHtmlSelect = function () {
321            var htmlSelectElement = $('<select/>').attr('id', htmlSelectId).attr('name', plugin.settings.inputName);
322
323            $.each(plugin.countries, function (code, country) {
324                var optionAttributes = {value: code};
325                if (plugin.settings.selectedCountry !== undefined) {
326                    if (plugin.settings.selectedCountry === code) {
327                        optionAttributes = {value: code, selected: "selected"};
328                        plugin.selected = {value: code, text: country}
329                    }
330                }
331                htmlSelectElement.append($('<option>', optionAttributes).text(country));
332            });
333
334            if (plugin.settings.placeholder !== false) {
335                htmlSelectElement.prepend($('<option>', {
336                    value: plugin.settings.placeholder.value,
337                    text: plugin.settings.placeholder.text
338                }));
339            }
340
341            return htmlSelectElement;
342        };
343
344        var buildDropDownButton = function () {
345
346            var selectedText = $(htmlSelect).find('option').first().text();
347            var selectedValue = $(htmlSelect).find('option').first().val();
348
349            selectedText = plugin.selected.text || selectedText;
350            selectedValue = plugin.selected.value || selectedValue;
351
352            if (selectedValue !== plugin.settings.placeholder.value) {
353                var $selectedLabel = $('<i/>').addClass('flagstrap-icon flagstrap-' + selectedValue.toLowerCase()).css('margin-right', plugin.settings.labelMargin);
354            } else {
355                var $selectedLabel = $('<i/>').addClass('flagstrap-icon flagstrap-placeholder');
356            }
357
358            var buttonLabel = $('<span/>')
359                .addClass('flagstrap-selected-' + uniqueId)
360                .html($selectedLabel)
361                .append(selectedText);
362
363            var button = $('<button/>')
364                .attr('type', 'button')
365                .attr('data-toggle', 'dropdown')
366                .attr('id', 'flagstrap-drop-down-' + uniqueId)
367                .addClass('btn ' + plugin.settings.buttonType + ' ' + plugin.settings.buttonSize + ' dropdown-toggle')
368                .html(buttonLabel);
369
370            $('<span/>')
371                .addClass('caret')
372                .css('margin-left', plugin.settings.labelMargin)
373                .insertAfter(buttonLabel);
374
375            return button;
376
377        };
378
379        var buildDropDownButtonItemList = function () {
380            var items = $('<ul/>')
381                .attr('id', 'flagstrap-drop-down-' + uniqueId + '-list')
382                .attr('aria-labelled-by', 'flagstrap-drop-down-' + uniqueId)
383                .addClass('dropdown-menu');
384
385            if (plugin.settings.scrollable) {
386                items.css('height', 'auto')
387                    .css('max-height', plugin.settings.scrollableHeight)
388                    .css('overflow-x', 'hidden');
389            }
390
391            // Populate the bootstrap dropdown item list
392            $(htmlSelect).find('option').each(function () {
393
394                // Get original select option values and labels
395                var text = $(this).text();
396                var value = $(this).val();
397
398                // Build the flag icon
399                if (value !== plugin.settings.placeholder.value) {
400                    var flagIcon = $('<i/>').addClass('flagstrap-icon flagstrap-' + value.toLowerCase()).css('margin-right', plugin.settings.labelMargin);
401                } else {
402                    var flagIcon = null;
403                }
404
405
406                // Build a clickable drop down option item, insert the flag and label, attach click event
407                var flagStrapItem = $('<a/>')
408                    .attr('data-val', $(this).val())
409                    .html(flagIcon)
410                    .append(text)
411                    .on('click', function (e) {
412                        $(htmlSelect).val($(this).data('val'));
413                        $(htmlSelect).trigger('change');
414                        $('.flagstrap-selected-' + uniqueId).html($(this).html());
415                        e.preventDefault();
416                    });
417
418                // Make it a list item
419                var listItem = $('<li/>').prepend(flagStrapItem);
420
421                // Append it to the drop down item list
422                items.append(listItem);
423
424            });
425
426            return items;
427        };
428
429        function generateId(length) {
430            var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'.split('');
431
432            if (!length) {
433                length = Math.floor(Math.random() * chars.length);
434            }
435
436            var str = '';
437            for (var i = 0; i < length; i++) {
438                str += chars[Math.floor(Math.random() * chars.length)];
439            }
440            return str;
441        }
442
443        plugin.init();
444
445    };
446
447    $.fn.flagStrap = function (options) {
448
449        return this.each(function (i) {
450            if ($(this).data('flagStrap') === undefined) {
451                $(this).data('flagStrap', new $.flagStrap(this, options, i)
451);
452            }
453        });
454
455    }
456
457})(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.