search.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. var searchAttr = 'data-search-mode';
  2. function contains(a,m){
  3. return (a.textContent || a.innerText || "").toUpperCase().indexOf(m) !== -1;
  4. };
  5. //on search
  6. document.getElementById("nav-search").addEventListener("keyup", function(event) {
  7. var search = this.value.toUpperCase();
  8. if (!search) {
  9. //no search, show all results
  10. document.documentElement.removeAttribute(searchAttr);
  11. document.querySelectorAll("nav > ul > li:not(.level-hide)").forEach(function(elem) {
  12. elem.style.display = "block";
  13. });
  14. if (typeof hideAllButCurrent === "function"){
  15. //let's do what ever collapse wants to do
  16. hideAllButCurrent();
  17. } else {
  18. //menu by default should be opened
  19. document.querySelectorAll("nav > ul > li > ul li").forEach(function(elem) {
  20. elem.style.display = "block";
  21. });
  22. }
  23. } else {
  24. //we are searching
  25. document.documentElement.setAttribute(searchAttr, '');
  26. //show all parents
  27. document.querySelectorAll("nav > ul > li").forEach(function(elem) {
  28. elem.style.display = "block";
  29. });
  30. document.querySelectorAll("nav > ul").forEach(function(elem) {
  31. elem.style.display = "block";
  32. });
  33. //hide all results
  34. document.querySelectorAll("nav > ul > li > ul li").forEach(function(elem) {
  35. elem.style.display = "none";
  36. });
  37. //show results matching filter
  38. document.querySelectorAll("nav > ul > li > ul a").forEach(function(elem) {
  39. if (!contains(elem.parentNode, search)) {
  40. return;
  41. }
  42. elem.parentNode.style.display = "block";
  43. });
  44. //hide parents without children
  45. document.querySelectorAll("nav > ul > li").forEach(function(parent) {
  46. var countSearchA = 0;
  47. parent.querySelectorAll("a").forEach(function(elem) {
  48. if (contains(elem, search)) {
  49. countSearchA++;
  50. }
  51. });
  52. var countUl = 0;
  53. var countUlVisible = 0;
  54. parent.querySelectorAll("ul").forEach(function(ulP) {
  55. // count all elements that match the search
  56. if (contains(ulP, search)) {
  57. countUl++;
  58. }
  59. // count all visible elements
  60. var children = ulP.children
  61. for (i=0; i<children.length; i++) {
  62. var elem = children[i];
  63. if (elem.style.display != "none") {
  64. countUlVisible++;
  65. }
  66. }
  67. });
  68. if (countSearchA == 0 && countUl === 0){
  69. //has no child at all and does not contain text
  70. parent.style.display = "none";
  71. } else if(countSearchA == 0 && countUlVisible == 0){
  72. //has no visible child and does not contain text
  73. parent.style.display = "none";
  74. }
  75. });
  76. document.querySelectorAll("nav > ul.collapse_top").forEach(function(parent) {
  77. var countVisible = 0;
  78. parent.querySelectorAll("li").forEach(function(elem) {
  79. if (elem.style.display !== "none") {
  80. countVisible++;
  81. }
  82. });
  83. if (countVisible == 0) {
  84. //has no child at all and does not contain text
  85. parent.style.display = "none";
  86. }
  87. });
  88. }
  89. });