-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
333 lines (286 loc) · 11.3 KB
/
script.js
File metadata and controls
333 lines (286 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
const container = document.getElementById("ruleContainer");
const typeFilter = document.getElementById("typeFilter");
const tagFilter = document.getElementById("tagFilter");
const stakeholderFilter = document.getElementById("stakeholderFilter"); // Stakeholder filter
const sidebar = document.getElementById("infoSidebar");
const sidebarContent = document.getElementById("sidebarContent");
const languageSelect = document.getElementById("languageSelect");
let allRules = [];
let currentLanguage = "english";
async function fetchRules() {
const response = await fetch("rules.json");
return await response.json();
}
function getUniqueTags(rules) {
const tagSet = new Set();
rules.forEach(rule => {
if (rule.tags) {
rule.tags.forEach(tag => tagSet.add(tag));
}
});
return Array.from(tagSet);
}
function getUniqueStakeholders(rules) {
const stakeholderSet = new Set();
rules.forEach(rule => {
if (rule.stakeholders) {
rule.stakeholders.forEach(stakeholder => stakeholderSet.add(stakeholder));
}
});
return Array.from(stakeholderSet);
}
function updateTagFilterOptions(tags) {
tagFilter.innerHTML = '<option value="all">All Tags</option>';
tags.forEach(tag => {
const opt = document.createElement('option');
opt.value = tag;
opt.textContent = tag;
tagFilter.appendChild(opt);
});
}
function updateStakeholderFilterOptions(stakeholders) {
stakeholderFilter.innerHTML = ''; // Clear existing options
const allOpt = document.createElement('option');
allOpt.value = 'all';
allOpt.textContent = 'All Stakeholders';
stakeholderFilter.appendChild(allOpt);
stakeholders.forEach(stakeholder => {
const opt = document.createElement('option');
opt.value = stakeholder;
opt.textContent = stakeholder;
stakeholderFilter.appendChild(opt);
});
}
function findAllUpstream(rule, rules, visited = new Set()) {
const results = [];
if (!rule.links || rule.links.length === 0 || visited.has(rule.id)) return results;
visited.add(rule.id);
rule.links.forEach(linkId => {
const parent = rules.find(r => r.id === linkId);
if (parent && !visited.has(parent.id)) {
results.push({
...parent,
nestedParents: findAllUpstream(parent, rules, visited) // Find nested parents
});
}
});
return results;
}
function findChildren(rule, rules, visited = new Set()) {
if (visited.has(rule.id)) return []; // Avoid circular references
visited.add(rule.id);
const children = rules.filter(r => r.links && r.links.includes(rule.id));
return children.map(child => ({
...child,
nestedChildren: findChildren(child, rules, visited) // Find nested children
}));
}
function getDisplayedText(rule) {
if (currentLanguage === "french") {
return `<p class="fancy-cursive">${rule.translation}</p>`;
} else {
return `<p>${rule.content}</p>`;
}
}
function renderRules(rules, type, tag, singleRuleId = null) {
container.innerHTML = "";
sidebar.style.display = "none";
sidebarContent.innerHTML = "";
let filtered = rules;
if (singleRuleId !== null) {
const rule = rules.find(r => r.id === singleRuleId);
console.log("Fetching rule with ID:", singleRuleId); // Debugging line
console.log("Found rule:", rule); // Debugging line
if (!rule) {
container.innerHTML = `<p>Rule #${singleRuleId} not found.</p>`;
return;
}
function createRuleDiv(rule, nested = false, collapsed = true) {
const wrapper = document.createElement("div");
wrapper.className = `rule ${rule.type}`;
wrapper.id = `rule-${rule.id}`;
if (nested) wrapper.style.marginLeft = "1rem";
const bodyId = `body-${rule.id}`;
wrapper.innerHTML = `
<div class="rule-header" onclick="toggleRuleBody(${rule.id})">
<span><strong>#${rule.id} ${rule.name}</strong></span>
<span class="type-label">${rule.type}</span>
</div>
<div class="rule-body" id="${bodyId}" style="display: ${collapsed ? 'none' : 'block'};">
${getDisplayedText(rule)}
${rule.tags ? `<p>${rule.tags.map(tag => `<span class="tag-link" onclick="filterByTag('${tag}')">#${tag}</span>`).join(' ')}</p>` : ''}
</div>
`;
return wrapper;
}
function renderLinkedSection(title, relatedRules, direction) {
if (relatedRules.length === 0) return null;
const sectionWrapper = document.createElement("div");
const toggleId = `toggle-${direction}`;
sectionWrapper.innerHTML = `
<div class="rule-header" onclick="document.getElementById('${toggleId}').style.display =
document.getElementById('${toggleId}').style.display === 'none' ? 'block' : 'none'">
<strong>${title} (${relatedRules.length})</strong>
</div>
<div id="${toggleId}" style="display: block; margin-left: 1rem;"></div>
`;
const innerContainer = sectionWrapper.querySelector(`#${toggleId}`);
// Recursive function to render nested parents or children
function renderNestedRules(rules, margin = "1rem") {
rules.forEach(r => {
const wrapper = document.createElement("div");
wrapper.className = `rule ${r.type}`;
wrapper.style.marginLeft = margin; // Add margin for nesting
const bodyId = `body-${r.id}`;
wrapper.innerHTML = `
<div class="rule-header" onclick="toggleRuleBody(${r.id})">
<span>
<strong>#${r.id} ${r.name}</strong>
<a class="external-link" href="?id=${r.id}" onclick="event.stopPropagation()">🔗</a>
</span>
<span class="type-label">${r.type}</span>
</div>
<div class="rule-body" id="${bodyId}" style="display: none;">
${getDisplayedText(r)}
${r.tags ? `<p>${r.tags.map(tag => `<span class="tag-link" onclick="filterByTag('${tag}'); event.stopPropagation();">#${tag}</span>`).join(' ')}</p>` : ''}
</div>
`;
innerContainer.appendChild(wrapper);
// If the rule has nested parents or children, render them recursively
if (r.nestedParents && r.nestedParents.length > 0) {
renderNestedRules(r.nestedParents, "2rem"); // Increase margin for nesting
}
if (r.nestedChildren && r.nestedChildren.length > 0) {
renderNestedRules(r.nestedChildren, "2rem"); // Increase margin for nesting
}
});
}
renderNestedRules(relatedRules); // Initial call to render
return sectionWrapper;
}
container.innerHTML = "";
// Main rule
container.appendChild(createRuleDiv(rule, false, false));
// Links (upstream)
const upstream = findAllUpstream(rule, rules);
const upstreamSection = renderLinkedSection("🔗 Links", upstream, "upstream");
if (upstreamSection) container.appendChild(upstreamSection);
// Linked By (downstream)
const downstream = findChildren(rule, rules);
const downstreamSection = renderLinkedSection("🔗 Linked By", downstream, "downstream");
if (downstreamSection) container.appendChild(downstreamSection);
// Navigation buttons
const nav = document.createElement("div");
nav.style.marginTop = "2rem";
nav.style.display = "flex";
nav.style.justifyContent = "space-between";
const prevBtn = document.createElement("button");
prevBtn.textContent = "⬅️ Previous";
prevBtn.onclick = () => {
const prevId = rule.id - 1;
history.pushState({}, "", `?id=${prevId}`);
showRuleById(prevId);
};
if (rule.id <= 1) prevBtn.disabled = true;
const nextBtn = document.createElement("button");
nextBtn.textContent = "Next ➡️";
nextBtn.onclick = () => {
const nextId = rule.id + 1;
history.pushState({}, "", `?id=${nextId}`);
showRuleById(nextId);
};
nav.appendChild(prevBtn);
nav.appendChild(nextBtn);
container.appendChild(nav);
} else {
if (type !== "all") filtered = filtered.filter(r => r.type === type);
if (tag !== "all") filtered = filtered.filter(r => r.tags && r.tags.includes(tag));
filtered.sort((a, b) => a.id - b.id);
filtered.forEach(rule => {
const div = document.createElement("div");
div.className = `rule ${rule.type}`;
div.id = `rule-${rule.id}`;
div.innerHTML = `
<div class="rule-header" onclick="toggleRuleBody(${rule.id})">
<span><strong>#${rule.id} ${rule.name}</strong>
<a class="external-link" href="?id=${rule.id}">🔗</a></span>
<span class="type-label"><a href="#" onclick="filterByType('${rule.type}'); event.stopPropagation();">${rule.type}</a></span>
</div>
<div class="rule-body" id="body-${rule.id}" style="display: none;">
${getDisplayedText(rule)}
<div>${rule.tags ? rule.tags.map(tag => `<span class="tag-link" onclick="filterByTag('${tag}')">#${tag}</span>`).join('') : ''}</div>
</div>
`;
container.appendChild(div);
});
}
}
function toggleRuleBody(id) {
const el = document.getElementById(`body-${id}`);
if (el) {
el.style.display = el.style.display === "block" ? "none" : "block";
}
}
function filterByTag(tag) {
tagFilter.value = tag;
renderRules(allRules, typeFilter.value, tagFilter.value);
}
function filterByType(type) {
typeFilter.value = type;
renderRules(allRules, typeFilter.value, tagFilter.value);
}
function goBack() {
history.pushState({}, "", window.location.pathname);
renderRules(allRules, typeFilter.value, tagFilter.value);
}
function goNext(id) {
const nextId = id + 1;
const rule = allRules.find(r => r.id === nextId);
if (rule) {
history.pushState({}, "", `?id=${nextId}`);
showRuleById(nextId);
}
}
function showRuleById(id) {
renderRules(allRules, null, null, parseInt(id)); // Ignore stakeholders when showing specific rule
}
// Fetch data and render
fetchRules().then(rules => {
allRules = rules;
updateTagFilterOptions(getUniqueTags(rules));
updateStakeholderFilterOptions(getUniqueStakeholders(rules)); // Update stakeholder options
const urlParams = new URLSearchParams(window.location.search);
const currentId = urlParams.get('id');
if (currentId) {
showRuleById(parseInt(currentId)); // Ensure we parse the ID as an integer
} else {
stakeholderFilter.value = 'all'; // Set default to all stakeholders
renderRules(allRules, typeFilter.value, tagFilter.value);
}
});
// Event listeners
typeFilter.addEventListener("change", () => {
renderRules(allRules, typeFilter.value, tagFilter.value);
});
tagFilter.addEventListener("change", () => {
renderRules(allRules, typeFilter.value, tagFilter.value);
});
document.getElementById("displayAllBtn").addEventListener("click", () => {
const btn = document.getElementById("displayAllBtn");
const isCollapsed = btn.dataset.state === "collapsed";
document.querySelectorAll(".rule-body").forEach(body => {
body.style.display = isCollapsed ? "block" : "none";
});
btn.textContent = isCollapsed ? "🔒 Collapse All" : "🔓 Display All";
btn.dataset.state = isCollapsed ? "expanded" : "collapsed";
});
languageSelect.addEventListener("change", () => {
currentLanguage = languageSelect.value;
const urlParams = new URLSearchParams(window.location.search);
const currentId = urlParams.get('id');
if (currentId) {
showRuleById(parseInt(currentId)); // Ensure we parse the ID as an integer
} else {
renderRules(allRules, typeFilter.value, tagFilter.value);
}
});