Skip to content

Commit 2aee868

Browse files
committed
Merge branch 'refactor/assets-bootstrap'
assets/*.js の保守性・可読性向上リファクタを取り込み: - Jig.bootstrap.register によるページ初期化の共通化 - outbound.js の Mermaid 生成 3 関数の共通部分抽出 - domain.js のセクション区切り整理と関数の並び替え - Jig.mermaid.createBuilder() ファクトリ追加 - library-dependency データの Jig.data 経由化 - jig-data.js のパッケージキャッシュ lazy 化 - 軽微なクリーンアップ(index.js innerHTML、fqnToId コメント)
2 parents 2e63c88 + 0bcf951 commit 2aee868

26 files changed

Lines changed: 396 additions & 427 deletions

jig-core/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ def bundleJsTask = tasks.register("bundleJs") {
9191
'src/main/resources/templates/assets/jig-data.js',
9292
'src/main/resources/templates/assets/jig-glossary.js',
9393
'src/main/resources/templates/assets/jig-dom.js',
94-
'src/main/resources/templates/assets/jig-mermaid.js'
94+
'src/main/resources/templates/assets/jig-mermaid.js',
95+
'src/main/resources/templates/assets/jig-bootstrap.js'
9596
)
9697
def outputFile = bundleJsOutputDir.map { it.file("templates/assets/jig-bundle.js") }
9798

jig-core/src/main/resources/templates/assets/domain.js

Lines changed: 161 additions & 217 deletions
Large diffs are not rendered by default.

jig-core/src/main/resources/templates/assets/glossary.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,6 @@ const GlossaryApp = (() => {
251251
}
252252

253253
function init() {
254-
if (typeof document === "undefined" || !document.body.classList.contains("glossary")) {
255-
return;
256-
}
257-
258254
const terms = getGlossaryData();
259255

260256
const controls = {
@@ -344,11 +340,7 @@ const GlossaryApp = (() => {
344340
};
345341
})();
346342

347-
if (typeof document !== "undefined") {
348-
document.addEventListener("DOMContentLoaded", () => {
349-
GlossaryApp.init();
350-
});
351-
}
343+
Jig.bootstrap.register("glossary", GlossaryApp.init);
352344

353345
if (typeof module !== "undefined" && module.exports) {
354346
module.exports = GlossaryApp;

jig-core/src/main/resources/templates/assets/inbound.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ const InboundApp = (() => {
8484

8585
function buildDiagramBuilder(data, builder, showPhysicalName = false) {
8686
const fqnToNodeId = (fqn) => Jig.util.fqnToId("n", fqn);
87-
builder.applyThemeClassDefs();
8887
const {type: typeLabel, method: mLabel} = Jig.glossary.makeLabels(showPhysicalName);
8988

9089
data.entrypointGroups.forEach((eps, typeFqn) => {
@@ -448,7 +447,7 @@ const InboundApp = (() => {
448447
Jig.mermaid.diagram.createAndRegister(jigCard, (mmdContainer) => {
449448
const diagramGenerator = (dir, opts) => {
450449
const data = prepareDiagramData(adapter, usecaseData);
451-
const builder = new Jig.mermaid.Builder();
450+
const builder = Jig.mermaid.createBuilder();
452451
buildDiagramBuilder(data, builder, opts?.showPhysicalName);
453452
return builder.build(dir);
454453
};
@@ -469,11 +468,7 @@ const InboundApp = (() => {
469468
};
470469
})();
471470

472-
if (typeof document !== 'undefined') {
473-
document.addEventListener("DOMContentLoaded", () => {
474-
InboundApp.init();
475-
});
476-
}
471+
Jig.bootstrap.register("inbound-interface", InboundApp.init);
477472

478473
if (typeof module !== "undefined" && module.exports) {
479474
module.exports = InboundApp;

jig-core/src/main/resources/templates/assets/index.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const IndexApp = (() => {
1212
const domainPackageDiagram = Jig.dom.createElement("div", {className: "mermaid-diagram"});
1313
packageDiagramContainer.appendChild(domainPackageDiagram);
1414

15-
domainPackageDiagram.innerHTML = "";
1615
const generator = (dir, opts) => Jig.mermaid.createPackageLevelDiagram(
1716
{fqn: packageRoot},
1817
allPackages, allPackageRelations,
@@ -97,10 +96,6 @@ const IndexApp = (() => {
9796
}
9897

9998
function init() {
100-
if (typeof document === "undefined" || !document.body.classList.contains("index")) {
101-
return;
102-
}
103-
10499
renderSummary();
105100
renderDocumentLinks();
106101

@@ -170,8 +165,4 @@ const IndexApp = (() => {
170165
};
171166
})();
172167

173-
if (typeof document !== 'undefined') {
174-
document.addEventListener("DOMContentLoaded", () => {
175-
IndexApp.init();
176-
});
177-
}
168+
Jig.bootstrap.register("index", IndexApp.init);

jig-core/src/main/resources/templates/assets/insight.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,6 @@ const InsightApp = (() => {
151151
}
152152

153153
function init() {
154-
if (typeof document === "undefined" || !document.body.classList.contains("insight")) {
155-
return;
156-
}
157-
158154
const insightData = parseInsightData();
159155
if (insightData) {
160156
renderPackageInsights(insightData.packages || []);
@@ -184,11 +180,7 @@ const InsightApp = (() => {
184180
}
185181
})();
186182

187-
if (typeof document !== "undefined") {
188-
document.addEventListener("DOMContentLoaded", () => {
189-
InsightApp.init();
190-
});
191-
}
183+
Jig.bootstrap.register("insight", InsightApp.init);
192184

193185
if (typeof module !== "undefined" && module.exports) {
194186
module.exports = InsightApp;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
globalThis.Jig ??= {};
2+
3+
globalThis.Jig.bootstrap = (() => {
4+
5+
function register(bodyClass, initFn) {
6+
if (typeof document === "undefined") return;
7+
document.addEventListener("DOMContentLoaded", () => {
8+
if (document.body.classList.contains(bodyClass)) initFn();
9+
});
10+
}
11+
12+
return {register};
13+
})();
14+
15+
if (typeof module !== "undefined" && module.exports) {
16+
module.exports = globalThis.Jig.bootstrap;
17+
}

jig-core/src/main/resources/templates/assets/jig-data.js

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,48 @@ globalThis.Jig.data = (() => {
2020
/** @type {Map<string, object>|null} */
2121
let usecaseTypesMap = null;
2222

