flagstrap 0.0.2

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