UNPKG

dtable-utils

Version:

dtable common utils

119 lines (113 loc) 5.63 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var core = require('./core.js'); var checkbox = require('./sort-column/checkbox.js'); var collaborator$1 = require('./sort-column/collaborator.js'); var date = require('./sort-column/date.js'); var collaborator = require('../cell-value-get/collaborator.js'); require('../cell-value-get/cell-value.js'); var number = require('./sort-column/number.js'); var text = require('./sort-column/text.js'); var cellType = require('../constants/cell-type.js'); var column = require('../constants/column.js'); var formula = require('../constants/formula.js'); var formula$1 = require('./sort-column/formula.js'); var link = require('./sort-column/link.js'); var department = require('./sort-column/department.js'); var multipleSelect = require('./sort-column/multiple-select.js'); var singleSelect = require('./sort-column/single-select.js'); var sort = require('../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$1) { var column_key = sort$1.column_key, sort_type = sort$1.sort_type, column$1 = sort$1.column; var columnType = column$1.type, columnData = column$1.data; var currCellVal = currentRow[column_key]; var nextCellVal = nextRow[column_key]; if (column.DATE_COLUMN_OPTIONS.includes(columnType)) { initValue = initValue || date.sortDate(currCellVal, nextCellVal, sort_type); } else if (columnType === cellType.CellType.SINGLE_SELECT) { initValue = initValue || singleSelect.sortSingleSelect(currCellVal, nextCellVal, sort$1); } else if (columnType === cellType.CellType.DEPARTMENT_SINGLE_SELECT) { initValue = initValue || department.sortDepartment(currCellVal, nextCellVal, sort_type); } else if (sort.NUMBER_SORTER_COLUMN_TYPES.includes(columnType)) { initValue = initValue || number.sortNumber(currCellVal, nextCellVal, sort_type); } else if (formula.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 || formula$1.sortFormula(currCellVal, nextCellVal, sort_type, { columnData: columnData, value: value }); } else if (columnType === cellType.CellType.COLLABORATOR) { var collaborators = value.collaborators; var currValidCollaborators = currCellVal; var nextValidCollaborators = nextCellVal; if (collaborators) { currValidCollaborators = collaborator.getCollaboratorsNames(currCellVal, collaborators); nextValidCollaborators = collaborator.getCollaboratorsNames(nextCellVal, collaborators); } initValue = initValue || collaborator$1.sortCollaborator(currValidCollaborators, nextValidCollaborators, sort_type); } else if (columnType === cellType.CellType.CHECKBOX) { initValue = initValue || checkbox.sortCheckbox(currCellVal, nextCellVal, sort_type); } else if (columnType === cellType.CellType.MULTIPLE_SELECT) { initValue = initValue || multipleSelect.sortMultipleSelect(currCellVal, nextCellVal, sort$1); } else if (columnType === cellType.CellType.LINK) { var _currentFormulaRow = formulaRows[currentRow._id] || {}; var _nextFormulaRow = formulaRows[nextRow._id] || {}; currCellVal = _currentFormulaRow[column_key]; nextCellVal = _nextFormulaRow[column_key]; initValue = initValue || link.sortLink(currCellVal, nextCellVal, sort_type, { columnData: columnData, value: value }); } else { initValue = initValue || text.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 = core.deleteInvalidSort(sorts, columns); var normalizedFormulaRows = formulaRows || {}; sortRowsWithMultiSorts(sortRows, validSorts, { formulaRows: normalizedFormulaRows, value: value }); return sortRows.map(function (row) { return row._id; }); }; exports.sortRowsWithMultiSorts = sortRowsWithMultiSorts; exports.sortTableRows = sortTableRows;