Files
wekan/models/lib/columnSets.js
T
Lauri Ojansivu 03d0144538 Column sets: the same cards in several named sets of columns.
A new board view, Column sets. A board keeps named column sets - say
Completion, Priority, Milestone - each with its own coloured columns, and
every card is in one column of each set, or "Not placed". Picking a set
shows its columns; dragging a card to another column changes only where
it is in that set - its list, swimlane, labels and members stay - and
each card shows, as coloured labels, its column in the other sets, as the
issue asked. Board admins make and delete sets and columns; moving cards
needs the right to edit a card. Clicking a title opens the card.

Registered like every view: the Board View menu (appended after Version
Control), the default order and VIEWS_ADDED_LATER, the user's boardView
values and the API description, and an instance feature group so the
Admin Panel can turn it off. models/lib/columnSets.js holds the logic;
tests/columnSets.test.cjs covers places, removed columns, the other
sets' badges, foreign columns, dotted keys and the bounds, and
boardViewMenu / boardViewSettings now expect the view last. A Meteor run
started with the new fields. Documented in
docs/Features/Board/Column-Sets.md. The new strings are English
placeholders until the next translation pass.

Thanks to kentonv and xet7 !

Fixes #211.
2026-10-14 15:48:42 +03:00

85 lines
3.7 KiB
JavaScript

'use strict';
// #211: the same cards in several named column sets. A board keeps column
// sets (`columnSets`: [{ _id, name, columns: [{ _id, title, color }] }]) - say
// "Completion", "Priority", "Milestone" - and a card keeps which column it is
// in for each set (`columnSetValues`: { setId: columnId }). The Column sets
// view shows one set's columns and moves cards between them by dragging,
// which changes only the card's place in THAT set; each card also shows, as
// coloured badges, its column in the other sets. Lists, swimlanes, labels
// and members are not touched. Pure: tests/columnSets.test.cjs.
const MAX_SETS = 20;
const MAX_COLUMNS = 40;
const COLORS = Object.freeze(['', 'green', 'yellow', 'orange', 'red', 'purple', 'blue', 'sky', 'lime', 'pink', 'black', 'silver']);
function cleanName(value, max = 60) {
return typeof value === 'string' ? value.replace(/\s+/g, ' ').trim().slice(0, max) : '';
}
function sets(board) {
return Array.isArray(board && board.columnSets) ? board.columnSets : [];
}
function addColumnSet(current, name, newId) {
const n = cleanName(name);
const list = Array.isArray(current) ? current : [];
if (!n || list.length >= MAX_SETS) return null;
return [...list, { _id: newId, name: n, columns: [] }];
}
function addColumn(current, setId, title, color, newId) {
const t = cleanName(title);
if (!t) return null;
let done = false;
const next = (Array.isArray(current) ? current : []).map(s => {
if (s._id !== setId || (s.columns || []).length >= MAX_COLUMNS) return s;
done = true;
return { ...s, columns: [...(s.columns || []), { _id: newId, title: t, color: COLORS.includes(color) ? color : '' }] };
});
return done ? next : null;
}
function removeColumn(current, setId, columnId) {
return (Array.isArray(current) ? current : []).map(s => (s._id === setId
? { ...s, columns: (s.columns || []).filter(c => c._id !== columnId) } : s));
}
function removeColumnSet(current, setId) {
return (Array.isArray(current) ? current : []).filter(s => s._id !== setId);
}
// The column a card is in for `setId`, if that column still exists.
function cardColumnId(card, set) {
const id = card && card.columnSetValues && card.columnSetValues[set && set._id];
return id && (set.columns || []).some(c => c._id === id) ? id : null;
}
// The view's columns: "not placed" first, then the set's, each with its cards.
function columnsWithCards(set, cards) {
const columns = [{ _id: null, title: null, color: '', cards: [] }, ...((set && set.columns) || []).map(c => ({ ...c, cards: [] }))];
const byId = new Map(columns.map(c => [c._id, c]));
for (const card of Array.isArray(cards) ? cards : []) byId.get(cardColumnId(card, set)).cards.push(card);
return columns;
}
// Where the card is in every OTHER set, as badges.
function otherSetBadges(card, allSets, currentSetId) {
return (Array.isArray(allSets) ? allSets : []).filter(s => s._id !== currentSetId).map(s => {
const id = cardColumnId(card, s);
const column = id && s.columns.find(c => c._id === id);
return column ? { setName: s.name, title: column.title, color: column.color || '' } : null;
}).filter(Boolean);
}
// The card update that puts it in `columnId` (null: not placed) of `setId`.
function placeModifier(set, columnId) {
if (!set || typeof set._id !== 'string' || !/^[A-Za-z0-9_-]+$/.test(set._id)) return null;
if (columnId === null || columnId === '') return { $unset: { [`columnSetValues.${set._id}`]: '' } };
if (!(set.columns || []).some(c => c._id === columnId)) return null;
return { $set: { [`columnSetValues.${set._id}`]: columnId } };
}
module.exports = { MAX_SETS, MAX_COLUMNS, COLORS, sets, addColumnSet, addColumn, removeColumn, removeColumnSet,
cardColumnId, columnsWithCards, otherSetBadges, placeModifier };