Debian Codegraph @

gopls/internal/server/assets/splitpkg.js

raw symbols SCIP
1 // Copyright 2025 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // data holds the application state, of Go type splitpkg.ResultJSON.
6 // Each reload() replaces it by the current server state.
7 var data = null;
8
9 // One-time setup.
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 * onClickAddComponent adds a new component, posts it to the server,
19 * and reloads the page.
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 * onClickDeleteComponent deletes a component, posts it to the server,
38 * and reloads the page.
39 */
40 function onClickDeleteComponent(event) {
41 const li = event.target.parentNode;
42 li.parentNode.removeChild(li);
43
44 const index = li.index; // of deleted component
45
46 const names = data.Components.Names;
47 names.splice(index, 1);
48
49 // Update assignments after implicit renumbering of components.
50 const assignments = data.Components.Assignments;
51 Object.entries(assignments).forEach(([k, compIndex]) => {
52 if (compIndex == index) {
53 assignments[k] = 0; // => default component
54 } else if (compIndex > index) {
55 assignments[k] = compIndex - 1;
56 }
57 });
58
59 postComponents();
60 reload();
61 }
62
63 /** postComponents notifies the server of a change in the Components information. */
64 function postComponents() {
65 // Post the updated components.
66 const xhr = new XMLHttpRequest();
67 xhr.open("POST", makeURL('/splitpkg-components'), false); // false => synchronous
68 xhr.setRequestHeader('Content-Type', 'application/json');
69 xhr.send(JSON.stringify(data.Components));
70 if (xhr.status === 200) {
71 // ok
72 } else {
73 alert("failed to post new component list: " + xhr.statusText);
74 return;
75 }
76 }
77
78 /**
79 * onClickApplyAssignment is called when the Apply button is clicked.
80 * It updates the component assignment mapping.
81 */
82 function onClickApplyAssignment(event) {
83 const componentIndex = document.getElementById('assign-select').selectedIndex;
84
85 // Update the component assignment of each spec
86 // whose <li class='spec-node'> checkbox is checked.
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(); // update the server
95 reload(); // recompute the page state
96 }
97
98 /**
99 * reload requests the current server state,
100 * updates the global 'data' variable,
101 * and rebuilds the page DOM to reflect the state.
102 */
103 function reload() {
104 const xhr = new XMLHttpRequest();
105 xhr.open("GET", makeURL('/splitpkg-json'), false); // false => synchronous
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 // Ensure there is always a default component.
120 if (!data.Components.Names) { // undefined, null, or empty
121 data.Components.Names = ["default"];
122 }
123 if (!data.Components.Assignments) { // undefined, null, or empty
124 data.Components.Assignments = {};
125 }
126
127 // Rebuild list of components.
128 const componentsContainer = document.getElementById('components');
129 componentsContainer.replaceChildren(); // clear out previous state
130 const assignSelect = document.getElementById('assign-select');
131 assignSelect.replaceChildren(); // clear out previous state
132 const componentsList = document.createElement('ul');
133 componentsContainer.appendChild(componentsList);
134 data.Components.Names.forEach((name, i) => {
135 // <li>■ name ×
136 const li = document.createElement('li');
137 li.index = i; // custom index field holds component index (for onClickDeleteComponent)
138 componentsList.appendChild(li);
139
140 // ■ name
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) { // the default component cannot be deleted
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 // Add component to the assignment dropdown.
157 assignSelect.add(new Option(name, null));
158 })
159
160 // Rebuild list of decls grouped by file.
161 const filesContainer = document.getElementById('files');
162 filesContainer.replaceChildren(); // clear out previous state
163 data.Files.forEach(fileJson => {
164 filesContainer.appendChild(createFileDiv(fileJson));
165 });
166
167 // Display strongly connected components.
168 const deps = document.getElementById('deps');
169 deps.replaceChildren(); // clear out previous state
170 const depsList = document.createElement('ul');
171 deps.append(depsList);
172
173 // Be explicit if there are no component dependencies.
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 // List all sets of mutually dependent components.
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 // Show intercomponent edges.
190 data.Edges.forEach((edge) => {
191 const edgeItem = document.createElement('li');
192 depsList.append(edgeItem);
193
194 // component edge
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 // sublist of symbol references that induced the edge
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 * makeURL returns a URL string with the specified path,
211 * but preserving the current page's query parameters (view, pkg).
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 /** createFileDiv creates a <div> for a fileJSON object. */
220 function createFileDiv(fileData) {
221 // Create the main container for the file entry.
222 const fileContainer = document.createElement('div');
223 fileContainer.className = 'file-node';
224
225 // Create and append the file's base name as a para.
226 const para = document.createElement('p');
227 fileContainer.appendChild(para);
228
229 // The file's checkbox applies in bulk to all specs within it.
230 var specCheckboxes = [];
231 const fileCheckbox = document.createElement('input');
232 fileCheckbox.type = 'checkbox';
233 fileCheckbox.addEventListener('click', (event) => {
234 // Select/deselect all specs belonging to the file.
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 // Link file name to start of file.
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 // Process declarations if they exist.
251 if (fileData.Decls && fileData.Decls.length > 0) {
252 const declsList = document.createElement('ul');
253 declsList.className = 'decls-list';
254 // For now we flatten out the decl/spec grouping.
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 /** createSpecItem creates an <li> element for a specJSON object (one declared name). */
269 function createSpecItem(kind, specData, checkboxes) {
270 // <li><checkbox/>ⓕ <a>myfunc</a>...
271 const specItem = document.createElement('li');
272 specItem.className = 'spec-node';
273 specItem.dataset.name = specData.Name; // custom .name field holds symbol's unique logical name
274
275 // First child is a checkbox.
276 const specCheckbox = document.createElement('input');
277 specCheckbox.type = 'checkbox';
278 specItem.appendChild(specCheckbox);
279 checkboxes.push(specCheckbox);
280
281 // Next is the component assignment color swatch.
282 const assignSpan = document.createElement('span');
283 assignSpan.className = 'component-swatch';
284 assignSpan.textContent = "■";
285 {
286 var index = data.Components.Assignments[specData.Name]; // may be undefined
287 if (!index) {
288 index = 0; // default
289 }
290 assignSpan.style.color = componentColors[index % componentColors.length];
291 assignSpan.title = "Component " + data.Components.Names[index]; // tooltip
292 }
293 specItem.appendChild(assignSpan);
294
295 // Encircle the func/var/const/type indicator.
296 const symbolSpan = document.createElement('span');
297 const symbol = String.fromCodePoint(kind.codePointAt(0) - 'a'.codePointAt(0) + 'ⓐ'.codePointAt(0));
298 symbolSpan.title = kind; // tooltip
299 symbolSpan.append(`${symbol} `);
300 specItem.append(symbolSpan);
301
302 // Link name to declaration.
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 /** createRefItem creates an <li> element for a refJSON object (a reference). */
312 function createRefItem(refData) {
313 const refItem = document.createElement('li');
314 refItem.className = 'ref-node';
315
316 // Link (from -> to) to the reference in from.
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 /** componentColors is a palette of dark, high-contrast colors. */
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 ];