|
| 1 | +const owner = "efrat-dev"; |
| 2 | +const repo = "algo-ds-leetcode"; |
| 3 | +const branch = "main"; |
| 4 | +const baseUrl = `https://raw.githubusercontent.com/${owner}/${repo}/${branch}/`; |
| 5 | + |
| 6 | +let fullTree = {}; |
| 7 | +let allPaths = []; // גם קבצים וגם תיקיות |
| 8 | +let history = []; |
| 9 | + |
| 10 | +fetch(`https://api.github.com/repos/${owner}/${repo}/git/trees/${branch}?recursive=1`) |
| 11 | + .then(res => res.json()) |
| 12 | + .then(data => { |
| 13 | + const treeItems = data.tree.filter(item => item.type === "blob" || item.type === "tree"); |
| 14 | + allPaths = treeItems.map(item => item.path); |
| 15 | + const filesOnly = treeItems.filter(item => item.type === "blob" && item.path.endsWith(".cs")); |
| 16 | + fullTree = buildTree(filesOnly.map(f => f.path)); |
| 17 | + renderLevel(fullTree, "Click to view solutions"); |
| 18 | + }); |
| 19 | + |
| 20 | + document.getElementById("searchInput").addEventListener("input", function () { |
| 21 | + const query = this.value.trim().toLowerCase(); |
| 22 | + if (query === "") { |
| 23 | + renderLevel(fullTree, "Click to view solutions"); |
| 24 | + } else { |
| 25 | + // שלוף רק פריטים ששמם (ולא כל הנתיב) תואם לחיפוש |
| 26 | + const results = allPaths.filter(path => { |
| 27 | + const parts = path.split("/"); |
| 28 | + const name = parts[parts.length - 1]; |
| 29 | + return name.toLowerCase().includes(query); |
| 30 | + }); |
| 31 | + renderFlatResults(results, "Search results"); |
| 32 | + } |
| 33 | + }); |
| 34 | +function buildTree(paths) { |
| 35 | + const root = {}; |
| 36 | + for (const path of paths) { |
| 37 | + const parts = path.split("/"); |
| 38 | + let curr = root; |
| 39 | + for (let i = 0; i < parts.length; i++) { |
| 40 | + const part = parts[i]; |
| 41 | + if (!curr[part]) { |
| 42 | + curr[part] = (i === parts.length - 1) ? path : {}; |
| 43 | + } |
| 44 | + curr = curr[part]; |
| 45 | + } |
| 46 | + } |
| 47 | + return root; |
| 48 | +} |
| 49 | + |
| 50 | +function renderLevel(level, title) { |
| 51 | + const container = document.getElementById("container"); |
| 52 | + container.innerHTML = ""; |
| 53 | + document.getElementById("codeViewer").innerHTML = ""; |
| 54 | + document.getElementById("pathTitle").innerText = title; |
| 55 | + document.getElementById("backBtn").style.display = history.length ? "inline-block" : "none"; |
| 56 | + |
| 57 | + for (const key in level) { |
| 58 | + const card = document.createElement("div"); |
| 59 | + card.className = "card"; |
| 60 | + card.innerText = key.replace(/\.cs$/, ""); |
| 61 | + card.onclick = () => { |
| 62 | + if (typeof level[key] === "string") { |
| 63 | + loadFile(level[key]); |
| 64 | + } else { |
| 65 | + history.push({ level, title }); |
| 66 | + renderLevel(level[key], key); |
| 67 | + } |
| 68 | + }; |
| 69 | + container.appendChild(card); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +function renderFlatResults(paths, title) { |
| 74 | + const container = document.getElementById("container"); |
| 75 | + container.innerHTML = ""; |
| 76 | + document.getElementById("codeViewer").innerHTML = ""; |
| 77 | + document.getElementById("pathTitle").innerText = title; |
| 78 | + document.getElementById("backBtn").style.display = "inline-block"; |
| 79 | + |
| 80 | + if (paths.length === 0) { |
| 81 | + container.innerHTML = "<p>No results found</p>"; |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + for (const path of paths) { |
| 86 | + const parts = path.split("/"); |
| 87 | + const name = parts[parts.length - 1].replace(/\.cs$/, ""); |
| 88 | + |
| 89 | + const card = document.createElement("div"); |
| 90 | + card.className = "card"; |
| 91 | + card.innerText = name; |
| 92 | + |
| 93 | + card.onclick = () => { |
| 94 | + if (path.endsWith(".cs")) { |
| 95 | + loadFile(path); |
| 96 | + } else { |
| 97 | + const subtree = getSubTree(fullTree, path.split("/")); |
| 98 | + if (subtree) { |
| 99 | + history.push({ level: fullTree, title: "Click to view solutions" }); |
| 100 | + renderLevel(subtree, name); |
| 101 | + } else { |
| 102 | + alert("Folder not found."); |
| 103 | + } |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + container.appendChild(card); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // פונקציה שמקבלת fullTree ונתיב, ומחזירה את הסאב-עץ המתאים |
| 112 | + function getSubTree(tree, parts) { |
| 113 | + let curr = tree; |
| 114 | + for (const part of parts) { |
| 115 | + if (curr[part] && typeof curr[part] === "object") { |
| 116 | + curr = curr[part]; |
| 117 | + } else { |
| 118 | + return null; |
| 119 | + } |
| 120 | + } |
| 121 | + return curr; |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | +function loadFile(path) { |
| 127 | + const url = baseUrl + path; |
| 128 | + fetch(url) |
| 129 | + .then(res => res.text()) |
| 130 | + .then(code => { |
| 131 | + document.getElementById("container").innerHTML = ""; |
| 132 | + document.getElementById("pathTitle").innerText = path; |
| 133 | + document.getElementById("codeViewer").innerHTML = ` |
| 134 | + <pre><code class="language-csharp">${escapeHtml(code)}</code></pre> |
| 135 | + `; |
| 136 | + hljs.highlightAll(); |
| 137 | + document.getElementById("backBtn").style.display = "inline-block"; |
| 138 | + }); |
| 139 | +} |
| 140 | + |
| 141 | +document.getElementById("backBtn").onclick = () => { |
| 142 | + const prev = history.pop(); |
| 143 | + if (prev) { |
| 144 | + renderLevel(prev.level, prev.title); |
| 145 | + } else { |
| 146 | + renderLevel(fullTree, "Click to view solutions"); |
| 147 | + } |
| 148 | +}; |
| 149 | + |
| 150 | +function escapeHtml(text) { |
| 151 | + return text.replace(/[&<>"']/g, m => ({ |
| 152 | + '&': '&', |
| 153 | + '<': '<', |
| 154 | + '>': '>', |
| 155 | + '"': '"', |
| 156 | + "'": ''' |
| 157 | + })[m]); |
| 158 | +} |
0 commit comments