search.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. $( document ).ready(function() {
  2. jQuery.expr[':'].Contains = function(a,i,m){
  3. return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase())>=0;
  4. };
  5. //on search
  6. $("#nav-search").on("keyup", function(event) {
  7. var search = $(this).val();
  8. if (!search) {
  9. //no search, show all results
  10. $("nav > ul > li").show();
  11. if(typeof hideAllButCurrent === "function"){
  12. //let's do what ever collapse wants to do
  13. hideAllButCurrent();
  14. }
  15. else{
  16. //menu by default should be opened
  17. $("nav > ul > li > ul li").show();
  18. }
  19. }
  20. else{
  21. //we are searching
  22. //show all parents
  23. $("nav > ul > li").show();
  24. //hide all results
  25. $("nav > ul > li > ul li").hide();
  26. //show results matching filter
  27. $("nav > ul > li > ul").find("a:Contains("+search+")").parent().show();
  28. //hide parents without children
  29. $("nav > ul > li").each(function(){
  30. if($(this).find("a:Contains("+search+")").length == 0 && $(this).children("ul").length === 0){
  31. //has no child at all and does not contain text
  32. $(this).hide();
  33. }
  34. else if($(this).find("a:Contains("+search+")").length == 0 && $(this).find("ul").children(':visible').length == 0){
  35. //has no visible child and does not contain text
  36. $(this).hide();
  37. }
  38. });
  39. }
  40. });
  41. });