384 lines
20 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

@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("tap", "node", event => {
const characterId = event.target.data("characterID");
const characterName = event.target.data("label");
const character = nodeById.get(characterId);
document.getElementById("relationshipMapEmptyDetail")?.classList.add("d-none");
document.getElementById("relationshipMapDetail")?.classList.remove("d-none");
document.querySelector("[data-map-detail-title]").textContent = characterName;
renderDetailAvatar(character);
document.querySelector("[data-map-detail-category]").textContent = "Character";
document.querySelector("[data-map-detail-reciprocal]").textContent = characterId === focusCharacterId ? "Focused" : "Node";
document.querySelector("[data-map-detail-type]").textContent = "Character";
document.querySelector("[data-map-detail-initial]").textContent = "Select a relationship line to see relationship state.";
document.querySelector("[data-map-detail-current]").textContent = "Use Character focus to centre this character.";
const history = document.querySelector("[data-map-detail-history]");
history.innerHTML = "";
const li = document.createElement("li");
li.innerHTML = `<a href="/Characters/Details/${characterId}">Open character details</a> or <a href="/RelationshipMap?projectId=@Model.Project.ProjectID&focusCharacterId=${characterId}">focus this character</a>.`;
history.append(li);
});
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, 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 = "";
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 isDark = document.documentElement.dataset.theme === "dark";
const nodeFill = isDark ? "#222126" : "#fffdf8";
const nodeText = isDark ? "#f7efe4" : "#1f2937";
const focusFill = isDark ? "#3b2a16" : "#fff3d8";
const majorFill = isDark ? "#332417" : "#fff0d5";
const supportingFill = isDark ? "#29252c" : "#fbf6ef";
const labelFill = isDark ? "#f7efe4" : "#2f3742";
const labelBackground = isDark ? "#17171c" : "#fffdf8";
return [
{
selector: "node",
style: {
"label": "data(label)",
"shape": "round-rectangle",
"width": "data(width)",
"height": "data(height)",
"background-color": nodeFill,
"border-color": "#6b7280",
"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": isDark ? "#b9a891" : "#b99970",
"border-width": 2.5,
"font-weight": 700
}
},
{
selector: "node.is-major-character",
style: {
"background-color": majorFill,
"border-color": "#c47c2c",
"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": "#c47c2c",
"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": "#7c3f12" } },
{ selector: "edge.relationship-category-friendship", style: { "line-color": "#2f9e5f", "color": "#14532d" } },
{ selector: "edge.relationship-category-romantic", style: { "line-color": "#d9467f", "color": "#831843" } },
{ selector: "edge.relationship-category-hostile", style: { "line-color": "#dc3f3f", "color": "#7f1d1d" } },
{ selector: "edge.relationship-category-professional", style: { "line-color": "#4f7fd8", "color": "#1e3a8a" } },
{ selector: "edge.relationship-category-other", style: { "line-color": "#7a8796", "color": "#374151" } },
{
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": "#c47c2c",
"border-width": 4
}
}
];
}
})();
</script>
<script src="~/js/relationship-workspace.js" asp-append-version="true"></script>