2025-06-27 17:44:29 +03:00
|
|
|
import froca from "../../../services/froca.js";
|
|
|
|
|
import ViewMode, { type ViewModeArgs } from "../view_mode.js";
|
2025-06-27 17:39:57 +03:00
|
|
|
import { createGrid, AllCommunityModule, ModuleRegistry, GridOptions } from "ag-grid-community";
|
2025-06-27 17:44:29 +03:00
|
|
|
import { setLabel } from "../../../services/attributes.js";
|
|
|
|
|
import getPromotedAttributeInformation, { buildData, TableData } from "./data.js";
|
|
|
|
|
import applyHeaderCustomization from "./header-customization.js";
|
|
|
|
|
import server from "../../../services/server.js";
|
2025-06-27 17:40:56 +03:00
|
|
|
import type { GridState } from "ag-grid-community";
|
2025-06-25 10:31:41 +03:00
|
|
|
|
|
|
|
|
const TPL = /*html*/`
|
|
|
|
|
<div class="table-view">
|
2025-06-25 10:40:04 +03:00
|
|
|
<style>
|
|
|
|
|
.table-view {
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
position: relative;
|
|
|
|
|
height: 100%;
|
|
|
|
|
user-select: none;
|
|
|
|
|
padding: 10px;
|
|
|
|
|
}
|
2025-06-25 10:49:33 +03:00
|
|
|
|
|
|
|
|
.table-view-container {
|
|
|
|
|
height: 100%;
|
|
|
|
|
}
|
2025-06-27 17:18:52 +03:00
|
|
|
|
|
|
|
|
.search-result-widget-content .table-view {
|
|
|
|
|
position: absolute;
|
|
|
|
|
top: 0;
|
|
|
|
|
left: 0;
|
|
|
|
|
right: 0;
|
|
|
|
|
bottom: 0;
|
|
|
|
|
}
|
2025-06-25 10:40:04 +03:00
|
|
|
</style>
|
|
|
|
|
|
2025-06-27 17:22:47 +03:00
|
|
|
<div class="table-view-container"></div>
|
2025-06-25 10:31:41 +03:00
|
|
|
</div>
|
|
|
|
|
`;
|
|
|
|
|
|
2025-06-27 17:40:56 +03:00
|
|
|
export interface StateInfo {
|
|
|
|
|
gridState: GridState;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-25 16:18:34 +03:00
|
|
|
export default class TableView extends ViewMode<StateInfo> {
|
2025-06-25 10:31:41 +03:00
|
|
|
|
|
|
|
|
private $root: JQuery<HTMLElement>;
|
2025-06-25 10:49:33 +03:00
|
|
|
private $container: JQuery<HTMLElement>;
|
2025-06-25 11:23:34 +03:00
|
|
|
private args: ViewModeArgs;
|
2025-06-25 10:31:41 +03:00
|
|
|
|
|
|
|
|
constructor(args: ViewModeArgs) {
|
2025-06-25 16:18:34 +03:00
|
|
|
super(args, "table");
|
2025-06-25 10:31:41 +03:00
|
|
|
|
|
|
|
|
this.$root = $(TPL);
|
2025-06-25 10:49:33 +03:00
|
|
|
this.$container = this.$root.find(".table-view-container");
|
2025-06-25 11:23:34 +03:00
|
|
|
this.args = args;
|
2025-06-25 10:31:41 +03:00
|
|
|
args.$parent.append(this.$root);
|
2025-06-27 17:39:57 +03:00
|
|
|
|
|
|
|
|
ModuleRegistry.registerModules([ AllCommunityModule ]);
|
2025-06-25 10:31:41 +03:00
|
|
|
}
|
|
|
|
|
|
2025-06-25 10:40:04 +03:00
|
|
|
get isFullHeight(): boolean {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-25 10:31:41 +03:00
|
|
|
async renderList() {
|
2025-06-27 17:39:57 +03:00
|
|
|
this.$container.empty();
|
|
|
|
|
this.renderTable(this.$container[0]);
|
|
|
|
|
return this.$root;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async renderTable(el: HTMLElement) {
|
2025-06-25 11:23:34 +03:00
|
|
|
const { noteIds, parentNote } = this.args;
|
|
|
|
|
const notes = await froca.getNotes(noteIds);
|
2025-06-25 11:03:43 +03:00
|
|
|
|
2025-06-27 17:39:57 +03:00
|
|
|
const info = getPromotedAttributeInformation(parentNote);
|
|
|
|
|
const viewStorage = await this.viewStorage.restore();
|
|
|
|
|
const initialState = viewStorage?.gridState;
|
|
|
|
|
|
|
|
|
|
createGrid(el, {
|
|
|
|
|
...buildData(info, notes),
|
|
|
|
|
...setupEditing(),
|
|
|
|
|
initialState,
|
|
|
|
|
async onGridReady(event) {
|
|
|
|
|
applyHeaderCustomization(el, event.api);
|
|
|
|
|
},
|
|
|
|
|
onStateUpdated: (event) => this.viewStorage.store({
|
|
|
|
|
gridState: event.api.getState()
|
|
|
|
|
})
|
|
|
|
|
});
|
2025-06-25 10:31:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2025-06-27 17:39:57 +03:00
|
|
|
|
|
|
|
|
function setupEditing(): GridOptions<TableData> {
|
|
|
|
|
return {
|
|
|
|
|
onCellValueChanged(event) {
|
|
|
|
|
if (event.type !== "cellValueChanged") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const noteId = event.data.noteId;
|
|
|
|
|
const name = event.colDef.field;
|
|
|
|
|
if (!name) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { newValue } = event;
|
|
|
|
|
if (name === "title") {
|
|
|
|
|
// TODO: Deduplicate with note_title.
|
|
|
|
|
server.put(`notes/${noteId}/title`, { title: newValue });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (name.startsWith("labels.")) {
|
|
|
|
|
const labelName = name.split(".", 2)[1];
|
|
|
|
|
setLabel(noteId, labelName, newValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|