changelog_search.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. $(document).ready(function() {
  2. // add the search form and bind the events
  3. $('h1').after([
  4. '<p>Filter entries by content:',
  5. '<input type="text" value="" id="searchbox" style="width: 50%">',
  6. '<input type="submit" id="searchbox-submit" value="Filter"></p>'
  7. ].join('\n'));
  8. function dofilter() {
  9. try {
  10. var query = new RegExp($('#searchbox').val(), 'i');
  11. }
  12. catch (e) {
  13. return; // not a valid regex (yet)
  14. }
  15. // find headers for the versions (What's new in Python X.Y.Z?)
  16. $('#changelog h2').each(function(index1, h2) {
  17. var h2_parent = $(h2).parent();
  18. var sections_found = 0;
  19. // find headers for the sections (Core, Library, etc.)
  20. h2_parent.find('h3').each(function(index2, h3) {
  21. var h3_parent = $(h3).parent();
  22. var entries_found = 0;
  23. // find all the entries
  24. h3_parent.find('li').each(function(index3, li) {
  25. var li = $(li);
  26. // check if the query matches the entry
  27. if (query.test(li.text())) {
  28. li.show();
  29. entries_found++;
  30. }
  31. else {
  32. li.hide();
  33. }
  34. });
  35. // if there are entries, show the section, otherwise hide it
  36. if (entries_found > 0) {
  37. h3_parent.show();
  38. sections_found++;
  39. }
  40. else {
  41. h3_parent.hide();
  42. }
  43. });
  44. if (sections_found > 0)
  45. h2_parent.show();
  46. else
  47. h2_parent.hide();
  48. });
  49. }
  50. $('#searchbox').keyup(dofilter);
  51. $('#searchbox-submit').click(dofilter);
  52. });