UNPKG

synapse-react-client

Version:

[![npm version](https://badge.fury.io/js/synapse-react-client.svg)](https://badge.fury.io/js/synapse-react-client) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettie

236 lines (235 loc) 6.37 kB
import { ColumnTypeEnum as i } from "@sage-bionetworks/synapse-types"; import { atomWithReducer as C } from "jotai/utils"; import { cloneDeep as c } from "lodash-es"; import { findMatchingColumnModel as b, isFormDataValueSmallerThanRecommendedValue as t, DEFAULT_STRING_SIZE as r, canHaveSize as S, canHaveMaxListLength as j, configureFacetsForType as M } from "./TableColumnSchemaEditorUtils.js"; function g(l) { return l.length > 0 && l.every( (n) => ( // Is selected n.isSelected && // And all subcolumns are selected (true if there are no subcolumns) (n.jsonSubColumns ?? []).every( (e) => e.isSelected ) ) ); } function N(l) { return l.reduce((n, e) => (e.isSelected && (n += 1), e.jsonSubColumns && e.jsonSubColumns.forEach((o) => { o.isSelected && (n += 1); }), n), 0); } function x() { return { name: "", columnType: i.STRING, maximumSize: r, isOriginallyDefaultColumn: !1, isSelected: !1 }; } function h() { return { name: "", jsonPath: "", columnType: i.STRING, facetType: "enumeration", isSelected: !1 }; } function f(l, n) { const e = [...l]; return e.forEach((o, u) => { if ( // Cannot move index 0 up u > 0 && // Only move if the predicate matches n(o) && // If the previous item should also move, skip the swap !n(e[u - 1]) ) { const s = e[u - 1]; e[u - 1] = e[u], e[u] = s; } }), e; } function a(l, n) { const e = [...l]; for (let o = e.length - 1; o >= 0; o--) { const u = e[o]; if ( // Cannot move the last item down o < e.length - 1 && // Only move if the predicate matches n(u) && // If the next item should also be moved, don't swap !n(e[o + 1]) ) { const s = e[o]; e[o] = e[o + 1], e[o + 1] = s; } } return e; } function I(l, n) { const { annotationColumnModels: e } = l; return n.map((o) => { const u = e ? b(e, o) : void 0; return u == null || (t( o.maximumSize, u.maximumSize ) && (o = { ...o, maximumSize: u.maximumSize }), t( o.maximumListLength, u.maximumListLength ) && (o = { ...o, maximumListLength: u.maximumListLength })), o; }); } function y(l, n) { const { columnModelIndex: e, jsonSubColumnModelIndex: o, newColumnType: u } = l; let s; if (n && n[e]) n[e].jsonSubColumns && o !== void 0 ? s = c( n[e].jsonSubColumns[o] ) : s = c(n[e]); else throw new Error( "Cannot change column model type for a column that does not exist" ); s.columnType = u, !S(u) && "maximumSize" in s && delete s.maximumSize, !j(u) && "maximumListLength" in s && delete s.maximumListLength, "enumValues" in s && delete s.enumValues, "defaultValue" in s && delete s.defaultValue; const m = M( u, !!o ); "facetType" in s && (m === null || !m.includes(s.facetType)) && delete s.facetType, "jsonSubColumns" in s && u !== i.JSON && delete s.jsonSubColumns, n && n[e] && (n[e].jsonSubColumns && o !== void 0 ? n[e].jsonSubColumns[o] = s : n[e] = s); } function T(l) { return g(l) ? l.map((e) => ({ ...e, jsonSubColumns: e.jsonSubColumns?.map((o) => ({ ...o, isSelected: !1 })), isSelected: !1 })) : l.map((e) => ({ ...e, jsonSubColumns: e.jsonSubColumns?.map((o) => ({ ...o, isSelected: !0 })), isSelected: !0 })); } function L(l, n) { const { columnModelIndex: e, jsonSubColumnModelIndex: o, value: u } = l; n && n[e] && (n[e].jsonSubColumns && o !== void 0 ? n[e].jsonSubColumns[o] = u : n[e] = u); } function w(l, n) { const { columnModelIndex: e, jsonSubColumnModelIndex: o } = l; if (n && n[e]) if (n[e].jsonSubColumns && o !== void 0) { const u = n[e].jsonSubColumns[o]; n[e].jsonSubColumns[o] = { ...u, isSelected: !u.isSelected }; } else { const u = n[e]; n[e] = { ...u, isSelected: !u.isSelected }; } } function k(l) { return l.filter((n) => !n.isSelected).map((n) => (n.jsonSubColumns && (n = { ...n, jsonSubColumns: n.jsonSubColumns.filter( (e) => !e.isSelected ) }), n)); } function V(l, n, e) { if (n == e) return l; const o = [...l], u = o[n]; return o.splice(n, 1), o.splice(e, 0, u), o; } function A(l, n) { const { from: e, to: o } = l, u = e.jsonSubColumnModelIndex !== void 0 ? n[e.columnModelIndex].jsonSubColumns : n, s = e.jsonSubColumnModelIndex !== void 0 ? e.jsonSubColumnModelIndex : e.columnModelIndex, m = o.jsonSubColumnModelIndex !== void 0 ? o.jsonSubColumnModelIndex : o.columnModelIndex, d = V( u, s, m ); return e.jsonSubColumnModelIndex !== void 0 ? (n[e.columnModelIndex].jsonSubColumns = d, n) : d; } function z(l, n) { switch (n.type) { case "setValue": l = n.value; break; case "toggleSelectAll": { l = T(l); break; } case "setColumnModelValue": { L(n, l); break; } case "changeColumnModelType": { y(n, l); break; } case "appendColumn": l.push(x()); break; case "appendJsonSubColumn": { const { columnModelIndex: e } = n; l[e] = { ...l[e], jsonSubColumns: [ ...l[e].jsonSubColumns ?? [], h() ] }; break; } case "toggleSelect": { w(n, l); break; } case "delete": l = k(l); break; case "move": { l = A(n, l); break; } case "moveUp": l = f(l, (e) => e.isSelected), l.forEach((e) => { e.jsonSubColumns && (e.jsonSubColumns = f( e.jsonSubColumns, (o) => o.isSelected )); }); break; case "moveDown": l = a(l, (e) => e.isSelected), l.forEach((e) => { e.jsonSubColumns && (e.jsonSubColumns = a( e.jsonSubColumns, (o) => o.isSelected )); }); break; case "updateColumnSizesToRecommendedValues": l = I(n, l); break; default: throw new Error("Unexpected action", n); } return [...l]; } const U = C([], z); export { x as getDefaultColumnModelFormData, h as getDefaultJsonSubColumnFormData, g as getIsAllSelected, N as getNumberOfSelectedItems, z as reducer, U as tableColumnSchemaFormDataAtom }; //# sourceMappingURL=TableColumnSchemaFormReducer.js.map