From 05aa0878515211bbd4937350652a48224108c949 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Wed, 25 Jun 2025 11:23:34 +0300 Subject: [PATCH] feat(book/table): support basic text columns --- .../src/widgets/view_widgets/table_view.ts | 9 +-- .../widgets/view_widgets/table_view/data.ts | 56 +++++++++++++++---- .../view_widgets/table_view/renderer.ts | 11 +--- 3 files changed, 53 insertions(+), 23 deletions(-) diff --git a/apps/client/src/widgets/view_widgets/table_view.ts b/apps/client/src/widgets/view_widgets/table_view.ts index b87b72702..0505f09c7 100644 --- a/apps/client/src/widgets/view_widgets/table_view.ts +++ b/apps/client/src/widgets/view_widgets/table_view.ts @@ -28,14 +28,14 @@ export default class TableView extends ViewMode { private $root: JQuery; private $container: JQuery; - private noteIds: string[]; + private args: ViewModeArgs; constructor(args: ViewModeArgs) { super(args); this.$root = $(TPL); this.$container = this.$root.find(".table-view-container"); - this.noteIds = args.noteIds; + this.args = args; args.$parent.append(this.$root); } @@ -44,10 +44,11 @@ export default class TableView extends ViewMode { } async renderList() { - const notes = await froca.getNotes(this.noteIds); + const { noteIds, parentNote } = this.args; + const notes = await froca.getNotes(noteIds); this.$container.empty(); - renderTable(this.$container[0], notes); + renderTable(this.$container[0], parentNote, notes); return this.$root; } diff --git a/apps/client/src/widgets/view_widgets/table_view/data.ts b/apps/client/src/widgets/view_widgets/table_view/data.ts index a9b164cc0..be6a968e6 100644 --- a/apps/client/src/widgets/view_widgets/table_view/data.ts +++ b/apps/client/src/widgets/view_widgets/table_view/data.ts @@ -1,29 +1,63 @@ import { GridOptions } from "ag-grid-community"; import FNote from "../../../entities/fnote"; -interface Data { +type Data = { title: string; +} & Record; + +export function buildData(parentNote: FNote, notes: FNote[]) { + const { columnDefs, expectedLabels } = buildColumnDefinitions(parentNote); + const rowData = buildRowDefinitions(notes, expectedLabels); + + return { + rowData, + columnDefs + } } -export function buildColumnDefinitions(): GridOptions["columnDefs"] { - return [ +export function buildColumnDefinitions(parentNote: FNote) { + const columnDefs: GridOptions["columnDefs"] = [ { field: "title" } ]; + + const expectedLabels: string[] = []; + + for (const promotedAttribute of parentNote.getPromotedDefinitionAttributes()) { + console.log(promotedAttribute); + if (promotedAttribute.type !== "label") { + console.warn("Relations are not supported for now"); + continue; + } + + const def = promotedAttribute.getDefinition(); + const attributeName = promotedAttribute.name.split(":", 2)[1]; + const title = def.promotedAlias ?? attributeName; + + columnDefs.push({ + field: attributeName, + headerName: title + }); + expectedLabels.push(attributeName); + } + + return { columnDefs, expectedLabels }; } -export function buildRowDefinitions(notes: FNote[]): GridOptions["rowData"] { +export function buildRowDefinitions(notes: FNote[], expectedLabels: string[]): GridOptions["rowData"] { const definitions: GridOptions["rowData"] = []; for (const note of notes) { - definitions.push(buildRowDefinition(note)); + const data = { + title: note.title + }; + + for (const expectedLabel of expectedLabels) { + data[expectedLabel] = note.getLabelValue(expectedLabel); + } + + definitions.push(data); } return definitions; } - -export function buildRowDefinition(note: FNote): Data { - return { - title: note.title - } -} diff --git a/apps/client/src/widgets/view_widgets/table_view/renderer.ts b/apps/client/src/widgets/view_widgets/table_view/renderer.ts index 385acd1bf..b8e9b92ca 100644 --- a/apps/client/src/widgets/view_widgets/table_view/renderer.ts +++ b/apps/client/src/widgets/view_widgets/table_view/renderer.ts @@ -1,16 +1,11 @@ import { createGrid, AllCommunityModule, ModuleRegistry } from "ag-grid-community"; -import { buildColumnDefinitions, buildRowDefinitions } from "./data.js"; +import { buildData } from "./data.js"; import FNote from "../../../entities/fnote.js"; ModuleRegistry.registerModules([ AllCommunityModule ]); -export default function renderTable(el: HTMLElement, notes: FNote[]) { - const rowData = buildRowDefinitions(notes); - +export default function renderTable(el: HTMLElement, parentNote: FNote, notes: FNote[]) { createGrid(el, { - // Row Data: The data to be displayed. - rowData: rowData, - // Column Definitions: Defines the columns to be displayed. - columnDefs: buildColumnDefinitions() + ...buildData(parentNote, notes) }); }