You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wizardFilterOnType.js 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.updateOptions();
  22. this.lastSearch = '';
  23. var fity = this;
  24. $textInput.bind('change keyup', function () {
  25. if(fity.runID) {
  26. window.clearTimeout(fity.runID);
  27. }
  28. fity.runID = window.setTimeout(function() {
  29. fity.filter(fity);
  30. }, 250);
  31. });
  32. },
  33. /**
  34. * the options will be read in again. Should be called after a
  35. * configuration switch.
  36. */
  37. updateOptions: function() {
  38. var options = [];
  39. this.$select.find('option').each(function() {
  40. options.push({
  41. value: $(this).val(),
  42. normalized: $(this).val().toLowerCase()
  43. }
  44. );
  45. });
  46. this._options = options;
  47. },
  48. /**
  49. * the actual search or filter method
  50. *
  51. * @param {FilterOnType} fity
  52. */
  53. filter: function(fity) {
  54. var filterVal = fity.$textInput.val().toLowerCase();
  55. if(filterVal === fity.lastSearch) {
  56. return;
  57. }
  58. fity.lastSearch = filterVal;
  59. fity.$select.empty();
  60. $.each(fity._options, function() {
  61. if(!filterVal || this.normalized.indexOf(filterVal) > -1) {
  62. fity.$select.append($('<option>').val(this.value).text(this.value));
  63. }
  64. });
  65. delete(fity.runID);
  66. }
  67. });
  68. OCA.LDAP.Wizard.FilterOnType = FilterOnType;
  69. })();