This commit is contained in:
@@ -2,13 +2,17 @@ const state = {
|
||||
bootstrap: null,
|
||||
rackTemplateId: null,
|
||||
projectName: "Neues Rack-Projekt",
|
||||
rackColor: "#50545c",
|
||||
components: [],
|
||||
placedItems: [],
|
||||
nextPlacementId: 1,
|
||||
dragPlacementId: null,
|
||||
pointerDrag: null,
|
||||
selectedComponentId: null,
|
||||
};
|
||||
|
||||
const ui = {};
|
||||
const RACK_UNIT_PX = 38;
|
||||
|
||||
document.addEventListener("DOMContentLoaded", async () => {
|
||||
cacheDom();
|
||||
@@ -19,6 +23,8 @@ document.addEventListener("DOMContentLoaded", async () => {
|
||||
function cacheDom() {
|
||||
ui.templateSelect = document.getElementById("rack-template-select");
|
||||
ui.projectName = document.getElementById("project-name");
|
||||
ui.rackColor = document.getElementById("rack-color");
|
||||
ui.selectedComponentInfo = document.getElementById("selected-component-info");
|
||||
ui.componentFilter = document.getElementById("component-filter");
|
||||
ui.pluginInput = document.getElementById("plugin-input");
|
||||
ui.componentLibrary = document.getElementById("component-library");
|
||||
@@ -45,6 +51,10 @@ function bindEvents() {
|
||||
state.projectName = ui.projectName.value.trim() || "Neues Rack-Projekt";
|
||||
renderSummary();
|
||||
});
|
||||
ui.rackColor.addEventListener("input", () => {
|
||||
state.rackColor = ui.rackColor.value;
|
||||
renderRack();
|
||||
});
|
||||
|
||||
ui.componentFilter.addEventListener("input", renderLibrary);
|
||||
ui.cableSlack.addEventListener("input", renderCableEstimate);
|
||||
@@ -52,6 +62,8 @@ function bindEvents() {
|
||||
ui.cableTo.addEventListener("change", renderCableEstimate);
|
||||
ui.pluginInput.addEventListener("change", importPluginPack);
|
||||
ui.exportBom.addEventListener("click", copyBomCsv);
|
||||
document.addEventListener("pointermove", handlePointerMove);
|
||||
document.addEventListener("pointerup", handlePointerUp);
|
||||
}
|
||||
|
||||
async function loadBootstrap() {
|
||||
@@ -76,6 +88,7 @@ async function loadBootstrap() {
|
||||
function renderAll() {
|
||||
renderSummary();
|
||||
renderLibrary();
|
||||
renderSelectionInfo();
|
||||
renderRack();
|
||||
renderStats();
|
||||
renderBom();
|
||||
@@ -155,16 +168,19 @@ function renderLibrary() {
|
||||
<span>${component.depthMm} mm tief</span>
|
||||
<span>${formatCurrency(component.priceNet, component.currency)}</span>
|
||||
</div>
|
||||
<button type="button" data-action="add-component" data-component-id="${escapeHtml(component.id)}">
|
||||
In Rack einfuegen
|
||||
<button type="button" data-action="select-component" data-component-id="${escapeHtml(component.id)}">
|
||||
${state.selectedComponentId === component.id ? "Ausgewaehlt" : "Zum Einstecken waehlen"}
|
||||
</button>
|
||||
</article>
|
||||
`
|
||||
)
|
||||
.join("");
|
||||
|
||||
ui.componentLibrary.querySelectorAll("[data-action='add-component']").forEach((button) => {
|
||||
button.addEventListener("click", () => addComponentToRack(button.dataset.componentId));
|
||||
ui.componentLibrary.querySelectorAll("[data-action='select-component']").forEach((button) => {
|
||||
button.addEventListener("click", () => {
|
||||
state.selectedComponentId = button.dataset.componentId;
|
||||
renderAll();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -184,49 +200,62 @@ function addComponentToRack(componentId) {
|
||||
state.placedItems.push({
|
||||
placementId: `p${state.nextPlacementId++}`,
|
||||
componentId: component.id,
|
||||
startU: position,
|
||||
y: position,
|
||||
x: getDefaultComponentX(component),
|
||||
});
|
||||
|
||||
renderAll();
|
||||
}
|
||||
|
||||
function renderSelectionInfo() {
|
||||
const component = getComponent(state.selectedComponentId);
|
||||
if (!component) {
|
||||
ui.selectedComponentInfo.textContent = "Kein Element ausgewaehlt";
|
||||
ui.selectedComponentInfo.classList.remove("is-active");
|
||||
return;
|
||||
}
|
||||
|
||||
ui.selectedComponentInfo.textContent = `${component.name} · ${component.heightU}U · freie HE-Zone anklicken`;
|
||||
ui.selectedComponentInfo.classList.add("is-active");
|
||||
}
|
||||
|
||||
function renderRack() {
|
||||
const rack = getCurrentRackTemplate();
|
||||
if (!rack) {
|
||||
return;
|
||||
}
|
||||
|
||||
ui.rackGrid.style.setProperty("--rack-unit-height", "38px");
|
||||
ui.rackGrid.style.setProperty("--rack-unit-height", `${RACK_UNIT_PX}px`);
|
||||
applyRackColorTheme(ui.rackGrid, state.rackColor);
|
||||
|
||||
const slots = [];
|
||||
for (let u = rack.totalU; u >= 1; u -= 1) {
|
||||
slots.push(`
|
||||
<div class="rack-slot" data-slot-u="${u}">
|
||||
<span class="rack-slot__label">${u}U</span>
|
||||
<span class="rack-slot__label rack-slot__label--right">${u}</span>
|
||||
</div>
|
||||
`);
|
||||
}
|
||||
|
||||
ui.rackGrid.innerHTML = `${slots.join("")}<div class="rack-items-layer" id="rack-items-layer"></div>`;
|
||||
const insertionZones = renderInsertionZones(rack);
|
||||
|
||||
ui.rackGrid.querySelectorAll(".rack-slot").forEach((slot) => {
|
||||
slot.addEventListener("dragover", (event) => {
|
||||
event.preventDefault();
|
||||
slot.classList.add("is-drop-target");
|
||||
});
|
||||
slot.addEventListener("dragleave", () => slot.classList.remove("is-drop-target"));
|
||||
slot.addEventListener("drop", (event) => {
|
||||
event.preventDefault();
|
||||
slot.classList.remove("is-drop-target");
|
||||
if (!state.dragPlacementId) {
|
||||
return;
|
||||
}
|
||||
movePlacedItem(state.dragPlacementId, Number(slot.dataset.slotU));
|
||||
});
|
||||
ui.rackGrid.innerHTML = `
|
||||
<div class="rack-grid__header-badge rack-grid__header-badge--left">${rack.rackStandard === "19_inch" ? '19" Rack' : '10" Rack'}</div>
|
||||
<div class="rack-grid__header-badge rack-grid__header-badge--right">${rack.totalU} HE</div>
|
||||
<div class="rack-grid__bay">
|
||||
<div class="rack-grid__guides">${slots.join("")}</div>
|
||||
<div class="rack-insertion-layer">${insertionZones}</div>
|
||||
<div class="rack-items-layer" id="rack-items-layer"></div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
ui.rackGrid.querySelectorAll("[data-action='insert-component']").forEach((element) => {
|
||||
element.addEventListener("click", () => insertSelectedComponentAt(Number(element.dataset.insertY)));
|
||||
});
|
||||
|
||||
const layer = document.getElementById("rack-items-layer");
|
||||
const rackHeight = rack.totalU * 38;
|
||||
const rackHeight = rack.totalU * RACK_UNIT_PX;
|
||||
layer.style.height = `${rackHeight}px`;
|
||||
|
||||
state.placedItems.forEach((item) => {
|
||||
@@ -235,14 +264,14 @@ function renderRack() {
|
||||
return;
|
||||
}
|
||||
|
||||
const top = (rack.totalU - (item.startU + component.heightU) + 1) * 38;
|
||||
const height = component.heightU * 38 - 2;
|
||||
const rect = getPlacementRect(item, component);
|
||||
const element = document.createElement("article");
|
||||
element.className = `rack-item rack-item--${component.category.replace(/_/g, "-")}`;
|
||||
element.dataset.standard = component.rackStandard;
|
||||
element.draggable = true;
|
||||
element.style.top = `${top}px`;
|
||||
element.style.height = `${height}px`;
|
||||
element.style.top = `${rect.top}px`;
|
||||
element.style.left = `${rect.left}px`;
|
||||
element.style.width = `${rect.width}px`;
|
||||
element.style.height = `${rect.height}px`;
|
||||
element.innerHTML = `
|
||||
<div class="rack-item__face rack-item__face--${escapeHtml(component.category)}">
|
||||
${renderComponentFace(component, "rack")}
|
||||
@@ -253,9 +282,11 @@ function renderRack() {
|
||||
<strong>${escapeHtml(component.name)}</strong>
|
||||
<div class="rack-item__meta">${component.heightU}U · ${component.depthMm} mm · ${formatCurrency(component.priceNet, component.currency)}</div>
|
||||
</div>
|
||||
<span class="chip">${item.startU}U</span>
|
||||
<span class="chip">${formatRackPosition(item.y)}</span>
|
||||
</div>
|
||||
<div class="rack-item__actions">
|
||||
<button type="button" data-action="move-left">←</button>
|
||||
<button type="button" data-action="move-right">→</button>
|
||||
<button type="button" data-action="move-up">+1U</button>
|
||||
<button type="button" data-action="move-down">-1U</button>
|
||||
<button type="button" data-action="remove">Entfernen</button>
|
||||
@@ -263,16 +294,11 @@ function renderRack() {
|
||||
</div>
|
||||
`;
|
||||
|
||||
element.addEventListener("dragstart", () => {
|
||||
state.dragPlacementId = item.placementId;
|
||||
element.classList.add("is-dragging");
|
||||
});
|
||||
element.addEventListener("dragend", () => {
|
||||
state.dragPlacementId = null;
|
||||
element.classList.remove("is-dragging");
|
||||
});
|
||||
element.querySelector("[data-action='move-up']").addEventListener("click", () => movePlacedItem(item.placementId, item.startU + 1));
|
||||
element.querySelector("[data-action='move-down']").addEventListener("click", () => movePlacedItem(item.placementId, item.startU - 1));
|
||||
element.addEventListener("pointerdown", (event) => beginPointerDrag(event, item, component));
|
||||
element.querySelector("[data-action='move-left']").addEventListener("click", () => nudgePlacedItem(item.placementId, -2, 0));
|
||||
element.querySelector("[data-action='move-right']").addEventListener("click", () => nudgePlacedItem(item.placementId, 2, 0));
|
||||
element.querySelector("[data-action='move-up']").addEventListener("click", () => nudgePlacedItem(item.placementId, 0, -RACK_UNIT_PX));
|
||||
element.querySelector("[data-action='move-down']").addEventListener("click", () => nudgePlacedItem(item.placementId, 0, RACK_UNIT_PX));
|
||||
element.querySelector("[data-action='remove']").addEventListener("click", () => {
|
||||
state.placedItems = state.placedItems.filter((entry) => entry.placementId !== item.placementId);
|
||||
renderAll();
|
||||
@@ -282,7 +308,7 @@ function renderRack() {
|
||||
});
|
||||
}
|
||||
|
||||
function movePlacedItem(placementId, requestedStartU) {
|
||||
function nudgePlacedItem(placementId, deltaX, deltaY) {
|
||||
const rack = getCurrentRackTemplate();
|
||||
const item = state.placedItems.find((entry) => entry.placementId === placementId);
|
||||
const component = item ? getComponent(item.componentId) : null;
|
||||
@@ -290,23 +316,56 @@ function movePlacedItem(placementId, requestedStartU) {
|
||||
return;
|
||||
}
|
||||
|
||||
const normalizedStart = Math.max(1, Math.min(requestedStartU, rack.totalU - component.heightU + 1));
|
||||
const occupied = state.placedItems.some((entry) => {
|
||||
if (entry.placementId === placementId) {
|
||||
return false;
|
||||
}
|
||||
const other = getComponent(entry.componentId);
|
||||
if (!other) {
|
||||
return false;
|
||||
}
|
||||
return rangesOverlap(normalizedStart, component.heightU, entry.startU, other.heightU);
|
||||
});
|
||||
item.x = clamp(item.x + deltaX, 0, 100 - getComponentWidthPercent(component));
|
||||
item.y = clamp(item.y + deltaY, 0, getRackHeightPx(rack) - getComponentHeightPx(component));
|
||||
renderAll();
|
||||
}
|
||||
|
||||
if (occupied) {
|
||||
function insertSelectedComponentAt(y) {
|
||||
if (!state.selectedComponentId) {
|
||||
return;
|
||||
}
|
||||
|
||||
item.startU = normalizedStart;
|
||||
const component = getComponent(state.selectedComponentId);
|
||||
const rack = getCurrentRackTemplate();
|
||||
if (!component || !rack) {
|
||||
return;
|
||||
}
|
||||
|
||||
const nextRect = {
|
||||
left: getDefaultComponentX(component),
|
||||
top: y,
|
||||
width: getComponentWidthPercent(component),
|
||||
height: getComponentHeightPx(component),
|
||||
};
|
||||
const hasOverlap = state.placedItems.some((item) => {
|
||||
const other = getComponent(item.componentId);
|
||||
if (!other) {
|
||||
return false;
|
||||
}
|
||||
return rectanglesOverlap(
|
||||
nextRect,
|
||||
{
|
||||
left: item.x,
|
||||
top: item.y,
|
||||
width: getComponentWidthPercent(other),
|
||||
height: getComponentHeightPx(other),
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
if (hasOverlap) {
|
||||
alert("Diese Einbauposition ist bereits belegt.");
|
||||
return;
|
||||
}
|
||||
|
||||
state.placedItems.push({
|
||||
placementId: `p${state.nextPlacementId++}`,
|
||||
componentId: component.id,
|
||||
y,
|
||||
x: getDefaultComponentX(component),
|
||||
});
|
||||
state.selectedComponentId = null;
|
||||
renderAll();
|
||||
}
|
||||
|
||||
@@ -405,7 +464,7 @@ function renderCableSelectors() {
|
||||
if (!component) {
|
||||
return "";
|
||||
}
|
||||
return `<option value="${escapeHtml(item.placementId)}">${escapeHtml(component.name)} @ ${item.startU}U</option>`;
|
||||
return `<option value="${escapeHtml(item.placementId)}">${escapeHtml(component.name)} @ ${formatRackPosition(item.y)}</option>`;
|
||||
})
|
||||
)
|
||||
.join("");
|
||||
@@ -435,7 +494,7 @@ function renderCableEstimate() {
|
||||
return;
|
||||
}
|
||||
|
||||
const verticalMm = Math.abs(from.startU - to.startU) * 44.45;
|
||||
const verticalMm = Math.abs(getPlacementCenterY(from, fromComponent) - getPlacementCenterY(to, toComponent)) * (44.45 / RACK_UNIT_PX);
|
||||
const depthAllowance = Math.min(rack.usableDepthMm * 0.35, 280);
|
||||
const sideAllowance = estimateSideAllowance(fromComponent, toComponent);
|
||||
const rawLength = verticalMm + depthAllowance + sideAllowance;
|
||||
@@ -481,6 +540,8 @@ function renderValidation() {
|
||||
.map((item) => ({ item, component: getComponent(item.componentId) }))
|
||||
.filter(({ component }) => component && component.depthMm > rack.usableDepthMm);
|
||||
|
||||
const overlaps = getPlacementOverlaps();
|
||||
|
||||
if (tooDeep.length === 0) {
|
||||
notes.push({ className: "ok", text: "Keine Tiefenkonflikte erkannt." });
|
||||
} else {
|
||||
@@ -489,6 +550,14 @@ function renderValidation() {
|
||||
});
|
||||
}
|
||||
|
||||
if (overlaps.length === 0) {
|
||||
notes.push({ className: "ok", text: "Keine visuellen Ueberschneidungen im Rack erkannt." });
|
||||
} else {
|
||||
overlaps.forEach((entry) => {
|
||||
notes.push({ className: "warn", text: `${entry.a} ueberlappt mit ${entry.b}.` });
|
||||
});
|
||||
}
|
||||
|
||||
ui.validationOutput.innerHTML = notes.map((note) => `<li class="${note.className}">${escapeHtml(note.text)}</li>`).join("");
|
||||
}
|
||||
|
||||
@@ -587,25 +656,36 @@ function findFirstFreePosition(heightU) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (let start = 1; start <= rack.totalU - heightU + 1; start += 1) {
|
||||
const testHeight = heightU * RACK_UNIT_PX;
|
||||
for (let y = 0; y <= getRackHeightPx(rack) - testHeight; y += RACK_UNIT_PX) {
|
||||
const blocked = state.placedItems.some((item) => {
|
||||
const component = getComponent(item.componentId);
|
||||
return component && rangesOverlap(start, heightU, item.startU, component.heightU);
|
||||
const otherComponent = getComponent(item.componentId);
|
||||
if (!otherComponent) {
|
||||
return false;
|
||||
}
|
||||
return rectanglesOverlap(
|
||||
{
|
||||
left: getDefaultComponentX(component),
|
||||
top: y,
|
||||
width: getComponentWidthPercent(component),
|
||||
height: testHeight,
|
||||
},
|
||||
{
|
||||
left: item.x,
|
||||
top: item.y,
|
||||
width: getComponentWidthPercent(otherComponent),
|
||||
height: getComponentHeightPx(otherComponent),
|
||||
}
|
||||
);
|
||||
});
|
||||
if (!blocked) {
|
||||
return start;
|
||||
return y;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function rangesOverlap(startA, heightA, startB, heightB) {
|
||||
const endA = startA + heightA - 1;
|
||||
const endB = startB + heightB - 1;
|
||||
return startA <= endB && startB <= endA;
|
||||
}
|
||||
|
||||
function getCurrentRackTemplate() {
|
||||
return state.bootstrap?.rackTemplates.find((template) => template.id === state.rackTemplateId) ?? null;
|
||||
}
|
||||
@@ -634,6 +714,60 @@ function recommendCableLength(lengthMm) {
|
||||
return options.find((option) => option >= meters) ?? Math.ceil(meters);
|
||||
}
|
||||
|
||||
function beginPointerDrag(event, item, component) {
|
||||
if (event.target.closest("button")) {
|
||||
return;
|
||||
}
|
||||
|
||||
const rect = event.currentTarget.getBoundingClientRect();
|
||||
state.pointerDrag = {
|
||||
placementId: item.placementId,
|
||||
offsetX: event.clientX - rect.left,
|
||||
offsetY: event.clientY - rect.top,
|
||||
widthPercent: getComponentWidthPercent(component),
|
||||
heightPx: getComponentHeightPx(component),
|
||||
};
|
||||
event.currentTarget.setPointerCapture?.(event.pointerId);
|
||||
event.currentTarget.classList.add("is-dragging");
|
||||
}
|
||||
|
||||
function handlePointerMove(event) {
|
||||
if (!state.pointerDrag) {
|
||||
return;
|
||||
}
|
||||
|
||||
const layer = document.getElementById("rack-items-layer");
|
||||
const rack = getCurrentRackTemplate();
|
||||
if (!layer || !rack) {
|
||||
return;
|
||||
}
|
||||
|
||||
const item = state.placedItems.find((entry) => entry.placementId === state.pointerDrag.placementId);
|
||||
if (!item) {
|
||||
return;
|
||||
}
|
||||
|
||||
const layerRect = layer.getBoundingClientRect();
|
||||
const widthPx = (state.pointerDrag.widthPercent / 100) * layerRect.width;
|
||||
const xPx = clamp(event.clientX - layerRect.left - state.pointerDrag.offsetX, 0, layerRect.width - widthPx);
|
||||
const yPx = clamp(event.clientY - layerRect.top - state.pointerDrag.offsetY, 0, getRackHeightPx(rack) - state.pointerDrag.heightPx);
|
||||
|
||||
item.x = (xPx / layerRect.width) * 100;
|
||||
item.y = yPx;
|
||||
renderAll();
|
||||
}
|
||||
|
||||
function handlePointerUp() {
|
||||
if (!state.pointerDrag) {
|
||||
return;
|
||||
}
|
||||
|
||||
state.pointerDrag = null;
|
||||
ui.rackGrid.querySelectorAll(".rack-item.is-dragging").forEach((element) => {
|
||||
element.classList.remove("is-dragging");
|
||||
});
|
||||
}
|
||||
|
||||
function formatCurrency(value, currency) {
|
||||
return new Intl.NumberFormat("de-DE", {
|
||||
style: "currency",
|
||||
@@ -647,11 +781,15 @@ function renderComponentFace(component, mode) {
|
||||
const leds = '<div class="device-leds"><span></span><span></span><span></span></div>';
|
||||
const label = `<div class="device-silkscreen">${escapeHtml(component.manufacturer || component.category)}</div>`;
|
||||
const modeClass = mode === "rack" ? "device-face--rack" : "device-face--library";
|
||||
const header = `<div class="device-brand">${escapeHtml(component.name)}</div>`;
|
||||
|
||||
switch (component.category) {
|
||||
case "switch":
|
||||
return `
|
||||
<div class="device-face device-face--switch ${modeClass}">
|
||||
<div class="device-mount-hole device-mount-hole--left"></div>
|
||||
<div class="device-mount-hole device-mount-hole--right"></div>
|
||||
${header}
|
||||
${label}
|
||||
${leds}
|
||||
${ports}
|
||||
@@ -661,6 +799,9 @@ function renderComponentFace(component, mode) {
|
||||
case "patch_panel":
|
||||
return `
|
||||
<div class="device-face device-face--patch-panel ${modeClass}">
|
||||
<div class="device-mount-hole device-mount-hole--left"></div>
|
||||
<div class="device-mount-hole device-mount-hole--right"></div>
|
||||
${header}
|
||||
${label}
|
||||
${renderPatchPanel(component)}
|
||||
</div>
|
||||
@@ -668,6 +809,9 @@ function renderComponentFace(component, mode) {
|
||||
case "pdu":
|
||||
return `
|
||||
<div class="device-face device-face--pdu ${modeClass}">
|
||||
<div class="device-mount-hole device-mount-hole--left"></div>
|
||||
<div class="device-mount-hole device-mount-hole--right"></div>
|
||||
${header}
|
||||
${label}
|
||||
<div class="device-sockets">${renderSocketStrip(component)}</div>
|
||||
${leds}
|
||||
@@ -676,14 +820,21 @@ function renderComponentFace(component, mode) {
|
||||
case "ups":
|
||||
return `
|
||||
<div class="device-face device-face--ups ${modeClass}">
|
||||
<div class="device-mount-hole device-mount-hole--left"></div>
|
||||
<div class="device-mount-hole device-mount-hole--right"></div>
|
||||
${header}
|
||||
<div class="device-display"></div>
|
||||
<div class="device-vents"></div>
|
||||
<div class="device-handles"><span></span><span></span></div>
|
||||
${label}
|
||||
</div>
|
||||
`;
|
||||
case "shelf":
|
||||
return `
|
||||
<div class="device-face device-face--shelf ${modeClass}">
|
||||
<div class="device-mount-hole device-mount-hole--left"></div>
|
||||
<div class="device-mount-hole device-mount-hole--right"></div>
|
||||
${header}
|
||||
<div class="device-shelf-top"></div>
|
||||
${label}
|
||||
</div>
|
||||
@@ -691,12 +842,18 @@ function renderComponentFace(component, mode) {
|
||||
case "blank_panel":
|
||||
return `
|
||||
<div class="device-face device-face--blank ${modeClass}">
|
||||
<div class="device-mount-hole device-mount-hole--left"></div>
|
||||
<div class="device-mount-hole device-mount-hole--right"></div>
|
||||
${header}
|
||||
${label}
|
||||
</div>
|
||||
`;
|
||||
default:
|
||||
return `
|
||||
<div class="device-face device-face--generic ${modeClass}">
|
||||
<div class="device-mount-hole device-mount-hole--left"></div>
|
||||
<div class="device-mount-hole device-mount-hole--right"></div>
|
||||
${header}
|
||||
${label}
|
||||
</div>
|
||||
`;
|
||||
@@ -720,6 +877,158 @@ function renderSocketStrip(component) {
|
||||
return Array.from({ length: totalSockets }, () => "<span></span>").join("");
|
||||
}
|
||||
|
||||
function renderInsertionZones(rack) {
|
||||
const component = getComponent(state.selectedComponentId);
|
||||
if (!component) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const zones = [];
|
||||
for (let y = 0; y <= getRackHeightPx(rack) - getComponentHeightPx(component); y += RACK_UNIT_PX) {
|
||||
const occupied = state.placedItems.some((item) => {
|
||||
const other = getComponent(item.componentId);
|
||||
if (!other) {
|
||||
return false;
|
||||
}
|
||||
return rectanglesOverlap(
|
||||
{
|
||||
left: getDefaultComponentX(component),
|
||||
top: y,
|
||||
width: getComponentWidthPercent(component),
|
||||
height: getComponentHeightPx(component),
|
||||
},
|
||||
{
|
||||
left: item.x,
|
||||
top: item.y,
|
||||
width: getComponentWidthPercent(other),
|
||||
height: getComponentHeightPx(other),
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
zones.push(`
|
||||
<button
|
||||
type="button"
|
||||
class="rack-insert-zone${occupied ? " is-disabled" : ""}"
|
||||
data-action="insert-component"
|
||||
data-insert-y="${y}"
|
||||
style="top:${y}px;height:${getComponentHeightPx(component)}px;left:${getDefaultComponentX(component)}%;width:${getComponentWidthPercent(component)}%;"
|
||||
${occupied ? "disabled" : ""}
|
||||
>
|
||||
<span class="rack-insert-zone__ear rack-insert-zone__ear--left"></span>
|
||||
<span class="rack-insert-zone__body">
|
||||
<span class="rack-insert-zone__icon">☞</span>
|
||||
<span>Element hier einstecken</span>
|
||||
</span>
|
||||
<span class="rack-insert-zone__ear rack-insert-zone__ear--right"></span>
|
||||
</button>
|
||||
`);
|
||||
}
|
||||
|
||||
return zones.join("");
|
||||
}
|
||||
|
||||
function getRackHeightPx(rack) {
|
||||
return rack.totalU * RACK_UNIT_PX;
|
||||
}
|
||||
|
||||
function getComponentHeightPx(component) {
|
||||
return component.heightU * RACK_UNIT_PX;
|
||||
}
|
||||
|
||||
function getComponentWidthPercent(component) {
|
||||
return component.rackStandard === "10_inch" ? 58 : 86;
|
||||
}
|
||||
|
||||
function getDefaultComponentX(component) {
|
||||
return (100 - getComponentWidthPercent(component)) / 2;
|
||||
}
|
||||
|
||||
function getPlacementRect(item, component) {
|
||||
const rackInnerWidthPx = getRackInnerWidthPx();
|
||||
const width = (getComponentWidthPercent(component) / 100) * rackInnerWidthPx;
|
||||
const left = (item.x / 100) * rackInnerWidthPx;
|
||||
return {
|
||||
left,
|
||||
top: item.y,
|
||||
width,
|
||||
height: getComponentHeightPx(component),
|
||||
};
|
||||
}
|
||||
|
||||
function getPlacementCenterY(item, component) {
|
||||
return item.y + getComponentHeightPx(component) / 2;
|
||||
}
|
||||
|
||||
function formatRackPosition(y) {
|
||||
return `${(y / RACK_UNIT_PX + 1).toFixed(1)}U`;
|
||||
}
|
||||
|
||||
function getPlacementOverlaps() {
|
||||
const overlaps = [];
|
||||
for (let index = 0; index < state.placedItems.length; index += 1) {
|
||||
for (let compareIndex = index + 1; compareIndex < state.placedItems.length; compareIndex += 1) {
|
||||
const aItem = state.placedItems[index];
|
||||
const bItem = state.placedItems[compareIndex];
|
||||
const aComponent = getComponent(aItem.componentId);
|
||||
const bComponent = getComponent(bItem.componentId);
|
||||
if (!aComponent || !bComponent) {
|
||||
continue;
|
||||
}
|
||||
if (rectanglesOverlap(getPlacementRect(aItem, aComponent), getPlacementRect(bItem, bComponent))) {
|
||||
overlaps.push({ a: aComponent.name, b: bComponent.name });
|
||||
}
|
||||
}
|
||||
}
|
||||
return overlaps;
|
||||
}
|
||||
|
||||
function rectanglesOverlap(a, b) {
|
||||
return a.left < b.left + b.width && a.left + a.width > b.left && a.top < b.top + b.height && a.top + a.height > b.top;
|
||||
}
|
||||
|
||||
function getRackInnerWidthPx() {
|
||||
const layer = document.getElementById("rack-items-layer");
|
||||
if (layer) {
|
||||
return layer.clientWidth || 620;
|
||||
}
|
||||
return 620;
|
||||
}
|
||||
|
||||
function clamp(value, min, max) {
|
||||
return Math.max(min, Math.min(max, value));
|
||||
}
|
||||
|
||||
function applyRackColorTheme(element, color) {
|
||||
const rgb = hexToRgb(color);
|
||||
if (!rgb) {
|
||||
return;
|
||||
}
|
||||
|
||||
element.style.setProperty("--rack-frame", color);
|
||||
element.style.setProperty("--rack-frame-dark", shadeRgb(rgb, -34));
|
||||
element.style.setProperty("--rack-frame-light", shadeRgb(rgb, 28));
|
||||
element.style.setProperty("--rack-rail", shadeRgb(rgb, 18));
|
||||
}
|
||||
|
||||
function hexToRgb(hex) {
|
||||
const normalized = String(hex).replace("#", "");
|
||||
if (!/^[a-f0-9]{6}$/i.test(normalized)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
r: Number.parseInt(normalized.slice(0, 2), 16),
|
||||
g: Number.parseInt(normalized.slice(2, 4), 16),
|
||||
b: Number.parseInt(normalized.slice(4, 6), 16),
|
||||
};
|
||||
}
|
||||
|
||||
function shadeRgb(rgb, amount) {
|
||||
const channel = (value) => Math.max(0, Math.min(255, value + amount));
|
||||
return `rgb(${channel(rgb.r)}, ${channel(rgb.g)}, ${channel(rgb.b)})`;
|
||||
}
|
||||
|
||||
function escapeHtml(value) {
|
||||
return String(value)
|
||||
.replaceAll("&", "&")
|
||||
|
||||
Reference in New Issue
Block a user