use std::sync::LazyLock;
pub const D3_JS: &str = include_str!("d3.min.js");
pub const GRAPH_CSS: &str = include_str!("graph.css");
pub const LIVE_JS: &str = include_str!("live.js");
const GRAPH_VIEW_PARTS: &[&str] = &[
include_str!("graph/00-data.js"),
include_str!("graph/10-state.js"),
include_str!("graph/20-theme.js"),
include_str!("graph/30-model.js"),
include_str!("graph/40-sim.js"),
include_str!("graph/50-render.js"),
include_str!("graph/60-interaction.js"),
include_str!("graph/70-tooltip-panel.js"),
include_str!("graph/80-controls.js"),
include_str!("graph/90-main.js"),
];
pub static GRAPH_VIEW_JS: LazyLock<String> = LazyLock::new(|| {
format!(
"(function () {{\n\"use strict\";\n\n{}\n}})();\n",
GRAPH_VIEW_PARTS.join("\n")
)
});
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Delivery {
Inline,
Server,
}
const BODY: &str = r#"<div id="app">
<header>
<h1>code-rcl graph</h1>
<span class="stat">__STAT__</span>
<label><input type="checkbox" data-kind="imports" checked> imports</label>
<label><input type="checkbox" data-kind="calls" checked> calls</label>
<label><input type="checkbox" data-kind="references"> references</label>
<label><input type="checkbox" id="expandAll"> expand all</label>
<button id="collapseAll" type="button">collapse</button>
<input type="search" id="search" placeholder="filter nodes…">
<label><input type="checkbox" id="isolate"> isolate</label>
<button id="fitBtn" type="button">fit</button>
<span id="focusCtl" hidden>
<span id="focusLabel"></span>
<button id="focusShallower" type="button" title="shallower">−</button>
<span id="focusDepth">2</span>
<button id="focusDeeper" type="button" title="deeper">+</button>
<button id="focusClear" type="button">clear focus</button>
</span>
</header>
<div id="stage">
<canvas id="scene"></canvas>
<aside id="sidePanel" hidden></aside>
<div class="legend">
<div><span class="dot sq" style="background:var(--dir)"></span>directory</div>
<div><span class="dot" style="background:var(--file)"></span>file</div>
<div><span class="dot" style="background:var(--func)"></span>function / method</div>
<div><span class="dot" style="background:var(--type)"></span>type</div>
<div><span class="dot" style="background:var(--var)"></span>variable</div>
</div>
<div id="status"></div>
</div>
</div>"#;
pub fn graph_page(data_json: &str, stat: &str, delivery: Delivery) -> String {
let safe = data_json.replace('<', "\\u003c");
let body = BODY.replace("__STAT__", stat);
let (head, tail) = match delivery {
Delivery::Inline => (
format!("<style>{GRAPH_CSS}</style>"),
format!(
"<script>{D3_JS}</script>\n<script>{}</script>",
&*GRAPH_VIEW_JS
),
),
Delivery::Server => (
"<link rel=\"stylesheet\" href=\"/assets/graph.css\">".to_string(),
"<script src=\"/assets/d3.min.js\"></script>\n\
<script src=\"/assets/graph-view.js\"></script>\n\
<script src=\"/assets/live.js\"></script>"
.to_string(),
),
};
format!(
"<!doctype html>\n\
<html lang=\"en\">\n\
<head>\n\
<meta charset=\"utf-8\">\n\
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n\
<title>code-rcl graph</title>\n\
{head}\n\
</head>\n\
<body>\n\
{body}\n\
<script id=\"graph-data\" type=\"application/json\">{safe}</script>\n\
{tail}\n\
</body>\n\
</html>\n"
)
}