@craftercms/studio-ui
Version:
Services, components, models & utils to build CrafterCMS authoring extensions.
174 lines (172 loc) • 6.46 kB
JavaScript
/*
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { createLookupTable } from './object';
import Jabber from 'jabber';
export function getRelatedContentTypeIds(contentType) {
return Object.values(contentType.fields).reduce((accumulator, field) => {
if (
field.type === 'array' &&
field.validations != null &&
'validations' in field &&
'allowedContentTypes' in field.validations
) {
field.validations.allowedContentTypes.value.forEach(
(ctid) => !accumulator.includes(ctid) && accumulator.push(ctid)
);
}
return accumulator;
}, []);
}
export function isGroupItem(contentType, fieldId) {
return fieldId.includes('.');
}
export function isComponentHolder(contentType, fieldId) {
return getField(contentType, fieldId).type === 'node-selector';
}
export function isGroup(contentType, fieldId) {
return getField(contentType, fieldId).type === 'repeat';
}
export function doesFieldAccept(contentType, fieldId) {
// const field = ContentType.getField(contentType, fieldId);
throw new Error('[doesFieldAccept] Not implemented');
}
export function getField(type, fieldId, contentTypes) {
const fields = fieldId.split('.');
let accumulator = Array.isArray(type.fields) ? createLookupTable(type.fields) : type.fields;
let parsedFieldId = [];
fields.forEach((field) => {
parsedFieldId.push(field);
if (accumulator.type === 'node-selector') {
if (!contentTypes) {
throw new Error(
`Content types not provided to content type helper \`getField\` method. ` +
`Unable to retrieve the field \`${fieldId}\` without full list of content types.`
);
}
const contentTypeWithTargetFieldId = accumulator.validations.allowedContentTypes.value.find((ct) =>
Boolean(contentTypes[ct].fields[field])
);
accumulator = contentTypes[contentTypeWithTargetFieldId].fields[field];
} else {
if (accumulator.type === 'repeat') {
// For repeat groups, the field inside the repeat group field will be
// under {repeatName}.fields.{fieldName}. To abstract this complexity from devs
// we parse it here.
accumulator = accumulator.fields[field];
} else {
accumulator = accumulator[field];
}
}
});
return accumulator;
}
export function getFields(type, ...ids) {
return ids.map((id) => getField(type, id));
}
export function getFieldsByType(contentType, fieldType) {
return Object.values(contentType.fields).filter((field) => field.type === fieldType);
}
// TODO: See variable.js for additional case items to possibly include here
export function getDefaultValue(field) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
if (field.defaultValue) {
return field.defaultValue;
} else if ((_a = field.validations.required) === null || _a === void 0 ? void 0 : _a.value) {
switch (field.type) {
case 'image': {
const width =
(_e =
(_c = (_b = field.validations.width) === null || _b === void 0 ? void 0 : _b.value) !== null &&
_c !== void 0
? _c
: (_d = field.validations.minWidth) === null || _d === void 0
? void 0
: _d.value) !== null && _e !== void 0
? _e
: 150;
const height =
(_j =
(_g = (_f = field.validations.height) === null || _f === void 0 ? void 0 : _f.value) !== null &&
_g !== void 0
? _g
: (_h = field.validations.minHeight) === null || _h === void 0
? void 0
: _h.value) !== null && _j !== void 0
? _j
: width;
return `https://via.placeholder.com/${width}x${height}`;
}
case 'text':
case 'textarea': {
let maxLength = parseInt((_k = field.validations.maxLength) === null || _k === void 0 ? void 0 : _k.value);
let textGen = new Jabber();
return maxLength
? `${textGen.createParagraph(50).substring(0, maxLength)}.`.replace(/\.+/, '.')
: textGen.createParagraph(10);
}
case 'html': {
let textGen = new Jabber();
return textGen.createParagraph(10);
}
case 'numeric-input': {
return (_m = (_l = field.validations.minValue) === null || _l === void 0 ? void 0 : _l.value) !== null &&
_m !== void 0
? _m
: 1;
}
case 'boolean': {
return 'false';
}
case 'date-time': {
return new Date().toISOString();
}
case 'repeat': {
const repeat = [];
if (field.validations.minCount && field.fields) {
new Array(field.validations.minCount).fill(null).forEach(() => {
const item = {};
for (const subFieldId in field.fields) {
item[subFieldId] = getDefaultValue(field.fields[subFieldId]);
}
repeat.push(item);
});
}
return repeat;
}
case 'node-selector':
// TODO: CHECK MIN/MAX COUNT
return [];
default: {
return null;
}
}
}
}