Debian Codegraph @

demos/autocomplete/combobox.html

raw symbols SCIP
1 <!doctype html>
2 <html lang="en">
3 <head>
4 <meta charset="utf-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1">
6 <title>jQuery UI Autocomplete - Combobox</title>
7 <link rel="stylesheet" href="../../themes/base/all.css">
8 <link rel="stylesheet" href="../demos.css">
9 <style>
10 .custom-combobox {
11 position: relative;
12 display: inline-block;
13 }
14 .custom-combobox-toggle {
15 position: absolute;
16 top: 0;
17 bottom: 0;
18 margin-left: -1px;
19 padding: 0;
20 }
21 .custom-combobox-input {
22 margin: 0;
23 padding: 5px 10px;
24 }
25 </style>
26 <script src="../../external/requirejs/require.js"></script>
27 <script src="../bootstrap.js" data-modules="tooltip button">
28 $.widget( "custom.combobox", {
29 _create: function() {
30 this.wrapper = $( "<span>" )
31 .addClass( "custom-combobox" )
32 .insertAfter( this.element );
33
34 this.element.hide();
35 this._createAutocomplete();
36 this._createShowAllButton();
37 },
38
39 _createAutocomplete: function() {
40 var selected = this.element.children( ":selected" ),
41 value = selected.val() ? selected.text() : "";
42
43 this.input = $( "<input>" )
44 .appendTo( this.wrapper )
45 .val( value )
46 .attr( "title", "" )
47 .addClass( "custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left" )
48 .autocomplete({
49 delay: 0,
50 minLength: 0,
51 source: this._source.bind( this )
52 })
53 .tooltip({
54 classes: {
55 "ui-tooltip": "ui-state-highlight"
56 }
57 });
58
59 this._on( this.input, {
60 autocompleteselect: function( event, ui ) {
61 ui.item.option.selected = true;
62 this._trigger( "select", event, {
63 item: ui.item.option
64 });
65 },
66
67 autocompletechange: "_removeIfInvalid"
68 });
69 },
70
71 _createShowAllButton: function() {
72 var input = this.input,
73 wasOpen = false;
74
75 $( "<a>" )
76 .attr( "tabIndex", -1 )
77 .attr( "title", "Show All Items" )
78 .tooltip()
79 .appendTo( this.wrapper )
80 .button({
81 icon: "ui-icon-triangle-1-s",
82 showLabel: false
83 })
84 .removeClass( "ui-corner-all" )
85 .addClass( "custom-combobox-toggle ui-corner-right" )
86 .on( "mousedown", function() {
87 wasOpen = input.autocomplete( "widget" ).is( ":visible" );
88 })
89 .on( "click", function() {
90 input.trigger( "focus" );
91
92 // Close if already visible
93 if ( wasOpen ) {
94 return;
95 }
96
97 // Pass empty string as value to search for, displaying all results
98 input.autocomplete( "search", "" );
99 });
100 },
101
102 _source: function( request, response ) {
103 var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
104 response( this.element.children( "option" ).map(function() {
105 var text = $( this ).text();
106 if ( this.value && ( !request.term || matcher.test(text) ) )
107 return {
108 label: text,
109 value: text,
110 option: this
111 };
112 }) );
113 },
114
115 _removeIfInvalid: function( event, ui ) {
116
117 // Selected an item, nothing to do
118 if ( ui.item ) {
119 return;
120 }
121
122 // Search for a match (case-insensitive)
123 var value = this.input.val(),
124 valueLowerCase = value.toLowerCase(),
125 valid = false;
126 this.element.children( "option" ).each(function() {
127 if ( $( this ).text().toLowerCase() === valueLowerCase ) {
128 this.selected = valid = true;
129 return false;
130 }
131 });
132
133 // Found a match, nothing to do
134 if ( valid ) {
135 return;
136 }
137
138 // Remove invalid value
139 this.input
140 .val( "" )
141 .attr( "title", value + " didn't match any item" )
142 .tooltip( "open" );
143 this.element.val( "" );
144 this._delay(function() {
145 this.input.tooltip( "close" ).attr( "title", "" );
146 }, 2500 );
147 this.input.autocomplete( "instance" ).term = "";
148 },
149
150 _destroy: function() {
151 this.wrapper.remove();
152 this.element.show();
153 }
154 });
155
156 $( "#combobox" ).combobox();
157 $( "#toggle" ).on( "click", function() {
158 $( "#combobox" ).toggle();
159 });
160 </script>
161 </head>
162 <body>
163
164 <div class="ui-widget">
165 <label>Your preferred programming language: </label>
166 <select id="combobox">
167 <option value="">Select one...</option>
168 <option value="ActionScript">ActionScript</option>
169 <option value="AppleScript">AppleScript</option>
170 <option value="Asp">Asp</option>
171 <option value="BASIC">BASIC</option>
172 <option value="C">C</option>
173 <option value="C++">C++</option>
174 <option value="Clojure">Clojure</option>
175 <option value="COBOL">COBOL</option>
176 <option value="ColdFusion">ColdFusion</option>
177 <option value="Erlang">Erlang</option>
178 <option value="Fortran">Fortran</option>
179 <option value="Groovy">Groovy</option>
180 <option value="Haskell">Haskell</option>
181 <option value="Java">Java</option>
182 <option value="JavaScript">JavaScript</option>
183 <option value="Lisp">Lisp</option>
184 <option value="Perl">Perl</option>
185 <option value="PHP">PHP</option>
186 <option value="Python">Python</option>
187 <option value="Ruby">Ruby</option>
188 <option value="Scala">Scala</option>
189 <option value="Scheme">Scheme</option>
190 </select>
191 </div>
192 <button id="toggle">Show underlying select</button>
193
194 <div class="demo-description">
195 <p>A custom widget built by composition of Autocomplete and Button. You can either type something into the field to get filtered suggestions based on your input, or use the button to get the full list of selections.</p>
196 <p>The input is read from an existing select-element for progressive enhancement, passed to Autocomplete with a customized source-option.</p>
197 <p>This is not a supported or even complete widget. Its purely for demoing what autocomplete can do with a bit of customization. <a href="https://www.learningjquery.com/2010/06/a-jquery-ui-combobox-under-the-hood">For a detailed explanation of how the widget works, check out this Learning jQuery article.</a></p>
198 </div>
199 </body>
200 </html>