-
Notifications
You must be signed in to change notification settings - Fork 0
/
FontListCombobox.html
144 lines (131 loc) · 4.5 KB
/
FontListCombobox.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<script>
jQuery.widget( "custom.combobox", {
_create: function() {
this.wrapper = jQuery( "<span>" )
.addClass( "custom-combobox" )
.attr( "title", this.element.attr('title') )
.insertAfter( this.element );
this.element.hide();
this._createAutocomplete();
this._createShowAllButton();
},
_createAutocomplete: function() {
var selected = this.element.children( ":selected" ),
//thistitle = this.element.title,
value = selected.val() ? selected.text() : "",
$wrapper = this.wrapper;
this.input = jQuery( "<input>" )
.appendTo( this.wrapper )
.val( value )
//.attr( "title", thistitle )
.addClass( "custom-combobox-input ui-widget ui-widget-content ui-corner-left" )
.autocomplete({
delay: 0,
minLength: 0,
source: jQuery.proxy( this, "_source" ),
open: function(event, ui){
let $btnwidth = $wrapper.find('.custom-combobox-toggle').length > 0 ? $wrapper.find('.custom-combobox-toggle').width() : 0;
$(this).autocomplete("widget").css({"width": (($wrapper.width() + $btnwidth) + "px")});
}
})
.tooltip({
classes: {
"ui-tooltip": "ui-state-highlight"
}
});
//input.width(getwid);
this._on( this.input, {
autocompleteselect: function( event, ui ) {
ui.item.option.selected = true;
this._trigger( "select", event, {
item: ui.item.option
});
this.element.trigger("change");
},
autocompletechange: function( event, ui ) {
this.element.val(this.input.val());
this.element.trigger("change");
this._removeIfInvalid(event,ui);
}
});
},
_createShowAllButton: function() {
var input = this.input,
wasOpen = false;
jQuery( "<a>" )
.attr( "tabIndex", -1 )
.attr( "title", "Show All Items" )
.tooltip()
.appendTo( this.wrapper )
.button({
icons: {
primary: "ui-icon-triangle-1-s"
},
text: false
})
.removeClass( "ui-corner-all" )
.addClass( "custom-combobox-toggle ui-corner-right" )
.on( "mousedown", function() {
wasOpen = input.autocomplete( "widget" ).is( ":visible" );
})
.on( "click", function() {
input.trigger( "focus" );
// Close if already visible
if ( wasOpen ) {
return;
}
// Pass empty string as value to search for, displaying all results
input.autocomplete( "search", "" );
});
},
_source: function( request, response ) {
var matcher = new RegExp( jQuery.ui.autocomplete.escapeRegex(request.term), "i" );
response( this.element.children( "option" ).map(function() {
var text = jQuery( this ).text();
if ( this.value && ( !request.term || matcher.test(text) ) )
return {
label: text,
value: text,
option: this
};
}) ); //.slice(0, 15) applied to map() will limit results to 15
},
_removeIfInvalid: function( event, ui ) {
// Selected an item, nothing to do
if ( ui.item ) {
return;
}
// Search for a match (case-insensitive)
var value = this.input.val(),
valueLowerCase = value.toLowerCase(),
valid = false;
this.element.children( "option" ).each(function() {
if ( jQuery( this ).text().toLowerCase() === valueLowerCase ) {
this.selected = valid = true;
return false;
}
});
// Found a match, nothing to do
if ( valid ) {
return;
}
// Remove invalid value
this.input
.val( "" )
.attr( "title", value + " didn't match any item" )
.tooltip( "open" );
this.element.val( "" );
this._delay(function() {
this.input.tooltip( "close" ).attr( "title", "" );
}, 2500 );
this.input.autocomplete().term = "";
},
_destroy: function() {
this.wrapper.remove();
this.element.show();
},
widget: function() {
return this.input;
}
});
</script>