Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to Module loaders and package managers, AMD and CommonJS #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
290 changes: 149 additions & 141 deletions js/jquery.nice-select.js
Original file line number Diff line number Diff line change
@@ -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($('<div></div>')
.addClass('nice-select')
.addClass($select.attr('class') || '')
.addClass($select.attr('disabled') ? 'disabled' : '')
.attr('tabindex', $select.attr('disabled') ? null : '0')
.html('<span class="current"></span><ul class="list"></ul>')
);

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($('<li></li>')
.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($('<div></div>')
.addClass('nice-select')
.addClass($select.attr('class') || '')
.addClass($select.attr('disabled') ? 'disabled' : '')
.attr('tabindex', $select.attr('disabled') ? null : '0')
.html('<span class="current"></span><ul class="list"></ul>')
);

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($('<li></li>')
.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));
// 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');
}

};

}));
2 changes: 1 addition & 1 deletion js/jquery.nice-select.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 29 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}