Files
wekan/models/lib/boardMap.js
T
Lauri Ojansivu 3d891ace09 Add a Map board view: cards as markers on an uploaded image.
A board admin uploads an image - a floor plan, a site map, a drawing - and
the board's cards become markers on it, coloured by their first label and
numbered like the card. Members who can edit place a card by dragging it
from the "Not on the map" list, or by choosing it and clicking where it
belongs, and move a marker by dragging it; a click opens the card. A place
is stored as percentages of the image (card.mapX, card.mapY, 0-100), so a
marker stays on its spot at any size; the image is a board attachment
served under the board's own access rules. Read-only members see the map
but cannot change it. New strings are English, pending Transifex.

Fixes #3256,

Thanks to javen9881 and xet7 !
2026-09-29 17:43:58 +03:00

51 lines
1.9 KiB
JavaScript

'use strict';
// #3256 (maintainer decision 2026-09-29): the Map board view - an uploaded
// image (a floor plan, a site map, a machine drawing) with the board's cards
// as markers on it. A card's place is stored as percentages of the image's
// width and height (card.mapX, card.mapY, 0-100), so a marker stays on the same
// spot at any screen size.
const round = value => Math.round(value * 100) / 100;
// A coordinate from 0 to 100, or null.
// Only a number or a numeric string: Number(null) is 0, and a card whose mapX
// is null must not count as placed in the top-left corner.
function clampPercent(value) {
let n = NaN;
if (typeof value === 'number') n = value;
else if (typeof value === 'string' && value.trim() !== '') n = Number(value);
if (!Number.isFinite(n)) return null;
return round(Math.min(100, Math.max(0, n)));
}
// Where a pointer at (clientX, clientY) falls on an image drawn in `rect`.
function percentFromPoint({ clientX, clientY }, rect) {
if (!rect || !(rect.width > 0) || !(rect.height > 0)) return null;
return {
x: clampPercent(((clientX - rect.left) / rect.width) * 100),
y: clampPercent(((clientY - rect.top) / rect.height) * 100),
};
}
function isPlaced(card) {
return !!card && clampPercent(card.mapX) !== null && clampPercent(card.mapY) !== null;
}
// The marker for a placed card: its place, its first label's colour, and a
// short text - the card number when the board numbers cards.
function mapMarker(card, board) {
if (!isPlaced(card)) return null;
const labels = (board && board.labels) || [];
const first = (card.labelIds || []).map(id => labels.find(label => label._id === id)).find(Boolean);
return {
x: clampPercent(card.mapX),
y: clampPercent(card.mapY),
color: first ? first.color : null,
text: card.cardNumber ? String(card.cardNumber) : '',
title: String(card.title || ''),
};
}
module.exports = { clampPercent, percentFromPoint, isPlaced, mapMarker };