dtable-utils
Version:
dtable common utils
411 lines (401 loc) • 14.4 kB
JavaScript
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { DateUtils } from '../date.js';
import { getCollaboratorsName } from './collaborator.js';
import { getDateDisplayString } from './date.js';
import { getDigitalSignImageUrl } from './digital-sign.js';
import { getDurationDisplayString } from './duration.js';
import { getGeolocationDisplayString } from './geolocation.js';
import { getLongtextDisplayString } from './long-text.js';
import { getNumberDisplayString } from './number.js';
import { getMultipleOptionName, getOptionName } from './option.js';
import { getDepartmentName } from './department.js';
import { CellType } from '../constants/cell-type.js';
import { COLLABORATOR_COLUMN_TYPES, DEFAULT_DATE_FORMAT } from '../constants/column.js';
import { FORMULA_RESULT_TYPE } from '../constants/formula.js';
var ARRAY_VALUE_COLUMN_TYPES = [CellType.IMAGE, CellType.FILE, CellType.MULTIPLE_SELECT, CellType.COLLABORATOR];
/**
* the helper of 'getCellValueStringResult'
* @param {any} cellValue
* @param {object} column
* @param {array} collaborators
* @param {bool} isArchiveView
* @returns formatted array cell value, string
*/
var _getArrayColumnCellValue = function _getArrayColumnCellValue(cellValue, column, _ref) {
var collaborators = _ref.collaborators,
isArchiveView = _ref.isArchiveView;
if (!Array.isArray(cellValue) || cellValue.length === 0 || !column.data) {
return '';
}
var key = column.key,
data = column.data;
var array_type = data.array_type,
array_data = data.array_data;
var newColumn = {
key: key,
type: array_type,
data: array_data
};
// cellValue itself is an array
if (ARRAY_VALUE_COLUMN_TYPES.includes(array_type)) {
return getCellValueStringResult(_defineProperty({}, key, cellValue), newColumn, {
collaborators: collaborators
});
}
// cellValue itself is a single value
if (array_type === CellType.STRING) {
newColumn.type = CellType.TEXT;
var _result = cellValue.map(function (item) {
return getCellValueStringResult(_defineProperty({}, key, item), newColumn);
});
return _result.filter(Boolean).join(', ');
}
if (array_type === CellType.BOOL) {
newColumn.type = CellType.CHECKBOX;
var _result2 = cellValue.map(function (item) {
return getCellValueStringResult(_defineProperty({}, key, item), newColumn);
});
return _result2.filter(Boolean).join(', ');
}
var result = cellValue.map(function (item) {
return getCellValueStringResult(_defineProperty({}, key, item), newColumn, {
collaborators: collaborators,
isArchiveView: isArchiveView
});
});
return result.filter(function (item) {
return item || item === 0;
}).join(', ');
};
/**
* Get formatted formula result to display. It will not be formatted
* if formula result_type is array and array_type is collaborator, creator or last-modifier etc.
* @param {any} cellValue
* @param {object} columnData the data from column
* @returns formatted formula result, string|array
*/
var getFormulaDisplayString = function getFormulaDisplayString(cellValue, columnData) {
if (!columnData) {
return '';
}
var result_type = columnData.result_type;
if (result_type === FORMULA_RESULT_TYPE.NUMBER) {
return getNumberDisplayString(cellValue, columnData);
}
if (result_type === FORMULA_RESULT_TYPE.DATE) {
var format = columnData.format;
return getDateDisplayString(cellValue, format);
}
// rollup result_type is an array, cellValue may not be an array
if (result_type === FORMULA_RESULT_TYPE.ARRAY) {
var array_type = columnData.array_type,
array_data = columnData.array_data;
if (!array_type) {
return '';
}
if (COLLABORATOR_COLUMN_TYPES.includes(array_type)) {
return cellValue;
}
if (!ARRAY_VALUE_COLUMN_TYPES.includes(array_type) && Array.isArray(cellValue)) {
return cellValue.map(function (val) {
return getCellValueDisplayString({
FORMULA_ARRAY: val
}, array_type, 'FORMULA_ARRAY', {
data: array_data
});
}).join(', ');
}
return getCellValueDisplayString({
FORMULA_ARRAY: cellValue
}, array_type, 'FORMULA_ARRAY', {
data: array_data
});
}
if (Object.prototype.toString.call(cellValue) === '[object Boolean]') {
return String(cellValue);
}
return cellValue;
};
/**
* Get formatted cell value to display.
* @param {object} row e.g. { [column.key]: '' }
* @param {string} type column type
* @param {string} key column key
* @param {object} data column data
* @param {object} formulaRows formula results of rows. Default as "{}"
* @param {array} collaborators e.g. [{ email, name, ... }, ...]. Default as "[]"
* @param {array} departments e.g. [{ name, id }, ...]. Default as "[]"
* @param {bool} isBaiduMap Determine the way to connect latitude and longitude. Default as true
* @param {string} geolocationHyphen Used as a connector between province, city, district and detail. Default as empty string
* @returns formatted cell value, string|array
*/
var getCellValueDisplayString = function getCellValueDisplayString(row, type, key) {
var _ref2 = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {},
data = _ref2.data,
_ref2$formulaRows = _ref2.formulaRows,
formulaRows = _ref2$formulaRows === void 0 ? {} : _ref2$formulaRows,
_ref2$collaborators = _ref2.collaborators,
collaborators = _ref2$collaborators === void 0 ? [] : _ref2$collaborators,
_ref2$departments = _ref2.departments,
departments = _ref2$departments === void 0 ? [] : _ref2$departments,
_ref2$isBaiduMap = _ref2.isBaiduMap,
isBaiduMap = _ref2$isBaiduMap === void 0 ? true : _ref2$isBaiduMap,
_ref2$geolocationHyph = _ref2.geolocationHyphen,
geolocationHyphen = _ref2$geolocationHyph === void 0 ? '' : _ref2$geolocationHyph;
if (!row) return '';
var cellValue = row[key];
switch (type) {
case CellType.LONG_TEXT:
{
return getLongtextDisplayString(cellValue);
}
case CellType.NUMBER:
{
return getNumberDisplayString(cellValue, data);
}
case CellType.DURATION:
{
return getDurationDisplayString(cellValue, data);
}
case CellType.GEOLOCATION:
{
return getGeolocationDisplayString(cellValue, data, {
isBaiduMap: isBaiduMap,
hyphen: geolocationHyphen
});
}
case CellType.SINGLE_SELECT:
{
if (!data) return '';
var options = data.options;
return getOptionName(options, cellValue);
}
case CellType.MULTIPLE_SELECT:
{
if (!data) return '';
var _options = data.options;
return getMultipleOptionName(_options, cellValue);
}
case CellType.DATE:
{
var _ref3 = data || {},
_ref3$format = _ref3.format,
format = _ref3$format === void 0 ? DEFAULT_DATE_FORMAT : _ref3$format;
return getDateDisplayString(cellValue, format);
}
case CellType.CTIME:
case CellType.MTIME:
{
return DateUtils.format(cellValue, 'YYYY-MM-DD HH:MM:SS');
}
case CellType.COLLABORATOR:
{
return getCollaboratorsName(collaborators, cellValue);
}
case CellType.DEPARTMENT_SINGLE_SELECT:
{
return getDepartmentName(departments, cellValue);
}
case CellType.CREATOR:
case CellType.LAST_MODIFIER:
{
return cellValue === 'anonymous' ? cellValue : getCollaboratorsName(collaborators, [cellValue]);
}
case CellType.FORMULA:
case CellType.LINK_FORMULA:
{
var formulaRow = formulaRows && formulaRows[row._id];
if (!formulaRow) return '';
return getFormulaDisplayString(formulaRow[key], data);
}
case CellType.DIGITAL_SIGN:
{
return getDigitalSignImageUrl(cellValue);
}
default:
{
if (cellValue || typeof cellValue === 'boolean') {
return String(cellValue);
}
return '';
}
}
};
/**
* Format cell value to string
* @param {object} row { [column.key]: '', ... }
* @param {object} column
* @param {object} formulaRows formula results of rows. Default as "{}"
* @param {array} collaborators e.g. [{ email, name, ... }, ...]. Default as "[]"
* @param {array} departments e.g. [{ name, id }, ...]. Default as "[]"
* @param {bool} isArchiveView If is archive view, get original cell value from "row", otherwise get cell value from "formulaRows"
* @returns formatted cell value, string
*/
var getCellValueStringResult = function getCellValueStringResult(row, column) {
var _ref4 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
_ref4$formulaRows = _ref4.formulaRows,
formulaRows = _ref4$formulaRows === void 0 ? {} : _ref4$formulaRows,
_ref4$collaborators = _ref4.collaborators,
collaborators = _ref4$collaborators === void 0 ? [] : _ref4$collaborators,
_ref4$departments = _ref4.departments,
departments = _ref4$departments === void 0 ? [] : _ref4$departments,
_ref4$isArchiveView = _ref4.isArchiveView,
isArchiveView = _ref4$isArchiveView === void 0 ? false : _ref4$isArchiveView;
if (!row || !column) return '';
var key = column.key,
type = column.type,
data = column.data;
var cellValue = row[key];
switch (type) {
case CellType.TEXT:
case CellType.EMAIL:
case CellType.URL:
case CellType.AUTO_NUMBER:
{
return cellValue || '';
}
case CellType.RATE:
{
// number
return cellValue ? String(cellValue) : '';
}
case CellType.CHECKBOX:
{
if (typeof cellValue === 'boolean') {
return String(cellValue);
}
return cellValue === 'true' ? 'true' : 'false';
}
case CellType.LONG_TEXT:
{
// the data returned by archive view is a string, not a result in long-text format
if (isArchiveView) {
cellValue = {
text: cellValue
};
}
return getLongtextDisplayString(cellValue);
}
case CellType.NUMBER:
{
return getNumberDisplayString(cellValue, data);
}
case CellType.DURATION:
{
return getDurationDisplayString(cellValue, data);
}
case CellType.GEOLOCATION:
{
return getGeolocationDisplayString(cellValue, data);
}
case CellType.SINGLE_SELECT:
{
if (!data) return '';
return getOptionName(data.options, cellValue);
}
case CellType.MULTIPLE_SELECT:
{
if (!data) return '';
return getMultipleOptionName(data.options, cellValue);
}
case CellType.DATE:
{
var _ref5 = data || {},
_ref5$format = _ref5.format,
format = _ref5$format === void 0 ? DEFAULT_DATE_FORMAT : _ref5$format;
// patch: compatible with previous format
var normalizedFormat = format === 'D/M/YYYY' ? format.replace(/D\/M\/YYYY/, 'DD/MM/YYYY') : format;
return getDateDisplayString(cellValue, normalizedFormat);
}
case CellType.CTIME:
case CellType.MTIME:
{
return DateUtils.format(cellValue, 'YYYY-MM-DD HH:MM:SS');
}
case CellType.COLLABORATOR:
{
return getCollaboratorsName(collaborators, cellValue);
}
case CellType.DEPARTMENT_SINGLE_SELECT:
{
return getDepartmentName(departments, cellValue);
}
case CellType.CREATOR:
case CellType.LAST_MODIFIER:
{
return cellValue === 'anonymous' ? cellValue : getCollaboratorsName(collaborators, [cellValue]);
}
case CellType.LINK:
{
var computedCellValue = cellValue;
if (!isArchiveView) {
var formulaRow = formulaRows && formulaRows[row._id];
computedCellValue = formulaRow && formulaRow[key];
}
if (!Array.isArray(computedCellValue) || computedCellValue.length === 0) {
return '';
}
var displayCellValue = computedCellValue.map(function (linked) {
return linked.display_value;
});
var isTwoDimensionalArray = displayCellValue.some(function (item) {
return Array.isArray(item);
});
if (isTwoDimensionalArray) {
var result = displayCellValue.map(function (item) {
return _getArrayColumnCellValue(item, column, {
collaborators: collaborators,
isArchiveView: isArchiveView
});
});
return result.filter(Boolean).join(', ');
}
return _getArrayColumnCellValue(displayCellValue, column, {
collaborators: collaborators
});
}
case CellType.FORMULA:
case CellType.LINK_FORMULA:
{
var _computedCellValue = cellValue;
if (!isArchiveView) {
var _formulaRow = formulaRows && formulaRows[row._id];
_computedCellValue = _formulaRow && _formulaRow[key];
}
var result_type = data.result_type;
if (result_type === FORMULA_RESULT_TYPE.STRING) {
return _computedCellValue || '';
}
if (result_type === FORMULA_RESULT_TYPE.BOOL) {
if (typeof _computedCellValue === 'boolean') return String(_computedCellValue);
return _computedCellValue === 'true' ? 'true' : 'false';
}
if (result_type === FORMULA_RESULT_TYPE.NUMBER) {
// data is stored in the data of the current column
return getNumberDisplayString(_computedCellValue, data);
}
if (result_type === FORMULA_RESULT_TYPE.DATE) {
// data is stored in the data of the current column
var _format = data.format;
return getDateDisplayString(_computedCellValue, _format);
}
// FORMULA_RESULT_TYPE.ARRAY
return _getArrayColumnCellValue(_computedCellValue, column, {
collaborators: collaborators,
isArchiveView: isArchiveView
});
}
case CellType.FILE:
case CellType.IMAGE:
case CellType.DIGITAL_SIGN:
case CellType.BUTTON:
{
return '';
}
default:
{
return cellValue ? String(cellValue) : '';
}
}
};
export { getCellValueDisplayString, getCellValueStringResult, getFormulaDisplayString };