searchBar.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * A text field which calls a function whenever the input value changes.
  3. */
  4. (function() {
  5. (function() {
  6. try {return angular.module('ieat.ui')}
  7. catch {return angular.module('ieat.ui', [])}}
  8. )().component('searchBar', {
  9. templateUrl: 'static/templates/searchBar.html',
  10. bindings: {
  11. /* Sets default value to search */
  12. defaultValue: '@',
  13. /* Minimum query length before search is called */
  14. minLength: '<',
  15. /* Delay before search function is called */
  16. delay: '<',
  17. /* Function to call when search value changes */
  18. onChange: '&'
  19. },
  20. controller: ['$scope', '$timeout', function($scope, $timeout) {
  21. var self = this;
  22. this.$onInit = function() {
  23. self.searchTerm = self.defaultValue || "";
  24. self.minLength = self.minLength || 3;
  25. self.delay = self.delay || 10;
  26. };
  27. var searchTimeout = false;
  28. var lastSearch = null;
  29. var submitChange = function() {
  30. // Only callback if defined and something changed
  31. // Timeout/min length may have cause change to be undone
  32. if (self.onChange && lastSearch != self.searchTerm) {
  33. lastSearch = self.searchTerm;
  34. self.onChange({searchTerm: self.searchTerm});
  35. }
  36. };
  37. $scope.$watchGroup([function() {
  38. return self.searchTerm;
  39. }], function(newValues, oldValues) {
  40. if (searchTimeout)
  41. {
  42. $timeout.cancel(searchTimeout);
  43. }
  44. if (self.searchTerm.length >= self.minLength)
  45. {
  46. searchTimeout = $timeout(submitChange, self.delay);
  47. }
  48. });
  49. }]
  50. });
  51. })();