90 lines
5.6 KiB
JavaScript
90 lines
5.6 KiB
JavaScript
// The forms of the API Platform window, without the page: how the arguments of an attribute become form values and back
|
|
// (a context is its "groups", an expression is locked, only what changed is written). Tested by api-platform-model.test.mjs.
|
|
|
|
export const CTX = { normalizationGroups: 'normalizationContext', denormalizationGroups: 'denormalizationContext', validationGroups: 'validationContext' };
|
|
export const isExpr = (v) => v && typeof v === 'object' && !Array.isArray(v) && '@expr' in v;
|
|
export const clone = (v) => JSON.parse(JSON.stringify(v ?? null));
|
|
export const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
|
|
// the fields of a resource / an operation: key => [label, kind]
|
|
export const FIELD = {
|
|
shortName: ['Short name', 'text'], uriTemplate: ['URI template', 'text'], uriVariables: ['URI variables (links)', 'links'],
|
|
normalizationGroups: ['Normalization groups (read)', 'groups'], denormalizationGroups: ['Denormalization groups (write)', 'groups'], validationGroups: ['Validation groups', 'groups'],
|
|
security: ['Security (expression)', 'text'], securityPostDenormalize: ['Security after denormalization', 'text'], securityMessage: ['Security message', 'text'],
|
|
paginationEnabled: ['Pagination', 'tri'], paginationItemsPerPage: ['Items per page', 'int'], paginationClientEnabled: ['The client can turn pagination on or off', 'tri'],
|
|
paginationClientItemsPerPage: ['The client can set the items per page', 'tri'], paginationMaximumItemsPerPage: ['Most items per page', 'int'],
|
|
formats: ['Formats', 'formats'], order: ['Default order', 'order'],
|
|
provider: ['State provider', 'class'], processor: ['State processor', 'class'], input: ['Input class (false: none)', 'class'], output: ['Output class (false: none)', 'class'],
|
|
controller: ['Controller', 'class'], name: ['Route name', 'text'],
|
|
};
|
|
export const RES_SECTIONS = [['General', ['shortName', 'uriTemplate', 'uriVariables']], ['Serialization and validation', ['normalizationGroups', 'denormalizationGroups', 'validationGroups']],
|
|
['Security', ['security', 'securityPostDenormalize', 'securityMessage']], ['Pagination', ['paginationEnabled', 'paginationItemsPerPage', 'paginationClientEnabled', 'paginationClientItemsPerPage', 'paginationMaximumItemsPerPage']],
|
|
['Formats and order', ['formats', 'order']], ['State', ['provider', 'processor', 'input', 'output']]];
|
|
export const OP_FIELDS = ['name', 'uriTemplate', 'controller', 'security', 'securityPostDenormalize', 'securityMessage', 'validationGroups', 'normalizationGroups', 'denormalizationGroups',
|
|
'paginationEnabled', 'paginationItemsPerPage', 'provider', 'processor', 'input', 'output'];
|
|
|
|
/** the value of a field as the form holds it (and whether the code holds something the form cannot edit) */
|
|
export function readField(args, k) {
|
|
const kind = FIELD[k][1];
|
|
let v = args[k];
|
|
if (CTX[k]) {
|
|
const c = args[CTX[k]];
|
|
if (isExpr(c)) return { locked: c['@expr'] };
|
|
v = c?.groups;
|
|
if (c && !Array.isArray(c) && v === undefined) return { value: [] };
|
|
if (Array.isArray(v) && v.some((x) => typeof x !== 'string')) return { locked: JSON.stringify(v) };
|
|
return { value: Array.isArray(v) ? v : [] };
|
|
}
|
|
if (isExpr(v)) return { locked: v['@expr'] };
|
|
switch (kind) {
|
|
case 'tri': return { value: v === true ? 'true' : v === false ? 'false' : '' };
|
|
case 'int': return { value: v === undefined || v === null ? '' : String(v) };
|
|
case 'class': return { value: v === false ? 'false' : typeof v === 'string' ? v : '' };
|
|
case 'formats': return Array.isArray(v) && v.every((x) => typeof x === 'string') ? { value: v } : v === undefined ? { value: [] } : { locked: JSON.stringify(v) };
|
|
case 'order': return v && !Array.isArray(v) && typeof v === 'object' ? { value: Object.entries(v).map(([p, d]) => [p, String(d)]) } : v === undefined ? { value: [] } : { locked: JSON.stringify(v) };
|
|
case 'links': {
|
|
if (v === undefined) return { value: [] };
|
|
if (!v || Array.isArray(v) || typeof v !== 'object') return { locked: JSON.stringify(v) };
|
|
return { value: Object.entries(v).map(([n, l]) => [n, l?.fromClass || '', l?.fromProperty || '', l?.toProperty || '']) };
|
|
}
|
|
case 'groups': return { value: [] };
|
|
default: return { value: typeof v === 'string' ? v : '' };
|
|
}
|
|
}
|
|
|
|
/** the argument to write for a field's form value (null: remove the argument) */
|
|
export function writeField(k, v) {
|
|
switch (FIELD[k][1]) {
|
|
case 'tri': return v === '' ? null : v === 'true';
|
|
case 'int': return v === '' ? null : Number(v);
|
|
case 'class': return v === '' ? null : v === 'false' ? false : v;
|
|
case 'formats': case 'groups': return v.length ? v : null;
|
|
case 'order': return v.filter((r) => r[0]).length ? Object.fromEntries(v.filter((r) => r[0])) : null;
|
|
case 'links': {
|
|
const rows = v.filter((r) => r[0] && r[1]);
|
|
return rows.length ? Object.fromEntries(rows.map((r) => [r[0], { fromClass: r[1], ...(r[2] ? { fromProperty: r[2] } : {}), ...(r[3] ? { toProperty: r[3] } : {}) }])) : null;
|
|
}
|
|
default: return v === '' ? null : v;
|
|
}
|
|
}
|
|
|
|
/** a form model for the fields of some args: {k: value}, the locked ones listed apart */
|
|
export function modelOf(args, keys) {
|
|
const m = {};
|
|
const locked = {};
|
|
for (const k of keys) {
|
|
const r = readField(args, k);
|
|
if (r.locked !== undefined) locked[k] = r.locked; else m[k] = r.value;
|
|
}
|
|
return { m, init: clone(m), locked };
|
|
}
|
|
|
|
/** the arguments that changed between a model and its first state */
|
|
export function changed(model, keys, all = false) {
|
|
const out = {};
|
|
for (const k of keys) {
|
|
if (k in model.locked) continue;
|
|
if (all ? (writeField(k, model.m[k]) !== null) : !same(model.m[k], model.init[k])) out[k] = writeField(k, model.m[k]);
|
|
}
|
|
return out;
|
|
}
|