Guest User

Combobox

a guest
Nov 7th, 2012
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($, undefined ) {
  2.     $.widget( "ui.combobox", {
  3.     _create: function() {
  4.         var input,
  5.         that = this,
  6.         select = this.element.hide(),
  7.         selected = select.children( ":selected" ),
  8.         value = selected.val() ? selected.text() : "",
  9.         wrapper = this.wrapper = $( "<span>" )
  10.         .addClass( "ui-combobox" )
  11.         .insertAfter( select );
  12.  
  13.         function removeIfInvalid(element) {
  14.         var value = $( element ).val(),
  15.         matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( value ) + "$", "i" ),
  16.         valid = false;
  17.         select.children( "option" ).each(function() {
  18.             if ( $( this ).text().match( matcher ) ) {
  19.             this.selected = valid = true;
  20.             return false;
  21.             }
  22.         });
  23.         if ( !valid ) {
  24.             // remove invalid value, as it didn't match anything
  25.             $( element )
  26.             .val( "" )
  27.             .attr( "title", value + " nenalezeno" )
  28.             .tooltip( "open" );
  29.             select.val( "" );
  30.             setTimeout(function() {
  31.             input.tooltip( "close" ).attr( "title", "" );
  32.             }, 2500 );
  33.             input.data( "autocomplete" ).term = "";
  34.             return false;
  35.         }
  36.         }
  37.  
  38.         input = $( "<input>" )
  39.         .appendTo( wrapper )
  40.         .val( value )
  41.         .attr( "title", "" )
  42.         .addClass( "ui-state-default ui-combobox-input" )
  43.         .autocomplete({
  44.         delay: 200,
  45.         minLength: 3,  
  46.         source: function( request, response ) {
  47.             var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
  48.             response( select.children( "option" ).map(function() {
  49.             var text = $( this ).text();
  50.             if ( this.value && ( !request.term || matcher.test(text) ) )
  51.                 return {
  52.                 label: text.replace(
  53.                     new RegExp(
  54.                     "(?![^&;]+;)(?!<[^<>]*)(" +
  55.                     $.ui.autocomplete.escapeRegex(request.term) +
  56.                     ")(?![^<>]*>)(?![^&;]+;)", "gi"
  57.                     ), "<strong>$1</strong>" ),
  58.                 value: text,
  59.                 option: this
  60.                 };
  61.             }) );
  62.         },
  63.         select: function( event, ui ) {
  64.             ui.item.option.selected = true;
  65.             that._trigger( "selected", event, {
  66.             item: ui.item.option
  67.             });
  68.         },
  69.         change: function( event, ui ) {
  70.             if ( !ui.item )
  71.             return removeIfInvalid( this );
  72.         }
  73.         })
  74.         .addClass( "ui-widget ui-widget-content ui-corner-left" );
  75.  
  76.         input.data( "autocomplete" )._renderItem = function( ul, item ) {
  77.         return $( "<li>" )
  78.         .data( "item.autocomplete", item )
  79.         .append( "<a>" + item.label + "</a>" )
  80.         .appendTo( ul );
  81.         };
  82.  
  83.         $( "<a>" )
  84.         .attr( "tabIndex", -1 )
  85.         .attr( "title", "Vše" )
  86.         .tooltip()
  87.         .appendTo( wrapper )
  88.         .button({
  89.         icons: {
  90.             primary: "ui-icon-triangle-1-s"
  91.         },
  92.         text: false
  93.         })
  94.         .removeClass( "ui-corner-all" )
  95.         .addClass( "ui-corner-right ui-combobox-toggle" )
  96.         .click(function() {
  97.         // close if already visible
  98.         if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
  99.             input.autocomplete( "close" );
  100.             removeIfInvalid( input );
  101.             return;
  102.         }
  103.  
  104.         // work around a bug (likely same cause as #5265)
  105.         $( this ).blur();
  106.  
  107.         // pass empty string as value to search for, displaying all results
  108.         input.autocomplete( "search", "" );
  109.         input.focus();
  110.         });
  111.  
  112.         input
  113.         .tooltip({
  114.         position: {
  115.             of: this.button
  116.         },
  117.         tooltipClass: "ui-state-highlight"
  118.         });
  119.     },
  120.  
  121.     destroy: function() {
  122.         this.wrapper.remove();
  123.         this.element.show();
  124.         $.Widget.prototype.destroy.call( this );
  125.     }
  126.     });
  127. })(jQuery);
  128.  
  129. $(function() {
  130.     $( "#combobox" ).combobox();
  131.     $( "#toggle" ).click(function() {
  132.     $( "#combobox" ).toggle();
  133.     });
  134. });
Advertisement
Add Comment
Please, Sign In to add comment