2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* GristDoc manages an open Grist document on the client side.
|
|
|
|
*/
|
|
|
|
// tslint:disable:no-console
|
|
|
|
|
2020-12-23 03:18:07 +00:00
|
|
|
import {AccessRules} from 'app/client/aclui/AccessRules';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {ActionLog} from 'app/client/components/ActionLog';
|
2021-09-20 20:35:14 +00:00
|
|
|
import * as BaseView from 'app/client/components/BaseView';
|
2022-01-17 16:08:43 +00:00
|
|
|
import {isNumericLike, isNumericOnly} from 'app/client/components/ChartView';
|
2020-10-02 15:10:00 +00:00
|
|
|
import * as CodeEditorPanel from 'app/client/components/CodeEditorPanel';
|
|
|
|
import * as commands from 'app/client/components/commands';
|
|
|
|
import {CursorPos} from 'app/client/components/Cursor';
|
2021-09-20 20:35:14 +00:00
|
|
|
import {CursorMonitor, ViewCursorPos} from "app/client/components/CursorMonitor";
|
2020-10-02 15:10:00 +00:00
|
|
|
import {DocComm, DocUserAction} from 'app/client/components/DocComm';
|
|
|
|
import * as DocConfigTab from 'app/client/components/DocConfigTab';
|
2021-09-20 20:35:14 +00:00
|
|
|
import {Drafts} from "app/client/components/Drafts";
|
|
|
|
import {EditorMonitor} from "app/client/components/EditorMonitor";
|
2020-10-02 15:10:00 +00:00
|
|
|
import * as GridView from 'app/client/components/GridView';
|
|
|
|
import {Importer} from 'app/client/components/Importer';
|
2022-04-01 18:17:06 +00:00
|
|
|
import {RawData} from 'app/client/components/RawData';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {ActionGroupWithCursorPos, UndoStack} from 'app/client/components/UndoStack';
|
|
|
|
import {ViewLayout} from 'app/client/components/ViewLayout';
|
|
|
|
import {get as getBrowserGlobals} from 'app/client/lib/browserGlobals';
|
|
|
|
import {DocPluginManager} from 'app/client/lib/DocPluginManager';
|
|
|
|
import {ImportSourceElement} from 'app/client/lib/ImportSourceElement';
|
|
|
|
import {createSessionObs} from 'app/client/lib/sessionObs';
|
|
|
|
import {setTestState} from 'app/client/lib/testState';
|
|
|
|
import {selectFiles} from 'app/client/lib/uploads';
|
|
|
|
import {reportError} from 'app/client/models/AppModel';
|
|
|
|
import * as DataTableModel from 'app/client/models/DataTableModel';
|
|
|
|
import {DataTableModelWithDiff} from 'app/client/models/DataTableModelWithDiff';
|
|
|
|
import {DocData} from 'app/client/models/DocData';
|
|
|
|
import {DocInfoRec, DocModel, ViewRec, ViewSectionRec} from 'app/client/models/DocModel';
|
|
|
|
import {DocPageModel} from 'app/client/models/DocPageModel';
|
|
|
|
import {UserError} from 'app/client/models/errors';
|
2020-12-04 23:29:29 +00:00
|
|
|
import {urlState} from 'app/client/models/gristUrlState';
|
2021-09-20 20:35:14 +00:00
|
|
|
import {getFilterFunc, QuerySetManager} from 'app/client/models/QuerySet';
|
2022-03-21 03:41:59 +00:00
|
|
|
import {getUserOrgPrefObs, getUserOrgPrefsObs, markAsSeen} from 'app/client/models/UserPrefs';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {App} from 'app/client/ui/App';
|
|
|
|
import {DocHistory} from 'app/client/ui/DocHistory';
|
2021-09-20 20:35:14 +00:00
|
|
|
import {startDocTour} from "app/client/ui/DocTour";
|
2021-07-22 22:17:55 +00:00
|
|
|
import {showDocSettingsModal} from 'app/client/ui/DocumentSettings';
|
2022-03-21 03:41:59 +00:00
|
|
|
import {isTourActive} from "app/client/ui/OnBoardingPopups";
|
2020-10-02 15:10:00 +00:00
|
|
|
import {IPageWidget, toPageWidget} from 'app/client/ui/PageWidgetPicker';
|
|
|
|
import {IPageWidgetLink, linkFromId, selectBy} from 'app/client/ui/selectBy';
|
2021-07-19 08:49:44 +00:00
|
|
|
import {startWelcomeTour} from 'app/client/ui/welcomeTour';
|
2021-07-30 15:16:33 +00:00
|
|
|
import {isNarrowScreen, mediaSmall, testId} from 'app/client/ui2018/cssVars';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {IconName} from 'app/client/ui2018/IconList';
|
2021-09-20 20:35:14 +00:00
|
|
|
import {FieldEditor} from "app/client/widgets/FieldEditor";
|
2021-09-29 13:57:55 +00:00
|
|
|
import {MinimalActionGroup} from 'app/common/ActionGroup';
|
2021-09-20 20:35:14 +00:00
|
|
|
import {ClientQuery} from "app/common/ActiveDocAPI";
|
2020-10-02 15:10:00 +00:00
|
|
|
import {delay} from 'app/common/delay';
|
|
|
|
import {DisposableWithEvents} from 'app/common/DisposableWithEvents';
|
2022-01-17 16:08:43 +00:00
|
|
|
import {isSchemaAction, UserAction} from 'app/common/DocActions';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {OpenLocalDocResult} from 'app/common/DocListAPI';
|
2021-09-20 20:35:14 +00:00
|
|
|
import {isList, isRefListType, RecalcWhen} from 'app/common/gristTypes';
|
2022-03-22 12:38:56 +00:00
|
|
|
import {HashLink, IDocPage, isViewDocPage, SpecialDocPage, ViewDocPage} from 'app/common/gristUrls';
|
2021-09-01 21:07:53 +00:00
|
|
|
import {undef, waitObs} from 'app/common/gutil';
|
2021-08-05 15:12:46 +00:00
|
|
|
import {LocalPlugin} from "app/common/plugin";
|
2020-10-02 15:10:00 +00:00
|
|
|
import {StringUnion} from 'app/common/StringUnion';
|
|
|
|
import {TableData} from 'app/common/TableData';
|
|
|
|
import {DocStateComparison} from 'app/common/UserAPI';
|
2021-09-20 20:35:14 +00:00
|
|
|
import {Computed, dom, Emitter, Holder, IDisposable, IDomComponent, Observable, styled, subscribe, toKo} from 'grainjs';
|
2020-10-02 15:10:00 +00:00
|
|
|
import * as ko from 'knockout';
|
|
|
|
import cloneDeepWith = require('lodash/cloneDeepWith');
|
|
|
|
import isEqual = require('lodash/isEqual');
|
|
|
|
|
|
|
|
const G = getBrowserGlobals('document', 'window');
|
|
|
|
|
2021-07-22 22:17:55 +00:00
|
|
|
// Re-export some tools to move them from main webpack bundle to the one with GristDoc.
|
2021-07-27 16:03:35 +00:00
|
|
|
export {DocComm, showDocSettingsModal, startDocTour};
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
export interface TabContent {
|
|
|
|
showObs?: any;
|
|
|
|
header?: boolean;
|
|
|
|
label?: any;
|
|
|
|
items?: any;
|
|
|
|
buildDom?: any;
|
|
|
|
keywords?: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface TabOptions {
|
|
|
|
shortLabel?: string;
|
|
|
|
hideSearchContent?: boolean;
|
|
|
|
showObs?: any;
|
|
|
|
category?: any;
|
|
|
|
}
|
|
|
|
|
2021-07-20 12:52:21 +00:00
|
|
|
const RightPanelTool = StringUnion("none", "docHistory", "validations");
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
export interface IExtraTool {
|
|
|
|
icon: IconName;
|
|
|
|
label: string;
|
|
|
|
content: TabContent[]|IDomComponent;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class GristDoc extends DisposableWithEvents {
|
|
|
|
public docModel: DocModel;
|
|
|
|
public viewModel: ViewRec;
|
|
|
|
public activeViewId: Computed<IDocPage>;
|
|
|
|
public currentPageName: Observable<string>;
|
|
|
|
public docData: DocData;
|
|
|
|
public docInfo: DocInfoRec;
|
|
|
|
public docPluginManager: DocPluginManager;
|
|
|
|
public querySetManager: QuerySetManager;
|
|
|
|
public rightPanelTool: Observable<IExtraTool|null>;
|
|
|
|
public isReadonly = this.docPageModel.isReadonly;
|
|
|
|
public isReadonlyKo = toKo(ko, this.isReadonly);
|
|
|
|
public comparison: DocStateComparison|null;
|
2021-05-17 14:05:49 +00:00
|
|
|
// component for keeping track of latest cursor position
|
|
|
|
public cursorMonitor: CursorMonitor;
|
|
|
|
// component for keeping track of a cell that is being edited
|
|
|
|
public editorMonitor: EditorMonitor;
|
2021-05-25 09:24:00 +00:00
|
|
|
// component for keeping track of a cell that is being edited
|
|
|
|
public draftMonitor: Drafts;
|
2021-07-22 09:14:35 +00:00
|
|
|
// will document perform its own navigation (from anchor link)
|
|
|
|
public hasCustomNav: Observable<boolean>;
|
2020-10-02 15:10:00 +00:00
|
|
|
// Emitter triggered when the main doc area is resized.
|
|
|
|
public readonly resizeEmitter = this.autoDispose(new Emitter());
|
|
|
|
|
|
|
|
// This holds a single FieldEditor. When a new FieldEditor is created (on edit), it replaces the
|
|
|
|
// previous one if any. The holder is maintained by GristDoc, so that we are guaranteed at
|
|
|
|
// most one instance of FieldEditor at any time.
|
|
|
|
public readonly fieldEditorHolder = Holder.create(this);
|
2021-05-25 09:24:00 +00:00
|
|
|
// active field editor
|
|
|
|
public readonly activeEditor: Observable<FieldEditor | null> = Observable.create(this, null);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2021-05-17 14:05:49 +00:00
|
|
|
// Holds current view that is currently rendered
|
2021-05-23 17:43:11 +00:00
|
|
|
public currentView: Observable<BaseView | null>;
|
2021-05-17 14:05:49 +00:00
|
|
|
|
|
|
|
// Holds current cursor position with a view id
|
2021-05-23 17:43:11 +00:00
|
|
|
public cursorPosition: Computed<ViewCursorPos | undefined>;
|
2021-05-17 14:05:49 +00:00
|
|
|
|
2022-01-04 00:20:58 +00:00
|
|
|
public readonly userOrgPrefs = getUserOrgPrefsObs(this.docPageModel.appModel);
|
|
|
|
|
2022-03-21 03:41:59 +00:00
|
|
|
// If the doc has a docTour. Used also to enable the UI button to restart the tour.
|
|
|
|
public readonly hasDocTour: Computed<boolean>;
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
private _actionLog: ActionLog;
|
|
|
|
private _undoStack: UndoStack;
|
|
|
|
private _lastOwnActionGroup: ActionGroupWithCursorPos|null = null;
|
|
|
|
private _rightPanelTabs = new Map<string, TabContent[]>();
|
|
|
|
private _docHistory: DocHistory;
|
|
|
|
private _rightPanelTool = createSessionObs(this, "rightPanelTool", "none", RightPanelTool.guard);
|
|
|
|
private _viewLayout: ViewLayout|null = null;
|
2022-01-04 00:20:58 +00:00
|
|
|
private _showGristTour = getUserOrgPrefObs(this.userOrgPrefs, 'showGristTour');
|
2022-03-21 03:41:59 +00:00
|
|
|
private _seenDocTours = getUserOrgPrefObs(this.userOrgPrefs, 'seenDocTours');
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
public readonly app: App,
|
|
|
|
public readonly docComm: DocComm,
|
|
|
|
public readonly docPageModel: DocPageModel,
|
|
|
|
openDocResponse: OpenLocalDocResult,
|
2021-08-05 15:12:46 +00:00
|
|
|
plugins: LocalPlugin[],
|
2020-10-02 15:10:00 +00:00
|
|
|
options: {
|
|
|
|
comparison?: DocStateComparison // initial comparison with another document
|
|
|
|
} = {}
|
|
|
|
) {
|
|
|
|
super();
|
(core) Grace period and delete-only mode when exceeding row limit
Summary:
Builds upon https://phab.getgrist.com/D3328
- Add HomeDB column `Document.gracePeriodStart`
- When the row count moves above the limit, set it to the current date. When it moves below, set it to null.
- Add DataLimitStatus type indicating if the document is approaching the limit, is in a grace period, or is in delete only mode if the grace period started at least 14 days ago. Compute it in ActiveDoc and send it to client when opening.
- Only allow certain user actions when in delete-only mode.
Follow-up tasks related to this diff:
- When DataLimitStatus in the client is non-empty, show a banner to the appropriate users.
- Only send DataLimitStatus to users with the appropriate access. There's no risk landing this now since real users will only see null until free team sites are released.
- Update DataLimitStatus immediately in the client when it changes, e.g. when user actions are applied or the product is changed. Right now it's only sent when the document loads.
- Update row limit, grace period start, and data limit status in ActiveDoc when the product changes, i.e. the user upgrades/downgrades.
- Account for data size when computing data limit status, not just row counts.
See also the tasks mentioned in https://phab.getgrist.com/D3331
Test Plan: Extended FreeTeam nbrowser test, testing the 4 statuses.
Reviewers: georgegevoian
Reviewed By: georgegevoian
Differential Revision: https://phab.getgrist.com/D3331
2022-03-24 12:05:51 +00:00
|
|
|
console.log("RECEIVED DOC RESPONSE", openDocResponse);
|
2020-10-02 15:10:00 +00:00
|
|
|
this.docData = new DocData(this.docComm, openDocResponse.doc);
|
|
|
|
this.docModel = new DocModel(this.docData);
|
|
|
|
this.querySetManager = QuerySetManager.create(this, this.docModel, this.docComm);
|
2021-08-05 15:12:46 +00:00
|
|
|
this.docPluginManager = new DocPluginManager(plugins,
|
|
|
|
app.topAppModel.getUntrustedContentOrigin(), this.docComm, app.clientScope);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// Maintain the MetaRowModel for the global document info, including docId and peers.
|
2021-08-26 16:35:11 +00:00
|
|
|
this.docInfo = this.docModel.docInfoRow;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2022-03-21 03:41:59 +00:00
|
|
|
this.hasDocTour = Computed.create(this, use =>
|
|
|
|
use(this.docModel.allTableIds.getObservable()).includes('GristDocTour'));
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
const defaultViewId = this.docInfo.newDefaultViewId;
|
|
|
|
|
|
|
|
// Grainjs observable for current view id, which may be a string such as 'code'.
|
2021-06-10 03:48:39 +00:00
|
|
|
this.activeViewId = Computed.create(this, (use) => {
|
2022-02-10 13:17:46 +00:00
|
|
|
const {docPage} = use(urlState().state);
|
|
|
|
|
|
|
|
// Return most special pages like 'code' and 'acl' as is
|
|
|
|
if (typeof docPage === 'string' && docPage !== 'GristDocTour' && SpecialDocPage.guard(docPage)) {
|
|
|
|
return docPage;
|
2021-07-30 18:05:16 +00:00
|
|
|
}
|
2022-02-10 13:17:46 +00:00
|
|
|
|
|
|
|
// GristDocTour is a special table that is usually hidden from users, but putting /p/GristDocTour
|
|
|
|
// in the URL navigates to it and makes it visible in the list of pages in the sidebar
|
|
|
|
// For GristDocTour, find the view with that name.
|
|
|
|
// Otherwise find the view with the given row ID, because letting a non-existent row ID pass through here is bad.
|
|
|
|
// If no such view exists, return the default view.
|
|
|
|
const viewId = this.docModel.views.tableData.findRow(docPage === 'GristDocTour' ? 'name' : 'id', docPage);
|
|
|
|
return viewId || use(defaultViewId);
|
2021-06-10 03:48:39 +00:00
|
|
|
});
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// This viewModel reflects the currently active view, relying on the fact that
|
|
|
|
// createFloatingRowModel() supports an observable rowId for its argument.
|
|
|
|
// Although typings don't reflect it, createFloatingRowModel() accepts non-numeric values,
|
|
|
|
// which yield an empty row, which is why we can cast activeViewId.
|
|
|
|
this.viewModel = this.autoDispose(
|
|
|
|
this.docModel.views.createFloatingRowModel(toKo(ko, this.activeViewId) as ko.Computed<number>));
|
|
|
|
|
|
|
|
// Grainjs observable reflecting the name of the current document page.
|
|
|
|
this.currentPageName = Computed.create(this, this.activeViewId,
|
|
|
|
(use, docPage) => typeof docPage === 'number' ? use(this.viewModel.name) : docPage);
|
|
|
|
|
|
|
|
// Whenever the active viewModel is deleted, switch to the default view.
|
|
|
|
this.autoDispose(this.viewModel._isDeleted.subscribe((isDeleted) => {
|
|
|
|
if (isDeleted) {
|
|
|
|
// This should not be done synchronously, as that affects the same viewModel that triggered
|
|
|
|
// this callback, and causes some obscure effects on knockout subscriptions.
|
|
|
|
Promise.resolve().then(() => urlState().pushUrl({docPage: undefined})).catch(() => null);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Navigate to an anchor if one is present in the url hash.
|
|
|
|
this.autoDispose(subscribe(urlState().state, async (use, state) => {
|
|
|
|
if (state.hash) {
|
|
|
|
try {
|
2021-05-17 14:05:49 +00:00
|
|
|
const cursorPos = this._getCursorPosFromHash(state.hash);
|
|
|
|
await this.recursiveMoveToCursorPos(cursorPos, true);
|
2020-10-02 15:10:00 +00:00
|
|
|
} catch (e) {
|
|
|
|
reportError(e);
|
|
|
|
} finally {
|
|
|
|
setTimeout(finalizeAnchor, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2021-07-19 08:49:44 +00:00
|
|
|
// Start welcome tour if flag is present in the url hash.
|
2022-03-21 03:41:59 +00:00
|
|
|
let tourStarting = false;
|
2021-07-19 08:49:44 +00:00
|
|
|
this.autoDispose(subscribe(urlState().state, async (_use, state) => {
|
2022-03-21 03:41:59 +00:00
|
|
|
// Onboarding tours were not designed with mobile support in mind. Disable until fixed.
|
|
|
|
if (isNarrowScreen()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// If we have an active tour (or are in the process of starting one), don't start a new one.
|
|
|
|
if (tourStarting || isTourActive()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const autoStartDocTour = this.hasDocTour.get() && !this._seenDocTours.get()?.includes(this.docId());
|
|
|
|
const docTour = state.docTour || autoStartDocTour;
|
|
|
|
const welcomeTour = state.welcomeTour || this._shouldAutoStartWelcomeTour();
|
2021-07-30 15:16:33 +00:00
|
|
|
|
2022-03-21 03:41:59 +00:00
|
|
|
if (welcomeTour || docTour) {
|
|
|
|
tourStarting = true;
|
|
|
|
try {
|
|
|
|
await this._waitForView();
|
2021-07-30 15:16:33 +00:00
|
|
|
|
2022-03-21 03:41:59 +00:00
|
|
|
// Remove any tour-related hash-tags from the URL. So #repeat-welcome-tour and
|
|
|
|
// #repeat-doc-tour are used as triggers, but will immediately disappear.
|
|
|
|
await urlState().pushUrl({welcomeTour: false, docTour: false},
|
|
|
|
{replace: true, avoidReload: true});
|
|
|
|
|
|
|
|
if (!docTour) {
|
|
|
|
startWelcomeTour(() => this._showGristTour.set(false));
|
|
|
|
} else {
|
|
|
|
const onFinishCB = () => (autoStartDocTour && markAsSeen(this._seenDocTours, this.docId()));
|
|
|
|
await startDocTour(this.docData, this.docComm, onFinishCB);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
tourStarting = false;
|
2021-07-23 16:24:17 +00:00
|
|
|
}
|
2021-07-19 08:49:44 +00:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// Importer takes a function for creating previews.
|
|
|
|
const createPreview = (vs: ViewSectionRec) => GridView.create(this, vs, true);
|
|
|
|
|
|
|
|
const importSourceElems = ImportSourceElement.fromArray(this.docPluginManager.pluginsList);
|
|
|
|
const importMenuItems = [
|
|
|
|
{
|
|
|
|
label: 'Import from file',
|
2021-08-03 10:34:05 +00:00
|
|
|
action: () => Importer.selectAndImport(this, importSourceElems, null, createPreview),
|
2020-10-02 15:10:00 +00:00
|
|
|
},
|
|
|
|
...importSourceElems.map(importSourceElem => ({
|
|
|
|
label: importSourceElem.importSource.label,
|
2021-08-03 10:34:05 +00:00
|
|
|
action: () => Importer.selectAndImport(this, importSourceElems, importSourceElem, createPreview)
|
2020-10-02 15:10:00 +00:00
|
|
|
}))
|
|
|
|
];
|
|
|
|
|
|
|
|
// Set the available import sources in the DocPageModel.
|
|
|
|
this.docPageModel.importSources = importMenuItems;
|
|
|
|
|
|
|
|
this._actionLog = this.autoDispose(ActionLog.create({ gristDoc: this }));
|
|
|
|
this._undoStack = this.autoDispose(UndoStack.create(openDocResponse.log, { gristDoc: this }));
|
|
|
|
this._docHistory = DocHistory.create(this, this.docPageModel, this._actionLog);
|
|
|
|
|
|
|
|
// Tap into docData's sendActions method to save the cursor position with every action, so that
|
|
|
|
// undo/redo can jump to the right place.
|
|
|
|
this.autoDispose(this.docData.sendActionsEmitter.addListener(this._onSendActionsStart, this));
|
|
|
|
this.autoDispose(this.docData.sendActionsDoneEmitter.addListener(this._onSendActionsEnd, this));
|
|
|
|
|
|
|
|
/* Command binding */
|
|
|
|
this.autoDispose(commands.createGroup({
|
2021-01-27 19:38:12 +00:00
|
|
|
undo() { this._undoStack.sendUndoAction().catch(reportError); },
|
|
|
|
redo() { this._undoStack.sendRedoAction().catch(reportError); },
|
2020-10-02 15:10:00 +00:00
|
|
|
reloadPlugins() { this.docComm.reloadPlugins().then(() => G.window.location.reload(false)); },
|
|
|
|
}, this, true));
|
|
|
|
|
|
|
|
this.listenTo(app.comm, 'docUserAction', this.onDocUserAction);
|
|
|
|
|
|
|
|
this.autoDispose(DocConfigTab.create({gristDoc: this}));
|
|
|
|
|
|
|
|
this.rightPanelTool = Computed.create(this, (use) => this._getToolContent(use(this._rightPanelTool)));
|
|
|
|
|
|
|
|
this.comparison = options.comparison || null;
|
|
|
|
|
|
|
|
// We need prevent default here to allow drop events to fire.
|
|
|
|
this.autoDispose(dom.onElem(window, 'dragover', (ev) => ev.preventDefault()));
|
|
|
|
// The default action is to open dragged files as a link, navigating out of the app.
|
|
|
|
this.autoDispose(dom.onElem(window, 'drop', (ev) => ev.preventDefault()));
|
2021-02-04 03:17:17 +00:00
|
|
|
|
|
|
|
// On window resize, trigger the resizeEmitter to update ViewLayout and individual BaseViews.
|
|
|
|
this.autoDispose(dom.onElem(window, 'resize', () => this.resizeEmitter.emit()));
|
2021-05-17 14:05:49 +00:00
|
|
|
|
|
|
|
// create current view observer
|
|
|
|
this.currentView = Observable.create<BaseView | null>(this, null);
|
2021-06-09 15:07:16 +00:00
|
|
|
|
|
|
|
// create computed observable for viewInstance - if it is loaded or not
|
|
|
|
|
|
|
|
// Add an artificial intermediary computed only to delay the evaluation of currentView, so
|
|
|
|
// that it happens after section.viewInstance is set. If it happens before, then
|
|
|
|
// section.viewInstance is seen as null, and as it gets updated, GrainJS refuses to
|
|
|
|
// recalculate this computed since it was already calculated in the same tick.
|
|
|
|
const activeViewId = Computed.create(this, (use) => use(this.activeViewId));
|
2021-05-17 14:05:49 +00:00
|
|
|
const viewInstance = Computed.create(this, (use) => {
|
|
|
|
const section = use(this.viewModel.activeSection);
|
2021-06-09 15:07:16 +00:00
|
|
|
const viewId = use(activeViewId);
|
2021-05-17 14:05:49 +00:00
|
|
|
const view = use(section.viewInstance);
|
2022-03-22 12:38:56 +00:00
|
|
|
return isViewDocPage(viewId) ? view : null;
|
2021-05-17 14:05:49 +00:00
|
|
|
});
|
|
|
|
// then listen if the view is present, because we still need to wait for it load properly
|
|
|
|
this.autoDispose(viewInstance.addListener(async (view) => {
|
2022-03-22 12:38:56 +00:00
|
|
|
if (view) {
|
|
|
|
await view.getLoadingDonePromise();
|
|
|
|
}
|
2021-06-09 15:07:16 +00:00
|
|
|
// finally set the current view as fully loaded
|
2021-05-17 14:05:49 +00:00
|
|
|
this.currentView.set(view);
|
2021-05-23 17:43:11 +00:00
|
|
|
}));
|
2021-05-17 14:05:49 +00:00
|
|
|
|
|
|
|
// create observable for current cursor position
|
|
|
|
this.cursorPosition = Computed.create<ViewCursorPos | undefined>(this, use => {
|
|
|
|
// get the BaseView
|
|
|
|
const view = use(viewInstance);
|
2021-05-23 17:43:11 +00:00
|
|
|
if (!view) { return undefined; }
|
2021-05-17 14:05:49 +00:00
|
|
|
// get current viewId
|
|
|
|
const viewId = use(this.activeViewId);
|
2022-03-22 12:38:56 +00:00
|
|
|
if (!isViewDocPage(viewId)) { return undefined; }
|
2021-05-17 14:05:49 +00:00
|
|
|
// read latest position
|
|
|
|
const currentPosition = use(view.cursor.currentPosition);
|
2021-05-23 17:43:11 +00:00
|
|
|
if (currentPosition) { return { ...currentPosition, viewId }; }
|
2021-05-17 14:05:49 +00:00
|
|
|
return undefined;
|
|
|
|
});
|
|
|
|
|
2021-07-22 09:14:35 +00:00
|
|
|
this.hasCustomNav = Computed.create(this, urlState().state, (_, state) => {
|
|
|
|
const hash = state.hash;
|
|
|
|
return !!(hash && (undef(hash.colRef, hash.rowId, hash.sectionId) !== undefined));
|
|
|
|
});
|
|
|
|
|
2021-05-25 09:24:00 +00:00
|
|
|
this.draftMonitor = Drafts.create(this, this);
|
2021-05-17 14:05:49 +00:00
|
|
|
this.cursorMonitor = CursorMonitor.create(this, this);
|
|
|
|
this.editorMonitor = EditorMonitor.create(this, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns current document's id
|
|
|
|
*/
|
|
|
|
public docId() {
|
|
|
|
return this.docPageModel.currentDocId.get()!;
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public addOptionsTab(label: string, iconElem: any, contentObj: TabContent[], options: TabOptions): IDisposable {
|
|
|
|
this._rightPanelTabs.set(label, contentObj);
|
|
|
|
// Return a do-nothing disposable, to satisfy the previous interface.
|
|
|
|
return {dispose: () => null};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the DOM for this GristDoc.
|
|
|
|
*/
|
|
|
|
public buildDom() {
|
|
|
|
return cssViewContentPane(testId('gristdoc'),
|
2022-03-11 10:48:36 +00:00
|
|
|
cssViewContentPane.cls("-contents", use => use(this.activeViewId) === 'data'),
|
2020-10-02 15:10:00 +00:00
|
|
|
dom.domComputed<IDocPage>(this.activeViewId, (viewId) => (
|
|
|
|
viewId === 'code' ? dom.create((owner) => owner.autoDispose(CodeEditorPanel.create(this))) :
|
2020-12-04 23:29:29 +00:00
|
|
|
viewId === 'acl' ? dom.create((owner) => owner.autoDispose(AccessRules.create(this, this))) :
|
2022-04-01 18:17:06 +00:00
|
|
|
viewId === 'data' ? dom.create((owner) => owner.autoDispose(RawData.create(this, this))) :
|
2022-02-03 15:08:25 +00:00
|
|
|
viewId === 'GristDocTour' ? null :
|
2020-10-02 15:10:00 +00:00
|
|
|
dom.create((owner) => (this._viewLayout = ViewLayout.create(owner, this, viewId)))
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the given page. Note that links to pages should use <a> elements together with setLinkUrl().
|
|
|
|
public openDocPage(viewId: IDocPage) {
|
|
|
|
return urlState().pushUrl({docPage: viewId});
|
|
|
|
}
|
|
|
|
|
|
|
|
public showTool(tool: typeof RightPanelTool.type): void {
|
|
|
|
this._rightPanelTool.set(tool);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an object representing the position of the cursor, including the section. It will have
|
|
|
|
* fields { sectionId, rowId, fieldIndex }. Fields may be missing if no section is active.
|
|
|
|
*/
|
|
|
|
public getCursorPos(): CursorPos {
|
|
|
|
const pos = { sectionId: this.viewModel.activeSectionId() };
|
|
|
|
const viewInstance = this.viewModel.activeSection.peek().viewInstance.peek();
|
|
|
|
return Object.assign(pos, viewInstance ? viewInstance.cursor.getCursorPos() : {});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Switch to the view/section and scroll to the record indicated by cursorPos. If cursorPos is
|
|
|
|
* null, then moves to a position best suited for optActionGroup (not yet implemented).
|
|
|
|
*/
|
2021-09-29 13:57:55 +00:00
|
|
|
public async moveToCursorPos(cursorPos?: CursorPos, optActionGroup?: MinimalActionGroup): Promise<void> {
|
2020-10-02 15:10:00 +00:00
|
|
|
if (!cursorPos || cursorPos.sectionId == null) {
|
|
|
|
// TODO We could come up with a suitable cursorPos here based on the action itself.
|
|
|
|
// This should only come up if trying to undo/redo after reloading a page (since the cursorPos
|
|
|
|
// associated with the action is only stored in memory of the current JS process).
|
|
|
|
// A function like `getCursorPosForActionGroup(ag)` would also be useful to jump to the best
|
|
|
|
// place from any action in the action log.
|
|
|
|
return;
|
|
|
|
}
|
2021-05-17 14:05:49 +00:00
|
|
|
try {
|
2021-05-23 17:43:11 +00:00
|
|
|
const viewInstance = await this._switchToSectionId(cursorPos.sectionId);
|
2021-05-17 14:05:49 +00:00
|
|
|
if (viewInstance) {
|
|
|
|
viewInstance.setCursorPos(cursorPos);
|
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
reportError(e);
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process actions received from the server by forwarding them to `docData.receiveAction()` and
|
|
|
|
* pushing them to actionLog.
|
|
|
|
*/
|
|
|
|
public onDocUserAction(message: DocUserAction) {
|
|
|
|
console.log("GristDoc.onDocUserAction", message);
|
|
|
|
let schemaUpdated = false;
|
2021-03-01 16:51:30 +00:00
|
|
|
/**
|
|
|
|
* If an operation is applied successfully to a document, and then information about
|
|
|
|
* it is broadcast to clients, and one of those broadcasts has a failure (due to
|
|
|
|
* granular access control, which is client-specific), then that error is logged on
|
|
|
|
* the server and also sent to the client via an `error` field. Under normal operation,
|
|
|
|
* there should be no such errors, but if they do arise it is best to make them as visible
|
|
|
|
* as possible.
|
|
|
|
*/
|
|
|
|
if (message.data.error) {
|
|
|
|
reportError(new Error(message.data.error));
|
|
|
|
return;
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
if (this.docComm.isActionFromThisDoc(message)) {
|
|
|
|
const docActions = message.data.docActions;
|
|
|
|
for (let i = 0, len = docActions.length; i < len; i++) {
|
|
|
|
console.log("GristDoc applying #%d", i, docActions[i]);
|
|
|
|
this.docData.receiveAction(docActions[i]);
|
|
|
|
this.docPluginManager.receiveAction(docActions[i]);
|
|
|
|
|
|
|
|
if (!schemaUpdated && isSchemaAction(docActions[i])) {
|
|
|
|
schemaUpdated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add fromSelf property to actionGroup indicating if it's from the current session.
|
|
|
|
const actionGroup = message.data.actionGroup;
|
|
|
|
actionGroup.fromSelf = message.fromSelf || false;
|
|
|
|
// Push to the actionLog and the undoStack.
|
|
|
|
if (!actionGroup.internal) {
|
|
|
|
this._actionLog.pushAction(actionGroup);
|
|
|
|
this._undoStack.pushAction(actionGroup);
|
|
|
|
if (actionGroup.fromSelf) {
|
|
|
|
this._lastOwnActionGroup = actionGroup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (schemaUpdated) {
|
|
|
|
this.trigger('schemaUpdateAction', docActions);
|
|
|
|
}
|
2022-03-14 18:16:30 +00:00
|
|
|
if (typeof actionGroup.rowCount === "number") {
|
|
|
|
this.docPageModel.rowCount.set(actionGroup.rowCount);
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public getTableModel(tableId: string): DataTableModel {
|
|
|
|
return this.docModel.dataTables[tableId];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a DataTableModel, possibly wrapped to include diff data if a comparison is
|
|
|
|
// in effect.
|
|
|
|
public getTableModelMaybeWithDiff(tableId: string): DataTableModel {
|
|
|
|
const tableModel = this.getTableModel(tableId);
|
|
|
|
if (!this.comparison?.details) { return tableModel; }
|
|
|
|
// TODO: cache wrapped models and share between views.
|
|
|
|
return new DataTableModelWithDiff(tableModel, this.comparison.details);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends an action to create a new empty table and switches to that table's primary view.
|
|
|
|
*/
|
|
|
|
public async addEmptyTable(): Promise<void> {
|
|
|
|
const tableInfo = await this.docData.sendAction(['AddEmptyTable']);
|
|
|
|
await this.openDocPage(this.docModel.tables.getRowModel(tableInfo.id).primaryViewId());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a view section described by val to the current page.
|
|
|
|
*/
|
|
|
|
public async addWidgetToPage(val: IPageWidget) {
|
|
|
|
const docData = this.docModel.docData;
|
|
|
|
const viewName = this.viewModel.name.peek();
|
|
|
|
|
|
|
|
const res = await docData.bundleActions(
|
|
|
|
`Added new linked section to view ${viewName}`,
|
|
|
|
() => this.addWidgetToPageImpl(val)
|
|
|
|
);
|
|
|
|
|
|
|
|
// The newly-added section should be given focus.
|
|
|
|
this.viewModel.activeSectionId(res.sectionRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The actual implementation of addWidgetToPage
|
|
|
|
*/
|
|
|
|
public async addWidgetToPageImpl(val: IPageWidget) {
|
|
|
|
const viewRef = this.activeViewId.get();
|
|
|
|
const tableRef = val.table === 'New Table' ? 0 : val.table;
|
|
|
|
const link = linkFromId(val.link);
|
|
|
|
|
|
|
|
const result = await this.docData.sendAction(
|
|
|
|
['CreateViewSection', tableRef, viewRef, val.type, val.summarize ? val.columns : null]
|
|
|
|
);
|
2022-01-17 16:08:43 +00:00
|
|
|
if (val.type === 'chart') {
|
|
|
|
await this._ensureOneNumericSeries(result.sectionRef);
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
await this.docData.sendAction(
|
|
|
|
['UpdateRecord', '_grist_Views_section', result.sectionRef, {
|
|
|
|
linkSrcSectionRef: link.srcSectionRef,
|
|
|
|
linkSrcColRef: link.srcColRef,
|
|
|
|
linkTargetColRef: link.targetColRef
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new page (aka: view) with a single view section (aka: page widget) described by `val`.
|
|
|
|
*/
|
|
|
|
public async addNewPage(val: IPageWidget) {
|
|
|
|
if (val.table === 'New Table') {
|
|
|
|
const result = await this.docData.sendAction(['AddEmptyTable']);
|
|
|
|
await this.openDocPage(result.views[0].id);
|
|
|
|
} else {
|
2022-01-17 16:08:43 +00:00
|
|
|
let result: any;
|
|
|
|
await this.docData.bundleActions(`Add new page`, async () => {
|
|
|
|
result = await this.docData.sendAction(
|
|
|
|
['CreateViewSection', val.table, 0, val.type, val.summarize ? val.columns : null]
|
|
|
|
);
|
|
|
|
if (val.type === 'chart') {
|
|
|
|
await this._ensureOneNumericSeries(result.sectionRef);
|
|
|
|
}
|
|
|
|
});
|
2020-10-02 15:10:00 +00:00
|
|
|
await this.openDocPage(result.viewRef);
|
|
|
|
// The newly-added section should be given focus.
|
|
|
|
this.viewModel.activeSectionId(result.sectionRef);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens a dialog to upload one or multiple files as tables and then switches to the first table's
|
|
|
|
* primary view.
|
|
|
|
*/
|
|
|
|
public async uploadNewTable(): Promise<void> {
|
|
|
|
const uploadResult = await selectFiles({docWorkerUrl: this.docComm.docWorkerUrl,
|
|
|
|
multiple: true});
|
|
|
|
if (uploadResult) {
|
|
|
|
const dataSource = {uploadId: uploadResult.uploadId, transforms: []};
|
2021-09-15 06:12:34 +00:00
|
|
|
const importResult = await this.docComm.finishImportFiles(dataSource, [], {});
|
2020-10-02 15:10:00 +00:00
|
|
|
const tableId = importResult.tables[0].hiddenTableId;
|
|
|
|
const tableRowModel = this.docModel.dataTables[tableId].tableMetaRow;
|
|
|
|
await this.openDocPage(tableRowModel.primaryViewId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async saveViewSection(section: ViewSectionRec, newVal: IPageWidget) {
|
|
|
|
const docData = this.docModel.docData;
|
|
|
|
const oldVal: IPageWidget = toPageWidget(section);
|
|
|
|
const viewModel = section.view();
|
|
|
|
|
|
|
|
if (isEqual(oldVal, newVal)) {
|
|
|
|
// nothing to be done
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this._viewLayout!.freezeUntil(docData.bundleActions(
|
|
|
|
`Saved linked section ${section.title()} in view ${viewModel.name()}`,
|
|
|
|
async () => {
|
|
|
|
|
|
|
|
// if table changes or a table is made a summary table, let's replace the view section by a
|
|
|
|
// new one, and return.
|
|
|
|
if (oldVal.table !== newVal.table || oldVal.summarize !== newVal.summarize) {
|
|
|
|
await this._replaceViewSection(section, newVal);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if type changes, let's save it.
|
|
|
|
if (oldVal.type !== newVal.type) {
|
|
|
|
await section.parentKey.saveOnly(newVal.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if grouped by column changes, let's use the specific user action.
|
|
|
|
if (!isEqual(oldVal.columns, newVal.columns)) {
|
|
|
|
await docData.sendAction(
|
|
|
|
['UpdateSummaryViewSection', section.getRowId(), newVal.columns]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// update link
|
|
|
|
if (oldVal.link !== newVal.link) {
|
|
|
|
await this.saveLink(linkFromId(newVal.link));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save link for the active section.
|
|
|
|
public async saveLink(link: IPageWidgetLink) {
|
|
|
|
const viewModel = this.viewModel;
|
|
|
|
return this.docData.sendAction(
|
|
|
|
['UpdateRecord', '_grist_Views_section', viewModel.activeSection.peek().getRowId(), {
|
|
|
|
linkSrcSectionRef: link.srcSectionRef,
|
|
|
|
linkSrcColRef: link.srcColRef,
|
|
|
|
linkTargetColRef: link.targetColRef
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Returns the list of all the valid links to link from one of the sections in the active view to
|
|
|
|
// the page widget 'widget'.
|
|
|
|
public selectBy(widget: IPageWidget) {
|
|
|
|
const viewSections = this.viewModel.viewSections.peek().peek();
|
|
|
|
return selectBy(this.docModel, viewSections, widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fork the document if it is in prefork mode.
|
|
|
|
public async forkIfNeeded() {
|
|
|
|
if (this.docPageModel.isPrefork.get()) {
|
|
|
|
await this.docComm.forkAndUpdateUrl();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-17 03:45:44 +00:00
|
|
|
// Turn the given columns into empty columns, losing any data stored in them.
|
|
|
|
public async clearColumns(colRefs: number[]): Promise<void> {
|
|
|
|
await this.docModel.columns.sendTableAction(
|
|
|
|
['BulkUpdateRecord', colRefs, {
|
|
|
|
isFormula: colRefs.map(f => true),
|
|
|
|
formula: colRefs.map(f => ''),
|
2021-06-29 04:47:59 +00:00
|
|
|
// Set recalc settings to defaults when emptying a column.
|
|
|
|
recalcWhen: colRefs.map(f => RecalcWhen.DEFAULT),
|
|
|
|
recalcDeps: colRefs.map(f => null),
|
2021-03-17 03:45:44 +00:00
|
|
|
}]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the given columns to data, saving the calculated values and unsetting the formulas.
|
2021-06-29 04:47:59 +00:00
|
|
|
public async convertIsFormula(colRefs: number[], opts: {toFormula: boolean, noRecalc?: boolean}): Promise<void> {
|
2021-03-17 03:45:44 +00:00
|
|
|
return this.docModel.columns.sendTableAction(
|
|
|
|
['BulkUpdateRecord', colRefs, {
|
2021-06-29 04:47:59 +00:00
|
|
|
isFormula: colRefs.map(f => opts.toFormula),
|
|
|
|
recalcWhen: colRefs.map(f => opts.noRecalc ? RecalcWhen.NEVER : RecalcWhen.DEFAULT),
|
|
|
|
recalcDeps: colRefs.map(f => null),
|
2021-03-17 03:45:44 +00:00
|
|
|
}]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-30 08:59:04 +00:00
|
|
|
// Updates formula for a column.
|
|
|
|
public async updateFormula(colRef: number, formula: string): Promise<void> {
|
|
|
|
return this.docModel.columns.sendTableAction(
|
|
|
|
['UpdateRecord', colRef, {
|
|
|
|
formula,
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-05 10:25:05 +00:00
|
|
|
// Convert column to pure formula column.
|
2021-11-30 08:59:04 +00:00
|
|
|
public async convertToFormula(colRef: number, formula: string): Promise<void> {
|
2021-11-05 10:25:05 +00:00
|
|
|
return this.docModel.columns.sendTableAction(
|
2021-11-30 08:59:04 +00:00
|
|
|
['UpdateRecord', colRef, {
|
2021-11-05 10:25:05 +00:00
|
|
|
isFormula: true,
|
|
|
|
formula,
|
|
|
|
recalcWhen: RecalcWhen.DEFAULT,
|
|
|
|
recalcDeps: null,
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert column to data column with a trigger formula
|
|
|
|
public async convertToTrigger(colRefs: number, formula: string): Promise<void> {
|
|
|
|
return this.docModel.columns.sendTableAction(
|
|
|
|
['UpdateRecord', colRefs, {
|
|
|
|
isFormula: false,
|
|
|
|
formula,
|
|
|
|
recalcWhen: RecalcWhen.DEFAULT,
|
|
|
|
recalcDeps: null,
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
public getCsvLink() {
|
2021-11-19 20:30:11 +00:00
|
|
|
const filters = this.viewModel.activeSection.peek().activeFilters.get().map(filterInfo => ({
|
|
|
|
colRef : filterInfo.fieldOrColumn.origCol().origColRef(),
|
|
|
|
filter : filterInfo.filter()
|
2021-05-27 11:06:26 +00:00
|
|
|
}));
|
2021-08-30 20:06:40 +00:00
|
|
|
|
2021-05-27 11:06:26 +00:00
|
|
|
const params = {
|
2020-10-02 15:10:00 +00:00
|
|
|
viewSection: this.viewModel.activeSectionId(),
|
|
|
|
tableId: this.viewModel.activeSection().table().tableId(),
|
2021-05-27 11:06:26 +00:00
|
|
|
activeSortSpec: JSON.stringify(this.viewModel.activeSection().activeSortSpec()),
|
|
|
|
filters : JSON.stringify(filters),
|
|
|
|
};
|
2021-09-01 21:07:53 +00:00
|
|
|
|
|
|
|
return this.docPageModel.appModel.api.getDocAPI(this.docId()).getDownloadCsvUrl(params);
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 23:15:34 +00:00
|
|
|
public hasGranularAccessRules(): boolean {
|
2021-12-07 11:21:16 +00:00
|
|
|
const rulesTable = this.docData.getMetaTable('_grist_ACLRules');
|
2021-03-25 23:15:34 +00:00
|
|
|
// To check if there are rules, ignore the default no-op rule created for an older incarnation
|
|
|
|
// of ACLs. It exists in older documents, and is still created for new ones. We detect it by
|
|
|
|
// the use of the deprecated 'permissions' field, and not the new 'permissionsText' field.
|
|
|
|
return rulesTable.numRecords() > rulesTable.filterRowIds({permissionsText: '', permissions: 63}).length;
|
|
|
|
}
|
|
|
|
|
2021-05-17 14:05:49 +00:00
|
|
|
/**
|
|
|
|
* Move to the desired cursor position. If colRef is supplied, the cursor will be
|
|
|
|
* moved to a field with that colRef. Any linked sections that need their cursors
|
|
|
|
* moved in order to achieve the desired outcome are handled recursively.
|
|
|
|
* If setAsActiveSection is true, the section in cursorPos is set as the current
|
|
|
|
* active section.
|
|
|
|
*/
|
2022-03-22 12:38:56 +00:00
|
|
|
public async recursiveMoveToCursorPos(
|
|
|
|
cursorPos: CursorPos,
|
|
|
|
setAsActiveSection: boolean,
|
|
|
|
silent: boolean = false): Promise<void> {
|
2021-05-17 14:05:49 +00:00
|
|
|
try {
|
|
|
|
if (!cursorPos.sectionId) { throw new Error('sectionId required'); }
|
|
|
|
if (!cursorPos.rowId) { throw new Error('rowId required'); }
|
|
|
|
const section = this.docModel.viewSections.getRowModel(cursorPos.sectionId);
|
|
|
|
const srcSection = section.linkSrcSection.peek();
|
|
|
|
if (srcSection.id.peek()) {
|
|
|
|
// We're in a linked section, so we need to recurse to make sure the row we want
|
|
|
|
// will be visible.
|
|
|
|
const linkTargetCol = section.linkTargetCol.peek();
|
|
|
|
let controller: any;
|
|
|
|
if (linkTargetCol.colId.peek()) {
|
|
|
|
const destTable = await this._getTableData(section);
|
|
|
|
controller = destTable.getValue(cursorPos.rowId, linkTargetCol.colId.peek());
|
|
|
|
} else {
|
|
|
|
controller = cursorPos.rowId;
|
|
|
|
}
|
|
|
|
const colId = section.linkSrcCol.peek().colId.peek();
|
|
|
|
let srcRowId: any;
|
|
|
|
const isSrcSummary = srcSection.table.peek().summarySource.peek().id.peek();
|
|
|
|
if (!colId && !isSrcSummary) {
|
|
|
|
// Simple case - source linked by rowId, not a summary.
|
2021-09-20 20:35:14 +00:00
|
|
|
if (isList(controller)) {
|
|
|
|
// Should be a reference list. Pick the first reference.
|
|
|
|
controller = controller[1]; // [0] is the L type code, [1] is the first value
|
|
|
|
}
|
2021-05-17 14:05:49 +00:00
|
|
|
srcRowId = controller;
|
|
|
|
} else {
|
|
|
|
const srcTable = await this._getTableData(srcSection);
|
2021-09-20 20:35:14 +00:00
|
|
|
const query: ClientQuery = {tableId: srcTable.tableId, filters: {}, operations: {}};
|
|
|
|
if (colId) {
|
2022-02-07 14:02:26 +00:00
|
|
|
query.operations[colId] = isRefListType(section.linkSrcCol.peek().type.peek()) ? 'intersects' : 'in';
|
2021-09-20 20:35:14 +00:00
|
|
|
query.filters[colId] = isList(controller) ? controller.slice(1) : [controller];
|
|
|
|
} else {
|
2021-05-17 14:05:49 +00:00
|
|
|
// must be a summary -- otherwise dealt with earlier.
|
|
|
|
const destTable = await this._getTableData(section);
|
2021-09-20 20:35:14 +00:00
|
|
|
for (const srcCol of srcSection.table.peek().groupByColumns.peek()) {
|
|
|
|
const filterColId = srcCol.summarySource.peek().colId.peek();
|
|
|
|
controller = destTable.getValue(cursorPos.rowId, filterColId);
|
2022-02-07 14:02:26 +00:00
|
|
|
query.operations[filterColId] = 'in';
|
2021-09-20 20:35:14 +00:00
|
|
|
query.filters[filterColId] = isList(controller) ? controller.slice(1) : [controller];
|
2021-05-17 14:05:49 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-20 20:35:14 +00:00
|
|
|
srcRowId = srcTable.getRowIds().find(getFilterFunc(this.docData, query));
|
2021-05-17 14:05:49 +00:00
|
|
|
}
|
|
|
|
if (!srcRowId || typeof srcRowId !== 'number') { throw new Error('cannot trace rowId'); }
|
|
|
|
await this.recursiveMoveToCursorPos({
|
|
|
|
rowId: srcRowId,
|
2022-03-22 12:38:56 +00:00
|
|
|
sectionId: srcSection.id.peek(),
|
|
|
|
}, false, silent);
|
2021-05-17 14:05:49 +00:00
|
|
|
}
|
|
|
|
const view: ViewRec = section.view.peek();
|
2022-03-22 12:38:56 +00:00
|
|
|
const docPage: ViewDocPage = section.isRaw.peek() ? "data" : view.getRowId();
|
|
|
|
if (docPage != this.activeViewId.get()) { await this.openDocPage(docPage); }
|
2021-05-17 14:05:49 +00:00
|
|
|
if (setAsActiveSection) { view.activeSectionId(cursorPos.sectionId); }
|
|
|
|
const fieldIndex = cursorPos.fieldIndex;
|
|
|
|
const viewInstance = await waitObs(section.viewInstance);
|
|
|
|
if (!viewInstance) { throw new Error('view not found'); }
|
|
|
|
// Give any synchronous initial cursor setting a chance to happen.
|
|
|
|
await delay(0);
|
|
|
|
viewInstance.setCursorPos({ ...cursorPos, fieldIndex });
|
|
|
|
// TODO: column selection not working on card/detail view, or getting overridden -
|
|
|
|
// look into it (not a high priority for now since feature not easily discoverable
|
|
|
|
// in this view).
|
2021-05-19 11:56:31 +00:00
|
|
|
|
|
|
|
// even though the cursor is at right place, the scroll could not have yet happened
|
|
|
|
// wait for a bit (scroll is done in a setTimeout 0)
|
|
|
|
await delay(0);
|
2021-05-17 14:05:49 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.debug(`_recursiveMoveToCursorPos(${JSON.stringify(cursorPos)}): ${e}`);
|
2022-03-22 12:38:56 +00:00
|
|
|
if (!silent) {
|
|
|
|
throw new UserError('There was a problem finding the desired cell.');
|
|
|
|
}
|
2021-05-17 14:05:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens up an editor at cursor position
|
|
|
|
* @param input Optional. Cell's initial value
|
|
|
|
*/
|
|
|
|
public async activateEditorAtCursor(options: { init?: string, state?: any}) {
|
2021-05-23 17:43:11 +00:00
|
|
|
const view = await this._waitForView();
|
2021-05-17 14:05:49 +00:00
|
|
|
view?.activateEditorAtCursor(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Waits for a view to be ready
|
|
|
|
*/
|
2021-05-23 17:43:11 +00:00
|
|
|
private async _waitForView() {
|
2022-03-21 03:41:59 +00:00
|
|
|
// For pages like ACL's, there isn't a view instance to wait for.
|
|
|
|
if (!this.viewModel.activeSection.peek().getRowId()) {
|
2022-03-22 12:38:56 +00:00
|
|
|
return null;
|
2022-03-21 03:41:59 +00:00
|
|
|
}
|
2021-05-17 14:05:49 +00:00
|
|
|
const view = await waitObs(this.viewModel.activeSection.peek().viewInstance);
|
2022-03-22 12:38:56 +00:00
|
|
|
if (!view) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
await view.getLoadingDonePromise();
|
|
|
|
// Wait extra bit for scroll to happen.
|
|
|
|
await delay(0);
|
2021-05-17 14:05:49 +00:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
private _getToolContent(tool: typeof RightPanelTool.type): IExtraTool | null {
|
2020-10-02 15:10:00 +00:00
|
|
|
switch (tool) {
|
|
|
|
case 'docHistory': {
|
|
|
|
return {icon: 'Log', label: 'Document History', content: this._docHistory};
|
|
|
|
}
|
|
|
|
case 'validations': {
|
|
|
|
const content = this._rightPanelTabs.get("Validate Data");
|
|
|
|
return content ? {icon: 'Validation', label: 'Validation Rules', content} : null;
|
|
|
|
}
|
|
|
|
case 'none':
|
|
|
|
default: {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _replaceViewSection(section: ViewSectionRec, newVal: IPageWidget) {
|
|
|
|
|
|
|
|
const docModel = this.docModel;
|
|
|
|
const viewModel = section.view();
|
|
|
|
const docData = this.docModel.docData;
|
|
|
|
|
|
|
|
// we must read the current layout from the view layout because it can override the one in
|
|
|
|
// `section.layoutSpec` (in particular it provides a default layout when missing from the
|
|
|
|
// latter).
|
|
|
|
const layoutSpec = this._viewLayout!.layoutSpec();
|
|
|
|
|
|
|
|
const sectionTitle = section.title();
|
|
|
|
const sectionId = section.id();
|
|
|
|
|
|
|
|
// create a new section
|
|
|
|
const sectionCreationResult = await this.addWidgetToPageImpl(newVal);
|
|
|
|
|
|
|
|
// update section name
|
|
|
|
const newSection: ViewSectionRec = docModel.viewSections.getRowModel(sectionCreationResult.sectionRef);
|
|
|
|
await newSection.title.saveOnly(sectionTitle);
|
|
|
|
|
|
|
|
// replace old section id with new section id in the layout spec and save
|
|
|
|
const newLayoutSpec = cloneDeepWith(layoutSpec, (val) => {
|
|
|
|
if (typeof val === 'object' && val.leaf === sectionId) {
|
|
|
|
return {...val, leaf: newSection.id()};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await viewModel.layoutSpec.saveOnly(JSON.stringify(newLayoutSpec));
|
|
|
|
|
|
|
|
// The newly-added section should be given focus.
|
|
|
|
this.viewModel.activeSectionId(newSection.getRowId());
|
|
|
|
|
|
|
|
// remove old section
|
|
|
|
await docData.sendAction(['RemoveViewSection', sectionId]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper called before an action is sent to the server. It saves cursor position to come back to
|
|
|
|
* in case of Undo.
|
|
|
|
*/
|
|
|
|
private _onSendActionsStart(ev: {cursorPos: CursorPos}) {
|
|
|
|
this._lastOwnActionGroup = null;
|
|
|
|
ev.cursorPos = this.getCursorPos();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper called when server responds to an action. It attaches the saved cursor position to the
|
|
|
|
* received action (if any), and stores also the resulting position.
|
|
|
|
*/
|
|
|
|
private _onSendActionsEnd(ev: {cursorPos: CursorPos}) {
|
|
|
|
const a = this._lastOwnActionGroup;
|
|
|
|
if (a) {
|
|
|
|
a.cursorPos = ev.cursorPos;
|
|
|
|
if (a.rowIdHint) {
|
|
|
|
a.cursorPos.rowId = a.rowIdHint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Switch to a given sectionId, wait for it to load, and return a Promise for the instantiated
|
|
|
|
* viewInstance (such as an instance of GridView or DetailView).
|
|
|
|
*/
|
|
|
|
private async _switchToSectionId(sectionId: number) {
|
|
|
|
const section: ViewSectionRec = this.docModel.viewSections.getRowModel(sectionId);
|
2022-03-24 18:33:53 +00:00
|
|
|
if (section.isRaw.peek()) {
|
2022-03-11 10:48:36 +00:00
|
|
|
// This is raw data view
|
|
|
|
await urlState().pushUrl({docPage: 'data'});
|
|
|
|
this.viewModel.activeSectionId(sectionId);
|
|
|
|
} else {
|
2022-03-24 18:33:53 +00:00
|
|
|
const view: ViewRec = section.view.peek();
|
2022-03-11 10:48:36 +00:00
|
|
|
await this.openDocPage(view.getRowId());
|
|
|
|
view.activeSectionId(sectionId); // this.viewModel will reflect this with a delay.
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// Returns the value of section.viewInstance() as soon as it is truthy.
|
|
|
|
return waitObs(section.viewInstance);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _getTableData(section: ViewSectionRec): Promise<TableData> {
|
|
|
|
const viewInstance = await waitObs(section.viewInstance);
|
|
|
|
if (!viewInstance) { throw new Error('view not found'); }
|
|
|
|
await viewInstance.getLoadingDonePromise();
|
|
|
|
const table = this.docData.getTable(section.table.peek().tableId.peek());
|
|
|
|
if (!table) { throw new Error('no section table'); }
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
2021-05-17 14:05:49 +00:00
|
|
|
/**
|
|
|
|
* Convert a url hash to a cursor position.
|
|
|
|
*/
|
|
|
|
private _getCursorPosFromHash(hash: HashLink): CursorPos {
|
2021-05-23 17:43:11 +00:00
|
|
|
const cursorPos: CursorPos = { rowId: hash.rowId, sectionId: hash.sectionId };
|
2021-05-17 14:05:49 +00:00
|
|
|
if (cursorPos.sectionId != undefined && hash.colRef !== undefined){
|
|
|
|
// translate colRef to a fieldIndex
|
|
|
|
const section = this.docModel.viewSections.getRowModel(cursorPos.sectionId);
|
|
|
|
const fieldIndex = section.viewFields.peek().all()
|
|
|
|
.findIndex(x=> x.colRef.peek() == hash.colRef);
|
2021-05-23 17:43:11 +00:00
|
|
|
if (fieldIndex >= 0) { cursorPos.fieldIndex = fieldIndex; }
|
2021-05-17 14:05:49 +00:00
|
|
|
}
|
|
|
|
return cursorPos;
|
|
|
|
}
|
2021-07-30 15:16:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* For first-time users on personal org, start a welcome tour.
|
|
|
|
*/
|
|
|
|
private _shouldAutoStartWelcomeTour(): boolean {
|
2022-03-21 03:41:59 +00:00
|
|
|
// When both a docTour and grist welcome tour are available, show only the docTour, leaving
|
|
|
|
// the welcome tour for another doc (e.g. a new one).
|
|
|
|
if (this.hasDocTour.get()) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-07-30 15:16:33 +00:00
|
|
|
|
|
|
|
// Only show the tour if one is on a personal org and can edit. This excludes templates (on
|
|
|
|
// the Templates org, which may have their own tour) and team sites (where user's intended
|
|
|
|
// role is often other than document creator).
|
|
|
|
const appModel = this.docPageModel.appModel;
|
|
|
|
if (!appModel.currentOrg?.owner || this.isReadonly.get()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Use the showGristTour pref if set; otherwise default to true for anonymous users, and false
|
|
|
|
// for real returning users.
|
|
|
|
return this._showGristTour.get() ?? (!appModel.currentValidUser);
|
|
|
|
}
|
2022-01-17 16:08:43 +00:00
|
|
|
|
|
|
|
/**
|
2022-01-19 11:13:18 +00:00
|
|
|
* Makes sure that the first y-series (ie: the view fields at index 1) is a numeric series. Does
|
|
|
|
* not handle chart with the group by option on: it is only intended to be used to make sure that
|
|
|
|
* newly created chart do have a visible y series.
|
2022-01-17 16:08:43 +00:00
|
|
|
*/
|
|
|
|
private async _ensureOneNumericSeries(id: number) {
|
|
|
|
const viewSection = this.docModel.viewSections.getRowModel(id);
|
2022-01-19 11:13:18 +00:00
|
|
|
const viewFields = viewSection.viewFields.peek().peek();
|
|
|
|
|
|
|
|
// If no y-series, then simply return.
|
|
|
|
if (viewFields.length === 1) { return; }
|
|
|
|
|
2022-01-17 16:08:43 +00:00
|
|
|
const field = viewSection.viewFields.peek().peek()[1];
|
|
|
|
if (isNumericOnly(viewSection.chartTypeDef.peek()) &&
|
|
|
|
!isNumericLike(field.column.peek())) {
|
|
|
|
const actions: UserAction[] = [];
|
|
|
|
|
|
|
|
// remove non-numeric field
|
|
|
|
actions.push(['RemoveRecord', field.id.peek()]);
|
|
|
|
|
|
|
|
// add new field
|
|
|
|
const newField = viewSection.hiddenColumns.peek().find((col) => isNumericLike(col));
|
|
|
|
if (newField) {
|
|
|
|
const colInfo = {
|
|
|
|
parentId: viewSection.id.peek(),
|
|
|
|
colRef: newField.id.peek(),
|
|
|
|
};
|
|
|
|
actions.push(['AddRecord', null, colInfo]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// send actions
|
|
|
|
await this.docModel.viewFields.sendTableActions(actions);
|
|
|
|
}
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function finalizeAnchor() {
|
|
|
|
await urlState().pushUrl({ hash: {} }, { replace: true });
|
|
|
|
setTestState({anchorApplied: true});
|
|
|
|
}
|
|
|
|
|
|
|
|
const cssViewContentPane = styled('div', `
|
|
|
|
flex: auto;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
overflow: hidden;
|
|
|
|
position: relative;
|
|
|
|
min-width: 240px;
|
|
|
|
margin: 12px;
|
2021-02-09 04:40:04 +00:00
|
|
|
@media ${mediaSmall} {
|
|
|
|
& {
|
|
|
|
margin: 4px;
|
|
|
|
}
|
|
|
|
}
|
2020-10-09 21:39:13 +00:00
|
|
|
@media print {
|
|
|
|
& {
|
|
|
|
margin: 0px;
|
|
|
|
}
|
|
|
|
}
|
2022-03-11 10:48:36 +00:00
|
|
|
&-contents {
|
|
|
|
margin: 0px;
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|