From c36417189981580ce092a6bd039dfabcfffa28dc Mon Sep 17 00:00:00 2001 From: PsyStorm25 Date: Fri, 19 Aug 2016 18:27:59 +0200 Subject: [PATCH 1/5] Angularjs compatible This version is compatible with angularjs select list generated with ng-options. The code added could maybe be write in a nicer way, feel free to do so. --- angularjs-compatible | 415 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 415 insertions(+) create mode 100644 angularjs-compatible diff --git a/angularjs-compatible b/angularjs-compatible new file mode 100644 index 0000000..6e15a47 --- /dev/null +++ b/angularjs-compatible @@ -0,0 +1,415 @@ +/* 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('li[value=' + val[i] + ']') + } + if (selectors.length > 0) { + methods._select($dropdown, $dropdown.find(selectors.join(','))); + } + } + + // 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 = $("