diff --git a/js/jquery.nice-select.js b/js/jquery.nice-select.js index f1f9d0d..7703c84 100644 --- a/js/jquery.nice-select.js +++ b/js/jquery.nice-select.js @@ -1,150 +1,158 @@ /* jQuery Nice Select - v1.0 - https://github.com/hernansartorio/jquery-nice-select - Made by Hernán Sartorio */ + https://github.com/hernansartorio/jquery-nice-select + Made by Hernán Sartorio */ -(function($) { +((function (factory) { - $.fn.niceSelect = function() { - - // Hide native select - this.hide(); + if(typeof module === "object" && typeof module.exports === "object") { + factory(require("jquery"), window, document); + }else { + (jQuery, window, document); + } - // Create custom markup - this.each(function() { - var $select = $(this); - - if (!$select.next().hasClass('nice-select')) { - - $select.after($('
') - .addClass('nice-select') - .addClass($select.attr('class') || '') - .addClass($select.attr('disabled') ? 'disabled' : '') - .attr('tabindex', $select.attr('disabled') ? null : '0') - .html('') - ); - - var $dropdown = $select.next(); - var $options = $select.find('option'); - var $selected = $select.find('option:selected'); - - $dropdown.find('.current').html($selected.data('display') || $selected.text()); - - $options.each(function(i) { - var $option = $(this); - var display = $option.data('display'); +}(function($, window, document, undefined) { - $dropdown.find('ul').append($('
  • ') - .attr('data-value', $option.val()) - .attr('data-display', (display || null)) - .addClass('option' + - ($option.is(':selected') ? ' selected' : '') + - ($option.is(':disabled') ? ' disabled' : '')) - .html($option.text()) - ); - - }); - } - }); - - /* Event listeners */ - - // Unbind existing events in case that the plugin has been initialized before - $(document).off('.nice_select'); - - // Open/close - $(document).on('click.nice_select', '.nice-select', function(event) { - var $dropdown = $(this); - - $('.nice-select').not($dropdown).removeClass('open'); - $dropdown.toggleClass('open'); - - if ($dropdown.hasClass('open')) { - $dropdown.find('.option'); - $dropdown.find('.focus').removeClass('focus'); - $dropdown.find('.selected').addClass('focus'); - } else { - $dropdown.focus(); - } - }); - - // Close when clicking outside - $(document).on('click.nice_select', function(event) { - if ($(event.target).closest('.nice-select').length === 0) { - $('.nice-select').removeClass('open').find('.option'); - } - }); - - // Option click - $(document).on('click.nice_select', '.nice-select .option:not(.disabled)', function(event) { - var $option = $(this); - var $dropdown = $option.closest('.nice-select'); - - $dropdown.find('.selected').removeClass('selected'); - $option.addClass('selected'); - - var text = $option.data('display') || $option.text(); - $dropdown.find('.current').text(text); - - $dropdown.prev('select').val($option.data('value')).trigger('change'); - }); + $.fn.niceSelect = function() { + + // Hide native select + this.hide(); - // Keyboard events - $(document).on('keydown.nice_select', '.nice-select', function(event) { - var $dropdown = $(this); - var $focused_option = $($dropdown.find('.focus') || $dropdown.find('.list .option.selected')); - - // Space or Enter - if (event.keyCode == 32 || event.keyCode == 13) { - if ($dropdown.hasClass('open')) { - $focused_option.trigger('click'); - } else { - $dropdown.trigger('click'); - } - return false; - // Down - } else if (event.keyCode == 40) { - if (!$dropdown.hasClass('open')) { - $dropdown.trigger('click'); - } else { - var $next = $focused_option.nextAll('.option:not(.disabled)').first(); - if ($next.length > 0) { - $dropdown.find('.focus').removeClass('focus'); - $next.addClass('focus'); - } - } - return false; - // Up - } else if (event.keyCode == 38) { - if (!$dropdown.hasClass('open')) { - $dropdown.trigger('click'); - } else { - var $prev = $focused_option.prevAll('.option:not(.disabled)').first(); - if ($prev.length > 0) { - $dropdown.find('.focus').removeClass('focus'); - $prev.addClass('focus'); - } - } - return false; - // Esc - } else if (event.keyCode == 27) { - if ($dropdown.hasClass('open')) { - $dropdown.trigger('click'); - } - // Tab - } else if (event.keyCode == 9) { - if ($dropdown.hasClass('open')) { - return false; - } - } - }); + // Create custom markup + this.each(function() { + var $select = $(this); + + if (!$select.next().hasClass('nice-select')) { + + $select.after($('
    ') + .addClass('nice-select') + .addClass($select.attr('class') || '') + .addClass($select.attr('disabled') ? 'disabled' : '') + .attr('tabindex', $select.attr('disabled') ? null : '0') + .html('') + ); + + var $dropdown = $select.next(); + var $options = $select.find('option'); + var $selected = $select.find('option:selected'); + + $dropdown.find('.current').html($selected.data('display') || $selected.text()); + + $options.each(function(i) { + var $option = $(this); + var display = $option.data('display'); - // Detect CSS pointer-events support, for IE <= 10. From Modernizr. - var style = document.createElement('a').style; - style.cssText = 'pointer-events:auto'; - if (style.pointerEvents !== 'auto') { - $('html').addClass('no-csspointerevents'); - } + $dropdown.find('ul').append($('
  • ') + .attr('data-value', $option.val()) + .attr('data-display', (display || null)) + .addClass('option' + + ($option.is(':selected') ? ' selected' : '') + + ($option.is(':disabled') ? ' disabled' : '')) + .html($option.text()) + ); + + }); + } + }); + + /* Event listeners */ + + // Unbind existing events in case that the plugin has been initialized before + $(document).off('.nice_select'); + + // Open/close + $(document).on('click.nice_select', '.nice-select', function(event) { + var $dropdown = $(this); + + $('.nice-select').not($dropdown).removeClass('open'); + $dropdown.toggleClass('open'); + + if ($dropdown.hasClass('open')) { + $dropdown.find('.option'); + $dropdown.find('.focus').removeClass('focus'); + $dropdown.find('.selected').addClass('focus'); + } else { + $dropdown.focus(); + } + }); + + // Close when clicking outside + $(document).on('click.nice_select', function(event) { + if ($(event.target).closest('.nice-select').length === 0) { + $('.nice-select').removeClass('open').find('.option'); + } + }); + + // Option click + $(document).on('click.nice_select', '.nice-select .option:not(.disabled)', function(event) { + var $option = $(this); + var $dropdown = $option.closest('.nice-select'); + + $dropdown.find('.selected').removeClass('selected'); + $option.addClass('selected'); + + var text = $option.data('display') || $option.text(); + $dropdown.find('.current').text(text); + + $dropdown.prev('select').val($option.data('value')).trigger('change'); + }); - }; + // Keyboard events + $(document).on('keydown.nice_select', '.nice-select', function(event) { + var $dropdown = $(this); + var $focused_option = $($dropdown.find('.focus') || $dropdown.find('.list .option.selected')); + + // Space or Enter + if (event.keyCode == 32 || event.keyCode == 13) { + if ($dropdown.hasClass('open')) { + $focused_option.trigger('click'); + } else { + $dropdown.trigger('click'); + } + return false; + // Down + } else if (event.keyCode == 40) { + if (!$dropdown.hasClass('open')) { + $dropdown.trigger('click'); + } else { + var $next = $focused_option.nextAll('.option:not(.disabled)').first(); + if ($next.length > 0) { + $dropdown.find('.focus').removeClass('focus'); + $next.addClass('focus'); + } + } + return false; + // Up + } else if (event.keyCode == 38) { + if (!$dropdown.hasClass('open')) { + $dropdown.trigger('click'); + } else { + var $prev = $focused_option.prevAll('.option:not(.disabled)').first(); + if ($prev.length > 0) { + $dropdown.find('.focus').removeClass('focus'); + $prev.addClass('focus'); + } + } + return false; + // Esc + } else if (event.keyCode == 27) { + if ($dropdown.hasClass('open')) { + $dropdown.trigger('click'); + } + // Tab + } else if (event.keyCode == 9) { + if ($dropdown.hasClass('open')) { + return false; + } + } + }); -}(jQuery)); \ No newline at end of file + // Detect CSS pointer-events support, for IE <= 10. From Modernizr. + var style = document.createElement('a').style; + style.cssText = 'pointer-events:auto'; + if (style.pointerEvents !== 'auto') { + $('html').addClass('no-csspointerevents'); + } + + }; + +})); \ No newline at end of file diff --git a/js/jquery.nice-select.min.js b/js/jquery.nice-select.min.js index d3f2d5d..98ee058 100644 --- a/js/jquery.nice-select.min.js +++ b/js/jquery.nice-select.min.js @@ -1,4 +1,4 @@ /* jQuery Nice Select - v1.0 https://github.com/hernansartorio/jquery-nice-select Made by Hernán Sartorio */ -!function(e){e.fn.niceSelect=function(){this.hide(),this.each(function(){var s=e(this);if(!s.next().hasClass("nice-select")){s.after(e("
    ").addClass("nice-select").addClass(s.attr("class")||"").addClass(s.attr("disabled")?"disabled":"").attr("tabindex",s.attr("disabled")?null:"0").html(''));var t=s.next(),n=s.find("option"),l=s.find("option:selected");t.find(".current").html(l.data("display")||l.text()),n.each(function(s){var n=e(this),l=n.data("display");t.find("ul").append(e("
  • ").attr("data-value",n.val()).attr("data-display",l||null).addClass("option"+(n.is(":selected")?" selected":"")+(n.is(":disabled")?" disabled":"")).html(n.text()))})}}),e(document).off(".nice_select"),e(document).on("click.nice_select",".nice-select",function(s){var t=e(this);e(".nice-select").not(t).removeClass("open"),t.toggleClass("open"),t.hasClass("open")?(t.find(".option"),t.find(".focus").removeClass("focus"),t.find(".selected").addClass("focus")):t.focus()}),e(document).on("click.nice_select",function(s){0===e(s.target).closest(".nice-select").length&&e(".nice-select").removeClass("open").find(".option")}),e(document).on("click.nice_select",".nice-select .option:not(.disabled)",function(s){var t=e(this),n=t.closest(".nice-select");n.find(".selected").removeClass("selected"),t.addClass("selected");var l=t.data("display")||t.text();n.find(".current").text(l),n.prev("select").val(t.data("value")).trigger("change")}),e(document).on("keydown.nice_select",".nice-select",function(s){var t=e(this),n=e(t.find(".focus")||t.find(".list .option.selected"));if(32==s.keyCode||13==s.keyCode)return t.hasClass("open")?n.trigger("click"):t.trigger("click"),!1;if(40==s.keyCode){if(t.hasClass("open")){var l=n.nextAll(".option:not(.disabled)").first();l.length>0&&(t.find(".focus").removeClass("focus"),l.addClass("focus"))}else t.trigger("click");return!1}if(38==s.keyCode){if(t.hasClass("open")){var i=n.prevAll(".option:not(.disabled)").first();i.length>0&&(t.find(".focus").removeClass("focus"),i.addClass("focus"))}else t.trigger("click");return!1}if(27==s.keyCode)t.hasClass("open")&&t.trigger("click");else if(9==s.keyCode&&t.hasClass("open"))return!1});var s=document.createElement("a").style;s.cssText="pointer-events:auto","auto"!==s.pointerEvents&&e("html").addClass("no-csspointerevents")}}(jQuery); \ No newline at end of file +!function(e){"object"==typeof module&&"object"==typeof module.exports?e(require("jquery"),window,document):(jQuery,window,document)}(function(e,s,t,n){e.fn.niceSelect=function(){this.hide(),this.each(function(){var s=e(this);if(!s.next().hasClass("nice-select")){s.after(e("
    ").addClass("nice-select").addClass(s.attr("class")||"").addClass(s.attr("disabled")?"disabled":"").attr("tabindex",s.attr("disabled")?null:"0").html(''));var t=s.next(),n=s.find("option"),i=s.find("option:selected");t.find(".current").html(i.data("display")||i.text()),n.each(function(s){var n=e(this),i=n.data("display");t.find("ul").append(e("
  • ").attr("data-value",n.val()).attr("data-display",i||null).addClass("option"+(n.is(":selected")?" selected":"")+(n.is(":disabled")?" disabled":"")).html(n.text()))})}}),e(t).off(".nice_select"),e(t).on("click.nice_select",".nice-select",function(s){var t=e(this);e(".nice-select").not(t).removeClass("open"),t.toggleClass("open"),t.hasClass("open")?(t.find(".option"),t.find(".focus").removeClass("focus"),t.find(".selected").addClass("focus")):t.focus()}),e(t).on("click.nice_select",function(s){0===e(s.target).closest(".nice-select").length&&e(".nice-select").removeClass("open").find(".option")}),e(t).on("click.nice_select",".nice-select .option:not(.disabled)",function(s){var t=e(this),n=t.closest(".nice-select");n.find(".selected").removeClass("selected"),t.addClass("selected");var i=t.data("display")||t.text();n.find(".current").text(i),n.prev("select").val(t.data("value")).trigger("change")}),e(t).on("keydown.nice_select",".nice-select",function(s){var t=e(this),n=e(t.find(".focus")||t.find(".list .option.selected"));if(32==s.keyCode||13==s.keyCode)return t.hasClass("open")?n.trigger("click"):t.trigger("click"),!1;if(40==s.keyCode){if(t.hasClass("open")){var i=n.nextAll(".option:not(.disabled)").first();i.length>0&&(t.find(".focus").removeClass("focus"),i.addClass("focus"))}else t.trigger("click");return!1}if(38==s.keyCode){if(t.hasClass("open")){var l=n.prevAll(".option:not(.disabled)").first();l.length>0&&(t.find(".focus").removeClass("focus"),l.addClass("focus"))}else t.trigger("click");return!1}if(27==s.keyCode)t.hasClass("open")&&t.trigger("click");else if(9==s.keyCode&&t.hasClass("open"))return!1});var s=t.createElement("a").style;s.cssText="pointer-events:auto","auto"!==s.pointerEvents&&e("html").addClass("no-csspointerevents")}}); \ No newline at end of file diff --git a/package.json b/package.json index 8374dee..74091aa 100644 --- a/package.json +++ b/package.json @@ -1,29 +1,31 @@ { - "name": "jquery-nice-select", - "version": "1.0.0", - "description": "A lightweight jQuery plugin that replaces native select elements with customizable dropdowns.", - "main": "gulpfile.js", - "dependencies": {}, - "devDependencies": { - "gulp": "^3.9.0", - "gulp-autoprefixer": "^3.1.0", - "gulp-jshint": "^2.0.0", - "gulp-rename": "^1.2.2", - "gulp-sass": "^2.1.1", - "gulp-uglify": "^1.5.1", - "jshint": "^2.8.0" - }, - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/hernansartorio/jquery-nice-select.git" - }, - "author": "Hernán Sartorio", - "license": "ISC", - "bugs": { - "url": "https://github.com/hernansartorio/jquery-nice-select/issues" - }, - "homepage": "https://github.com/hernansartorio/jquery-nice-select#readme" + "name": "jquery-nice-select", + "version": "1.0.0", + "description": "A lightweight jQuery plugin that replaces native select elements with customizable dropdowns.", + "main": "gulpfile.js", + "dependencies": { + "jquery": "~2.1.4" + }, + "devDependencies": { + "gulp": "^3.9.0", + "gulp-autoprefixer": "^3.1.0", + "gulp-jshint": "^2.0.0", + "gulp-rename": "^1.2.2", + "gulp-sass": "^2.1.1", + "gulp-uglify": "^1.5.1", + "jshint": "^2.8.0" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/hernansartorio/jquery-nice-select.git" + }, + "author": "Hernán Sartorio", + "license": "ISC", + "bugs": { + "url": "https://github.com/hernansartorio/jquery-nice-select/issues" + }, + "homepage": "https://github.com/hernansartorio/jquery-nice-select#readme" }