jquery.combobox.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. input.dblclick(function() {
  81. // pass empty string as value to search for, displaying all results
  82. input.autocomplete('search', '');
  83. });
  84. if(this.options['showButton']) {
  85. this.button = $('<button type="button">&nbsp;</button>')
  86. .attr('tabIndex', -1 )
  87. .attr('title', 'Show All Items')
  88. .insertAfter( input )
  89. .addClass('svg')
  90. .addClass('action')
  91. .addClass('combo-button')
  92. .click(function() {
  93. // close if already visible
  94. if ( input.autocomplete('widget').is(':visible') ) {
  95. input.autocomplete('close');
  96. return;
  97. }
  98. // work around a bug (likely same cause as #5265)
  99. $( this ).blur();
  100. // pass empty string as value to search for, displaying all results
  101. input.autocomplete('search', '');
  102. input.focus();
  103. });
  104. }
  105. },
  106. destroy: function() {
  107. this.input.remove();
  108. //this.button.remove();
  109. this.element.show();
  110. $.Widget.prototype.destroy.call( this );
  111. },
  112. value: function(val) {
  113. if(val != undefined) {
  114. this.input.val(val);
  115. } else {
  116. return this.input.val();
  117. }
  118. },
  119. _setOption: function( key, value ) {
  120. switch( key ) {
  121. case 'id':
  122. this.options['id'] = value;
  123. this.input.attr('id', value);
  124. break;
  125. case 'name':
  126. this.options['name'] = value;
  127. this.input.attr('name', value);
  128. break;
  129. case 'attributes':
  130. var input = this.input;
  131. $.each(this.options['attributes'], function(key, value) {
  132. input.attr(key, value);
  133. });
  134. break;
  135. case 'classes':
  136. var input = this.input;
  137. $.each(this.options['classes'], function(key, value) {
  138. input.addClass(value);
  139. });
  140. break;
  141. case 'editable':
  142. case 'showButton':
  143. this.options[key] = value;
  144. break;
  145. }
  146. // In jQuery UI 1.8, you have to manually invoke the _setOption method from the base widget
  147. $.Widget.prototype._setOption.apply( this, arguments );
  148. // In jQuery UI 1.9 and above, you use the _super method instead
  149. //this._super( "_setOption", key, value );
  150. }
  151. });
  152. })( jQuery );