| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
var data = null; |
| 8 |
|
| 9 |
|
| 10 |
window.addEventListener('load', function() { |
| 11 |
document.getElementById('add-component').addEventListener('click', onClickAddComponent); |
| 12 |
document.getElementById('assign-apply').addEventListener('click', onClickApplyAssignment); |
| 13 |
|
| 14 |
reload(); |
| 15 |
}); |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
function onClickAddComponent(event) { |
| 22 |
const name = document.getElementById('new-component').value.trim(); |
| 23 |
if (name == "") { |
| 24 |
alert("empty component name"); |
| 25 |
return; |
| 26 |
} |
| 27 |
if (data.Components.Names.includes(name)) { |
| 28 |
alert("duplicate component name"); |
| 29 |
return; |
| 30 |
} |
| 31 |
data.Components.Names.push(name); |
| 32 |
postComponents(); |
| 33 |
reload(); |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
function onClickDeleteComponent(event) { |
| 41 |
const li = event.target.parentNode; |
| 42 |
li.parentNode.removeChild(li); |
| 43 |
|
| 44 |
const index = li.index; |
| 45 |
|
| 46 |
const names = data.Components.Names; |
| 47 |
names.splice(index, 1); |
| 48 |
|
| 49 |
|
| 50 |
const assignments = data.Components.Assignments; |
| 51 |
Object.entries(assignments).forEach(([k, compIndex]) => { |
| 52 |
if (compIndex == index) { |
| 53 |
assignments[k] = 0; |
| 54 |
} else if (compIndex > index) { |
| 55 |
assignments[k] = compIndex - 1; |
| 56 |
} |
| 57 |
}); |
| 58 |
|
| 59 |
postComponents(); |
| 60 |
reload(); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
function postComponents() { |
| 65 |
|
| 66 |
const xhr = new XMLHttpRequest(); |
| 67 |
xhr.open("POST", makeURL('/splitpkg-components'), false); |
| 68 |
xhr.setRequestHeader('Content-Type', 'application/json'); |
| 69 |
xhr.send(JSON.stringify(data.Components)); |
| 70 |
if (xhr.status === 200) { |
| 71 |
|
| 72 |
} else { |
| 73 |
alert("failed to post new component list: " + xhr.statusText); |
| 74 |
return; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
function onClickApplyAssignment(event) { |
| 83 |
const componentIndex = document.getElementById('assign-select').selectedIndex; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
document.querySelectorAll('li.spec-node').forEach((specItem) => { |
| 88 |
const checkbox = specItem.firstChild; |
| 89 |
if (checkbox.checked) { |
| 90 |
data.Components.Assignments[specItem.dataset.name] = componentIndex; |
| 91 |
} |
| 92 |
}); |
| 93 |
|
| 94 |
postComponents(); |
| 95 |
reload(); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
function reload() { |
| 104 |
const xhr = new XMLHttpRequest(); |
| 105 |
xhr.open("GET", makeURL('/splitpkg-json'), false); |
| 106 |
xhr.send(null); |
| 107 |
if (xhr.status === 200) { |
| 108 |
try { |
| 109 |
data = JSON.parse(xhr.responseText); |
| 110 |
} catch (e) { |
| 111 |
alert("error parsing JSON: " + e); |
| 112 |
return null; |
| 113 |
} |
| 114 |
} else { |
| 115 |
alert("request failed: " + xhr.statusText); |
| 116 |
return null; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
if (!data.Components.Names) { |
| 121 |
data.Components.Names = ["default"]; |
| 122 |
} |
| 123 |
if (!data.Components.Assignments) { |
| 124 |
data.Components.Assignments = {}; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
const componentsContainer = document.getElementById('components'); |
| 129 |
componentsContainer.replaceChildren(); |
| 130 |
const assignSelect = document.getElementById('assign-select'); |
| 131 |
assignSelect.replaceChildren(); |
| 132 |
const componentsList = document.createElement('ul'); |
| 133 |
componentsContainer.appendChild(componentsList); |
| 134 |
data.Components.Names.forEach((name, i) => { |
| 135 |
× |
| 136 |
const li = document.createElement('li'); |
| 137 |
li.index = i; |
| 138 |
componentsList.appendChild(li); |
| 139 |
|
| 140 |
me |
| 141 |
const span = document.createElement('span'); |
| 142 |
span.className = 'component'; |
| 143 |
span.style.color = componentColors[i % componentColors.length]; |
| 144 |
span.append('■ ', name) |
| 145 |
li.append(span); |
| 146 |
|
| 147 |
× |
| 148 |
if (i > 0) { |
| 149 |
const xSpan = document.createElement('span'); |
| 150 |
xSpan.className = 'delete'; |
| 151 |
xSpan.append(' ×') |
| 152 |
xSpan.addEventListener('click', onClickDeleteComponent); |
| 153 |
li.append(xSpan); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
assignSelect.add(new Option(name, null)); |
| 158 |
}) |
| 159 |
|
| 160 |
|
| 161 |
const filesContainer = document.getElementById('files'); |
| 162 |
filesContainer.replaceChildren(); |
| 163 |
data.Files.forEach(fileJson => { |
| 164 |
filesContainer.appendChild(createFileDiv(fileJson)); |
| 165 |
}); |
| 166 |
|
| 167 |
|
| 168 |
const deps = document.getElementById('deps'); |
| 169 |
deps.replaceChildren(); |
| 170 |
const depsList = document.createElement('ul'); |
| 171 |
deps.append(depsList); |
| 172 |
|
| 173 |
|
| 174 |
if (data.Edges.length == 0) { |
| 175 |
const li = document.createElement('li'); |
| 176 |
depsList.append(li); |
| 177 |
li.append("No dependencies"); |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
data.Cycles.forEach((scc) => { |
| 183 |
const item = document.createElement('li'); |
| 184 |
item.append("⚠ Component cycle: " + |
| 185 |
scc.map(index => data.Components.Names[index]).join(', ')) |
| 186 |
depsList.append(item); |
| 187 |
}) |
| 188 |
|
| 189 |
|
| 190 |
data.Edges.forEach((edge) => { |
| 191 |
const edgeItem = document.createElement('li'); |
| 192 |
depsList.append(edgeItem); |
| 193 |
|
| 194 |
|
| 195 |
const from = data.Components.Names[edge.From]; |
| 196 |
const to = data.Components.Names[edge.To]; |
| 197 |
edgeItem.append((edge.Cyclic ? "⚠ " : "") + from + " ➤ " + to); |
| 198 |
|
| 199 |
|
| 200 |
const refsList = document.createElement('ul'); |
| 201 |
edgeItem.appendChild(refsList); |
| 202 |
refsList.className = 'refs-list'; |
| 203 |
edge.Refs.forEach(ref => { |
| 204 |
refsList.appendChild(createRefItem(ref)); |
| 205 |
}); |
| 206 |
}) |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
function makeURL(path) { |
| 214 |
const url = new URL(window.location.href); |
| 215 |
url.pathname = url.pathname.substring(0, url.pathname.lastIndexOf('/')) + path; |
| 216 |
return url.href; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
function createFileDiv(fileData) { |
| 221 |
|
| 222 |
const fileContainer = document.createElement('div'); |
| 223 |
fileContainer.className = 'file-node'; |
| 224 |
|
| 225 |
|
| 226 |
const para = document.createElement('p'); |
| 227 |
fileContainer.appendChild(para); |
| 228 |
|
| 229 |
|
| 230 |
var specCheckboxes = []; |
| 231 |
const fileCheckbox = document.createElement('input'); |
| 232 |
fileCheckbox.type = 'checkbox'; |
| 233 |
fileCheckbox.addEventListener('click', (event) => { |
| 234 |
|
| 235 |
const checked = event.target.checked; |
| 236 |
specCheckboxes.forEach(checkbox => { |
| 237 |
checkbox.checked = checked; |
| 238 |
}); |
| 239 |
}) |
| 240 |
para.appendChild(fileCheckbox); |
| 241 |
para.append("File "); |
| 242 |
|
| 243 |
|
| 244 |
const baseName = document.createElement('a'); |
| 245 |
para.appendChild(baseName); |
| 246 |
baseName.className = 'file-link'; |
| 247 |
baseName.textContent = fileData.Base; |
| 248 |
baseName.addEventListener('click', () => httpGET(fileData.URL)); |
| 249 |
|
| 250 |
|
| 251 |
if (fileData.Decls && fileData.Decls.length > 0) { |
| 252 |
const declsList = document.createElement('ul'); |
| 253 |
declsList.className = 'decls-list'; |
| 254 |
|
| 255 |
fileData.Decls.forEach(decl => { |
| 256 |
if (decl.Specs && decl.Specs.length > 0) { |
| 257 |
decl.Specs.forEach(spec => { |
| 258 |
declsList.appendChild(createSpecItem(decl.Kind, spec, specCheckboxes)); |
| 259 |
}); |
| 260 |
} |
| 261 |
}); |
| 262 |
fileContainer.appendChild(declsList); |
| 263 |
} |
| 264 |
|
| 265 |
return fileContainer; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
function createSpecItem(kind, specData, checkboxes) { |
| 270 |
.. |
| 271 |
const specItem = document.createElement('li'); |
| 272 |
specItem.className = 'spec-node'; |
| 273 |
specItem.dataset.name = specData.Name; |
| 274 |
|
| 275 |
|
| 276 |
const specCheckbox = document.createElement('input'); |
| 277 |
specCheckbox.type = 'checkbox'; |
| 278 |
specItem.appendChild(specCheckbox); |
| 279 |
checkboxes.push(specCheckbox); |
| 280 |
|
| 281 |
|
| 282 |
const assignSpan = document.createElement('span'); |
| 283 |
assignSpan.className = 'component-swatch'; |
| 284 |
assignSpan.textContent = "■"; |
| 285 |
{ |
| 286 |
var index = data.Components.Assignments[specData.Name]; |
| 287 |
if (!index) { |
| 288 |
index = 0; |
| 289 |
} |
| 290 |
assignSpan.style.color = componentColors[index % componentColors.length]; |
| 291 |
assignSpan.title = "Component " + data.Components.Names[index]; |
| 292 |
} |
| 293 |
specItem.appendChild(assignSpan); |
| 294 |
|
| 295 |
|
| 296 |
const symbolSpan = document.createElement('span'); |
| 297 |
const symbol = String.fromCodePoint(kind.codePointAt(0) - 'a'.codePointAt(0) + 'ⓐ'.codePointAt(0)); |
| 298 |
symbolSpan.title = kind; |
| 299 |
symbolSpan.append(`${symbol} `); |
| 300 |
specItem.append(symbolSpan); |
| 301 |
|
| 302 |
|
| 303 |
const specName = document.createElement('a'); |
| 304 |
specItem.appendChild(specName); |
| 305 |
specName.textContent = ` ${specData.Name}`; |
| 306 |
specName.addEventListener('click', () => httpGET(specData.URL)); |
| 307 |
|
| 308 |
return specItem; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
function createRefItem(refData) { |
| 313 |
const refItem = document.createElement('li'); |
| 314 |
refItem.className = 'ref-node'; |
| 315 |
|
| 316 |
|
| 317 |
const refLink = document.createElement('a'); |
| 318 |
refItem.appendChild(refLink); |
| 319 |
refLink.addEventListener('click', () => httpGET(refData.URL)); |
| 320 |
refLink.textContent = "${refData.From} ➤ ${refData.To}"; |
| 321 |
|
| 322 |
return refItem; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
const componentColors = [ |
| 327 |
"#298429", |
| 328 |
"#4B4B8F", |
| 329 |
"#AD2C2C", |
| 330 |
"#A62CA6", |
| 331 |
"#6E65AF", |
| 332 |
"#D15050", |
| 333 |
"#2CA6A6", |
| 334 |
"#C55656", |
| 335 |
"#7B8C58", |
| 336 |
"#587676", |
| 337 |
"#B95EE1", |
| 338 |
"#AF6D41", |
| 339 |
]; |