jquery.combobox.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * Inspired by http://jqueryui.com/demos/autocomplete/#combobox
  3. */
  4. (function( $ ) {
  5. $.widget('ui.combobox', {
  6. options: {
  7. id: null,
  8. name: null,
  9. showButton: false,
  10. editable: true
  11. },
  12. _create: function() {
  13. var self = this,
  14. select = this.element.hide(),
  15. selected = select.children(':selected'),
  16. value = selected.val() ? selected.text() : '';
  17. var input = this.input = $('<input type="text">')
  18. .insertAfter( select )
  19. .val( value )
  20. .autocomplete({
  21. delay: 0,
  22. minLength: 0,
  23. source: function( request, response ) {
  24. var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
  25. response( select.children( "option" ).map(function() {
  26. var text = $( this ).text();
  27. if ( this.value && ( !request.term || matcher.test(text) ) )
  28. return {
  29. label: text.replace(
  30. new RegExp(
  31. "(?![^&;]+;)(?!<[^<>]*)(" +
  32. $.ui.autocomplete.escapeRegex(request.term) +
  33. ")(?![^<>]*>)(?![^&;]+;)", "gi"
  34. ), "<strong>$1</strong>" ),
  35. value: text,
  36. option: this
  37. };
  38. }) );
  39. },
  40. select: function( event, ui ) {
  41. self.input.val($(ui.item.option).text());
  42. self.input.trigger('change');
  43. ui.item.option.selected = true;
  44. self._trigger( "selected", event, {
  45. item: ui.item.option
  46. });
  47. },
  48. change: function( event, ui ) {
  49. if ( !ui.item ) {
  50. var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ),
  51. valid = false;
  52. self.input.val($(this).val());
  53. //self.input.trigger('change');
  54. select.children( "option" ).each(function() {
  55. if ( $( this ).text().match( matcher ) ) {
  56. this.selected = valid = true;
  57. return false;
  58. }
  59. });
  60. if ( !self.options['editable'] && !valid ) {
  61. // remove invalid value, as it didn't match anything
  62. $( this ).val( "" );
  63. select.val( "" );
  64. input.data( "autocomplete" ).term = "";
  65. return false;
  66. }
  67. }
  68. }
  69. })
  70. .addClass( "ui-widget ui-widget-content ui-corner-left" );
  71. input.data( "autocomplete" )._renderItem = function( ul, item ) {
  72. return $( "<li></li>" )
  73. .data( "item.autocomplete", item )
  74. .append( "<a>" + item.label + "</a>" )
  75. .appendTo( ul );
  76. };
  77. $.each(this.options, function(key, value) {
  78. self._setOption(key, value);
  79. });
  80. if(this.options['showButton']) {
  81. this.button = $( "<button type='button'>&nbsp;</button>" )
  82. .attr( "tabIndex", -1 )
  83. .attr( "title", "Show All Items" )
  84. .insertAfter( input )
  85. .addClass('svg')
  86. .addClass('action')
  87. .addClass('combo-button')
  88. .click(function() {
  89. // close if already visible
  90. if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
  91. input.autocomplete( "close" );
  92. return;
  93. }
  94. // work around a bug (likely same cause as #5265)
  95. $( this ).blur();
  96. // pass empty string as value to search for, displaying all results
  97. input.autocomplete( "search", "" );
  98. input.focus();
  99. });
  100. }
  101. },
  102. destroy: function() {
  103. this.input.remove();
  104. //this.button.remove();
  105. this.element.show();
  106. $.Widget.prototype.destroy.call( this );
  107. },
  108. value: function(val) {
  109. if(val != undefined) {
  110. this.input.val(val);
  111. } else {
  112. return this.input.val();
  113. }
  114. },
  115. _setOption: function( key, value ) {
  116. switch( key ) {
  117. case 'id':
  118. this.options['id'] = value;
  119. this.input.attr('id', value);
  120. break;
  121. case 'name':
  122. this.options['name'] = value;
  123. this.input.attr('name', value);
  124. break;
  125. case 'attributes':
  126. var input = this.input;
  127. $.each(this.options['attributes'], function(key, value) {
  128. input.attr(key, value);
  129. });
  130. break;
  131. case 'classes':
  132. var input = this.input;
  133. $.each(this.options['classes'], function(key, value) {
  134. input.addClass(value);
  135. });
  136. break;
  137. case 'editable':
  138. case 'showButton':
  139. this.options[key] = value;
  140. break;
  141. }
  142. // In jQuery UI 1.8, you have to manually invoke the _setOption method from the base widget
  143. $.Widget.prototype._setOption.apply( this, arguments );
  144. // In jQuery UI 1.9 and above, you use the _super method instead
  145. //this._super( "_setOption", key, value );
  146. }
  147. });
  148. })( jQuery );