Skip to content

Commit

Permalink
Avoid selection of disabled options
Browse files Browse the repository at this point in the history
  • Loading branch information
indrimuska committed Nov 9, 2016
1 parent 48591b2 commit 97a9490
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/jquery-editable-select.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ input.es-input.open {
.es-list { position: absolute; padding: 0; margin: 0; border: 1px solid #d1d1d1; display: none; z-index: 1000; background: #fff; max-height: 160px; overflow-y: auto;
-moz-box-shadow: 0 2px 3px #ccc; -webkit-box-shadow: 0 2px 3px #ccc; box-shadow: 0 2px 3px #ccc; }
.es-list li { display: block; padding: 5px 10px; margin: 0; }
.es-list li.selected { background: #f3f3f3; }
.es-list li.selected { background: #f3f3f3; }
.es-list li[disabled] { opacity: .5; }
23 changes: 13 additions & 10 deletions src/jquery-editable-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
this.$list[fn](this.options.duration, $.proxy(this.utility.trigger, this.utility, 'hidden'));
};
EditableSelect.prototype.select = function ($li) {
if (!this.$list.has($li) || !$li.is('li.es-visible')) return;
if (!this.$list.has($li) || !$li.is('li.es-visible:not([disabled])')) return;
this.$input.val($li.text());
this.hide();
this.filter();
Expand Down Expand Up @@ -125,12 +125,13 @@
EditableSelectUtility.prototype.initializeList = function () {
var that = this;
that.es.$list
.on('mousemove', 'li', function () {
.on('mousemove', 'li:not([disabled])', function () {
that.es.$list.find('.selected').removeClass('selected');
$(this).addClass('selected');
})
.on('mousedown', 'li', function () {
that.es.select($(this));
.on('mousedown', 'li', function (e) {
if ($(this).is('[disabled]')) e.preventDefault();
else that.es.select($(this));
})
.on('mouseup', function () {
that.es.$list.find('li.selected').removeClass('selected');
Expand All @@ -151,15 +152,17 @@
that.es.$input.on('input keydown', function (e) {
switch (e.keyCode) {
case 38: // Up
var visibles = that.es.$list.find('li.es-visible');
var selected = visibles.index(visibles.filter('li.selected')) || 0;
that.highlight(selected - 1);
var visibles = that.es.$list.find('li.es-visible:not([disabled])');
var nextNode = visibles.filter('li.selected').prev();
var nextIndex = visibles.index(nextElement.length > 0 ? nextElement : visibles.last());
that.highlight(nextIndex);
e.preventDefault();
break;
case 40: // Down
var visibles = that.es.$list.find('li.es-visible');
var selected = visibles.index(visibles.filter('li.selected')) || 0;
that.highlight(selected + 1);
var visibles = that.es.$list.find('li.es-visible:not([disabled])');
var nextNode = visibles.filter('li.selected').next();
var nextIndex = visibles.index(nextNode.length > 0 ? nextNode : visibles.first());
that.highlight(nextIndex);
e.preventDefault();
break;
case 13: // Enter
Expand Down

0 comments on commit 97a9490

Please sign in to comment.