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