-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathBreadCrumb.js
More file actions
63 lines (49 loc) · 1.56 KB
/
BreadCrumb.js
File metadata and controls
63 lines (49 loc) · 1.56 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
import { validation, checkDifference } from "../../validation.js";
export default function BreadCrumb({ $target, initialState, clickPath }) {
validation(new.target, "BreadCrumb");
const $breadCrumb = document.createElement("nav");
$breadCrumb.className = "linkContainer";
this.state = initialState;
this.setState = (nextState) => {
if (typeof nextState !== "object") throw new Error("변경될 상태가 객체가 아닙니다.");
if (checkDifference(this.state, nextState)) return;
this.state = nextState;
this.render();
};
const recursiveBC = (id, path) => {
const $curLi = document.getElementById(id);
if ($curLi) {
const $ul = $curLi.closest("ul");
if ($ul.className === "child" || $ul.className === "child--show") {
path.push([$curLi.querySelector("span").innerHTML, id]);
recursiveBC($ul.closest("li").id, path);
} else {
path.push([$curLi.querySelector("span").innerHTML, id]);
}
}
};
const renderBreadCrumb = (id) => {
const path = [];
recursiveBC(id, path);
return path
.reverse()
.map((el) => `<span class="link" data-id=${el[1]} >${el[0]}</span>`)
.join(" / ");
};
$breadCrumb.addEventListener("click", (e) => {
const $span = e.target.closest("span");
if ($span) {
const { id } = $span.dataset;
clickPath(id);
}
});
this.render = () => {
const { docId } = this.state;
$breadCrumb.innerHTML = `
<div>
${docId ? renderBreadCrumb(docId) : ""}
</div>
`;
};
$target.prepend($breadCrumb);
}