search.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. function search() {
  2. var searchTerm = document.getElementById('searchfield').value;
  3. var searchableElements = document.getElementsByTagName('section');
  4. var regexMatch = new RegExp(searchTerm, 'i');
  5. // Attempt to create anchor from search term (will default to 'localhost' on failure)
  6. var searchTermUri = document.createElement('a');
  7. searchTermUri.href = searchTerm;
  8. if(searchTermUri.hostname == 'localhost') {
  9. searchTermUri = null;
  10. } else {
  11. // Ignore "www."
  12. if(searchTermUri.hostname.indexOf('www.') === 0) {
  13. searchTermUri.hostname = searchTermUri.hostname.substr(4);
  14. }
  15. }
  16. for(var i = 0; i < searchableElements.length; i++) {
  17. var textValue = searchableElements[i].getAttribute('data-ref');
  18. var anchors = searchableElements[i].getElementsByTagName('a');
  19. if(anchors != null && anchors.length > 0) {
  20. var uriValue = anchors[0]; // First anchor is bridge URI
  21. // Ignore "www."
  22. if(uriValue.hostname.indexOf('www.') === 0) {
  23. uriValue.hostname = uriValue.hostname.substr(4);
  24. }
  25. }
  26. if(textValue != null || uriValue != null) {
  27. if(textValue.match(regexMatch) != null ||
  28. uriValue.hostname.match(regexMatch) ||
  29. searchTermUri != null &&
  30. uriValue.hostname != 'localhost' && (
  31. uriValue.href.match(regexMatch) != null ||
  32. uriValue.hostname == searchTermUri.hostname)) {
  33. searchableElements[i].style.display = 'block';
  34. } else {
  35. searchableElements[i].style.display = 'none';
  36. }
  37. }
  38. }
  39. }