dtable-utils
Version:
dtable common utils
114 lines (110 loc) • 5.41 kB
JavaScript
import { deleteInvalidSort } from './core.js';
import { sortCheckbox } from './sort-column/checkbox.js';
import { sortCollaborator } from './sort-column/collaborator.js';
import { sortDate } from './sort-column/date.js';
import { getCollaboratorsNames } from '../cell-value-get/collaborator.js';
import '../cell-value-get/cell-value.js';
import { sortNumber } from './sort-column/number.js';
import { sortText } from './sort-column/text.js';
import { CellType } from '../constants/cell-type.js';
import { DATE_COLUMN_OPTIONS } from '../constants/column.js';
import { FORMULA_COLUMN_TYPES_MAP } from '../constants/formula.js';
import { sortFormula } from './sort-column/formula.js';
import { sortLink } from './sort-column/link.js';
import { sortDepartment } from './sort-column/department.js';
import { sortMultipleSelect } from './sort-column/multiple-select.js';
import { sortSingleSelect } from './sort-column/single-select.js';
import { NUMBER_SORTER_COLUMN_TYPES } from '../constants/sort.js';
/**
* Sort rows with multiple sorts
* @param {array} tableRows e.g. [{ _id, [column.key]: '', ...}, ...]
* @param {array} sorts e.g. [{ column_key, sort_type, column, ... }, ...]
* @param {object} formulaRows computed value of formula, link-formula, link etc.
* @param {object} value e.g. { collaborators, ... }
*/
var sortRowsWithMultiSorts = function sortRowsWithMultiSorts(tableRows, sorts, _ref) {
var _ref$formulaRows = _ref.formulaRows,
formulaRows = _ref$formulaRows === void 0 ? {} : _ref$formulaRows,
value = _ref.value;
tableRows.sort(function (currentRow, nextRow) {
var initValue = 0;
sorts.forEach(function (sort) {
var column_key = sort.column_key,
sort_type = sort.sort_type,
column = sort.column;
var columnType = column.type,
columnData = column.data;
var currCellVal = currentRow[column_key];
var nextCellVal = nextRow[column_key];
if (DATE_COLUMN_OPTIONS.includes(columnType)) {
initValue = initValue || sortDate(currCellVal, nextCellVal, sort_type);
} else if (columnType === CellType.SINGLE_SELECT) {
initValue = initValue || sortSingleSelect(currCellVal, nextCellVal, sort);
} else if (columnType === CellType.DEPARTMENT_SINGLE_SELECT) {
initValue = initValue || sortDepartment(currCellVal, nextCellVal, sort_type);
} else if (NUMBER_SORTER_COLUMN_TYPES.includes(columnType)) {
initValue = initValue || sortNumber(currCellVal, nextCellVal, sort_type);
} else if (FORMULA_COLUMN_TYPES_MAP[columnType]) {
var currentFormulaRow = formulaRows[currentRow._id] || {};
var nextFormulaRow = formulaRows[nextRow._id] || {};
currCellVal = currentFormulaRow[column_key];
nextCellVal = nextFormulaRow[column_key];
initValue = initValue || sortFormula(currCellVal, nextCellVal, sort_type, {
columnData: columnData,
value: value
});
} else if (columnType === CellType.COLLABORATOR) {
var collaborators = value.collaborators;
var currValidCollaborators = currCellVal;
var nextValidCollaborators = nextCellVal;
if (collaborators) {
currValidCollaborators = getCollaboratorsNames(currCellVal, collaborators);
nextValidCollaborators = getCollaboratorsNames(nextCellVal, collaborators);
}
initValue = initValue || sortCollaborator(currValidCollaborators, nextValidCollaborators, sort_type);
} else if (columnType === CellType.CHECKBOX) {
initValue = initValue || sortCheckbox(currCellVal, nextCellVal, sort_type);
} else if (columnType === CellType.MULTIPLE_SELECT) {
initValue = initValue || sortMultipleSelect(currCellVal, nextCellVal, sort);
} else if (columnType === CellType.LINK) {
var _currentFormulaRow = formulaRows[currentRow._id] || {};
var _nextFormulaRow = formulaRows[nextRow._id] || {};
currCellVal = _currentFormulaRow[column_key];
nextCellVal = _nextFormulaRow[column_key];
initValue = initValue || sortLink(currCellVal, nextCellVal, sort_type, {
columnData: columnData,
value: value
});
} else {
initValue = initValue || sortText(currCellVal, nextCellVal, sort_type);
}
});
return initValue;
});
};
/**
* Get sorted rows ids from table rows with multiple sorts
* @param {array} sorts e.g. [{ column_key, sort_type, column, ... }, ...]
* @param {array} rows e.g. [{ _id, [column.key]: '', ...}, ...]
* @param {array} columns e.g. [{ key, type, ... }, ...]
* @param {object} formulaRows computed value of formula, link-formula, link etc.
* @param {object} value e.g. { collaborators, ... }
* @returns sorted rows ids, array
*/
var sortTableRows = function sortTableRows(sorts, rows, columns) {
var _ref2 = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {},
formulaRows = _ref2.formulaRows,
value = _ref2.value;
if (!Array.isArray(rows) || rows.length === 0) return [];
var sortRows = rows.slice(0);
var validSorts = deleteInvalidSort(sorts, columns);
var normalizedFormulaRows = formulaRows || {};
sortRowsWithMultiSorts(sortRows, validSorts, {
formulaRows: normalizedFormulaRows,
value: value
});
return sortRows.map(function (row) {
return row._id;
});
};
export { sortRowsWithMultiSorts, sortTableRows };