(function($) {
'use strict';
//
if (!String.prototype.includes) {
(function() {
'use strict'; // needed to support `apply`/`call` with `undefined`/`null`
var toString = {}.toString,
defineProperty = (function() {
// IE8 only supports `Object.defineProperty` on DOM elements
try {
var object = {},
$defineProperty = Object.defineProperty,
result = $defineProperty(object, object, object) && $defineProperty;
} catch (error) {}
return result;
}());
var indexOf = ''.indexOf,
includes = function(search) {
if (this===null) {
throw TypeError();
}
var string = String(this);
if (search && toString.call(search)==='[object RegExp]') {
throw TypeError();
}
var stringLength = string.length,
searchString = String(search),
searchLength = searchString.length,
position = arguments.length > 1 ? arguments[1] : undefined;
// `ToInteger`
var pos = position ? Number(position) : 0;
if (pos!==pos) { // better `isNaN`
pos = 0;
}
var start = Math.min(Math.max(pos, 0), stringLength);
// Avoid the `indexOf` call if no match is possible
if (searchLength + start > stringLength) {
return false;
}
return indexOf.call(string, searchString, pos)!==-1;
};
if (defineProperty) {
defineProperty(String.prototype, 'includes', {
'value': includes,
'configurable': true,
'writable': true
});
} else {
String.prototype.includes = includes;
}
}());
}
if (!String.prototype.startsWith) {
(function() {
'use strict'; // needed to support `apply`/`call` with `undefined`/`null`
var defineProperty = (function() {
// IE8 only supports `Object.defineProperty` on DOM elements
try {
var object = {},
$defineProperty = Object.defineProperty,
result = $defineProperty(object, object, object) && $defineProperty;
} catch (error) {}
return result;
}());
var toString = {}.toString,
startsWith = function(search) {
if (this===null) {
throw TypeError();
}
var string = String(this);
if (search && toString.call(search)==='[object RegExp]') {
throw TypeError();
}
var stringLength = string.length,
searchString = String(search),
searchLength = searchString.length,
position = arguments.length > 1 ? arguments[1] : undefined;
// `ToInteger`
var pos = position ? Number(position) : 0;
if (pos!==pos) { // better `isNaN`
pos = 0;
}
var start = Math.min(Math.max(pos, 0), stringLength);
// Avoid the `indexOf` call if no match is possible
if (searchLength + start > stringLength) {
return false;
}
var index = -1;
while (++index < searchLength) {
if (string.charCodeAt(start + index)!==searchString.charCodeAt(index)) {
return false;
}
}
return true;
};
if (defineProperty) {
defineProperty(String.prototype, 'startsWith', {
'value': startsWith,
'configurable': true,
'writable': true
});
} else {
String.prototype.startsWith = startsWith;
}
}());
}
//
// Case insensitive contains search
$.expr[':'].icontains = function(obj, index, meta) {
var $obj = $(obj),
haystack = ($obj.data('tokens') || $obj.text()).toUpperCase();
return haystack.includes(meta[3].toUpperCase());
};
// Case insensitive begins search
$.expr[':'].ibegins = function(obj, index, meta) {
var $obj = $(obj),
haystack = ($obj.data('tokens') || $obj.text()).toUpperCase();
return haystack.startsWith(meta[3].toUpperCase());
};
// Case and accent insensitive contains search
$.expr[':'].aicontains = function(obj, index, meta) {
var $obj = $(obj),
haystack = ($obj.data('tokens') || $obj.data('normalizedText') || $obj.text()).toUpperCase();
return haystack.includes(haystack, meta[3]);
};
// Case and accent insensitive begins search
$.expr[':'].aibegins = function(obj, index, meta) {
var $obj = $(obj),
haystack = ($obj.data('tokens') || $obj.data('normalizedText') || $obj.text()).toUpperCase();
return haystack.startsWith(meta[3].toUpperCase());
};
/**
* Remove all diatrics from the given text.
* @access private
* @param {String} text
* @returns {String}
*/
function normalizeToBase(text) {
var rExps = [
{re: /[\xC0-\xC6]/g, ch: 'A'},
{re: /[\xE0-\xE6]/g, ch: 'a'},
{re: /[\xC8-\xCB]/g, ch: 'E'},
{re: /[\xE8-\xEB]/g, ch: 'e'},
{re: /[\xCC-\xCF]/g, ch: 'I'},
{re: /[\xEC-\xEF]/g, ch: 'i'},
{re: /[\xD2-\xD6]/g, ch: 'O'},
{re: /[\xF2-\xF6]/g, ch: 'o'},
{re: /[\xD9-\xDC]/g, ch: 'U'},
{re: /[\xF9-\xFC]/g, ch: 'u'},
{re: /[\xC7-\xE7]/g, ch: 'c'},
{re: /[\xD1]/g, ch: 'N'},
{re: /[\xF1]/g, ch: 'n'}
];
$.each(rExps, function() {
text = text.replace(this.re, this.ch);
});
return text;
}
function htmlEscape(html) {
var escapeMap = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'`': '`'
};
var source = '(?:' + Object.keys(escapeMap).join('|') + ')',
testRegexp = new RegExp(source),
replaceRegexp = new RegExp(source, 'g'),
string = html===null ? '' : '' + html;
return testRegexp.test(string) ? string.replace(replaceRegexp, function(match) {
return escapeMap[match];
}) : string;
}
var Selectpicker = function(element, options, e) {
if (e) {
e.stopPropagation();
e.preventDefault();
}
this.$element = $(element);
this.$newElement = null;
this.$button = null;
this.$menu = null;
this.$lis = null;
this.options = options;
// If we have no title yet, try to pull it from the HTML title attribute
// (jQuery doesn't pick it up as it's not a data-attribute)
if (this.options.title===null) {
this.options.title = this.$element.attr('title');
}
// Expose public methods
this.val = Selectpicker.prototype.val;
this.render = Selectpicker.prototype.render;
this.refresh = Selectpicker.prototype.refresh;
this.setStyle = Selectpicker.prototype.setStyle;
this.selectAll = Selectpicker.prototype.selectAll;
this.deselectAll = Selectpicker.prototype.deselectAll;
this.destroy = Selectpicker.prototype.remove;
this.remove = Selectpicker.prototype.remove;
this.show = Selectpicker.prototype.show;
this.hide = Selectpicker.prototype.hide;
this.init();
};
Selectpicker.VERSION = '1.6.4';
// Part of this is duplicated in i18n/defaults-en_US.js (make sure to update both)
Selectpicker.DEFAULTS = {
noneSelectedText: 'Nothing selected',
noneResultsText: 'No results matched {0}',
countSelectedText: function(numSelected, numTotal) {
return (numSelected===1) ? '{0} item selected' : '{0} items selected';
},
maxOptionsText: function(numAll, numGroup) {
return [
(numAll===1) ? 'Limit reached ({n} item max)' : 'Limit reached ({n} items max)',
(numGroup===1) ? 'Group limit reached ({n} item max)' : 'Group limit reached ({n} items max)'
];
},
selectAllText: 'Select All',
deselectAllText: 'Deselect All',
doneButton: false,
doneButtonText: 'Close',
multipleSeparator: ', ',
style: 'btn-default',
size: 'auto',
title: null,
selectedTextFormat: 'values',
width: false,
container: false,
hideDisabled: false,
dropupAuto: true,
header: false,
liveSearch: false,
liveSearchPlaceholder: null,
liveSearchNormalize: false,
liveSearchStyle: 'contains',
actionsBox: false,
tickIcon: 'glyphicon glyphicon-ok',
caretIcon: 'caret',
maxOptions: false,
mobile: false,
selectOnTab: false,
dropdownAlignRight: false
};
Selectpicker.prototype = {
constructor: Selectpicker,
init: function() {
var that = this,
id = this.$element.attr('id');
this.$element.hide();
this.multiple = this.$element.prop('multiple');
this.autofocus = this.$element.prop('autofocus');
this.$newElement = this.createView();
this.$element.after(this.$newElement);
this.$button = this.$newElement.children('button');
this.$menu = this.$newElement.children('.dropdown-menu');
this.$searchbox = this.$menu.find('input');
if (this.options.dropdownAlignRight)
this.$menu.addClass('dropdown-menu-right');
if (id) {
this.$button.attr('data-id', id);
$('label[for="' + id + '"]').click(function(e) {
e.preventDefault();
that.$button.focus();
});
}
this.checkDisabled();
this.clickListener();
if (this.options.liveSearch) this.liveSearchListener();
this.render();
this.liHeight();
this.setStyle();
this.setWidth();
if (this.options.container) this.selectPosition();
this.$menu.data('this', this);
this.$newElement.data('this', this);
if (this.options.mobile) this.mobile();
},
createDropdown: function() {
// Options
// If we are multiple, then add the show-tick class by default
var multiple = this.multiple ? ' show-tick' : '',
inputGroup = this.$element.parent().hasClass('input-group') ? ' input-group-btn' : '',
autofocus = this.autofocus ? ' autofocus' : '';
// Elements
var header = this.options.header ?
'
' +
'' + this.options.header +
'
' : '',
searchbox = this.options.liveSearch ?
'' +
'' +
'
' : '',
actionsbox = this.multiple && this.options.actionsBox ?
'' +
'
' +
'' +
'' +
'
' +
'
' : '',
donebutton = this.multiple && this.options.doneButton ?
'' : '',
drop =
'' +
'' +
'' +
'
';
return $(drop);
},
createView: function() {
var $drop = this.createDropdown(),
$li = this.createLi();
$drop.find('ul').append($li);
return $drop;
},
reloadLi: function() {
// Remove all children
this.destroyLi();
// Re-build
var $li = this.createLi();
this.$menu.find('ul').append($li);
},
destroyLi: function() {
this.$menu.find('li').remove();
},
createLi: function() {
var that = this,
_li = [],
optID = 0;
// Helper functions
/**
* @param content
* @param [index]
* @param [classes]
* @param [optgroup]
* @returns {string}
*/
var generateLI = function(content, index, classes, optgroup) {
return '' + content + '';
};
/**
* @param text
* @param [classes]
* @param [inline]
* @param [title]
* @param [tokens]
* @param [multiple]
* @returns {string}
*/
var generateA = function(text, classes, inline, title, tokens, multiple) {
return '' + text +
(multiple ? '' : '') +
'';
};
this.$element.find('option').each(function(index) {
var $this = $(this);
// Get the class and text for the option
var optionClass = $this.attr('class') || '',
inline = $this.attr('style'),
title = $this.attr('title'),
text = $this.data('content') ? $this.data('content') : $this.html(),
tokens = $this.data('tokens') ? $this.data('tokens') : null,
subtext = $this.data('subtext') ? '' + $this.data('subtext') + '' : '',
icon = $this.data('icon') ? ' ' : '',
isDisabled = $this.is(':disabled') || $this.parent().is(':disabled');
if (icon!=='' && isDisabled) {
icon = '' + icon + '';
}
if ($this.data('thumbnail')) {
// Prepare thumbnail
text =
'' +
'' +
'' + text + '' +
'';
} else if (!$this.data('content')) {
// Prepend any icon and append any subtext to the main text
text = icon + '' + text + subtext + '';
}
if (that.options.hideDisabled && isDisabled) {
return;
}
if ($this.parent().is('optgroup') && $this.data('divider')!==true) {
if ($this.index()===0) { // Is it the first option of the optgroup?
optID += 1;
// Get the opt group label
var label = $this.parent().attr('label'),
labelSubtext = $this.parent().data('subtext') ? '' + $this.parent().data('subtext') + '' : '',
labelIcon = $this.parent().data('icon') ? ' ' : '';
label = labelIcon + '' + label + labelSubtext + '';
if (index!==0 && _li.length > 0) { // Is it NOT the first option of the select && are there elements in the dropdown?
_li.push(generateLI('', null, 'divider', optID + 'div'));
}
_li.push(generateLI(label, null, 'dropdown-header', optID));
}
_li.push(generateLI(generateA(text, 'opt ' + optionClass, inline, title, tokens, that.multiple), index, '', optID));
} else if ($this.data('divider')===true) {
_li.push(generateLI('', index, 'divider'));
} else if ($this.data('hidden')===true) {
_li.push(generateLI(generateA(text, optionClass, inline, title, tokens, that.multiple), index, 'hidden is-hidden'));
} else {
if ($this.prev().is('optgroup')) _li.push(generateLI('', null, 'divider', optID + 'div'));
_li.push(generateLI(generateA(text, optionClass, inline, title, tokens, that.multiple), index));
}
});
// If we are not multiple, we don't have a selected item, and we don't have
// a title, select the first element so something is set in the button
if (!this.multiple && this.$element.find('option:selected').length===0 && !this.options.title) {
this.$element.find('option').eq(0).prop('selected', true).attr('selected', 'selected');
}
return $(_li.join(''));
},
findLis: function() {
if (this.$lis===null) this.$lis = this.$menu.find('li');
return this.$lis;
},
/**
* @param [updateLi] defaults to true
*/
render: function(updateLi) {
var that = this;
// Update the LI to match the SELECT
if (updateLi!==false) {
this.$element.find('option').each(function(index) {
that.setDisabled(index, $(this).is(':disabled') || $(this).parent().is(':disabled'));
that.setSelected(index, $(this).is(':selected'));
});
}
this.tabIndex();
var notDisabled = this.options.hideDisabled ? ':enabled' : '',
selectedItems = this.$element.find('option:selected' + notDisabled).map(function() {
var $this = $(this),
icon = $this.data('icon') ? ' ' : '',
subtext;
if ($this.data('subtext') && !that.multiple) {
subtext = ' ' + $this.data('subtext') + '';
} else {
subtext = '';
}
if ($this.attr('title')) {
return $this.attr('title');
} else if ($this.data('content')) {
return $this.data('content');
} else {
return icon + $this.html() + subtext;
}
}).toArray();
// Fixes issue in IE10 occurring when no default option is selected and at least one option is disabled
// Convert all the values into a comma delimited string
var title = !this.multiple ? selectedItems[0] : selectedItems.join(this.options.multipleSeparator);
// If this is multi-select, and the selectText type is count, then show 1 of 2 selected, etc.
if (this.multiple && this.options.selectedTextFormat.indexOf('count') > -1) {
var max = this.options.selectedTextFormat.split('>');
if ((max.length > 1 && selectedItems.length > max[1]) || (max.length===1 && selectedItems.length >= 2)) {
notDisabled = this.options.hideDisabled ? ', [disabled]' : '';
var totalCount = this.$element.find('option').not('[data-divider="true"], [data-hidden="true"]' + notDisabled).length,
tr8nText = (typeof this.options.countSelectedText==='function') ? this.options.countSelectedText(selectedItems.length, totalCount) : this.options.countSelectedText;
title = tr8nText.replace('{0}', selectedItems.length.toString()).replace('{1}', totalCount.toString());
}
}
// If we dont have a title, then use the default; or if nothing is set at all, use the not selected text
if (!title) {
title = this.options.title || this.options.noneSelectedText;
}
// Strip all HTML tags and trim the result
this.$button.children('.filter-option').html((!this.multiple || this.options.selectedTextFormat==='values') ? title : this.options.title);
this.$button.attr('title', this.options.title);
},
/**
* @param [style]
* @param [status]
*/
setStyle: function(style, status) {
if (this.$element.attr('class')) {
this.$newElement.addClass(this.$element.attr('class').replace(/selectpicker|mobile-device|validate\[.*\]/gi, ''));
}
var buttonClass = style ? style : this.options.style;
if (status==='add') {
this.$button.addClass(buttonClass);
} else if (status==='remove') {
this.$button.removeClass(buttonClass);
} else {
this.$button.removeClass(this.options.style);
this.$button.addClass(buttonClass);
}
},
liHeight: function() {
if (this.options.size===false) return;
var $selectClone = this.$menu.parent().clone().children('.dropdown-toggle').prop('autofocus', false).end().appendTo('body'),
$menuClone = $selectClone.addClass('open').children('.dropdown-menu'),
liHeight = $menuClone.find('li').not('.divider, .dropdown-header').filter(':visible').children('a').outerHeight(),
headerHeight = this.options.header ? $menuClone.find('.popover-title').outerHeight() : 0,
searchHeight = this.options.liveSearch ? $menuClone.find('.bs-searchbox').outerHeight() : 0,
actionsHeight = this.options.actionsBox ? $menuClone.find('.bs-actionsbox').outerHeight() : 0,
doneButtonHeight = this.multiple ? $menuClone.find('.bs-donebutton').outerHeight() : 0;
$selectClone.remove();
this.$newElement
.data('liHeight', liHeight)
.data('headerHeight', headerHeight)
.data('searchHeight', searchHeight)
.data('actionsHeight', actionsHeight)
.data('doneButtonHeight', doneButtonHeight);
},
setSize: function() {
this.findLis();
var that = this,
$menu = this.$menu,
$menuInner = $menu.children('.inner'),
selectHeight = this.$newElement.outerHeight(),
liHeight = this.$newElement.data('liHeight'),
headerHeight = this.$newElement.data('headerHeight'),
searchHeight = this.$newElement.data('searchHeight'),
actionsHeight = this.$newElement.data('actionsHeight'),
doneButtonHeight = this.$newElement.data('doneButtonHeight'),
divHeight = this.$lis.filter('.divider').outerHeight(true),
menuPadding = parseInt($menu.css('padding-top')) +
parseInt($menu.css('padding-bottom')) +
parseInt($menu.css('border-top-width')) +
parseInt($menu.css('border-bottom-width')),
notDisabled = this.options.hideDisabled ? '.disabled' : '',
$window = $(window),
menuExtras = menuPadding + parseInt($menu.css('margin-top')) + parseInt($menu.css('margin-bottom')) + 2,
menuHeight,
selectOffsetTop,
selectOffsetBot,
posVert = function() {
// jQuery defines a scrollTop function, but in pure JS it's a property
//noinspection JSValidateTypes
selectOffsetTop = that.$newElement.offset().top - $window.scrollTop();
selectOffsetBot = $window.height() - selectOffsetTop - selectHeight;
};
posVert();
if (this.options.header) $menu.css('padding-top', 0);
if (this.options.size==='auto') {
var getSize = function() {
var minHeight,
lisVis = that.$lis.not('.hidden');
posVert();
menuHeight = selectOffsetBot - menuExtras;
if (that.options.dropupAuto) {
that.$newElement.toggleClass('dropup', selectOffsetTop > selectOffsetBot && (menuHeight - menuExtras) < $menu.height());
}
if (that.$newElement.hasClass('dropup')) {
menuHeight = selectOffsetTop - menuExtras;
}
if ((lisVis.length + lisVis.filter('.dropdown-header').length) > 3) {
minHeight = liHeight * 3 + menuExtras - 2;
} else {
minHeight = 0;
}
$menu.css({
'max-height': menuHeight + 'px',
'overflow': 'hidden',
'min-height': minHeight + headerHeight + searchHeight + actionsHeight + doneButtonHeight + 'px'
});
$menuInner.css({
'max-height': menuHeight - headerHeight - searchHeight - actionsHeight - doneButtonHeight - menuPadding + 'px',
'overflow-y': 'auto',
'min-height': Math.max(minHeight - menuPadding, 0) + 'px'
});
};
getSize();
this.$searchbox.off('input.getSize propertychange.getSize').on('input.getSize propertychange.getSize', getSize);
$window.off('resize.getSize scroll.getSize').on('resize.getSize scroll.getSize', getSize);
} else if (this.options.size && this.options.size!=='auto' && $menu.find('li').not(notDisabled).length > this.options.size) {
var optIndex = this.$lis.not('.divider').not(notDisabled).children().slice(0, this.options.size).last().parent().index(),
divLength = this.$lis.slice(0, optIndex + 1).filter('.divider').length;
menuHeight = liHeight * this.options.size + divLength * divHeight + menuPadding;
if (that.options.dropupAuto) {
//noinspection JSUnusedAssignment
this.$newElement.toggleClass('dropup', selectOffsetTop > selectOffsetBot && menuHeight < $menu.height());
}
$menu.css({
'max-height': menuHeight + headerHeight + searchHeight + actionsHeight + doneButtonHeight + 'px',
'overflow': 'hidden'
});
$menuInner.css({
'max-height': menuHeight - menuPadding + 'px',
'overflow-y': 'auto'
});
}
},
setWidth: function() {
if (this.options.width==='auto') {
this.$menu.css('min-width', '0');
// Get correct width if element hidden
var selectClone = this.$newElement.clone().appendTo('body'),
ulWidth = selectClone.children('.dropdown-menu').css('width'),
btnWidth = selectClone.css('width', 'auto').children('button').css('width');
selectClone.remove();
// Set width to whatever's larger, button title or longest option
this.$newElement.css('width', Math.max(parseInt(ulWidth), parseInt(btnWidth)) + 'px');
} else if (this.options.width==='fit') {
// Remove inline min-width so width can be changed from 'auto'
this.$menu.css('min-width', '');
this.$newElement.css('width', '').addClass('fit-width');
} else if (this.options.width) {
// Remove inline min-width so width can be changed from 'auto'
this.$menu.css('min-width', '');
this.$newElement.css('width', this.options.width);
} else {
// Remove inline min-width/width so width can be changed
this.$menu.css('min-width', '');
this.$newElement.css('width', '');
}
// Remove fit-width class if width is changed programmatically
if (this.$newElement.hasClass('fit-width') && this.options.width!=='fit') {
this.$newElement.removeClass('fit-width');
}
},
selectPosition: function() {
var that = this,
drop = '',
$drop = $(drop),
pos,
actualHeight,
getPlacement = function($element) {
$drop.addClass($element.attr('class').replace(/form-control/gi, '')).toggleClass('dropup', $element.hasClass('dropup'));
pos = $element.offset();
actualHeight = $element.hasClass('dropup') ? 0 : $element[0].offsetHeight;
$drop.css({
'top': pos.top + actualHeight,
'left': pos.left,
'width': $element[0].offsetWidth,
'position': 'absolute'
});
};
this.$newElement.on('click', function() {
if (that.isDisabled()) {
return;
}
getPlacement($(this));
$drop.appendTo(that.options.container);
$drop.toggleClass('open', !$(this).hasClass('open'));
$drop.append(that.$menu);
});
$(window).on('resize scroll', function() {
getPlacement(that.$newElement);
});
$('html').on('click', function(e) {
if ($(e.target).closest(that.$newElement).length < 1) {
$drop.removeClass('open');
}
});
},
setSelected: function(index, selected) {
this.findLis();
this.$lis.filter('[data-original-index="' + index + '"]').toggleClass('selected', selected);
},
setDisabled: function(index, disabled) {
this.findLis();
if (disabled) {
this.$lis.filter('[data-original-index="' + index + '"]').addClass('disabled').children('a').attr('href', '#').attr('tabindex', -1);
} else {
this.$lis.filter('[data-original-index="' + index + '"]').removeClass('disabled').children('a').removeAttr('href').attr('tabindex', 0);
}
},
isDisabled: function() {
return this.$element.is(':disabled');
},
checkDisabled: function() {
var that = this;
if (this.isDisabled()) {
this.$button.addClass('disabled').attr('tabindex', -1);
} else {
if (this.$button.hasClass('disabled')) {
// Also sync disabled state with