Skip to content

Commit 881d1cd

Browse files
authored
ruleset: nav bar, no results, some niceties, search stickiness (#229)
1 parent 5fadce4 commit 881d1cd

File tree

2 files changed

+103
-8
lines changed

2 files changed

+103
-8
lines changed

templates/ruleset_filter.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
const SEARCH_TAG_NAMES = ["h1", "h2", "h3", "h4", "h5", "h6", "p"];
22
const SEARCH_BAR_ID = "searchBar";
3+
const NO_RESULTS_DIV_ID = "ruleset-no-results";
4+
let NO_RESULTS_DIV = null;
35
let RULESET_ELEMENTS = null;
46
let NODE_MAP = null;
7+
let HEAD_NODE = "H0";
58

69
function treeifyHeaders() {
710
const stack = [{domNode: {tagName: "H0"}, children: [], parent: null}];
811
const nodeMap = new Map();
12+
nodeMap[HEAD_NODE] = stack[0];
913
const rulesetElements = document.querySelectorAll(SEARCH_TAG_NAMES.join(","))
1014
for (const rulesetElement of rulesetElements) {
1115
const node = {
@@ -41,7 +45,11 @@ function filterRuleset() {
4145
if (filter == "") {
4246
RULESET_ELEMENTS.forEach(rulesetElement => {
4347
rulesetElement.classList.remove("hidden");
48+
if (rulesetElement.tagName != "P") {
49+
document.getElementById(`navigation-${rulesetElement.tagName}-${rulesetElement.id}`).classList.remove("hidden");
50+
}
4451
});
52+
NO_RESULTS_DIV.classList.remove("hidden");
4553
return;
4654
}
4755

@@ -66,13 +74,58 @@ function filterRuleset() {
6674
RULESET_ELEMENTS.forEach(rulesetElement => {
6775
if (!toShow.has(rulesetElement)) {
6876
rulesetElement.classList.add("hidden");
77+
if (rulesetElement.tagName != "P") {
78+
document.getElementById(`navigation-${rulesetElement.tagName}-${rulesetElement.id}`).classList.add("hidden");
79+
}
6980
} else {
7081
rulesetElement.classList.remove("hidden");
82+
if (rulesetElement.tagName != "P") {
83+
document.getElementById(`navigation-${rulesetElement.tagName}-${rulesetElement.id}`).classList.remove("hidden");
84+
}
85+
}
86+
});
87+
88+
let allHidden = true;
89+
RULESET_ELEMENTS.forEach(rulesetElement => {
90+
if (!rulesetElement.classList.contains("hidden")) {
91+
allHidden = false;
92+
}
93+
});
94+
95+
if (allHidden) {
96+
NO_RESULTS_DIV.classList.remove("hidden");
97+
} else {
98+
NO_RESULTS_DIV.classList.add("hidden");
99+
}
100+
101+
}
102+
103+
function generateList(headNodes) {
104+
let list = `<ul>`;
105+
headNodes.forEach(child => {
106+
if (child.domNode.tagName != "P") {
107+
list += `<li id="navigation-${child.domNode.tagName}-${child.domNode.id}"><a href="#${child.domNode.id}">${child.domNode.textContent}</a>${generateList(child.children)}</li>`;
108+
}
109+
});
110+
return list + '</ul>';
111+
}
112+
113+
function generateAnchors(headNodes) {
114+
getAllChildren(NODE_MAP[HEAD_NODE]).forEach(rulesetElement => {
115+
if (rulesetElement.domNode.tagName != "P") {
116+
const anchor = rulesetElement.domNode.textContent
117+
.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"")
118+
.replace(/ /g, "-")
119+
.toLowerCase();
120+
rulesetElement.domNode.id = anchor;
71121
}
72122
});
73123
}
74124

75125
window.onload = function() {
76126
[RULESET_ELEMENTS, NODE_MAP] = treeifyHeaders();
77127
document.getElementById(SEARCH_BAR_ID).addEventListener("input", filterRuleset);
128+
generateAnchors();
129+
document.getElementById("navigator").innerHTML = generateList(NODE_MAP[HEAD_NODE].children);
130+
NO_RESULTS_DIV = document.getElementById(NO_RESULTS_DIV_ID);
78131
}

templates/template_ruleset.html

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,70 @@
11
{% include './header.html' %}
22
<script>{% include './ruleset_filter.js' %}</script>
33
<style>
4+
#searchBarDiv {
5+
position: sticky;
6+
top: 0;
7+
padding: 30px;
8+
background: black;
9+
}
410
#searchBar {
511
font-family: inherit;
612
font-size: 40px;
713
border: none;
814
outline: none;
915
padding: 10px;
1016
color: black;
11-
background: #549171;
12-
border-bottom: 5px solid #549171;
13-
width: 100%;
14-
margin-bottom: 30px;
17+
background: #80BA50;
18+
border-bottom: 5px solid transparent;
1519
border-radius: 10px 10px 0px 0px;
1620
transition: 0.3s;
21+
opacity: 0.7;
22+
width: calc(100% - 80px);
1723
}
1824
#searchBar:focus {
1925
color: black;
20-
background: #80BA50;
2126
border-bottom: 5px solid #1B4D3E;
27+
opacity: 1;
28+
}
29+
h1 {
30+
width: calc(100% - 10px);
31+
background: rgba(255, 255, 255, 0.1);
32+
margin-bottom: 5px !important;
33+
padding: 5px;
34+
}
35+
#contents {
36+
margin-left: calc(var(--page-margin) * -1);
37+
margin-right: calc(var(--page-margin) * -1);
38+
}
39+
#rulesetpage {
40+
background: black;
41+
}
42+
#ruleset {
43+
display: flex;
44+
flex-direction: row;
45+
}
46+
.writing {
47+
width: 100%;
48+
padding: 0;
49+
padding-right: 30px;
50+
padding-bottom: 30px;
51+
}
52+
#navigator {
53+
max-width: 370px;
54+
margin-right: 30px;
2255
}
2356
</style>
24-
<div class="writing">
25-
<input type="text" id="searchBar" placeholder="Search rules...">
26-
{{ data }}
57+
<div id="rulesetpage">
58+
<div id="searchBarDiv">
59+
<input type="text" id="searchBar" placeholder="Search rules...">
60+
</div>
61+
<div id="ruleset">
62+
<div id="navigator">
63+
</div>
64+
<div class="writing">
65+
{{ data }}
66+
<div id="ruleset-no-results">No results. Stop trying to make things up. No, the rules do not say that you're invincible, but good try.</div>
67+
</div>
68+
</div>
2769
</div>
2870
{% include './footer.html' %}

0 commit comments

Comments
 (0)