Files
wekan/models/lib/cardKinds.js
T
Lauri Ojansivu 0d77882c3b Story and Task cards, with custom fields of their own.
A card can be a Story or a Task: card menu / Card kind (or an ordinary
card again), with a book or a checked-box badge on the minicard. A custom
field can be limited to card kinds - "Only on these card kinds", none
ticked being every card as before. A kind's own fields are added to a
card when it gets that kind, and shown only on cards of it; a field of
no kind shows on every card. The one choke point every render uses,
customFieldsWD, leaves out fields of other kinds, so the card details and
minicard agree.

models/lib/cardKinds.js decides; tests/cardKinds.test.cjs covers the
fields a kind brings, not twice, other kinds' fields staying off, unknown
kinds, and the card menu, badge and the field's ticks. The new strings
are English placeholders until the next translation pass.

Thanks to nightwind27 and xet7 !

Fixes #2012.
2026-10-14 15:26:00 +03:00

35 lines
1.5 KiB
JavaScript

'use strict';
// #2012: story and task cards, with custom fields of their own. A card may be
// a Story or a Task (`cardKind`; unset is an ordinary card). A custom field
// can be limited to kinds (`cardKinds`; empty is every card): it is then
// added to a card when the card gets that kind, and shown only on cards of
// it. Pure: tests/cardKinds.test.cjs.
const CARD_KINDS = Object.freeze(['story', 'task']);
const KIND_ICONS = Object.freeze({ story: 'fa-book', task: 'fa-check-square-o' });
function cleanCardKind(kind) {
return CARD_KINDS.includes(kind) ? kind : null;
}
function cleanCardKinds(kinds) {
return Array.isArray(kinds) ? CARD_KINDS.filter(k => kinds.includes(k)) : [];
}
// Whether a field definition shows on a card of `kind`.
function definitionAppliesToKind(definition, kind) {
const kinds = definition && Array.isArray(definition.cardKinds) ? definition.cardKinds : [];
return kinds.length === 0 || kinds.includes(kind);
}
// The definitions of `kind`'s own fields a card does not have yet.
function fieldsToAddForKind(cardCustomFields, definitions, kind) {
if (!cleanCardKind(kind)) return [];
const have = new Set((Array.isArray(cardCustomFields) ? cardCustomFields : []).map(f => f && f._id));
return (Array.isArray(definitions) ? definitions : [])
.filter(d => d && Array.isArray(d.cardKinds) && d.cardKinds.includes(kind) && !have.has(d._id));
}
module.exports = { CARD_KINDS, KIND_ICONS, cleanCardKind, cleanCardKinds, definitionAppliesToKind, fieldsToAddForKind };