23+
function buildDomainPackages(domainPackageRoots, types) {
24+
const packageTypesMap = new Map();
25+
26+
for (const type of types) {
27+
const lastDot = type.fqn.lastIndexOf('.');
28+
if (lastDot < 0) continue;
29+
const pkgFqn = type.fqn.substring(0, lastDot);
30+
31+
globalThis.Jig.util.pushToMap(packageTypesMap, pkgFqn, {fqn: type.fqn});
32+
33+
// 子パッケージのみを持つ中間パッケージのナビゲーションを成立させるため、
34+
// pkgFqn から domainPackageRoots に到達するまで空の親パッケージも Map に登録する
35+
let current = pkgFqn;
36+
while (!domainPackageRoots.includes(current)) {
37+
const parentDot = current.lastIndexOf('.');
38+
if (parentDot < 0) break;
39+
const parent = current.substring(0, parentDot);
40+
const isUnderRoot = domainPackageRoots.some(
41+
root => parent === root || parent.startsWith(root + '.'));
42+
if (!isUnderRoot) break;
43+
if (!packageTypesMap.has(parent)) packageTypesMap.set(parent, []);
44+
current = parent;
45+
}
46+
}
47+
48+
return Array.from(packageTypesMap.entries())
49+
.map(([fqn, pkgTypes]) => ({
50+
fqn,
51+
types: pkgTypes.sort((a, b) => a.fqn.localeCompare(b.fqn))
52+
}))
53+
.sort((a, b) => a.fqn.localeCompare(b.fqn));
54+
}
55+
56+
function buildChildPackagesMap(packages) {
57+
const map = new Map(packages.map(p => [p.fqn, []]));
58+
packages.forEach(p => {
59+
const parentFqn = p.fqn.substring(0, p.fqn.lastIndexOf('.'));
60+
if (map.has(parentFqn)) map.get(parentFqn).push(p);
61+
});
62+
return map;
63+
}
64+
2365
const domain = {
2466
get() {
2567
return globalThis.domainData;
@@ -46,17 +88,18 @@ globalThis.Jig.data = (() => {
4688
return domainFqnSet;
4789
},
4890
getPackages() {
91+
if (!domainPackages) {
92+
const data = globalThis.domainData;
93+
domainPackages = data ? buildDomainPackages(data.domainPackageRoots, data.types) : [];
94+
}
4995
return domainPackages;
5096
},
51-
setPackages(packages) {
52-
domainPackages = packages;
53-
},
5497
getChildPackagesMap() {
98+
if (!domainChildPackagesMap) {
99+
domainChildPackagesMap = buildChildPackagesMap(domain.getPackages());
100+
}
55101
return domainChildPackagesMap;
56102
},
57-
setChildPackagesMap(map) {
58-
domainChildPackagesMap = map;
59-
},
60103
};
61104

62105
const glossary = {
@@ -124,6 +167,12 @@ globalThis.Jig.data = (() => {
124167
},
125168
};
126169

170+
const library = {
171+
get() {
172+
return globalThis.libraryDependencyData;
173+
},
174+
};
175+
127176
const navigation = {
128177
get() {
129178
return globalThis.navigationData;
@@ -213,6 +262,7 @@ globalThis.Jig.data = (() => {
213262
package: pkg,
214263
insight,
215264
list,
265+
library,
216266
navigation,
217267
summary,
218268
typeRelations,

jig-core/src/main/resources/templates/assets/jig-mermaid.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,15 @@ globalThis.Jig.mermaid = (() => {
594594
return parentFqns;
595595
}
596596

597+
function createBuilder() {
598+
const builder = new MermaidBuilder();
599+
builder.applyThemeClassDefs();
600+
return builder;
601+
}
602+
597603
return {
598604
MermaidBuilder,
605+
createBuilder,
599606
nodeStyleDefs,
600607
nodeShapes,
601608
escapeId,
@@ -1621,6 +1628,7 @@ globalThis.Jig.mermaid = (() => {
16211628
// 高レベルAPI
16221629
createPackageLevelDiagram,
16231630
Builder: builder.MermaidBuilder,
1631+
createBuilder: builder.createBuilder,
16241632
ClassDiagramBuilder,
16251633
nav,
16261634
};

jig-core/src/main/resources/templates/assets/jig-util.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ globalThis.Jig.util = (() => {
104104
* @return {string}
105105
*/
106106
function fqnToId(prefix, fqn) {
107-
// マルチバイト文字をハッシュ化して一意なIDを生成
107+
// DJB2 系の 32bit ハッシュ。衝突した場合は同一画面内で別の FQN が同じ ID を持ち、
108+
// ページ内アンカー (`#id`) や Mermaid のクリック先が混線する。FQN は通常重複しないため
109+
// 実害は出ていないが、衝突が観測されたらハッシュ幅拡張または完全な FQN を ID 化に切替える。
108110
let hash = 0;
109111
for (let i = 0; i < fqn.length; i++) {
110112
const char = fqn.charCodeAt(i);

0 commit comments

Comments
 (0)