From 6704fc833a273633c03a206a08e0cedd85af5b5a Mon Sep 17 00:00:00 2001 From: Florin Cosmin Date: Fri, 5 May 2017 13:26:12 +0200 Subject: [PATCH 1/2] added npm start task. fixed grunt jshint and added code so that countries can be passed as an array of objects --- .jshintrc | 26 +- Gruntfile.js | 136 ++--- dist/js/jquery.flagstrap.js | 945 ++++++++++++++++---------------- dist/js/jquery.flagstrap.min.js | 18 +- package.json | 27 +- src/jquery.flagstrap.js | 929 ++++++++++++++++--------------- 6 files changed, 1071 insertions(+), 1010 deletions(-) diff --git a/.jshintrc b/.jshintrc index 0a30119..7eb3a49 100644 --- a/.jshintrc +++ b/.jshintrc @@ -1,13 +1,13 @@ -{ - "boss": true, - "curly": true, - "eqeqeq": true, - "eqnull": true, - "expr": true, - "immed": true, - "noarg": true, - "onevar": true, - "quotmark": "double", - "unused": true, - "node": true -} \ No newline at end of file +{ + "boss": true, + "curly": true, + "eqeqeq": true, + "eqnull": true, + "expr": true, + "immed": true, + "noarg": true, + "onevar": true, + "quotmark": "double", + "unused": true, + "node": true +} diff --git a/Gruntfile.js b/Gruntfile.js index 7e23067..2403940 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1,67 +1,69 @@ -module.exports = function(grunt) { - - grunt.initConfig({ - - // Import package manifest - pkg: grunt.file.readJSON("flagstrap.jquery.json"), - - // Banner definitions - meta: { - banner: "/*\n" + - " * <%= pkg.title || pkg.name %> - v<%= pkg.version %>\n" + - " * <%= pkg.description %>\n" + - " * <%= pkg.homepage %>\n" + - " *\n" + - " * Made by <%= pkg.author.name %>\n" + - " * Under <%= pkg.licenses[0].type %> License\n" + - " */\n" - }, - - // Concat definitions - concat: { - dist: { - src: ["src/jquery.flagstrap.js"], - dest: "dist/js/jquery.flagstrap.js" - }, - options: { - banner: "<%= meta.banner %>" - } - }, - - // Lint definitions - jshint: { - files: ["src/jquery.boilerplate.js"], - options: { - jshintrc: ".jshintrc" - } - }, - - // Minify definitions - uglify: { - my_target: { - src: ["dist/js/jquery.flagstrap.js"], - dest: "dist/js/jquery.flagstrap.min.js" - }, - options: { - banner: "<%= meta.banner %>" - } - }, - - // watch for changes to source - // Better than calling grunt a million times - // (call 'grunt watch') - watch: { - files: ['src/*'], - tasks: ['default'] - } - - }); - - grunt.loadNpmTasks("grunt-contrib-concat"); - grunt.loadNpmTasks("grunt-contrib-jshint"); - grunt.loadNpmTasks("grunt-contrib-uglify"); - grunt.loadNpmTasks("grunt-contrib-watch"); - - grunt.registerTask("default", ["jshint", "concat", "uglify"]); - -}; \ No newline at end of file +module.exports = function(grunt) { + + grunt.initConfig({ + + // Import package manifest + pkg: grunt.file.readJSON("flagstrap.jquery.json"), + + // Banner definitions + meta: { + banner: "/*\n" + + " * <%= pkg.title || pkg.name %> - v<%= pkg.version %>\n" + + " * <%= pkg.description %>\n" + + " * <%= pkg.homepage %>\n" + + " *\n" + + " * Made by <%= pkg.author.name %>\n" + + " * Under <%= pkg.licenses[0].type %> License\n" + + " */\n" + }, + + // Concat definitions + concat: { + dist: { + src: ["src/jquery.flagstrap.js"], + dest: "dist/js/jquery.flagstrap.js" + }, + options: { + banner: "<%= meta.banner %>" + } + }, + + // Lint definitions + jshint: { + files: ["src/jquery.flagstrap.js"], + path: "src/jquery.flagstrap.js", + options: { + jshintrc: ".jshintrc", + reporterOutput: "" + } + }, + + // Minify definitions + uglify: { + my_target: { + src: ["dist/js/jquery.flagstrap.js"], + dest: "dist/js/jquery.flagstrap.min.js" + }, + options: { + banner: "<%= meta.banner %>" + } + }, + + // watch for changes to source + // Better than calling grunt a million times + // (call 'grunt watch') + watch: { + files: ['src/*'], + tasks: ['default'] + } + + }); + + grunt.loadNpmTasks("grunt-contrib-concat"); + grunt.loadNpmTasks("grunt-contrib-jshint"); + grunt.loadNpmTasks("grunt-contrib-uglify"); + grunt.loadNpmTasks("grunt-contrib-watch"); + + grunt.registerTask("default", ["jshint", "concat", "uglify"]); + +}; diff --git a/dist/js/jquery.flagstrap.js b/dist/js/jquery.flagstrap.js index bbe1e0b..33de3ed 100644 --- a/dist/js/jquery.flagstrap.js +++ b/dist/js/jquery.flagstrap.js @@ -1,458 +1,487 @@ -/* - * FlagStrap - v1.0 - * A lightwieght jQuery plugin for creating Bootstrap 3 compatible country select boxes with flags. - * http://www.blazeworx.com/flagstrap - * - * Made by Alex Carter - * Under MIT License - */ -(function ($) { - - var defaults = { - buttonSize: "btn-md", - buttonType: "btn-default", - labelMargin: "10px", - scrollable: true, - scrollableHeight: "250px", - placeholder: { - value: '', - text: 'Please select country' - } - }; - - var countries = { - "AF": "Afghanistan", - "AL": "Albania", - "DZ": "Algeria", - "AS": "American Samoa", - "AD": "Andorra", - "AO": "Angola", - "AI": "Anguilla", - "AG": "Antigua and Barbuda", - "AR": "Argentina", - "AM": "Armenia", - "AW": "Aruba", - "AU": "Australia", - "AT": "Austria", - "AZ": "Azerbaijan", - "BS": "Bahamas", - "BH": "Bahrain", - "BD": "Bangladesh", - "BB": "Barbados", - "BY": "Belarus", - "BE": "Belgium", - "BZ": "Belize", - "BJ": "Benin", - "BM": "Bermuda", - "BT": "Bhutan", - "BO": "Bolivia, Plurinational State of", - "BA": "Bosnia and Herzegovina", - "BW": "Botswana", - "BV": "Bouvet Island", - "BR": "Brazil", - "IO": "British Indian Ocean Territory", - "BN": "Brunei Darussalam", - "BG": "Bulgaria", - "BF": "Burkina Faso", - "BI": "Burundi", - "KH": "Cambodia", - "CM": "Cameroon", - "CA": "Canada", - "CV": "Cape Verde", - "KY": "Cayman Islands", - "CF": "Central African Republic", - "TD": "Chad", - "CL": "Chile", - "CN": "China", - "CO": "Colombia", - "KM": "Comoros", - "CG": "Congo", - "CD": "Congo, the Democratic Republic of the", - "CK": "Cook Islands", - "CR": "Costa Rica", - "CI": "C" + "ô" + "te d'Ivoire", - "HR": "Croatia", - "CU": "Cuba", - "CW": "Cura" + "ç" + "ao", - "CY": "Cyprus", - "CZ": "Czech Republic", - "DK": "Denmark", - "DJ": "Djibouti", - "DM": "Dominica", - "DO": "Dominican Republic", - "EC": "Ecuador", - "EG": "Egypt", - "SV": "El Salvador", - "GQ": "Equatorial Guinea", - "ER": "Eritrea", - "EE": "Estonia", - "ET": "Ethiopia", - "FK": "Falkland Islands (Malvinas)", - "FO": "Faroe Islands", - "FJ": "Fiji", - "FI": "Finland", - "FR": "France", - "GF": "French Guiana", - "PF": "French Polynesia", - "TF": "French Southern Territories", - "GA": "Gabon", - "GM": "Gambia", - "GE": "Georgia", - "DE": "Germany", - "GH": "Ghana", - "GI": "Gibraltar", - "GR": "Greece", - "GL": "Greenland", - "GD": "Grenada", - "GP": "Guadeloupe", - "GU": "Guam", - "GT": "Guatemala", - "GG": "Guernsey", - "GN": "Guinea", - "GW": "Guinea-Bissau", - "GY": "Guyana", - "HT": "Haiti", - "HM": "Heard Island and McDonald Islands", - "VA": "Holy See (Vatican City State)", - "HN": "Honduras", - "HK": "Hong Kong", - "HU": "Hungary", - "IS": "Iceland", - "IN": "India", - "ID": "Indonesia", - "IR": "Iran, Islamic Republic of", - "IQ": "Iraq", - "IE": "Ireland", - "IM": "Isle of Man", - "IL": "Israel", - "IT": "Italy", - "JM": "Jamaica", - "JP": "Japan", - "JE": "Jersey", - "JO": "Jordan", - "KZ": "Kazakhstan", - "KE": "Kenya", - "KI": "Kiribati", - "KP": "Korea, Democratic People's Republic of", - "KR": "Korea, Republic of", - "KW": "Kuwait", - "KG": "Kyrgyzstan", - "LA": "Lao People's Democratic Republic", - "LV": "Latvia", - "LB": "Lebanon", - "LS": "Lesotho", - "LR": "Liberia", - "LY": "Libya", - "LI": "Liechtenstein", - "LT": "Lithuania", - "LU": "Luxembourg", - "MO": "Macao", - "MK": "Macedonia, the former Yugoslav Republic of", - "MG": "Madagascar", - "MW": "Malawi", - "MY": "Malaysia", - "MV": "Maldives", - "ML": "Mali", - "MT": "Malta", - "MH": "Marshall Islands", - "MQ": "Martinique", - "MR": "Mauritania", - "MU": "Mauritius", - "YT": "Mayotte", - "MX": "Mexico", - "FM": "Micronesia, Federated States of", - "MD": "Moldova, Republic of", - "MC": "Monaco", - "MN": "Mongolia", - "ME": "Montenegro", - "MS": "Montserrat", - "MA": "Morocco", - "MZ": "Mozambique", - "MM": "Myanmar", - "NA": "Namibia", - "NR": "Nauru", - "NP": "Nepal", - "NL": "Netherlands", - "NC": "New Caledonia", - "NZ": "New Zealand", - "NI": "Nicaragua", - "NE": "Niger", - "NG": "Nigeria", - "NU": "Niue", - "NF": "Norfolk Island", - "MP": "Northern Mariana Islands", - "NO": "Norway", - "OM": "Oman", - "PK": "Pakistan", - "PW": "Palau", - "PS": "Palestinian Territory, Occupied", - "PA": "Panama", - "PG": "Papua New Guinea", - "PY": "Paraguay", - "PE": "Peru", - "PH": "Philippines", - "PN": "Pitcairn", - "PL": "Poland", - "PT": "Portugal", - "PR": "Puerto Rico", - "QA": "Qatar", - "RE": "R" + "é" + "union", - "RO": "Romania", - "RU": "Russian Federation", - "RW": "Rwanda", - "SH": "Saint Helena, Ascension and Tristan da Cunha", - "KN": "Saint Kitts and Nevis", - "LC": "Saint Lucia", - "MF": "Saint Martin (French part)", - "PM": "Saint Pierre and Miquelon", - "VC": "Saint Vincent and the Grenadines", - "WS": "Samoa", - "SM": "San Marino", - "ST": "Sao Tome and Principe", - "SA": "Saudi Arabia", - "SN": "Senegal", - "RS": "Serbia", - "SC": "Seychelles", - "SL": "Sierra Leone", - "SG": "Singapore", - "SX": "Sint Maarten (Dutch part)", - "SK": "Slovakia", - "SI": "Slovenia", - "SB": "Solomon Islands", - "SO": "Somalia", - "ZA": "South Africa", - "GS": "South Georgia and the South Sandwich Islands", - "SS": "South Sudan", - "ES": "Spain", - "LK": "Sri Lanka", - "SD": "Sudan", - "SR": "Suriname", - "SZ": "Swaziland", - "SE": "Sweden", - "CH": "Switzerland", - "SY": "Syrian Arab Republic", - "TW": "Taiwan, Province of China", - "TJ": "Tajikistan", - "TZ": "Tanzania, United Republic of", - "TH": "Thailand", - "TL": "Timor-Leste", - "TG": "Togo", - "TK": "Tokelau", - "TO": "Tonga", - "TT": "Trinidad and Tobago", - "TN": "Tunisia", - "TR": "Turkey", - "TM": "Turkmenistan", - "TC": "Turks and Caicos Islands", - "TV": "Tuvalu", - "UG": "Uganda", - "UA": "Ukraine", - "AE": "United Arab Emirates", - "GB": "United Kingdom", - "US": "United States", - "UM": "United States Minor Outlying Islands", - "UY": "Uruguay", - "UZ": "Uzbekistan", - "VU": "Vanuatu", - "VE": "Venezuela, Bolivarian Republic of", - "VN": "Viet Nam", - "VG": "Virgin Islands, British", - "VI": "Virgin Islands, U.S.", - "WF": "Wallis and Futuna", - "EH": "Western Sahara", - "YE": "Yemen", - "ZM": "Zambia", - "ZW": "Zimbabwe" - }; - - $.flagStrap = function (element, options, i) { - - var plugin = this; - - var uniqueId = generateId(8); - - plugin.countries = {}; - plugin.selected = {value: null, text: null}; - plugin.settings = {inputName: 'country-' + uniqueId}; - - var $container = $(element); - var htmlSelectId = 'flagstrap-' + uniqueId; - var htmlSelect = '#' + htmlSelectId; - - plugin.init = function () { - - // Merge in global settings then merge in individual settings via data attributes - plugin.countries = countries; - - // Initialize Settings, priority: defaults, init options, data attributes - plugin.countries = countries; - plugin.settings = $.extend({}, defaults, options, $container.data()); - - if (undefined !== plugin.settings.countries) { - plugin.countries = plugin.settings.countries; - } - - if (undefined !== plugin.settings.inputId) { - htmlSelectId = plugin.settings.inputId; - htmlSelect = '#' + htmlSelectId; - } - - // Build HTML Select, Construct the drop down button, Assemble the drop down list items element and insert - $container - .addClass('flagstrap') - .append(buildHtmlSelect) - .append(buildDropDownButton) - .append(buildDropDownButtonItemList); - - // Check to see if the onSelect callback method is assigned / callable, bind the change event for broadcast - if (plugin.settings.onSelect !== undefined && plugin.settings.onSelect instanceof Function) { - $(htmlSelect).change(function (event) { - var element = this; - options.onSelect($(element).val(), element); - }); - } - - // Hide the actual HTML select - $(htmlSelect).hide(); - - }; - - var buildHtmlSelect = function () { - var htmlSelectElement = $('").attr("id", htmlSelectId).attr("name", plugin.settings.inputName); + + // check if countries is an array instead + if ($.isArray(plugin.countries)) { + var key, value, cObj, cObjKeys, optionAttributes; + + // loop through each item in the countries array + for(var i=0; i", optionAttributes).text(value)); + } + } else { + $.each(plugin.countries, function (code, country) { + var optionAttributes = {value: code}; + if (plugin.settings.selectedCountry !== undefined) { + if (plugin.settings.selectedCountry === code) { + optionAttributes = {value: code, selected: "selected"}; + plugin.selected = {value: code, text: country}; + } + } + htmlSelectElement.append($("