2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* DocModel describes the observable models for all document data, including the built-in tables
|
|
|
|
* (aka metatables), which are used in the Grist application itself (e.g. to render views).
|
|
|
|
*
|
|
|
|
* Since all data is structured as tables, we have several levels of models:
|
|
|
|
* (1) DocModel maintains all tables
|
|
|
|
* (2) MetaTableModel maintains data for a built-in table.
|
|
|
|
* (3) DataTableModel maintains data for a user-defined table.
|
|
|
|
* (4) RowModels (defined in {Data,Meta}TableModel.js) maintains data for one record in a table.
|
|
|
|
* For built-in tables, the records are defined in this module, below.
|
|
|
|
*/
|
|
|
|
import {KoArray} from 'app/client/lib/koArray';
|
|
|
|
import {KoSaveableObservable} from 'app/client/models/modelUtil';
|
|
|
|
|
|
|
|
import * as ko from 'knockout';
|
2022-10-28 08:04:59 +00:00
|
|
|
import memoize from 'lodash/memoize';
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
import * as koArray from 'app/client/lib/koArray';
|
|
|
|
import * as koUtil from 'app/client/lib/koUtil';
|
2022-07-04 14:14:55 +00:00
|
|
|
import DataTableModel from 'app/client/models/DataTableModel';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {DocData} from 'app/client/models/DocData';
|
2021-08-27 17:25:20 +00:00
|
|
|
import {urlState} from 'app/client/models/gristUrlState';
|
2022-07-04 14:14:55 +00:00
|
|
|
import MetaRowModel from 'app/client/models/MetaRowModel';
|
|
|
|
import MetaTableModel from 'app/client/models/MetaTableModel';
|
2020-10-02 15:10:00 +00:00
|
|
|
import * as rowset from 'app/client/models/rowset';
|
2022-07-06 07:41:09 +00:00
|
|
|
import {isHiddenTable, isSummaryTable} from 'app/common/isHiddenTable';
|
|
|
|
import {RowFilterFunc} from 'app/common/RowFilterFunc';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {schema, SchemaTypes} from 'app/common/schema';
|
|
|
|
|
2021-03-25 17:37:09 +00:00
|
|
|
import {ACLRuleRec, createACLRuleRec} from 'app/client/models/entities/ACLRuleRec';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {ColumnRec, createColumnRec} from 'app/client/models/entities/ColumnRec';
|
|
|
|
import {createDocInfoRec, DocInfoRec} from 'app/client/models/entities/DocInfoRec';
|
2021-11-19 20:30:11 +00:00
|
|
|
import {createFilterRec, FilterRec} from 'app/client/models/entities/FilterRec';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {createPageRec, PageRec} from 'app/client/models/entities/PageRec';
|
|
|
|
import {createTabBarRec, TabBarRec} from 'app/client/models/entities/TabBarRec';
|
|
|
|
import {createTableRec, TableRec} from 'app/client/models/entities/TableRec';
|
|
|
|
import {createValidationRec, ValidationRec} from 'app/client/models/entities/ValidationRec';
|
|
|
|
import {createViewFieldRec, ViewFieldRec} from 'app/client/models/entities/ViewFieldRec';
|
|
|
|
import {createViewRec, ViewRec} from 'app/client/models/entities/ViewRec';
|
|
|
|
import {createViewSectionRec, ViewSectionRec} from 'app/client/models/entities/ViewSectionRec';
|
2022-10-17 09:47:16 +00:00
|
|
|
import {CellRec, createCellRec} from 'app/client/models/entities/CellRec';
|
2022-04-07 14:58:16 +00:00
|
|
|
import {RefListValue} from 'app/common/gristTypes';
|
2022-03-22 13:41:11 +00:00
|
|
|
import {decodeObject} from 'app/plugin/objtypes';
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// Re-export all the entity types available. The recommended usage is like this:
|
|
|
|
// import {ColumnRec, ViewFieldRec} from 'app/client/models/DocModel';
|
(core) Speed up and upgrade build.
Summary:
- Upgrades to build-related packages:
- Upgrade typescript, related libraries and typings.
- Upgrade webpack, eslint; add tsc-watch, node-dev, eslint_d.
- Build organization changes:
- Build webpack from original typescript, transpiling only; with errors still
reported by a background tsc watching process.
- Typescript-related changes:
- Reduce imports of AWS dependencies (very noticeable speedup)
- Avoid auto-loading global @types
- Client code is now built with isolatedModules flag (for safe transpilation)
- Use allowJs to avoid copying JS files manually.
- Linting changes
- Enhance Arcanist ESLintLinter to run before/after commands, and set up to use eslint_d
- Update eslint config, and include .eslintignore to avoid linting generated files.
- Include a bunch of eslint-prompted and eslint-generated fixes
- Add no-unused-expression rule to eslint, and fix a few warnings about it
- Other items:
- Refactor cssInput to avoid circular dependency
- Remove a bit of unused code, libraries, dependencies
Test Plan: No behavior changes, all existing tests pass. There are 30 tests fewer reported because `test_gpath.py` was removed (it's been unused for years)
Reviewers: paulfitz
Reviewed By: paulfitz
Subscribers: paulfitz
Differential Revision: https://phab.getgrist.com/D3498
2022-06-27 20:09:41 +00:00
|
|
|
export type {ColumnRec, DocInfoRec, FilterRec, PageRec, TabBarRec, TableRec, ValidationRec,
|
2022-10-17 09:47:16 +00:00
|
|
|
ViewFieldRec, ViewRec, ViewSectionRec, CellRec};
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the type for a MetaRowModel containing a KoSaveableObservable for each field listed in
|
|
|
|
* the auto-generated app/common/schema.ts. It represents the metadata record in the database.
|
|
|
|
* Particular DocModel entities derive from this, and add other helpful computed values.
|
|
|
|
*/
|
2022-10-17 09:47:16 +00:00
|
|
|
export type IRowModel<TName extends keyof SchemaTypes> = MetaRowModel<TName> & {
|
2020-10-02 15:10:00 +00:00
|
|
|
[ColId in keyof SchemaTypes[TName]]: KoSaveableObservable<SchemaTypes[TName][ColId]>;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an observable for an observable array of records from the given table.
|
|
|
|
*
|
|
|
|
* @param {RowModel} rowModel: RowModel that owns this recordSet.
|
|
|
|
* @param {TableModel} tableModel: The model for the table to return records from.
|
|
|
|
* @param {String} groupByField: The name of the field in the other table by which to group. The
|
|
|
|
* returned observable arrays will be for the group matching the value of rowModel.id().
|
|
|
|
* @param {String} [options.sortBy]: Keep the returned array sorted by this key. If omitted, the
|
|
|
|
* returned array will be sorted by rowId.
|
|
|
|
*/
|
|
|
|
export function recordSet<TRow extends MetaRowModel>(
|
|
|
|
rowModel: MetaRowModel, tableModel: MetaTableModel<TRow>, groupByField: string, options?: {sortBy: string}
|
|
|
|
): ko.Computed<KoArray<TRow>> {
|
|
|
|
|
|
|
|
const opts = {groupBy: groupByField, sortBy: 'id', ...options};
|
|
|
|
return koUtil.computedAutoDispose(
|
|
|
|
() => tableModel.createRowGroupModel(rowModel.id() || 0, opts),
|
|
|
|
null, { pure: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an observable for a record from another table, selected using the passed-in observable
|
|
|
|
* for a rowId. If rowId is invalid, returns the row model for the fake empty record.
|
|
|
|
* @param {TableModel} tableModel: The model for the table to return a record from.
|
|
|
|
* @param {ko.observable} rowIdObs: An observable for the row id to look up.
|
|
|
|
*/
|
|
|
|
export function refRecord<TRow extends MetaRowModel>(
|
|
|
|
tableModel: MetaTableModel<TRow>, rowIdObs: ko.Observable<number>|ko.Computed<number>
|
|
|
|
): ko.Computed<TRow> {
|
|
|
|
// Pass 'true' to getRowModel() to depend on the row version.
|
|
|
|
return ko.pureComputed(() => tableModel.getRowModel(rowIdObs() || 0, true));
|
|
|
|
}
|
|
|
|
|
2022-04-07 14:58:16 +00:00
|
|
|
|
2022-03-22 13:41:11 +00:00
|
|
|
/**
|
|
|
|
* Returns an observable with a list of records from another table, selected using RefList column.
|
|
|
|
* @param {TableModel} tableModel: The model for the table to return a record from.
|
|
|
|
* @param {ko.observable} rowsIdObs: An observable with a RefList value.
|
|
|
|
*/
|
|
|
|
export function refListRecords<TRow extends MetaRowModel>(
|
|
|
|
tableModel: MetaTableModel<TRow>, rowsIdObs: ko.Observable<RefListValue>|ko.Computed<RefListValue>
|
|
|
|
) {
|
|
|
|
return ko.pureComputed(() => {
|
|
|
|
const ids = decodeObject(rowsIdObs()) as number[]|null;
|
|
|
|
if (!Array.isArray(ids)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return ids.map(id => tableModel.getRowModel(id, true));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// Use an alias for brevity.
|
|
|
|
type MTM<RowModel extends MetaRowModel> = MetaTableModel<RowModel>;
|
|
|
|
|
|
|
|
export class DocModel {
|
|
|
|
// MTM is a shorthand for MetaTableModel below, to keep each item to one line.
|
|
|
|
public docInfo: MTM<DocInfoRec> = this._metaTableModel("_grist_DocInfo", createDocInfoRec);
|
|
|
|
public tables: MTM<TableRec> = this._metaTableModel("_grist_Tables", createTableRec);
|
|
|
|
public columns: MTM<ColumnRec> = this._metaTableModel("_grist_Tables_column", createColumnRec);
|
|
|
|
public views: MTM<ViewRec> = this._metaTableModel("_grist_Views", createViewRec);
|
|
|
|
public viewSections: MTM<ViewSectionRec> = this._metaTableModel("_grist_Views_section", createViewSectionRec);
|
|
|
|
public viewFields: MTM<ViewFieldRec> = this._metaTableModel("_grist_Views_section_field", createViewFieldRec);
|
|
|
|
public tabBar: MTM<TabBarRec> = this._metaTableModel("_grist_TabBar", createTabBarRec);
|
|
|
|
public validations: MTM<ValidationRec> = this._metaTableModel("_grist_Validations", createValidationRec);
|
|
|
|
public pages: MTM<PageRec> = this._metaTableModel("_grist_Pages", createPageRec);
|
2021-03-25 17:37:09 +00:00
|
|
|
public rules: MTM<ACLRuleRec> = this._metaTableModel("_grist_ACLRules", createACLRuleRec);
|
2021-11-19 20:30:11 +00:00
|
|
|
public filters: MTM<FilterRec> = this._metaTableModel("_grist_Filters", createFilterRec);
|
2022-10-17 09:47:16 +00:00
|
|
|
public cells: MTM<CellRec> = this._metaTableModel("_grist_Cells", createCellRec);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2021-08-26 16:35:11 +00:00
|
|
|
public docInfoRow: DocInfoRec;
|
|
|
|
|
2022-07-06 07:41:09 +00:00
|
|
|
public visibleTables: KoArray<TableRec>;
|
|
|
|
public rawDataTables: KoArray<TableRec>;
|
|
|
|
public rawSummaryTables: KoArray<TableRec>;
|
|
|
|
|
|
|
|
public visibleTableIds: KoArray<string>;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// A mapping from tableId to DataTableModel for user-defined tables.
|
|
|
|
public dataTables: {[tableId: string]: DataTableModel} = {};
|
|
|
|
|
|
|
|
// Another map, this one mapping tableRef (rowId) to DataTableModel.
|
|
|
|
public dataTablesByRef = new Map<number, DataTableModel>();
|
|
|
|
|
|
|
|
public allTabs: KoArray<TabBarRec> = this.tabBar.createAllRowsModel('tabPos');
|
2021-08-27 17:25:20 +00:00
|
|
|
|
2022-10-28 08:04:59 +00:00
|
|
|
/** Pages that are shown in the menu. These can include censored pages if they have children. */
|
|
|
|
public menuPages: ko.Computed<PageRec[]>;
|
2021-08-27 17:25:20 +00:00
|
|
|
// Excludes pages hidden by ACL rules or other reasons (e.g. doc-tour)
|
|
|
|
public visibleDocPages: ko.Computed<PageRec[]>;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// Flag for tracking whether document is in formula-editing mode
|
|
|
|
public editingFormula: ko.Observable<boolean> = ko.observable(false);
|
|
|
|
|
2021-08-27 17:25:20 +00:00
|
|
|
// TODO This is a temporary solution until we expose creation of doc-tours to users. This flag
|
|
|
|
// is initialized once on page load. If set, then the tour page (if any) will be visible.
|
|
|
|
public showDocTourTable: boolean = (urlState().state.get().docPage === 'GristDocTour');
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// List of all the metadata tables.
|
|
|
|
private _metaTables: Array<MetaTableModel<any>>;
|
|
|
|
|
|
|
|
constructor(public readonly docData: DocData) {
|
|
|
|
// For all the metadata tables, load their data (and create the RowModels).
|
|
|
|
for (const model of this._metaTables) {
|
|
|
|
model.loadData();
|
|
|
|
}
|
|
|
|
|
2021-08-26 16:35:11 +00:00
|
|
|
this.docInfoRow = this.docInfo.getRowModel(1);
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// An observable array of user-visible tables, sorted by tableId, excluding summary tables.
|
|
|
|
// This is a publicly exposed member.
|
2022-07-06 07:41:09 +00:00
|
|
|
this.visibleTables = createVisibleTablesArray(this.tables);
|
|
|
|
|
|
|
|
// Observable arrays of raw data and summary tables, sorted by tableId.
|
|
|
|
this.rawDataTables = createRawDataTablesArray(this.tables);
|
|
|
|
this.rawSummaryTables = createRawSummaryTablesArray(this.tables);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2022-07-06 07:41:09 +00:00
|
|
|
// An observable array of user-visible tableIds. A shortcut mapped from visibleTables.
|
|
|
|
const visibleTableIds = ko.computed(() => this.visibleTables.all().map(t => t.tableId()));
|
|
|
|
this.visibleTableIds = koArray.syncedKoArray(visibleTableIds);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// Create an observable array of RowModels for all the data tables. We'll trigger
|
|
|
|
// onAddTable/onRemoveTable in response to this array's splice events below.
|
|
|
|
const allTableMetaRows = this.tables.createAllRowsModel('id');
|
|
|
|
|
|
|
|
// For a new table, we get AddTable action followed by metadata actions to add a table record
|
|
|
|
// (which triggers this subscribeForEach) and to add all the column records. So we have to keep
|
|
|
|
// in mind that metadata for columns isn't available yet.
|
|
|
|
allTableMetaRows.subscribeForEach({
|
|
|
|
add: r => this._onAddTable(r),
|
|
|
|
remove: r => this._onRemoveTable(r),
|
|
|
|
});
|
2021-08-27 17:25:20 +00:00
|
|
|
|
|
|
|
// Get a list of only the visible pages.
|
|
|
|
const allPages = this.pages.createAllRowsModel('pagePos');
|
2022-10-28 08:04:59 +00:00
|
|
|
this.menuPages = ko.computed(() => {
|
|
|
|
const pagesToShow = allPages.all().filter(p => !p.isSpecial()).sort((a, b) => a.pagePos() - b.pagePos());
|
|
|
|
// Helper to find all children of a page.
|
|
|
|
const children = memoize((page: PageRec) => {
|
|
|
|
const following = pagesToShow.slice(pagesToShow.indexOf(page) + 1);
|
|
|
|
const firstOutside = following.findIndex(p => p.indentation() <= page.indentation());
|
|
|
|
return firstOutside >= 0 ? following.slice(0, firstOutside) : following;
|
|
|
|
});
|
|
|
|
// Helper to test if the page is hidden and all its children are hidden.
|
|
|
|
// In that case, we won't show it at all.
|
|
|
|
const hide = memoize((page: PageRec): boolean => page.isCensored() && children(page).every(p => hide(p)));
|
|
|
|
return pagesToShow.filter(p => !hide(p));
|
|
|
|
});
|
2021-08-27 17:25:20 +00:00
|
|
|
this.visibleDocPages = ko.computed(() => allPages.all().filter(p => !p.isHidden()));
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private _metaTableModel<TName extends keyof SchemaTypes, TRow extends IRowModel<TName>>(
|
|
|
|
tableId: TName,
|
|
|
|
rowConstructor: (this: TRow, docModel: DocModel) => void,
|
|
|
|
): MetaTableModel<TRow> {
|
|
|
|
const fields = Object.keys(schema[tableId]);
|
|
|
|
const model = new MetaTableModel<TRow>(this, this.docData.getTable(tableId)!, fields, rowConstructor);
|
|
|
|
// To keep _metaTables private member listed after public ones, initialize it on first use.
|
|
|
|
if (!this._metaTables) { this._metaTables = []; }
|
|
|
|
this._metaTables.push(model);
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
|
|
|
private _onAddTable(tableMetaRow: TableRec) {
|
|
|
|
let tid = tableMetaRow.tableId();
|
|
|
|
const dtm = new DataTableModel(this, this.docData.getTable(tid)!, tableMetaRow);
|
|
|
|
this.dataTables[tid] = dtm;
|
|
|
|
this.dataTablesByRef.set(tableMetaRow.getRowId(), dtm);
|
|
|
|
|
|
|
|
// Subscribe to tableMetaRow.tableId() to handle table renames.
|
|
|
|
tableMetaRow.tableId.subscribe(newTableId => {
|
|
|
|
this.dataTables[newTableId] = this.dataTables[tid];
|
|
|
|
delete this.dataTables[tid];
|
|
|
|
tid = newTableId;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private _onRemoveTable(tableMetaRow: TableRec) {
|
|
|
|
const tid = tableMetaRow.tableId();
|
|
|
|
this.dataTables[tid].dispose();
|
|
|
|
delete this.dataTables[tid];
|
|
|
|
this.dataTablesByRef.delete(tableMetaRow.getRowId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-07-06 07:41:09 +00:00
|
|
|
* Creates an observable array of tables, sorted by tableId.
|
|
|
|
*
|
|
|
|
* An optional `filterFunc` may be specified to filter tables.
|
2020-10-02 15:10:00 +00:00
|
|
|
*/
|
2022-07-06 07:41:09 +00:00
|
|
|
function createTablesArray(
|
|
|
|
tablesModel: MetaTableModel<TableRec>,
|
|
|
|
filterFunc: RowFilterFunc<rowset.RowId> = (_row) => true
|
|
|
|
) {
|
|
|
|
const rowSource = new rowset.FilteredRowSource(filterFunc);
|
2020-10-02 15:10:00 +00:00
|
|
|
rowSource.subscribeTo(tablesModel);
|
|
|
|
// Create an observable RowModel array based on this rowSource, sorted by tableId.
|
|
|
|
return tablesModel._createRowSetModel(rowSource, 'tableId');
|
|
|
|
}
|
2022-04-27 17:46:24 +00:00
|
|
|
|
|
|
|
/**
|
2022-07-06 07:41:09 +00:00
|
|
|
* Returns an observable array of user tables, sorted by tableId, and excluding hidden/summary
|
|
|
|
* tables.
|
2022-04-27 17:46:24 +00:00
|
|
|
*/
|
2022-07-06 07:41:09 +00:00
|
|
|
function createVisibleTablesArray(tablesModel: MetaTableModel<TableRec>): KoArray<TableRec> {
|
|
|
|
return createTablesArray(tablesModel, r => !isHiddenTable(tablesModel.tableData, r));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an observable array of raw data tables, sorted by tableId, and excluding summary
|
|
|
|
* tables.
|
|
|
|
*/
|
|
|
|
function createRawDataTablesArray(tablesModel: MetaTableModel<TableRec>): KoArray<TableRec> {
|
|
|
|
return createTablesArray(tablesModel, r => !isSummaryTable(tablesModel.tableData, r));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an observable array of raw summary tables, sorted by tableId.
|
|
|
|
*/
|
|
|
|
function createRawSummaryTablesArray(tablesModel: MetaTableModel<TableRec>): KoArray<TableRec> {
|
|
|
|
return createTablesArray(tablesModel, r => isSummaryTable(tablesModel.tableData, r));
|
2022-04-27 17:46:24 +00:00
|
|
|
}
|