dtable-utils
Advanced tools
Comparing version 4.4.3-alpha1 to 4.4.3-alpha2
@@ -19,3 +19,3 @@ export { CellType } from './constants/cell-type.js'; | ||
export { getRowById, getRowsByIds } from './table/row.js'; | ||
export { isTableRows } from './row/core.js'; | ||
export { getRowDefaultData, isTableRows, updateTableRowsWithRowsData } from './row/core.js'; | ||
export { convertRow, convertRowBack } from './row/convert.js'; | ||
@@ -22,0 +22,0 @@ export { getLinkColumnsUsedInFilters, getNonArchiveViews, getNonPrivateViews, getViewById, getViewByName, getViewShownColumns, isArchiveView, isDefaultView, isFilterView, isGroupView, isHiddenColumnsView, isSortView } from './view/core.js'; |
import _typeof from '@babel/runtime/helpers/typeof'; | ||
import '../constants/column.js'; | ||
import { getTableById } from '../table/core.js'; | ||
import { CellType } from '../constants/cell-type.js'; | ||
import { FILL_DEFAULT_VALUE_COLUMNS_TYPE, DATE_DEFAULT_TYPES } from '../constants/column.js'; | ||
import '../constants/filter/filter-column-options.js'; | ||
@@ -10,3 +12,3 @@ import '../constants/filter/filter-modifier.js'; | ||
import '../constants/group.js'; | ||
import '../date.js'; | ||
import { DateUtils } from '../date.js'; | ||
@@ -21,3 +23,123 @@ /** | ||
}; | ||
var updateTableRowsWithRowsData = function updateTableRowsWithRowsData(tables, tableId) { | ||
var rowsData = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; | ||
var table = getTableById(tables, tableId); | ||
var idRowDataMap = {}; | ||
rowsData.forEach(function (rowData) { | ||
return idRowDataMap[rowData._id] = rowData; | ||
}); | ||
table.rows.forEach(function (row, index) { | ||
var rowId = row._id; | ||
var newRowData = idRowDataMap[rowId]; | ||
if (!newRowData) { | ||
return; | ||
} | ||
var newRow = Object.assign({}, row, newRowData); | ||
table.rows[index] = newRow; | ||
table.id_row_map[rowId] = newRow; | ||
}); | ||
}; | ||
var getRowDefaultData = function getRowDefaultData(table, collaborators, _ref) { | ||
var username = _ref.username, | ||
userId = _ref.userId, | ||
userDepartmentIdsMap = _ref.userDepartmentIdsMap; | ||
var columns = table.columns; | ||
var defaultValueData = {}; | ||
columns.forEach(function (column) { | ||
var columnType = column.type, | ||
columnData = column.data; | ||
if (!FILL_DEFAULT_VALUE_COLUMNS_TYPE.includes(columnType)) return; | ||
var enable_fill_default_value = columnData && columnData.enable_fill_default_value; | ||
if (!enable_fill_default_value) return; | ||
var defaultValue = columnData.default_value; | ||
if (columnType === CellType.DATE) { | ||
var defaultValueType = columnData.default_date_type, | ||
dateFormat = columnData.format; | ||
var showHourAndMinute = dateFormat.indexOf('HH:mm'); | ||
var normalFormat = showHourAndMinute > -1 ? 'YYYY-MM-DD HH:mm' : 'YYYY-MM-DD'; | ||
switch (defaultValueType) { | ||
case DATE_DEFAULT_TYPES.SPECIFIC_DATE: | ||
{ | ||
var validDate = DateUtils.getValidDate(defaultValue); | ||
if (!validDate) break; | ||
defaultValueData[column.key] = DateUtils.format(defaultValue, normalFormat); | ||
break; | ||
} | ||
case DATE_DEFAULT_TYPES.CURRENT_DATE: | ||
{ | ||
var date = new Date(); | ||
var today = DateUtils.format(date, normalFormat); | ||
defaultValueData[column.key] = today; | ||
break; | ||
} | ||
case DATE_DEFAULT_TYPES.DAYS_BEFORE: | ||
case DATE_DEFAULT_TYPES.DAYS_AFTER: | ||
{ | ||
var num = Number(defaultValue); | ||
if (!Number.isInteger(num)) { | ||
break; | ||
} | ||
var _date = new Date(); | ||
var year = _date.getFullYear(); | ||
var month = _date.getMonth(); | ||
var day = _date.getDate(); | ||
var hours = _date.getHours(); | ||
var minutes = _date.getMinutes(); | ||
var numberOfDaysFromNow; | ||
if (defaultValueType === DATE_DEFAULT_TYPES.DAYS_AFTER) { | ||
numberOfDaysFromNow = new Date(year, month, day + num, hours, minutes); | ||
} else if (defaultValueType === DATE_DEFAULT_TYPES.DAYS_BEFORE) { | ||
numberOfDaysFromNow = new Date(year, month, day - num, hours, minutes); | ||
} | ||
var _validDate = DateUtils.format(numberOfDaysFromNow, normalFormat); | ||
defaultValueData[column.key] = _validDate; | ||
break; | ||
} | ||
} | ||
} else if (columnType === CellType.COLLABORATOR) { | ||
var default_collaborator_type = columnData.default_collaborator_type; | ||
if (default_collaborator_type === 'current_user') { | ||
defaultValueData[column.key] = [username]; | ||
} else { | ||
defaultValueData[column.key] = defaultValue; | ||
} | ||
} else if (columnType === CellType.DEPARTMENT_SINGLE_SELECT) { | ||
if (defaultValue === 'current_user_department') { | ||
var currentUserDepartmentIds = userDepartmentIdsMap && userDepartmentIdsMap.current_user_department_ids; | ||
if (!Array.isArray(currentUserDepartmentIds) || currentUserDepartmentIds.length === 0) { | ||
defaultValueData[column.key] = null; | ||
return; | ||
} | ||
var currentUserDepartmentId = currentUserDepartmentIds[0]; | ||
if (typeof currentUserDepartmentId !== 'number') { | ||
defaultValueData[column.key] = null; | ||
return; | ||
} | ||
defaultValueData[column.key] = currentUserDepartmentId; | ||
} | ||
} else if (defaultValue || defaultValue === 0) { | ||
if (columnType === CellType.TEXT) { | ||
defaultValueData[column.key] = defaultValue.replace(/\{[^\}]+\}/ig, function (specialVariable) { | ||
var collaborator = collaborators.find(function (item) { | ||
return item.email === username; | ||
}); | ||
if (specialVariable.toLocaleLowerCase() === '{creator.name}') { | ||
if (collaborator) { | ||
return collaborator.name; | ||
} | ||
} else if (specialVariable.toLocaleLowerCase() === '{creator.id}') { | ||
if (userId) { | ||
return userId; | ||
} | ||
} | ||
return ''; | ||
}); | ||
} else { | ||
defaultValueData[column.key] = defaultValue; | ||
} | ||
} | ||
}); | ||
return defaultValueData; | ||
}; | ||
export { isTableRows }; | ||
export { getRowDefaultData, isTableRows, updateTableRowsWithRowsData }; |
@@ -165,3 +165,5 @@ 'use strict'; | ||
exports.getRowsByIds = row.getRowsByIds; | ||
exports.getRowDefaultData = core$1.getRowDefaultData; | ||
exports.isTableRows = core$1.isTableRows; | ||
exports.updateTableRowsWithRowsData = core$1.updateTableRowsWithRowsData; | ||
exports.convertRow = convert.convertRow; | ||
@@ -168,0 +170,0 @@ exports.convertRowBack = convert.convertRowBack; |
@@ -6,3 +6,5 @@ 'use strict'; | ||
var _typeof = require('@babel/runtime/helpers/typeof'); | ||
require('../constants/column.js'); | ||
var core = require('../table/core.js'); | ||
var cellType = require('../constants/cell-type.js'); | ||
var column = require('../constants/column.js'); | ||
require('../constants/filter/filter-column-options.js'); | ||
@@ -15,3 +17,3 @@ require('../constants/filter/filter-modifier.js'); | ||
require('../constants/group.js'); | ||
require('../date.js'); | ||
var date = require('../date.js'); | ||
@@ -30,3 +32,125 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
}; | ||
var updateTableRowsWithRowsData = function updateTableRowsWithRowsData(tables, tableId) { | ||
var rowsData = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; | ||
var table = core.getTableById(tables, tableId); | ||
var idRowDataMap = {}; | ||
rowsData.forEach(function (rowData) { | ||
return idRowDataMap[rowData._id] = rowData; | ||
}); | ||
table.rows.forEach(function (row, index) { | ||
var rowId = row._id; | ||
var newRowData = idRowDataMap[rowId]; | ||
if (!newRowData) { | ||
return; | ||
} | ||
var newRow = Object.assign({}, row, newRowData); | ||
table.rows[index] = newRow; | ||
table.id_row_map[rowId] = newRow; | ||
}); | ||
}; | ||
var getRowDefaultData = function getRowDefaultData(table, collaborators, _ref) { | ||
var username = _ref.username, | ||
userId = _ref.userId, | ||
userDepartmentIdsMap = _ref.userDepartmentIdsMap; | ||
var columns = table.columns; | ||
var defaultValueData = {}; | ||
columns.forEach(function (column$1) { | ||
var columnType = column$1.type, | ||
columnData = column$1.data; | ||
if (!column.FILL_DEFAULT_VALUE_COLUMNS_TYPE.includes(columnType)) return; | ||
var enable_fill_default_value = columnData && columnData.enable_fill_default_value; | ||
if (!enable_fill_default_value) return; | ||
var defaultValue = columnData.default_value; | ||
if (columnType === cellType.CellType.DATE) { | ||
var defaultValueType = columnData.default_date_type, | ||
dateFormat = columnData.format; | ||
var showHourAndMinute = dateFormat.indexOf('HH:mm'); | ||
var normalFormat = showHourAndMinute > -1 ? 'YYYY-MM-DD HH:mm' : 'YYYY-MM-DD'; | ||
switch (defaultValueType) { | ||
case column.DATE_DEFAULT_TYPES.SPECIFIC_DATE: | ||
{ | ||
var validDate = date.DateUtils.getValidDate(defaultValue); | ||
if (!validDate) break; | ||
defaultValueData[column$1.key] = date.DateUtils.format(defaultValue, normalFormat); | ||
break; | ||
} | ||
case column.DATE_DEFAULT_TYPES.CURRENT_DATE: | ||
{ | ||
var date$1 = new Date(); | ||
var today = date.DateUtils.format(date$1, normalFormat); | ||
defaultValueData[column$1.key] = today; | ||
break; | ||
} | ||
case column.DATE_DEFAULT_TYPES.DAYS_BEFORE: | ||
case column.DATE_DEFAULT_TYPES.DAYS_AFTER: | ||
{ | ||
var num = Number(defaultValue); | ||
if (!Number.isInteger(num)) { | ||
break; | ||
} | ||
var _date = new Date(); | ||
var year = _date.getFullYear(); | ||
var month = _date.getMonth(); | ||
var day = _date.getDate(); | ||
var hours = _date.getHours(); | ||
var minutes = _date.getMinutes(); | ||
var numberOfDaysFromNow; | ||
if (defaultValueType === column.DATE_DEFAULT_TYPES.DAYS_AFTER) { | ||
numberOfDaysFromNow = new Date(year, month, day + num, hours, minutes); | ||
} else if (defaultValueType === column.DATE_DEFAULT_TYPES.DAYS_BEFORE) { | ||
numberOfDaysFromNow = new Date(year, month, day - num, hours, minutes); | ||
} | ||
var _validDate = date.DateUtils.format(numberOfDaysFromNow, normalFormat); | ||
defaultValueData[column$1.key] = _validDate; | ||
break; | ||
} | ||
} | ||
} else if (columnType === cellType.CellType.COLLABORATOR) { | ||
var default_collaborator_type = columnData.default_collaborator_type; | ||
if (default_collaborator_type === 'current_user') { | ||
defaultValueData[column$1.key] = [username]; | ||
} else { | ||
defaultValueData[column$1.key] = defaultValue; | ||
} | ||
} else if (columnType === cellType.CellType.DEPARTMENT_SINGLE_SELECT) { | ||
if (defaultValue === 'current_user_department') { | ||
var currentUserDepartmentIds = userDepartmentIdsMap && userDepartmentIdsMap.current_user_department_ids; | ||
if (!Array.isArray(currentUserDepartmentIds) || currentUserDepartmentIds.length === 0) { | ||
defaultValueData[column$1.key] = null; | ||
return; | ||
} | ||
var currentUserDepartmentId = currentUserDepartmentIds[0]; | ||
if (typeof currentUserDepartmentId !== 'number') { | ||
defaultValueData[column$1.key] = null; | ||
return; | ||
} | ||
defaultValueData[column$1.key] = currentUserDepartmentId; | ||
} | ||
} else if (defaultValue || defaultValue === 0) { | ||
if (columnType === cellType.CellType.TEXT) { | ||
defaultValueData[column$1.key] = defaultValue.replace(/\{[^\}]+\}/ig, function (specialVariable) { | ||
var collaborator = collaborators.find(function (item) { | ||
return item.email === username; | ||
}); | ||
if (specialVariable.toLocaleLowerCase() === '{creator.name}') { | ||
if (collaborator) { | ||
return collaborator.name; | ||
} | ||
} else if (specialVariable.toLocaleLowerCase() === '{creator.id}') { | ||
if (userId) { | ||
return userId; | ||
} | ||
} | ||
return ''; | ||
}); | ||
} else { | ||
defaultValueData[column$1.key] = defaultValue; | ||
} | ||
} | ||
}); | ||
return defaultValueData; | ||
}; | ||
exports.getRowDefaultData = getRowDefaultData; | ||
exports.isTableRows = isTableRows; | ||
exports.updateTableRowsWithRowsData = updateTableRowsWithRowsData; |
{ | ||
"name": "dtable-utils", | ||
"version": "4.4.3-alpha1", | ||
"version": "4.4.3-alpha2", | ||
"description": "dtable common utils", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
Sorry, the diff of this file is too big to display
874203
19268