63 lines
1.9 KiB
JavaScript
63 lines
1.9 KiB
JavaScript
import i18n from "@/i18n";
|
|
|
|
const normalizeDepartmentLabel = (label) => {
|
|
if (typeof label !== "string") {
|
|
return "";
|
|
}
|
|
|
|
return label.trim().toLocaleLowerCase();
|
|
};
|
|
|
|
export const isDepartmentLabelValid = (label) => {
|
|
const normalizedLabel = normalizeDepartmentLabel(label);
|
|
if (!normalizedLabel) {
|
|
return false;
|
|
}
|
|
|
|
const noDataLabel = normalizeDepartmentLabel(String(i18n.global.t("global.no_data") || ""));
|
|
return normalizedLabel !== noDataLabel;
|
|
};
|
|
|
|
export const hasValidDepartmentName = (department) => {
|
|
return isDepartmentLabelValid(department?.name);
|
|
};
|
|
|
|
export const isDepartmentVisible = (department) => {
|
|
return !(
|
|
department?.visible === false
|
|
|| department?.visible === 0
|
|
|| department?.visible === "0"
|
|
|| department?.visible === "false"
|
|
);
|
|
};
|
|
|
|
export const isAccessibleVisibleDepartment = (department, canAccessDepartment = () => true) => {
|
|
return Boolean(department) && canAccessDepartment(department.id) && isDepartmentVisible(department);
|
|
};
|
|
|
|
export const isAccessibleVisibleNamedDepartment = (department, canAccessDepartment = () => true) => {
|
|
return isAccessibleVisibleDepartment(department, canAccessDepartment) && hasValidDepartmentName(department);
|
|
};
|
|
|
|
const normalizeDepartmentPriorityOrder = (department) => {
|
|
const parsedPriorityOrder = Number.parseInt(
|
|
String(department?.priority_order ?? department?.priorityOrder ?? ""),
|
|
10
|
|
);
|
|
|
|
return Number.isInteger(parsedPriorityOrder) ? parsedPriorityOrder : Number.POSITIVE_INFINITY;
|
|
};
|
|
|
|
export const sortByDepartmentPriorityOrder = (items = []) => {
|
|
return [...items].sort((left, right) => {
|
|
const leftPriorityOrder = normalizeDepartmentPriorityOrder(left);
|
|
const rightPriorityOrder = normalizeDepartmentPriorityOrder(right);
|
|
|
|
if (leftPriorityOrder !== rightPriorityOrder) {
|
|
return leftPriorityOrder - rightPriorityOrder;
|
|
}
|
|
|
|
return 0;
|
|
});
|
|
};
|