/* globals jQuery, window, document */
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else if (typeof exports === 'object') {
// Node/CommonJS
module.exports = factory(require('jquery'));
} else {
// Browser globals
factory(jQuery);
}
}(function($) {
var methods = {
options : {
"optionClass": "",
"dropdownClass": "",
"autoinit": false,
"callback": false,
"onSelected": false,
"destroy": function(element) {
this.destroy(element);
},
"dynamicOptLabel": "Add a new option..."
},
init: function(options) {
// Apply user options if user has defined some
if (options) {
options = $.extend(methods.options, options);
} else {
options = methods.options;
}
function initElement($select) {
// Don't do anything if this is not a select or if this select was already initialized
if ($select.data("dropdownjs") || !$select.is("select")) {
return;
}
// Is it a multi select?
var multi = $select.attr("multiple");
// Does it allow to create new options dynamically?
var dynamicOptions = $select.attr("data-dynamic-opts"),
$dynamicInput = $();
// Create the dropdown wrapper
var $dropdown = $("
");
$dropdown.addClass("dropdownjs").addClass(options.dropdownClass);
$dropdown.data("select", $select);
// Create the fake input used as "select" element and cache it as $input
var $input = $("");
if ($.material) { $input.data("mdproc", true); }
// Append it to the dropdown wrapper
$dropdown.append($input);
// Create the UL that will be used as dropdown and cache it AS $ul
var $ul = $("
");
$ul.data("select", $select);
// Append it to the dropdown
$dropdown.append($ul);
// Transfer the placeholder attribute
$input.attr("placeholder", $select.attr("placeholder"));
// Loop trough options and transfer them to the dropdown menu
$select.find("option").each(function() {
// Cache $(this)
var $this = $(this);
methods._addOption($ul, $this);
});
// If this select allows dynamic options add the widget
if (dynamicOptions) {
$dynamicInput = $("");
$dynamicInput.append("");
$dynamicInput.find("input").attr("placeholder", options.dynamicOptLabel);
$ul.append($dynamicInput);
}
// Cache the dropdown options
var selectOptions = $dropdown.find("li");
// If is a single select, selected the first one or the last with selected attribute
if (!multi) {
var $selected;
if ($select.find(":selected").length) {
$selected = $select.find(":selected").last();
}
else {
$selected = $select.find("option, li").first();
// $selected = $select.find("option").first();
}
methods._select($dropdown, $selected);
} else {
var selectors = [], val = $select.val()
for (var i in val) {
selectors.push(val[i]);
}
if (selectors.length > 0) {
var $target = $dropdown.find(function() { return $.inArray($(this).data("value"), selectors) !== -1; });
$target.removeClass("selected");
methods._select($dropdown, $target);
}
}
// Transfer the classes of the select to the input dropdown
$input.addClass($select[0].className);
// Hide the old and ugly select
$select.hide().attr("data-dropdownjs", true);
// Bring to life our awesome dropdownjs
$select.after($dropdown);
// Call the callback
if (options.callback) {
options.callback($dropdown);
}
//---------------------------------------//
// DROPDOWN EVENTS //
//---------------------------------------//
// On click, set the clicked one as selected
$ul.on("click", "li:not(.dropdownjs-add)", function(e) {
methods._select($dropdown, $(this));
// trigger change event, if declared on the original selector
$select.change();
});
$ul.on("keydown", "li:not(.dropdownjs-add)", function(e) {
if (e.which === 27) {
$(".dropdownjs > ul > li").attr("tabindex", -1);
return $input.removeClass("focus").blur();
}
if (e.which === 32 && !$(e.target).is("input")) {
methods._select($dropdown, $(this));
return false;
}
});
$ul.on("focus", "li:not(.dropdownjs-add)", function() {
if ($select.is(":disabled")) {
return;
}
$input.addClass("focus");
});
// Add new options when the widget is used
if (dynamicOptions && dynamicOptions.length) {
$dynamicInput.on("keydown", function(e) {
if(e.which !== 13) return;
var $option = $("