Files
kintone-helper-extenstion/fields.js
2025-10-17 14:39:33 +08:00

194 lines
7.8 KiB
JavaScript

export const kintonePrettyFields = {
generateFields: (properties, layouts) => {
const fields = [];
const spacers = [];
const isStatus = field => field.type === "STATUS";
const isStatusAssignee = field => field.type === "STATUS_ASSIGNEE";
const isCategory = field => field.type === "CATEGORY";
const propertyList = Object.values(properties)
const statusField = propertyList.find(isStatus);
if (statusField && statusField.enabled) {
fields.push(statusField);
}
const statusAssigneeField = propertyList.find(isStatusAssignee);
if (statusAssigneeField && statusAssigneeField.enabled) {
fields.push(statusAssigneeField);
}
const categoryField = propertyList.find(isCategory);
if (categoryField && categoryField.enabled) {
fields.push(categoryField);
}
for (const r of layouts) {
if (r.type === "ROW") {
for (const f of r.fields) {
if (f.type === "LABEL" || f.type === "HR") continue;
if (f.type === "SPACER") {
spacers.push(f);
continue;
}
const ee = properties[f.code];
if (ee.type !== "SUBTABLE" && ee.type !== "GROUP") {
if (ee.type === "CHECK_BOX" || ee.type === "DROP_DOWN" || ee.type === "MULTI_SELECT" || ee.type === "RADIO_BUTTON") {
const sortedOptions = Object.values(ee.options).sort((a, b) => Number(a.index) - Number(b.index)).map(o => o.label);
fields.push({
...ee,
sortedOptions
});
continue;
}
fields.push(ee);
}
}
} else if (r.type === "SUBTABLE") {
const tableCode = r.code;
const tableField = properties[tableCode];
if (tableField.type !== "SUBTABLE") continue;
const tableFields = {};
for (const { code: fieldCode } of r.fields) {
const subf = tableField.fields[fieldCode];
if (subf.type === "CHECK_BOX" || subf.type === "DROP_DOWN" || subf.type === "MULTI_SELECT" || subf.type === "RADIO_BUTTON") {
const sortedOptions = Object.values(subf.options).sort((a, b) => Number(a.index) - Number(b.index)).map(o => o.label);
tableFields[fieldCode] = {
...subf,
table: tableCode,
sortedOptions
};
fields.push({
...subf,
table: tableCode,
sortedOptions
});
continue;
}
tableFields[fieldCode] = {
...subf,
table: tableCode
};
fields.push({
...subf,
table: tableCode
});
}
fields.push({
...tableField,
fields: tableFields
});
} else if (r.type === "GROUP") {
const groupCode = r.code;
const groupField = properties[groupCode];
if (groupField.type !== "GROUP") continue;
const groupFields = {};
for (const row of r.layout) {
for (const f of row.fields) {
if (f.type === "LABEL" || f.type === "HR") continue;
if (f.type === "SPACER") {
spacers.push({
...f,
group: groupCode
});
continue;
}
const gSubf = properties[f.code];
if (gSubf.type !== "CATEGORY" && gSubf.type !== "STATUS" && gSubf.type !== "STATUS_ASSIGNEE" && gSubf.type !== "SUBTABLE" && gSubf.type !== "GROUP") {
if (gSubf.type === "CHECK_BOX" || gSubf.type === "DROP_DOWN" || gSubf.type === "MULTI_SELECT" || gSubf.type === "RADIO_BUTTON") {
const sortedOptions = Object.values(gSubf.options).sort((a, b) => Number(a.index) - Number(b.index)).map(o => o.label);
groupFields[f.code] = {
...gSubf,
group: groupCode,
sortedOptions
};
fields.push({
...gSubf,
group: groupCode,
sortedOptions
});
continue;
}
groupFields[f.code] = {
...gSubf,
group: groupCode
};
fields.push({
...gSubf,
group: groupCode
});
}
}
}
fields.push({
...groupField,
fields: groupFields
});
}
}
// lookup copy
const flatFields = fields;
for (const f of flatFields) {
if ('lookup' in f && f.lookup !== null) {
for (const mapping of f.lookup.fieldMappings) {
const target = flatFields.find(ee => ["SINGLE_LINE_TEXT", "NUMBER", "MULTI_LINE_TEXT", "RICH_TEXT", "LINK", "CHECK_BOX", "RADIO_BUTTON", "DROP_DOWN", "MULTI_SELECT", "DATE", "TIME", "DATETIME", "USER_SELECT", "ORGANIZATION_SELECT", "GROUP_SELECT", "CALC", "RECORD_NUMBER"].includes(ee.type) && ee.code === mapping.field);
if (target) {
target.isLookupCopy = true;
}
}
}
}
const recordNumberField = propertyList.find(f => f.type === "RECORD_NUMBER");
if (recordNumberField && !fields.some(f => f.type === "RECORD_NUMBER")) fields.push(recordNumberField);
const creatorField = propertyList.find(f => f.type === "CREATOR");
if (creatorField && !fields.some(f => f.type === "CREATOR")) fields.push(creatorField);
const createdTimeField = propertyList.find(f => f.type === "CREATED_TIME");
if (createdTimeField && !fields.some(f => f.type === "CREATED_TIME")) fields.push(createdTimeField);
const modifierField = propertyList.find(f => f.type === "MODIFIER");
if (modifierField && !fields.some(f => f.type === "MODIFIER")) fields.push(modifierField);
const updatedTimeField = propertyList.find(f => f.type === "UPDATED_TIME");
if (updatedTimeField && !fields.some(f => f.type === "UPDATED_TIME")) fields.push(updatedTimeField);
return { fields, spacers };
},
isCategory: field => field.type === "CATEGORY",
isCheckBox: field => field.type === "CHECK_BOX",
isCreatedTime: field => field.type === "CREATED_TIME",
isCreator: field => field.type === "CREATOR",
isDate: field => field.type === "DATE",
isDatetime: field => field.type === "DATETIME",
isDropDown: field => field.type === "DROP_DOWN",
isFile: field => field.type === "FILE",
isGroup: field => field.type === "GROUP",
isGroupSelect: field => field.type === "GROUP_SELECT",
isInGroup: field => 'group' in field,
isInSubtable: field => 'table' in field,
isLink: field => field.type === "LINK",
isLookup: field => 'lookup' in field,
isLookupCopy: field => field.isLookupCopy === true,
isModifier: field => field.type === "MODIFIER",
isMultiLineText: field => field.type === "MULTI_LINE_TEXT",
isMultiSelect: field => field.type === "MULTI_SELECT",
isNotInSubtable: field => !('table' in field),
isNumber: field => field.type === "NUMBER",
isOrganizationSelect: field => field.type === "ORGANIZATION_SELECT",
isRadioButton: field => field.type === "RADIO_BUTTON",
isRecordNumber: field => field.type === "RECORD_NUMBER",
isReferenceTable: field => field.type === "REFERENCE_TABLE",
isRichText: field => field.type === "RICH_TEXT",
isSingleLineText: field => field.type === "SINGLE_LINE_TEXT",
isStatus: field => field.type === "STATUS",
isStatusAssignee: field => field.type === "STATUS_ASSIGNEE",
isSubtable: field => field.type === "SUBTABLE",
isTime: field => field.type === "TIME",
isUpdatedTime: field => field.type === "UPDATED_TIME",
isUserSelect: field => field.type === "USER_SELECT",
};