Line 0
Link Here
|
|
|
1 |
(function($) { |
2 |
$.widget("custom.combobox", { |
3 |
_create: function() { |
4 |
this.wrapper = $("<span>") |
5 |
.addClass("custom-combobox") |
6 |
.insertAfter(this.element); |
7 |
this.element.hide(); |
8 |
this._createAutocomplete(); |
9 |
this._createShowAllButton(); |
10 |
}, |
11 |
|
12 |
_createAutocomplete: function() { |
13 |
var selected = this.element.children(":selected"), |
14 |
value = selected.val() ? selected.text() : ""; |
15 |
|
16 |
this.input = $("<input>") |
17 |
.appendTo(this.wrapper) |
18 |
.val(value) |
19 |
.attr("title", "") |
20 |
.addClass("custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left") |
21 |
.autocomplete({ |
22 |
delay: 0, |
23 |
minLength: 0, |
24 |
source: $.proxy(this, "_source") |
25 |
}); |
26 |
|
27 |
this._on(this.input, { |
28 |
autocompleteselect: function(event, ui) { |
29 |
ui.item.option.selected = true; |
30 |
this._trigger("select", event, { |
31 |
item: ui.item.option |
32 |
}); |
33 |
}, |
34 |
autocompletechange: "_removeIfInvalid" |
35 |
}); |
36 |
}, |
37 |
|
38 |
_createShowAllButton: function() { |
39 |
var input = this.input, |
40 |
wasOpen = false; |
41 |
|
42 |
$("<a>") |
43 |
.append(' ') |
44 |
.attr("tabIndex", -1) |
45 |
.attr("title", "Show All Items") |
46 |
.appendTo(this.wrapper) |
47 |
.button({ |
48 |
icons: { |
49 |
primary: "ui-icon-triangle-1-s" |
50 |
}, |
51 |
text: false |
52 |
}) |
53 |
.removeClass("ui-corner-all") |
54 |
.addClass("custom-combobox-toggle ui-corner-right") |
55 |
.mousedown(function() { |
56 |
wasOpen = input.autocomplete("widget").is(":visible"); |
57 |
}) |
58 |
.click(function() { |
59 |
input.focus(); |
60 |
// Close if already visible |
61 |
if (wasOpen) { |
62 |
return; |
63 |
} |
64 |
// Pass empty string as value to search for, displaying all results |
65 |
input.autocomplete("search", ""); |
66 |
}); |
67 |
}, |
68 |
|
69 |
_source: function(request, response) { |
70 |
var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i"); |
71 |
response(this.element.children("option").map(function() { |
72 |
var text = $(this).text(); |
73 |
if (this.value && (!request.term || matcher.test(text))) |
74 |
return { |
75 |
label: text, |
76 |
value: text, |
77 |
option: this |
78 |
}; |
79 |
})); |
80 |
}, |
81 |
|
82 |
_removeIfInvalid: function(event, ui) { |
83 |
// Selected an item, nothing to do |
84 |
if (ui.item) { |
85 |
return; |
86 |
} |
87 |
|
88 |
// Search for a match (case-insensitive) |
89 |
var value = this.input.val(), |
90 |
valueLowerCase = value.toLowerCase(), |
91 |
valid = false; |
92 |
this.element.children("option").each(function() { |
93 |
if ($(this).text().toLowerCase() === valueLowerCase) { |
94 |
this.selected = valid = true; |
95 |
return false; |
96 |
} |
97 |
}); |
98 |
|
99 |
// Found a match, nothing to do |
100 |
if (valid) { |
101 |
return; |
102 |
} |
103 |
|
104 |
// Remove invalid value |
105 |
this.input.val(""); |
106 |
this.element.val(""); |
107 |
this.input.autocomplete("instance").term = ""; |
108 |
}, |
109 |
|
110 |
_destroy: function() { |
111 |
this.wrapper.remove(); |
112 |
this.element.show(); |
113 |
} |
114 |
}); |
115 |
})(jQuery); |