Last active
December 14, 2015 06:58
-
-
Save dkrnl/4ae798f457fde76eb245 to your computer and use it in GitHub Desktop.
Jquery UI.Combobox via UI.Autocomplete
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.widget("ui.combobox", { | |
_create: function() { | |
var input, | |
self = this, | |
select = this.element.hide(), | |
selected = select.find(":selected"), | |
value = selected.val() ? selected.text() : "", | |
wrapper = this.wrapper = $("<span>").addClass("ui-combobox").insertAfter(select); | |
input = $("<input>") | |
.appendTo(wrapper) | |
.width(this.element.width()) | |
.val(value) | |
.addClass("ui-state-default ui-combobox-input") | |
.autocomplete({ | |
delay: 0, | |
minLength: 0, | |
source: function(request, response) { | |
var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i"); | |
response(select.find("option").map(function() { | |
var self = $(this); | |
var text = self.text(); | |
if (this.value && (!request.term || matcher.test(text))) | |
return { | |
label: text.replace( | |
new RegExp( | |
"(?![^&;]+;)(?!<[^<>]*)(" + | |
$.ui.autocomplete.escapeRegex(request.term) + | |
")(?![^<>]*>)(?![^&;]+;)", "gi" | |
), "$1"), | |
value: text, | |
group: self.parent("optgroup").attr("label"), | |
option: this | |
}; | |
})); | |
}, | |
select: function(event, ui) { | |
ui.item.option.selected = true; | |
self._trigger("selected", event, { | |
item: ui.item.option | |
}); | |
self.element.trigger("change"); | |
}, | |
change: function(event, ui) { | |
if (!ui.item) { | |
var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex($(this).val()) + "$", "i"), | |
valid = false; | |
select.find("option").each(function() { | |
if ($(this).text().match(matcher)) { | |
this.selected = valid = true; | |
return false; | |
} | |
}); | |
if (!valid) { | |
$(this).val(""); | |
select.val(""); | |
input.data("autocomplete").term = ""; | |
return false; | |
} | |
} | |
}, | |
open: function(event, ui) { | |
input.data("toggle").addClass("ui-state-active"); | |
}, | |
close: function(event, ui) { | |
input.data("toggle").removeClass("ui-state-active"); | |
} | |
}) | |
.addClass("ui-widget ui-widget-content ui-corner-left"); | |
input.bind("blur", function() { | |
if ($(this).val() == "") { | |
input.data("autocomplete").element.val(""); | |
} | |
}); | |
this.input = input; | |
input.data("autocomplete")._renderItem = function(ul, item) { | |
return $("<li></li>") | |
.data("item.autocomplete", item) | |
.append("<a>" + htmlentities(item.label) + "</a>") | |
.appendTo(ul); | |
}; | |
input.data("autocomplete")._renderMenu = function(ul, items ) { | |
var self = this; | |
var group = null; | |
$.each(items, function(index, item ) { | |
if (item.group && group != item.group) { | |
$("<li></li>").append("<b>" + htmlentities(item.group) + "</b>").appendTo(ul); | |
} | |
self._renderItem(ul, item ); | |
group = item.group; | |
}); | |
}; | |
input.data("toggle", $("<a>") | |
.attr("tabIndex", -1) | |
.appendTo(wrapper) | |
.addClass("ui-icon-triangle-1-s ui-combobox-toggle ui-icon ui-state-default ui-corner-all") | |
.click(function() { | |
if (input.autocomplete("widget").is(":visible")) { | |
input.autocomplete("close"); | |
return; | |
} | |
$(this).blur(); | |
input.autocomplete("search", ""); | |
input.focus(); | |
})); | |
}, | |
destroy: function() { | |
this.wrapper.remove(); | |
this.element.show(); | |
$.Widget.prototype.destroy.call(this); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment