-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
586 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,92 @@ | ||
import { Row } from "@tanstack/react-table"; | ||
import { RowDataType } from "cdm/FolderModel"; | ||
import { FooterType } from "helpers/Constants"; | ||
|
||
export default class Footer { | ||
constructor(public readonly rows: Row<RowDataType>[]) { } | ||
|
||
public dispatch(footerType: string, key: string): string { | ||
let footerInfo: string; | ||
try { | ||
switch (footerType) { | ||
case FooterType.COUNT_UNIQUE: | ||
footerInfo = this.countUnique(key); | ||
break; | ||
case FooterType.COUNT_EMPTY: | ||
footerInfo = this.countEmpty(key); | ||
break; | ||
case FooterType.PERCENT_EMPTY: | ||
footerInfo = this.percentEmpty(key); | ||
break; | ||
case FooterType.COUNT_FILLED: | ||
footerInfo = this.countFilled(key); | ||
break; | ||
case FooterType.PERCENT_FILLED: | ||
footerInfo = this.percentFilled(key); | ||
break; | ||
case FooterType.SUM: | ||
footerInfo = this.sum(key); | ||
break; | ||
case FooterType.MIN: | ||
footerInfo = this.min(key); | ||
break; | ||
case FooterType.MAX: | ||
footerInfo = this.max(key); | ||
break; | ||
case FooterType.NONE: | ||
default: | ||
footerInfo = ""; | ||
} | ||
} catch (e) { | ||
footerInfo = `Error: ${e.message}`; | ||
} finally { | ||
return footerInfo; | ||
} | ||
} | ||
|
||
public sum(key: string): string { | ||
const total = this.rows.reduce((acc, row) => acc + row.getValue<number>(key), 0); | ||
const total = this.rows.reduce((acc, row) => acc + Number(row.getValue<number>(key)), 0); | ||
return `Total: ${total}`; | ||
} | ||
|
||
public min(key: string): string { | ||
const min = this.rows.reduce((acc, row) => Math.min(acc, row.getValue<number>(key)), Number.MAX_SAFE_INTEGER); | ||
return `Min: ${min}`; | ||
} | ||
|
||
public max(key: string): string { | ||
const max = this.rows.reduce((acc, row) => Math.max(acc, row.getValue<number>(key)), Number.MIN_SAFE_INTEGER); | ||
return `Max: ${max}`; | ||
} | ||
|
||
public countUnique(key: string): string { | ||
const uniqueValues = new Set(); | ||
this.rows | ||
.filter((row) => row.getValue(key) !== undefined) | ||
.forEach((row) => { | ||
uniqueValues.add(row.getValue(key)); | ||
}); | ||
return `Unique: ${uniqueValues.size}`; | ||
} | ||
|
||
public countEmpty(key: string): string { | ||
const empty = this.rows.filter((row) => !row.getValue(key)).length; | ||
return `Empty: ${empty}`; | ||
} | ||
|
||
public percentEmpty(key: string): string { | ||
const empty = this.rows.filter((row) => !row.getValue(key)).length; | ||
return `Empty: ${(empty / this.rows.length * 100).toFixed(2)}%`; | ||
} | ||
|
||
public countFilled(key: string): string { | ||
const filled = this.rows.filter((row) => row.getValue(key)).length; | ||
return `Filled: ${filled}`; | ||
} | ||
|
||
public percentFilled(key: string): string { | ||
const filled = this.rows.filter((row) => row.getValue(key)).length; | ||
return `Filled: ${(filled / this.rows.length * 100).toFixed(2)}%`; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,56 @@ | ||
import Footer from "automations/Footer"; | ||
import { DatabaseHeaderProps, TableColumn } from "cdm/FolderModel"; | ||
import { InputType } from "helpers/Constants"; | ||
import { c, getAlignmentClassname } from "helpers/StylesHelper"; | ||
import React from "react"; | ||
import { COLUMN_ALIGNMENT_OPTIONS, FooterType } from "helpers/Constants"; | ||
import { c } from "helpers/StylesHelper"; | ||
import React, { MouseEventHandler, useState } from "react"; | ||
import { showFooterMenu } from "components/obsidianArq/commands"; | ||
import { Literal } from "obsidian-dataview"; | ||
|
||
export default function DefaultFooter(headerProps: DatabaseHeaderProps) { | ||
/** Properties of footer */ | ||
const { header, table } = headerProps; | ||
const { tableState } = table.options.meta; | ||
const configInfo = tableState.configState((state) => state.info); | ||
const columnActions = tableState.columns((state) => state.actions); | ||
|
||
/** Column values */ | ||
const { input, config } = header.column.columnDef as TableColumn; | ||
let footerInfo: string; | ||
switch (input) { | ||
case InputType.NUMBER: | ||
footerInfo = new Footer(table.getRowModel().rows).sum(header.id); | ||
break; | ||
default: | ||
return null; | ||
// Do nothing | ||
const tableColumn = header.column.columnDef as TableColumn; | ||
const formulaInfo = tableState.automations((state) => state.info); | ||
const [footerType, setFooterValue] = useState(tableColumn.config.footer_type); | ||
const { config } = tableColumn; | ||
let footerInfo: Literal = ""; | ||
if (config.footer_type === FooterType.FORMULA) { | ||
footerInfo = formulaInfo.dispatchFooter( | ||
tableColumn, | ||
table.getRowModel().rows | ||
); | ||
} else { | ||
footerInfo = new Footer(table.getRowModel().rows).dispatch( | ||
footerType, | ||
header.id | ||
); | ||
} | ||
const handlerFooterOptions: MouseEventHandler<HTMLDivElement> = async ( | ||
event: React.MouseEvent | ||
) => { | ||
await showFooterMenu( | ||
event.nativeEvent, | ||
tableColumn, | ||
columnActions, | ||
footerType, | ||
setFooterValue | ||
); | ||
}; | ||
|
||
return ( | ||
<span | ||
key={`foot-th-cell-${header.id}`} | ||
className={c( | ||
getAlignmentClassname(config, configInfo.getLocalSettings()) | ||
)} | ||
<div | ||
key={`default-footer-${header.id}`} | ||
onClick={handlerFooterOptions} | ||
style={{ | ||
minHeight: "20px", | ||
}} | ||
> | ||
{footerInfo} | ||
</span> | ||
{footerInfo?.toString()} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.