367 lines
19 KiB
Plaintext
367 lines
19 KiB
Plaintext
@using System.Text.Json
|
||
@model RelationshipMapViewModel
|
||
@{ var jsonOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; }
|
||
<script src="https://cdn.jsdelivr.net/npm/cytoscape/dist/cytoscape.min.js"></script>
|
||
<script>
|
||
(() => {
|
||
let nodes = @Html.Raw(JsonSerializer.Serialize(Model.Nodes, jsonOptions));
|
||
let links = @Html.Raw(JsonSerializer.Serialize(Model.Links, jsonOptions));
|
||
let relationships = @Html.Raw(JsonSerializer.Serialize(Model.Relationships, jsonOptions));
|
||
const relationshipById = new Map(relationships.map(item => [item.characterRelationshipID, item]));
|
||
const castMetadata = @Html.Raw(JsonSerializer.Serialize(Model.Cast, jsonOptions));
|
||
const nodeById = new Map(castMetadata.map(item => [item.characterID, item]));
|
||
const graph = document.getElementById("relationshipMapGraph");
|
||
const error = document.getElementById("relationshipMapError");
|
||
if (!graph) return;
|
||
|
||
if (!window.cytoscape) {
|
||
error?.classList.remove("d-none");
|
||
return;
|
||
}
|
||
|
||
let focusCharacterId = @Html.Raw(JsonSerializer.Serialize(Model.FocusCharacterID));
|
||
const fullNetwork = @Html.Raw(JsonSerializer.Serialize(Model.ShowFullNetwork));
|
||
const connectedToFocus = new Set();
|
||
if (focusCharacterId && fullNetwork) {
|
||
links.forEach(link => {
|
||
if (link.sourceCharacterID === focusCharacterId) connectedToFocus.add(link.targetCharacterID);
|
||
if (link.targetCharacterID === focusCharacterId) connectedToFocus.add(link.sourceCharacterID);
|
||
});
|
||
}
|
||
|
||
const elements = [
|
||
...nodes.map(node => ({
|
||
group: "nodes",
|
||
data: {
|
||
id: `c-${node.characterID}`,
|
||
characterID: node.characterID,
|
||
label: node.characterName,
|
||
importance: node.characterImportance || 0,
|
||
width: node.isFocus ? 172 : node.characterImportance >= 8 ? 158 : node.characterImportance >= 4 ? 144 : 128,
|
||
height: node.isFocus ? 58 : node.characterImportance >= 8 ? 52 : 46,
|
||
isFocus: node.isFocus,
|
||
isNeighbour: connectedToFocus.has(node.characterID)
|
||
},
|
||
classes: [
|
||
node.isFocus ? "is-focus" : "",
|
||
node.characterImportance >= 8 ? "is-major-character" : "",
|
||
node.characterImportance >= 4 && node.characterImportance < 8 ? "is-supporting-character" : "",
|
||
focusCharacterId && fullNetwork && !node.isFocus && !connectedToFocus.has(node.characterID) ? "is-dimmed" : ""
|
||
].filter(Boolean).join(" ")
|
||
})),
|
||
...links.map(link => ({
|
||
group: "edges",
|
||
data: {
|
||
id: `r-${link.characterRelationshipID}`,
|
||
relationshipID: link.characterRelationshipID,
|
||
source: `c-${link.sourceCharacterID}`,
|
||
target: `c-${link.targetCharacterID}`,
|
||
label: link.relationshipLabel,
|
||
category: link.categoryName,
|
||
intensity: link.intensity || 4,
|
||
tooltip: buildRelationshipTooltip(relationshipById.get(link.characterRelationshipID))
|
||
},
|
||
classes: [
|
||
link.cssClass,
|
||
focusCharacterId && fullNetwork && link.sourceCharacterID !== focusCharacterId && link.targetCharacterID !== focusCharacterId ? "is-dimmed" : ""
|
||
].filter(Boolean).join(" ")
|
||
}))
|
||
];
|
||
|
||
const cy = cytoscape({
|
||
container: graph,
|
||
elements,
|
||
minZoom: 0.05,
|
||
maxZoom: 2.5,
|
||
wheelSensitivity: 0.18,
|
||
style: relationshipMapStyle(),
|
||
layout: {
|
||
name: nodes.length <= 8 ? "circle" : "cose",
|
||
animate: false,
|
||
fit: true,
|
||
padding: 48,
|
||
nodeRepulsion: 9000,
|
||
idealEdgeLength: 170,
|
||
edgeElasticity: 80,
|
||
numIter: 1200,
|
||
randomize: false
|
||
}
|
||
});
|
||
|
||
cy.ready(() => {
|
||
cy.fit(undefined, 48);
|
||
const first = relationships[0];
|
||
if (first) selectRelationship(first.characterRelationshipID);
|
||
});
|
||
|
||
cy.on("tap", "edge", event => {
|
||
selectRelationship(event.target.data("relationshipID"));
|
||
});
|
||
|
||
cy.on("mouseover", "node, edge", event => {
|
||
const label = event.target.isNode() ? event.target.data("label") : event.target.data("tooltip");
|
||
graph.setAttribute("title", label || "");
|
||
});
|
||
|
||
document.querySelector("[data-map-fit]")?.addEventListener("click", () => {
|
||
cy.resize();
|
||
cy.fit(undefined, 48);
|
||
});
|
||
|
||
document.querySelector("[data-map-reset]")?.addEventListener("click", () => {
|
||
cy.layout({
|
||
name: nodes.length <= 8 ? "circle" : "cose",
|
||
animate: false,
|
||
fit: true,
|
||
padding: 48,
|
||
randomize: false
|
||
}).run();
|
||
});
|
||
|
||
window.PlotDirectorRelationshipWorkspace = {
|
||
cy, focusCharacterId, selectRelationship, refreshStyle: () => cy.style(relationshipMapStyle()), selectedRelationship: relationships[0] || null,
|
||
cast: @Html.Raw(JsonSerializer.Serialize(Model.Cast, jsonOptions)),
|
||
scenes: @Html.Raw(JsonSerializer.Serialize(Model.SceneOptions, jsonOptions)),
|
||
changeSceneIDs: @Html.Raw(JsonSerializer.Serialize(Model.ChangeSceneIDs)),
|
||
projectId: @Model.Project.ProjectID,
|
||
bookId: @Html.Raw(JsonSerializer.Serialize(Model.BookID)),
|
||
sceneId: @Html.Raw(JsonSerializer.Serialize(Model.SceneID ?? Model.TimelineSceneID)),
|
||
update(model) {
|
||
relationships = model.relationships;
|
||
relationshipById.clear();
|
||
relationships.forEach(item => relationshipById.set(item.characterRelationshipID, item));
|
||
cy.edges().remove();
|
||
model.links.forEach(link => {
|
||
for (const id of [link.sourceCharacterID, link.targetCharacterID]) {
|
||
if (!cy.$id(`c-${id}`).length) {
|
||
const node = model.cast.find(n => n.characterID === id);
|
||
cy.add({ data: { id: `c-${id}`, characterID: id, label: node.characterName, width: 144, height: 46 } });
|
||
}
|
||
}
|
||
cy.add({ data: { id: `r-${link.characterRelationshipID}`, relationshipID: link.characterRelationshipID,
|
||
source: `c-${link.sourceCharacterID}`, target: `c-${link.targetCharacterID}`, label: link.relationshipLabel,
|
||
intensity: link.intensity || 4, tooltip: buildRelationshipTooltip(relationshipById.get(link.characterRelationshipID)) }, classes: link.cssClass });
|
||
});
|
||
const list = document.querySelector('.relationship-map-list');
|
||
if (list) {
|
||
list.replaceChildren();
|
||
relationships.forEach(item => {
|
||
const button = document.createElement('button');
|
||
button.type = 'button'; button.className = 'relationship-map-list-item';
|
||
button.dataset.pdContext = 'relationship'; button.dataset.pdContextId = item.characterRelationshipID; button.dataset.pdContextLabel = 'Select relationship';
|
||
button.textContent = `${item.characterAName} – ${item.characterBName}: ${item.relationshipTypeName} / ${item.currentStateName}`;
|
||
button.addEventListener('click', () => selectRelationship(item.characterRelationshipID)); list.append(button);
|
||
});
|
||
}
|
||
},
|
||
relationships: () => relationships
|
||
};
|
||
window.addEventListener("resize", () => {
|
||
window.requestAnimationFrame(() => {
|
||
cy.resize();
|
||
});
|
||
});
|
||
|
||
document.querySelectorAll("[data-map-select]").forEach(button => {
|
||
button.addEventListener("click", () => selectRelationship(Number(button.dataset.mapSelect)));
|
||
});
|
||
|
||
function selectRelationship(id) {
|
||
const relationship = relationshipById.get(id);
|
||
if (!relationship) return;
|
||
if (window.PlotDirectorRelationshipWorkspace) window.PlotDirectorRelationshipWorkspace.selectedRelationship = relationship;
|
||
|
||
cy.elements().removeClass("is-selected");
|
||
cy.$id(`r-${id}`).addClass("is-selected");
|
||
document.querySelectorAll(".relationship-map-list-item").forEach(item => item.classList.remove("is-selected"));
|
||
document.querySelectorAll(`[data-map-select="${id}"]`).forEach(item => item.classList.add("is-selected"));
|
||
|
||
document.getElementById("relationshipMapEmptyDetail")?.classList.add("d-none");
|
||
document.getElementById("relationshipMapDetail")?.classList.remove("d-none");
|
||
document.querySelector("[data-map-detail-title]").textContent = `${relationship.characterAName} - ${relationship.characterBName}`;
|
||
renderDetailAvatar(null);
|
||
document.querySelector("[data-map-detail-category]").textContent = relationship.relationshipCategoryName;
|
||
document.querySelector("[data-map-detail-reciprocal]").textContent = relationship.isReciprocal ? "Reciprocal" : "Directional";
|
||
document.querySelector("[data-map-detail-type]").textContent = relationship.relationshipTypeName;
|
||
document.querySelector("[data-map-detail-initial]").textContent = formatState(relationship.initialStateName, relationship.initialIntensity);
|
||
document.querySelector("[data-map-detail-current]").textContent = formatState(relationship.currentStateName, relationship.currentIntensity);
|
||
|
||
const history = document.querySelector("[data-map-detail-history]");
|
||
history.innerHTML = "";
|
||
window.dispatchEvent(new CustomEvent('pd:relationship-selected', { detail: relationship }));
|
||
relationship.history.forEach(item => {
|
||
const li = document.createElement("li");
|
||
const notes = item.notes ? ` - ${item.notes}` : "";
|
||
li.textContent = `${item.label}: ${formatState(item.stateName, item.intensity)}${notes}`;
|
||
history.append(li);
|
||
});
|
||
}
|
||
|
||
function formatState(name, intensity) {
|
||
return intensity ? `${name} / Intensity ${intensity}` : name;
|
||
}
|
||
|
||
function renderDetailAvatar(character) {
|
||
const avatar = document.querySelector("[data-map-detail-avatar]");
|
||
if (!avatar) return;
|
||
|
||
avatar.innerHTML = "";
|
||
if (!character) {
|
||
avatar.classList.add("d-none");
|
||
return;
|
||
}
|
||
|
||
const imagePath = character.avatarThumbnailPath || character.avatarImagePath || character.thumbnailPath || character.imagePath;
|
||
if (imagePath) {
|
||
const image = document.createElement("img");
|
||
image.src = imagePath;
|
||
image.alt = "";
|
||
image.loading = "lazy";
|
||
avatar.append(image);
|
||
} else {
|
||
const fallback = document.createElement("span");
|
||
fallback.textContent = initials(character.characterName);
|
||
avatar.append(fallback);
|
||
}
|
||
|
||
avatar.title = character.characterName;
|
||
avatar.classList.remove("d-none");
|
||
}
|
||
|
||
function initials(value) {
|
||
const parts = (value || "")
|
||
.trim()
|
||
.split(/\s+/)
|
||
.filter(Boolean)
|
||
.slice(0, 2);
|
||
const text = parts.map(part => part[0]).join("").toUpperCase();
|
||
return text || "?";
|
||
}
|
||
|
||
function buildRelationshipTooltip(relationship) {
|
||
if (!relationship) return "";
|
||
return `${relationship.characterAName} - ${relationship.characterBName}: ${relationship.relationshipTypeName}; current ${formatState(relationship.currentStateName, relationship.currentIntensity)}`;
|
||
}
|
||
|
||
function relationshipMapStyle() {
|
||
const tokens = getComputedStyle(document.documentElement);
|
||
const token = name => tokens.getPropertyValue(name).trim();
|
||
const isDark = document.documentElement.dataset.theme === "dark";
|
||
const nodeFill = token('--pd-card');
|
||
const nodeText = token('--pd-text');
|
||
const focusFill = token('--pd-selected');
|
||
const majorFill = token('--pd-nested');
|
||
const supportingFill = token('--pd-surface');
|
||
const labelFill = token('--pd-text');
|
||
const labelBackground = token('--pd-canvas');
|
||
return [
|
||
{
|
||
selector: "node",
|
||
style: {
|
||
"label": "data(label)",
|
||
"shape": "round-rectangle",
|
||
"width": "data(width)",
|
||
"height": "data(height)",
|
||
"background-color": nodeFill,
|
||
"border-color": token("--pd-border"),
|
||
"border-width": 2,
|
||
"color": nodeText,
|
||
"font-size": 12,
|
||
"font-weight": 650,
|
||
"text-wrap": "wrap",
|
||
"text-max-width": 118,
|
||
"text-valign": "center",
|
||
"text-halign": "center",
|
||
"text-margin-y": 0,
|
||
"padding": "10px",
|
||
"shadow-blur": 10,
|
||
"shadow-color": isDark ? "rgba(0, 0, 0, 0.45)" : "rgba(31, 41, 55, 0.18)",
|
||
"shadow-opacity": 0.32,
|
||
"shadow-offset-y": 3,
|
||
"overlay-opacity": 0
|
||
}
|
||
},
|
||
{
|
||
selector: "node.is-supporting-character",
|
||
style: {
|
||
"background-color": supportingFill,
|
||
"border-color": token("--pd-border"),
|
||
"border-width": 2.5,
|
||
"font-weight": 700
|
||
}
|
||
},
|
||
{
|
||
selector: "node.is-major-character",
|
||
style: {
|
||
"background-color": majorFill,
|
||
"border-color": token("--pd-focus"),
|
||
"border-width": 3,
|
||
"font-size": 13,
|
||
"font-weight": 800,
|
||
"shadow-opacity": 0.42
|
||
}
|
||
},
|
||
{
|
||
selector: "node.is-focus",
|
||
style: {
|
||
"width": "data(width)",
|
||
"height": "data(height)",
|
||
"background-color": focusFill,
|
||
"border-color": token("--pd-focus"),
|
||
"border-width": 4,
|
||
"font-size": 13,
|
||
"font-weight": 850,
|
||
"shadow-opacity": 0.5
|
||
}
|
||
},
|
||
{
|
||
selector: "edge",
|
||
style: {
|
||
"label": "data(label)",
|
||
"curve-style": "bezier",
|
||
"width": "mapData(intensity, 1, 10, 2, 8)",
|
||
"line-color": "#7a8796",
|
||
"target-arrow-shape": "none",
|
||
"font-size": 11,
|
||
"font-weight": 700,
|
||
"color": labelFill,
|
||
"text-background-color": labelBackground,
|
||
"text-background-opacity": 0.88,
|
||
"text-background-padding": 3,
|
||
"text-rotation": "none",
|
||
"overlay-opacity": 0
|
||
}
|
||
},
|
||
{ selector: "edge.relationship-category-family", style: { "line-color": "#b86522", "color": labelFill } },
|
||
{ selector: "edge.relationship-category-friendship", style: { "line-color": "#2f9e5f", "color": labelFill } },
|
||
{ selector: "edge.relationship-category-romantic", style: { "line-color": "#d9467f", "color": labelFill } },
|
||
{ selector: "edge.relationship-category-hostile", style: { "line-color": "#dc3f3f", "color": labelFill } },
|
||
{ selector: "edge.relationship-category-professional", style: { "line-color": "#4f7fd8", "color": labelFill } },
|
||
{ selector: "edge.relationship-category-other", style: { "line-color": "#7a8796", "color": labelFill } },
|
||
{
|
||
selector: ".is-dimmed",
|
||
style: {
|
||
"opacity": 0.25
|
||
}
|
||
},
|
||
{
|
||
selector: "edge.is-selected",
|
||
style: {
|
||
"width": 9,
|
||
"z-index": 20,
|
||
"text-background-opacity": 1
|
||
}
|
||
},
|
||
{
|
||
selector: "node:selected",
|
||
style: {
|
||
"border-color": token("--pd-focus"),
|
||
"border-width": 4
|
||
}
|
||
}
|
||
];
|
||
}
|
||
})();
|
||
</script>
|
||
<script src="~/js/relationship-workspace.js" asp-append-version="true"></script>
|