wizardFilterOnType.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * Copyright (c) 2015, Arthur Schiwon <blizzz@owncloud.com>
  3. * This file is licensed under the Affero General Public License version 3 or later.
  4. * See the COPYING-README file.
  5. */
  6. OCA = OCA || {};
  7. (function() {
  8. /**
  9. * @classdesc filters a select box when a text element is typed in
  10. */
  11. var FilterOnType = OCA.LDAP.Wizard.WizardObject.subClass({
  12. /**
  13. * initializes a type filter on a text input for a select element
  14. *
  15. * @param {jQuery} $select
  16. * @param {jQuery} $textInput
  17. */
  18. init: function($select, $textInput) {
  19. this.$select = $select;
  20. this.$textInput = $textInput;
  21. this.lastSearch = '';
  22. var fity = this;
  23. $textInput.bind('change keyup', function () {
  24. if(fity.runID) {
  25. window.clearTimeout(fity.runID);
  26. }
  27. fity.runID = window.setTimeout(function() {
  28. fity.filter(fity);
  29. }, 250);
  30. });
  31. },
  32. /**
  33. * the actual search or filter method
  34. *
  35. * @param {FilterOnType} fity
  36. */
  37. filter: function(fity) {
  38. var filterVal = fity.$textInput.val().toLowerCase();
  39. if(filterVal === fity.lastSearch) {
  40. return;
  41. }
  42. fity.lastSearch = filterVal;
  43. fity.$select.find('option').each(function() {
  44. if(!filterVal || $(this).val().toLowerCase().indexOf(filterVal) > -1) {
  45. $(this).removeAttr('hidden')
  46. } else {
  47. $(this).attr('hidden', 'hidden');
  48. }
  49. });
  50. delete(fity.runID);
  51. }
  52. });
  53. OCA.LDAP.Wizard.FilterOnType = FilterOnType;
  54. })();