search.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. $(function () {
  2. const $agentNavigate = $("#agent-navigate");
  3. // initialize typeahead listener
  4. $agentNavigate.bind("typeahead:selected", function (event, object, name) {
  5. const item = object["value"];
  6. $agentNavigate.typeahead("val", "");
  7. if (window.agentPaths[item]) {
  8. $(".spinner").show();
  9. const navigationData = window.agentPaths[item];
  10. if (
  11. !(navigationData instanceof Object) ||
  12. !navigationData.method ||
  13. navigationData.method === "GET"
  14. ) {
  15. return (window.location = navigationData.url || navigationData);
  16. } else {
  17. return $.rails.handleMethod.apply(
  18. $(
  19. `<a href='${navigationData.url}' data-method='${navigationData.method}'></a>`
  20. )
  21. .appendTo($("body"))
  22. .get(0)
  23. );
  24. }
  25. }
  26. });
  27. // substring matcher for typeahead
  28. const substringMatcher = function (strings) {
  29. let findMatches;
  30. return (findMatches = function (query, callback) {
  31. const matches = [];
  32. const substrRegex = new RegExp(query, "i");
  33. $.each(strings, function (i, str) {
  34. if (substrRegex.test(str)) {
  35. return matches.push({ value: str });
  36. }
  37. });
  38. return callback(matches.slice(0, 6));
  39. });
  40. };
  41. return $agentNavigate.typeahead(
  42. {
  43. minLength: 1,
  44. highlight: true,
  45. },
  46. { source: substringMatcher(window.agentNames) }
  47. );
  48. });