UNPKG

dtable-utils

Version:

dtable common utils

118 lines (115 loc) 5.87 kB
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray'; import { DEPARTMENT_SELECT_RANGE_MAP } from '../../constants/column.js'; import { FILTER_PREDICATE_TYPE } from '../../constants/filter/filter-predicate.js'; import { isCurrentUserDepartmentAndSub, isSpecificDepartment, isCurrentUserDepartment } from '../core.js'; /** * Filter multiple-select * @param {array} departmentIds e.g. [ 14, ... ] * @param {string} filter_predicate * @param {array} filter_term department ids * @param {object} userDepartmentIdsMap * @returns bool */ var departmentMultipleSelectFilter = function departmentMultipleSelectFilter(departmentIds, _ref, userDepartmentIdsMap) { var filter_predicate = _ref.filter_predicate, filter_term = _ref.filter_term; switch (filter_predicate) { case FILTER_PREDICATE_TYPE.HAS_ANY_OF: { if (filter_term.length === 0) return true; if (!Array.isArray(departmentIds)) return false; // CURRENT_USER_DEPARTMENT is a subset of CURRENT_USER_DEPARTMENT_AND_SUB, so it is necessary to first determine CURRENT_USER_DEPARTMENT_AND_SUB. if (filter_term.includes(DEPARTMENT_SELECT_RANGE_MAP.CURRENT_USER_DEPARTMENT_AND_SUB)) { return departmentIds.some(function (id) { return isCurrentUserDepartmentAndSub(id, userDepartmentIdsMap) || isSpecificDepartment(id, filter_term); }); } if (filter_term.includes(DEPARTMENT_SELECT_RANGE_MAP.CURRENT_USER_DEPARTMENT)) { return departmentIds.some(function (id) { return isCurrentUserDepartment(id, userDepartmentIdsMap) || isSpecificDepartment(id, filter_term); }); } return departmentIds.some(function (id) { return isSpecificDepartment(id, filter_term); }); } case FILTER_PREDICATE_TYPE.HAS_ALL_OF: { if (filter_term.length === 0) return true; if (!Array.isArray(departmentIds)) return false; var _ref2 = userDepartmentIdsMap || {}, current_user_department_and_sub_ids = _ref2.current_user_department_and_sub_ids, current_user_department_ids = _ref2.current_user_department_ids; if (filter_term.includes(DEPARTMENT_SELECT_RANGE_MAP.CURRENT_USER_DEPARTMENT_AND_SUB)) { if (Array.isArray(current_user_department_and_sub_ids) && !current_user_department_and_sub_ids.every(function (id) { return departmentIds.includes(id); })) return false; } else if (filter_term.includes(DEPARTMENT_SELECT_RANGE_MAP.CURRENT_USER_DEPARTMENT)) { if (Array.isArray(current_user_department_ids) && !current_user_department_ids.every(function (id) { return departmentIds.includes(id); })) return false; } return filter_term.every(function (id) { return id === DEPARTMENT_SELECT_RANGE_MAP.CURRENT_USER_DEPARTMENT || id === DEPARTMENT_SELECT_RANGE_MAP.CURRENT_USER_DEPARTMENT_AND_SUB || isSpecificDepartment(id, departmentIds); }); } case FILTER_PREDICATE_TYPE.HAS_NONE_OF: { if (filter_term.length === 0 || !Array.isArray(departmentIds) || departmentIds.length === 0) { return true; } if (filter_term.includes(DEPARTMENT_SELECT_RANGE_MAP.CURRENT_USER_DEPARTMENT_AND_SUB)) { return departmentIds.every(function (id) { return !isCurrentUserDepartmentAndSub(id, userDepartmentIdsMap) && !isSpecificDepartment(id, filter_term); }); } if (filter_term.includes(DEPARTMENT_SELECT_RANGE_MAP.CURRENT_USER_DEPARTMENT)) { return departmentIds.every(function (id) { return !isCurrentUserDepartment(id, userDepartmentIdsMap) && !isSpecificDepartment(id, filter_term); }); } return filter_term.every(function (departmentId) { return !isSpecificDepartment(departmentId, departmentIds); }); } case FILTER_PREDICATE_TYPE.IS_EXACTLY: { if (filter_term.length === 0) return true; if (!Array.isArray(departmentIds)) return false; var normalizedFilterTerm = filter_term.slice(0); if (filter_term.includes(DEPARTMENT_SELECT_RANGE_MAP.CURRENT_USER_DEPARTMENT_AND_SUB)) { var index = normalizedFilterTerm.indexOf(DEPARTMENT_SELECT_RANGE_MAP.CURRENT_USER_DEPARTMENT_AND_SUB); var _ref3 = userDepartmentIdsMap || {}, _current_user_department_and_sub_ids = _ref3.current_user_department_and_sub_ids; if (Array.isArray(_current_user_department_and_sub_ids)) { normalizedFilterTerm.splice.apply(normalizedFilterTerm, [index, 1].concat(_toConsumableArray(_current_user_department_and_sub_ids))); } } if (filter_term.includes(DEPARTMENT_SELECT_RANGE_MAP.CURRENT_USER_DEPARTMENT)) { var _index = normalizedFilterTerm.indexOf(DEPARTMENT_SELECT_RANGE_MAP.CURRENT_USER_DEPARTMENT); var _ref4 = userDepartmentIdsMap || {}, _current_user_department_ids = _ref4.current_user_department_ids; if (Array.isArray(_current_user_department_ids)) { normalizedFilterTerm.splice.apply(normalizedFilterTerm, [_index, 1].concat(_toConsumableArray(_current_user_department_ids))); } } var uniqueArr = function uniqueArr(arr) { return _toConsumableArray(new Set(arr)).sort(); }; return uniqueArr(departmentIds).toString() === uniqueArr(normalizedFilterTerm).toString(); } case FILTER_PREDICATE_TYPE.EMPTY: { return !Array.isArray(departmentIds) || departmentIds.length === 0; } case FILTER_PREDICATE_TYPE.NOT_EMPTY: { return Array.isArray(departmentIds) && departmentIds.length > 0; } default: { return false; } } }; export { departmentMultipleSelectFilter };