Skip to content

Commit

Permalink
[TASK] Moved changes to dist and minified the (changed) source.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjoller committed Oct 4, 2018
1 parent 16281ee commit 1839831
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 71 deletions.
87 changes: 46 additions & 41 deletions dist/js/jquery.flagstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
*/
(function ($) {

var defaults = {
let defaults = {
inputId: undefined,
buttonSize: "btn-md",
buttonType: "btn-default",
labelMargin: "10px",
Expand All @@ -17,10 +18,11 @@
placeholder: {
value: '',
text: 'Please select country'
}
},
selectedCountry: ''
};

var countries = {
let countries = {
"AF": "Afghanistan",
"AL": "Albania",
"DZ": "Algeria",
Expand Down Expand Up @@ -267,17 +269,17 @@

$.flagStrap = function (element, options, i) {

var plugin = this;
let plugin = this;

var uniqueId = generateId(8);
let uniqueId = generateId(8);

plugin.countries = {};
plugin.selected = {value: null, text: null};
plugin.settings = {inputName: 'country-' + uniqueId};
plugin.selected = { value: null, text: null };
plugin.settings = { inputName: 'country-' + uniqueId };

var $container = $(element);
var htmlSelectId = 'flagstrap-' + uniqueId;
var htmlSelect = '#' + htmlSelectId;
let $container = $(element);
let htmlSelectId = 'flagstrap-' + uniqueId;
let htmlSelect = '#' + htmlSelectId;

plugin.init = function () {

Expand Down Expand Up @@ -307,7 +309,7 @@
// 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;
let element = this;
options.onSelect($(element).val(), element);
});
}
Expand All @@ -317,54 +319,58 @@

};

var buildHtmlSelect = function () {
var htmlSelectElement = $('<select/>').attr('id', htmlSelectId).attr('name', plugin.settings.inputName);
let buildHtmlSelect = function () {
let htmlSelectElement = $('<select/>').attr('id', htmlSelectId).attr('name', plugin.settings.inputName);

$.each(plugin.countries, function (code, country) {
var optionAttributes = {value: code};
let optionAttributes = { value: code };
if (plugin.settings.selectedCountry !== undefined) {
if (plugin.settings.selectedCountry === code) {
optionAttributes = {value: code, selected: "selected"};
plugin.selected = {value: code, text: country}
optionAttributes = { value: code, selected: "selected" };
plugin.selected = { value: code, text: country }
}
}
htmlSelectElement.append($('<option>', optionAttributes).text(country));
});

if (plugin.settings.placeholder !== false) {
htmlSelectElement.prepend($('<option>', {
htmlSelectElement.prepend($('<option selected>', {
value: plugin.settings.placeholder.value,
text: plugin.settings.placeholder.text
text: plugin.settings.placeholder.text,
}));
plugin.selected = { value: plugin.settings.placeholder.value, text: plugin.settings.placeholder.text }
}

return htmlSelectElement;
};

var buildDropDownButton = function () {
let buildDropDownButton = function () {

var selectedText = $(htmlSelect).find('option').first().text();
var selectedValue = $(htmlSelect).find('option').first().val();
let firstOption = $(htmlSelect + ' option:first-child');
let selectedText = firstOption.text();
let selectedValue = firstOption.val();
let selectedLabel = $('<i/>').addClass('flagstrap-icon flagstrap-placeholder');

selectedText = plugin.selected.text || selectedText;
selectedValue = plugin.selected.value || selectedValue;

if (selectedValue !== plugin.settings.placeholder.value) {
var $selectedLabel = $('<i/>').addClass('flagstrap-icon flagstrap-' + selectedValue.toLowerCase()).css('margin-right', plugin.settings.labelMargin);
} else {
var $selectedLabel = $('<i/>').addClass('flagstrap-icon flagstrap-placeholder');
selectedLabel = $('<i/>').addClass('flagstrap-icon flagstrap-' + selectedValue.toLowerCase()).css('margin-right', plugin.settings.labelMargin);
}

var buttonLabel = $('<span/>')
let buttonLabel = $('<span/>')
.addClass('flagstrap-selected-' + uniqueId)
.html($selectedLabel)
.html(selectedLabel)
.append(selectedText);

var button = $('<button/>')
let button = $('<button/>')
.attr('type', 'button')
.attr('data-toggle', 'dropdown')
.attr('id', 'flagstrap-drop-down-' + uniqueId)
.addClass('btn ' + plugin.settings.buttonType + ' ' + plugin.settings.buttonSize + ' dropdown-toggle')
.on('click', function (e) {
$('#flagstrap-drop-down-' + uniqueId + '-list').toggle();
})
.html(buttonLabel);

$('<span/>')
Expand All @@ -376,8 +382,8 @@

};

var buildDropDownButtonItemList = function () {
var items = $('<ul/>')
let buildDropDownButtonItemList = function () {
let items = $('<ul/>')
.attr('id', 'flagstrap-drop-down-' + uniqueId + '-list')
.attr('aria-labelled-by', 'flagstrap-drop-down-' + uniqueId)
.addClass('dropdown-menu');
Expand All @@ -389,34 +395,33 @@
}

// Populate the bootstrap dropdown item list
$(htmlSelect).find('option').each(function () {
$(htmlSelect + ' option').each(function () {

// Get original select option values and labels
var text = $(this).text();
var value = $(this).val();
let text = $(this).text();
let value = $(this).val();
let flagIcon = null;

// Build the flag icon
if (value !== plugin.settings.placeholder.value) {
var flagIcon = $('<i/>').addClass('flagstrap-icon flagstrap-' + value.toLowerCase()).css('margin-right', plugin.settings.labelMargin);
} else {
var flagIcon = null;
flagIcon = $('<i/>').addClass('flagstrap-icon flagstrap-' + value.toLowerCase()).css('margin-right', plugin.settings.labelMargin);
}


// Build a clickable drop down option item, insert the flag and label, attach click event
var flagStrapItem = $('<a/>')
let flagStrapItem = $('<a/>')
.attr('data-val', $(this).val())
.html(flagIcon)
.append(text)
.on('click', function (e) {
$(htmlSelect).val($(this).data('val'));
$(htmlSelect).trigger('change');
$('.flagstrap-selected-' + uniqueId).html($(this).html());
$('#flagstrap-drop-down-' + uniqueId + '-list').toggle();
e.preventDefault();
});

// Make it a list item
var listItem = $('<li/>').prepend(flagStrapItem);
let listItem = $('<li/>').prepend(flagStrapItem);

// Append it to the drop down item list
items.append(listItem);
Expand All @@ -427,14 +432,14 @@
};

function generateId(length) {
var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'.split('');
let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'.split('');

if (!length) {
length = Math.floor(Math.random() * chars.length);
}

var str = '';
for (var i = 0; i < length; i++) {
let str = '';
for (let i = 0; i < length; i++) {
str += chars[Math.floor(Math.random() * chars.length)];
}
return str;
Expand Down
Loading

0 comments on commit 1839831

Please sign in to comment.