(function( $ ) { $.widget( "ui.combobox", { _create: function() { this.wrapper = $( "" ) .addClass( "ui-combobox" ) .insertAfter( this.element ); this.element.hide(); this._createAutocomplete(); this._createShowAllButton(); }, _createAutocomplete: function() { var selected = this.element.children( ":selected" ), select = this.element.hide(); value = selected.val() ? selected.text() : select.attr('title'); this.input = $( "" ) .appendTo( this.wrapper ) .val( value ) .attr( "title", "" ) .addClass( "ui-state-default ui-combobox-input ui-widget ui-widget-content ui-corner-left" ) .autocomplete({ delay: 0, minLength: 0, source: $.proxy( this, "_source" ) }) .tooltip({ tooltipClass: "ui-state-highlight" }) .blur(function() { if ($(this).data('new')) { $(this).data('new', false); return; } if ($(this).val() == '') { $(this).addClass('d_val'); $(this).val(select.attr('title')); $(this).parent().next('.err').show(); $(this).addClass('err-field'); $(this).siblings('a').addClass('err-field'); } else{ $(this).parent().next('.err').hide(); $(this).removeClass('err-field'); $(this).siblings('a').removeClass('err-field'); } // добавление класса placeholder if ($(this).val() === '' || $(this).val() === $(this.input).attr('placeholder')) { $(this).addClass('placeholder'); } //выставляем высоту $('#accordionConfig').height($(this).parents('form').height() + 42 + 26); }) .focus(function() { if ($(this).val() == select.attr('title')) { $(this).val(''); } else { $(this).parent().next('.err').hide(); $(this).removeClass('err-field'); $(this).siblings('a').removeClass('err-field'); } // удаляем класс плейсхолдер $(this).removeClass('placeholder'); //выставляем высоту $('#accordionConfig').height($(this).parents('form').height() + 42 + 26); }); this._on( this.input, { autocompleteselect: function( event, ui ) { ui.item.option.selected = true; select.change(); this._trigger( "select", event, { item: ui.item.option }); }, autocompletechange: "_removeIfInvalid" }); }, _createShowAllButton: function() { var input = this.input, wasOpen = false; $( "" ) .attr( "tabIndex", -1 ) .appendTo( this.wrapper ) .button({ icons: { primary: "ui-icon-triangle-1-s" }, text: false }) .removeClass( "ui-corner-all" ) .addClass( "ui-corner-right ui-combobox-toggle" ) .mousedown(function() { wasOpen = input.autocomplete( "widget" ).is( ":visible" ); }) .click(function() { input.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( $.ui.autocomplete.escapeRegex(request.term), "i" ); response(this.element.children( "option" ).map(function() { // если есть аттрибут data-words с массивом слов var words = $(this).data('words') ? $(this).data('words') : []; for (i in words) { // удаление пустых слов if (!words[i]) { delete words[i]; } } // текст внутри option words.push($(this).text()); // возвращаемый объект var ret = { 'label' : $(this).text(), 'value' : $(this).text(), 'option' : this }; // флаг, указывающий что option подходит для выбора var inMatch = this.value && !request.term; if (this.value && !inMatch) { for (i in words) { var w = words[i]; if (matcher.test(w)) { inMatch = true; break; } } } if (inMatch) { return ret; } })); }, _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 ( $( 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( "" ) this.element.val( "" ); this._delay(function() { this.input.tooltip( "close" ).attr( "title", "" ); }, 2500 ); this.input.data( "ui-autocomplete" ).term = ""; }, _destroy: function() { this.wrapper.remove(); this.element.show(); } }); })( jQuery ); $(function() { $( ".combobox" ).combobox(); });