'use strict'; // #3748: match a card's `customFields` entries to the custom-field definitions // attached to the card's CURRENT board ("customFields With Definitions"). // // Linked cards are by design pointers that mirror the original card; labels and // custom-field DEFINITIONS are board-scoped, so they are deliberately not // inherited across boards (that remapping-by-name behaviour belongs to the // COPY path — Cards.copy()/mapCustomFieldsToBoard — not to links). However, // Cards.link() deep-copies the original document, so a cross-board linked card // carries a `customFields` snapshot whose definition ids do not exist on the // viewing board. The old customFieldsWD() returned an empty `{}` placeholder // for every unmatched entry, which rendered a phantom empty row in the card // details and made the `cardCustomField` template's getTemplate() helper throw // "TypeError: Cannot read properties of undefined (reading 'type')" on every // render. Unmatched entries must be SKIPPED instead. // A dropdown custom field stores the selected item's id as `value`; the // human-readable "true value" is the matching dropdown item's name. A // dropdownMultiSelect custom field stores an ARRAY of selected item ids, and // its "true value" is the matching item names joined for display. Any other // field type (or an id/array entry that no longer matches an item) keeps the // raw value. function resolveDropdownItemName(items, id) { if (Array.isArray(items)) { for (const item of items) { if (item && item._id === id) { return item.name; } } } return id; } function resolveTrueValue(definition, value) { const items = definition && definition.settings && definition.settings.dropdownItems; if (definition && definition.type === 'dropdownMultiSelect') { const ids = Array.isArray(value) ? value : []; return ids.map(id => resolveDropdownItemName(items, id)).join(', '); } return resolveDropdownItemName(items, value); } // customFields: the card's own `customFields` array ([{_id, value}, ...]). // definitions: the CustomFields definitions attached to the card's board. // Returns [{_id, value, trueValue, definition}, ...] sorted by the // definition's user-settable `sort` (#4165), falling back to name order for // fields created before `sort` existed, with entries whose definition is not // on this board left out. function buildCustomFieldsWD(customFields, definitions) { if (!Array.isArray(customFields) || !Array.isArray(definitions)) { return []; } const ret = []; for (const customField of customFields) { if (!customField) { continue; } const definition = definitions.find( def => def && def._id === customField._id, ); if (!definition) { // e.g. a linked card's snapshot from the original board, or a definition // that was deleted / detached from this board. continue; } ret.push({ _id: customField._id, value: customField.value, trueValue: resolveTrueValue(definition, customField.value), definition, }); } ret.sort((a, b) => { const aSort = a.definition.sort; const bSort = b.definition.sort; const aHasSort = typeof aSort === 'number'; const bHasSort = typeof bSort === 'number'; if (aHasSort && bHasSort && aSort !== bSort) { return aSort - bSort; } // A field with no `sort` yet (created before #4165) sorts after any field // that has one, so a board that starts reordering does not have its // untouched fields jump ahead of the ones the user just placed. if (aHasSort !== bHasSort) { return aHasSort ? -1 : 1; } const aName = a.definition.name; const bName = b.definition.name; if (aName === undefined || bName === undefined) { return 0; } return aName.localeCompare(bName); }); return ret; } // #3141: an "Admin only" custom field definition is dropped entirely for a // non board-admin viewer, BEFORE it ever reaches buildCustomFieldsWD() - so // neither the card-detail view nor the minicard (both render off the one // customFieldsWD() call) nor the "assign to this card" popup list can show // its name or value to anyone but a board admin. A defensive filter: any // falsy/malformed definition is left as-is rather than dropped, since a // missing `adminOnly` (the default for every field created before this // existed) must never hide a field that was never marked admin-only. function filterAdminOnlyDefinitions(definitions, isBoardAdmin) { if (!Array.isArray(definitions)) { return definitions; } if (isBoardAdmin) { return definitions; } return definitions.filter(def => !(def && def.adminOnly)); } // #6811: whether a card's custom field holds something to show on its // minicard. A field the card only carries because it is "automatically on // card" / "always on card" is null until someone fills it in; drawing it gave // every minicard a stack of empty rows (before the minicard's sections were // rewritten, only fields with a value were drawn). 0 and an explicitly // unticked checkbox (false) are values; null, undefined, an empty or blank // string and an empty multi-select are not. function hasCustomFieldValue(value) { if (value === null || value === undefined) return false; if (typeof value === 'string') return value.trim() !== ''; if (Array.isArray(value)) return value.length > 0; return true; } export { hasCustomFieldValue, buildCustomFieldsWD, resolveTrueValue, resolveDropdownItemName, filterAdminOnlyDefinitions, };