dtable-utils
Version:
dtable common utils
381 lines (377 loc) • 12.5 kB
JavaScript
import _typeof from '@babel/runtime/helpers/typeof';
import { getFormulaDisplayString } from '../cell-value-get/cell-value.js';
import { getColumnOptionNameById } from '../cell-value-get/option.js';
import { getLinkCellValue } from '../link/core.js';
import { isEmpty } from '../common.js';
import { CONVERT_ROW_COLUMNS_LIMIT, TEXT_CELL_VALUE_LIMIT } from '../constants/limit.js';
import { CellType } from '../constants/cell-type.js';
import { getTableColumnByName } from '../table/column.js';
import { formatTextToNumber } from '../cell-value-set/number.js';
import { formatTextToCheckbox } from '../cell-value-set/checkbox.js';
import { formatTextToDate } from '../cell-value-set/date.js';
import { formatTextToSingleOption } from '../cell-value-set/single-select.js';
import { formatValueToMultipleOption, formatTextToMultipleOption } from '../cell-value-set/multiple-select.js';
import { formatTextToLongText } from '../cell-value-set/long-text.js';
import { formatTextToImage } from '../cell-value-set/image.js';
import { formatTextToGeolocation } from '../cell-value-set/geolocation.js';
import { formatTextToDuration } from '../cell-value-set/duration.js';
import 'dayjs';
import '../constants/column.js';
import '../constants/formula.js';
import { getCollaboratorEmailsByNames } from '../cell-value-get/collaborator.js';
import '../constants/group.js';
import '../date.js';
import '../constants/filter/filter-column-options.js';
import '../constants/filter/filter-modifier.js';
import '../constants/filter/filter-predicate.js';
import '../constants/filter/filter-is-within.js';
import '../constants/sort.js';
import { isFileValue } from '../column/file.js';
/**
* Convert row
* e.g. { '0000': 'a' } to { 'Name': 'a' }
* @param {object} row
* @param {object} value e.g. { links, ... }
* @param {object} table e.g. { _id, columns, ... }
* @param {object} view e.g. { hidden_columns, ... }
* @param {object} formulaResults computed value of formula, link-formula, link etc.
* @param {bool} convertLinkID use to convert link: get linked from formulaResults if true, otherwise get linked from links
* @param {func} debug use to debug if supplied
* @returns converted row, object
*/
var convertRow = function convertRow(row, value, table, view, formulaResults, convertLinkID) {
var _ref = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : {},
debug = _ref.debug;
var result = {};
// eslint-disable-next-line
if (row.hasOwnProperty('_id')) {
result._id = row._id;
}
// eslint-disable-next-line
if (row.hasOwnProperty('_mtime')) {
result._mtime = row._mtime;
}
// eslint-disable-next-line
if (row.hasOwnProperty('_ctime')) {
result._ctime = row._ctime;
}
var columns = table.columns;
if (columns.length > CONVERT_ROW_COLUMNS_LIMIT) {
columns = columns.slice(0, CONVERT_ROW_COLUMNS_LIMIT);
}
var hiddenColumns = view ? view.hidden_columns : null;
for (var i = 0; i < columns.length; i++) {
var column = columns[i];
var key = column.key,
type = column.type,
columnName = column.name;
if (hiddenColumns && hiddenColumns.includes(key)) {
continue;
}
var cellValue = row[key];
switch (type) {
case CellType.SINGLE_SELECT:
{
if (!column.data) {
if (debug) {
debug('No options found');
}
break;
}
result[columnName] = getColumnOptionNameById(column, cellValue);
break;
}
case CellType.MULTIPLE_SELECT:
{
if (!column.data) {
if (debug) {
debug('No options found');
}
break;
}
if (!Array.isArray(cellValue)) {
result[columnName] = '';
break;
}
var optionNames = [];
for (var optionIndex = 0; optionIndex < cellValue.length; optionIndex++) {
var optionName = getColumnOptionNameById(column, cellValue[optionIndex]);
if (optionName) {
optionNames.push(optionName);
}
}
result[columnName] = optionNames;
break;
}
case CellType.DATE:
{
var timeTextWithoutTimezone = cellValue ? cellValue.replace(/([+-]\d{2}:?\d{2})|Z$/g, '') : '';
var format = column.data && column.data.format;
result[columnName] = formatTextToDate(timeTextWithoutTimezone, format);
break;
}
case CellType.LONG_TEXT:
{
result[columnName] = cellValue ? cellValue.text : '';
break;
}
case CellType.LINK:
{
if (!column.data) {
if (debug) {
debug('No links found');
}
break;
}
// convertLinkID to display value
if (convertLinkID) {
// get values form formulaResults
var formulaRow = formulaResults && formulaResults[row._id];
var cellResult = formulaRow && formulaRow[key];
if (!Array.isArray(cellResult) || cellResult.length === 0) {
result[columnName] = null;
break;
}
// linked with common column:
// cellResult
// [
// {row_id: '', displayValue: ''},
// {row_id: '', displayValue: ''},
// ]
// linked with formula column and formula result is array
// cellResult
// [
// {row_id: '', display_value: []},
// {row_id: '', display_value: []},
// ]
result[columnName] = cellResult;
break;
}
var tableID = table._id;
var _column$data = column.data,
link_id = _column$data.link_id,
table_id = _column$data.table_id,
other_table_id = _column$data.other_table_id,
is_linked_back = _column$data.is_linked_back;
var otherTableID = tableID === table_id ? other_table_id : table_id;
var links = value.links;
result[columnName] = getLinkCellValue({
links: links,
link_id: link_id,
is_linked_back: is_linked_back,
table1_id: tableID,
table2_id: otherTableID,
row_id: row._id
});
break;
}
case CellType.FORMULA:
case CellType.LINK_FORMULA:
{
if (!column.data || !formulaResults) {
if (debug) {
debug('No formula found');
}
break;
}
var _formulaRow = formulaResults[row._id] || {};
result[columnName] = getFormulaDisplayString(_formulaRow[key], column.data);
break;
}
default:
{
// text/email/url/auto-number/
// check/
// rate/duration/
// file/image/button/
// translate in dtable-events
// number/
// ctime/mtime/
// geolocation/
// collaborator/creator/last-modifier/
result[columnName] = cellValue;
}
}
if (isEmpty(result[columnName])) {
delete result[columnName];
}
}
return result;
};
var convertRowBack = function convertRowBack(row, table) {
var collaborators = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
var result = {};
// eslint-disable-next-line no-restricted-syntax
for (var key in row) {
var cellValue = row[key];
if (key === '_id') {
result['_id'] = cellValue;
continue;
}
var column = getTableColumnByName(table, key);
if (!column) {
continue;
}
switch (column.type) {
case CellType.TEXT:
if (cellValue == null) {
result[column.key] = '';
break;
}
result[column.key] = (cellValue + '').slice(0, TEXT_CELL_VALUE_LIMIT);
break;
case CellType.EMAIL:
case CellType.URL:
result[column.key] = cellValue == null ? '' : cellValue + '';
break;
case CellType.NUMBER:
result[column.key] = formatTextToNumber(cellValue);
break;
case CellType.CHECKBOX:
result[column.key] = formatTextToCheckbox(cellValue);
break;
case CellType.DATE:
// eslint-disable-next-line
var format = column.data && column.data.format;
result[column.key] = formatTextToDate(cellValue, format);
break;
case CellType.COLLABORATOR:
// todo
if (collaborators) {
result[column.key] = getCollaboratorEmailsByNames(cellValue, collaborators);
} else {
result[column.key] = cellValue;
}
break;
case CellType.SINGLE_SELECT:
result[column.key] = formatTextToSingleOption(cellValue, column);
break;
case CellType.MULTIPLE_SELECT:
if (!cellValue) {
result[column.key] = null;
break;
}
if (Array.isArray(cellValue)) {
result[column.key] = formatValueToMultipleOption(cellValue, column);
break;
}
if (typeof cellValue === 'string') {
if (!cellValue.trim()) {
result[column.key] = null;
} else {
result[column.key] = formatTextToMultipleOption(cellValue, column);
}
break;
}
result[column.key] = null;
break;
case CellType.LONG_TEXT:
// eslint-disable-next-line
var text = cellValue;
if (!text) {
result[column.key] = null;
break;
}
if (_typeof(text) === 'object') {
result[column.key] = text;
break;
}
if (typeof text === 'string') {
if (!text.trim()) {
result[column.key] = null;
} else {
result[column.key] = formatTextToLongText(text);
}
break;
}
result[column.key] = null;
break;
case CellType.IMAGE:
if (!cellValue) {
result[column.key] = null;
break;
}
if (Array.isArray(cellValue)) {
result[column.key] = cellValue;
break;
}
if (typeof cellValue === 'string') {
if (!cellValue.trim()) {
result[column.key] = null;
} else {
result[column.key] = formatTextToImage(cellValue);
}
break;
}
result[column.key] = null;
break;
case CellType.FILE:
if (!cellValue) {
result[column.key] = null;
break;
}
// add data from api
if (Array.isArray(cellValue)) {
var fileValue = cellValue.filter(function (value) {
return isFileValue(value);
});
result[column.key] = fileValue.length > 0 ? fileValue : null;
break;
}
// add data form other app
if (typeof cellValue === 'string') {
result[column.key] = null;
break;
}
result[column.key] = null;
break;
// don't support link/formula/digital-sign column convertRowBack
case CellType.LINK:
case CellType.FORMULA:
case CellType.LINK_FORMULA:
{
result[column.key] = null;
break;
}
case CellType.DIGITAL_SIGN:
if (!cellValue) {
result[column.key] = null;
break;
}
if (_typeof(cellValue) === 'object') {
result[column.key] = cellValue;
break;
}
result[column.key] = null;
break;
case CellType.GEOLOCATION:
if (!cellValue) {
result[column.key] = null;
break;
}
if (_typeof(cellValue) === 'object') {
result[column.key] = cellValue;
break;
}
if (typeof cellValue === 'string') {
if (!cellValue.trim()) {
result[column.key] = null;
} else {
result[column.key] = formatTextToGeolocation(cellValue, column.data);
}
break;
}
result[column.key] = null;
break;
case CellType.DURATION:
{
result[column.key] = formatTextToDuration(cellValue, column.data);
break;
}
default:
result[column.key] = cellValue;
}
}
return result;
};
export { convertRow, convertRowBack };