2020-10-02 15:10:00 +00:00
|
|
|
import {ClientScope} from 'app/client/components/ClientScope';
|
|
|
|
import * as Clipboard from 'app/client/components/Clipboard';
|
|
|
|
import {Comm} from 'app/client/components/Comm';
|
|
|
|
import * as commandList from 'app/client/components/commandList';
|
|
|
|
import * as commands from 'app/client/components/commands';
|
|
|
|
import {unsavedChanges} from 'app/client/components/UnsavedChanges';
|
|
|
|
import {get as getBrowserGlobals} from 'app/client/lib/browserGlobals';
|
|
|
|
import {isDesktop} from 'app/client/lib/browserInfo';
|
2021-02-04 03:17:17 +00:00
|
|
|
import {FocusLayer} from 'app/client/lib/FocusLayer';
|
2020-10-02 15:10:00 +00:00
|
|
|
import * as koUtil from 'app/client/lib/koUtil';
|
|
|
|
import {reportError, TopAppModel, TopAppModelImpl} from 'app/client/models/AppModel';
|
2022-05-18 16:05:37 +00:00
|
|
|
import {DocPageModel} from 'app/client/models/DocPageModel';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {setUpErrorHandling} from 'app/client/models/errors';
|
|
|
|
import {createAppUI} from 'app/client/ui/AppUI';
|
2021-01-21 19:12:24 +00:00
|
|
|
import {addViewportTag} from 'app/client/ui/viewport';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {attachCssRootVars} from 'app/client/ui2018/cssVars';
|
|
|
|
import {BaseAPI} from 'app/common/BaseAPI';
|
2022-06-04 04:12:30 +00:00
|
|
|
import {CommDocError} from 'app/common/CommTypes';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {DisposableWithEvents} from 'app/common/DisposableWithEvents';
|
|
|
|
import {fetchFromHome} from 'app/common/urlUtils';
|
|
|
|
import {ISupportedFeatures} from 'app/common/UserConfig';
|
2022-02-04 10:21:43 +00:00
|
|
|
import {dom} from 'grainjs';
|
2020-10-02 15:10:00 +00:00
|
|
|
import * as ko from 'knockout';
|
2022-10-28 16:11:08 +00:00
|
|
|
import {makeT} from 'app/client/lib/localization';
|
|
|
|
|
|
|
|
const t = makeT('App');
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// tslint:disable:no-console
|
|
|
|
|
|
|
|
const G = getBrowserGlobals('document', 'window');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main Grist App UI component.
|
|
|
|
*/
|
|
|
|
export class App extends DisposableWithEvents {
|
|
|
|
// Used by #newui code to avoid a dependency on commands.js, and by tests to issue commands.
|
|
|
|
public allCommands = commands.allCommands;
|
|
|
|
|
2022-05-18 16:05:37 +00:00
|
|
|
public comm = this.autoDispose(Comm.create(this._checkError.bind(this)));
|
2020-10-02 15:10:00 +00:00
|
|
|
public clientScope: ClientScope;
|
|
|
|
public features: ko.Computed<ISupportedFeatures>;
|
|
|
|
public topAppModel: TopAppModel; // Exposed because used by test/nbrowser/gristUtils.
|
|
|
|
|
|
|
|
private _settings: ko.Observable<{features?: ISupportedFeatures}>;
|
|
|
|
|
|
|
|
// Track the version of the server we are communicating with, so that if it changes
|
|
|
|
// we can choose to refresh the client also.
|
|
|
|
private _serverVersion: string|null = null;
|
|
|
|
|
2022-05-18 16:05:37 +00:00
|
|
|
// Track the most recently created DocPageModel, for some error handling.
|
|
|
|
private _mostRecentDocPageModel?: DocPageModel;
|
|
|
|
|
2020-10-09 20:47:22 +00:00
|
|
|
constructor() {
|
2020-10-02 15:10:00 +00:00
|
|
|
super();
|
|
|
|
|
|
|
|
commands.init(); // Initialize the 'commands' module using the default command list.
|
|
|
|
|
|
|
|
// Create the notifications box, and use it for reporting errors we can catch.
|
|
|
|
setUpErrorHandling(reportError, koUtil);
|
|
|
|
|
|
|
|
this.clientScope = this.autoDispose(ClientScope.create());
|
|
|
|
|
|
|
|
// Settings, initialized by initSettings event triggered by a server message.
|
|
|
|
this._settings = ko.observable({});
|
|
|
|
this.features = ko.computed(() => this._settings().features || {});
|
|
|
|
|
|
|
|
if (isDesktop()) {
|
|
|
|
this.autoDispose(Clipboard.create(this));
|
2021-02-04 03:17:17 +00:00
|
|
|
} else {
|
|
|
|
// On mobile, we do not want to keep focus on a special textarea (which would cause unwanted
|
2021-08-05 15:12:46 +00:00
|
|
|
// scrolling and showing of mobile keyboard). But we still rely on 'clipboard_focus' and
|
2021-02-04 03:17:17 +00:00
|
|
|
// 'clipboard_blur' events to know when the "app" has a focus (rather than a particular
|
|
|
|
// input), by making document.body focusable and using a FocusLayer with it as the default.
|
|
|
|
document.body.setAttribute('tabindex', '-1');
|
|
|
|
FocusLayer.create(this, {
|
|
|
|
defaultFocusElem: document.body,
|
|
|
|
allowFocus: Clipboard.allowFocus,
|
|
|
|
onDefaultFocus: () => this.trigger('clipboard_focus'),
|
|
|
|
onDefaultBlur: () => this.trigger('clipboard_blur'),
|
|
|
|
});
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.topAppModel = this.autoDispose(TopAppModelImpl.create(null, G.window));
|
|
|
|
|
|
|
|
const isHelpPaneVisible = ko.observable(false);
|
|
|
|
|
|
|
|
G.document.querySelector('#grist-logo-wrapper').remove();
|
|
|
|
|
|
|
|
// Help pop-up pane
|
2020-10-09 20:47:22 +00:00
|
|
|
const helpDiv = document.body.appendChild(
|
2020-10-02 15:10:00 +00:00
|
|
|
dom('div.g-help',
|
|
|
|
dom.show(isHelpPaneVisible),
|
|
|
|
dom('table.g-help-table',
|
|
|
|
dom('thead',
|
|
|
|
dom('tr',
|
2022-12-06 13:57:29 +00:00
|
|
|
dom('th', t("Key")),
|
|
|
|
dom('th', t("Description"))
|
2020-10-02 15:10:00 +00:00
|
|
|
)
|
|
|
|
),
|
|
|
|
dom.forEach(commandList.groups, (group: any) => {
|
2022-10-21 10:55:01 +00:00
|
|
|
const cmds = group.commands.filter((cmd: any) => Boolean(cmd.desc && cmd.keys.length && !cmd.deprecated));
|
2020-10-02 15:10:00 +00:00
|
|
|
return cmds.length > 0 ?
|
|
|
|
dom('tbody',
|
|
|
|
dom('tr',
|
|
|
|
dom('td', {colspan: 2}, group.group)
|
|
|
|
),
|
|
|
|
dom.forEach(cmds, (cmd: any) =>
|
|
|
|
dom('tr',
|
|
|
|
dom('td', commands.allCommands[cmd.name].getKeysDom()),
|
|
|
|
dom('td', cmd.desc)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
) : null;
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
this.onDispose(() => { dom.domDispose(helpDiv); helpDiv.remove(); });
|
|
|
|
|
|
|
|
this.autoDispose(commands.createGroup({
|
|
|
|
help() { G.window.open('help', '_blank').focus(); },
|
|
|
|
shortcuts() { isHelpPaneVisible(true); },
|
|
|
|
historyBack() { G.window.history.back(); },
|
|
|
|
historyForward() { G.window.history.forward(); },
|
|
|
|
}, this, true));
|
|
|
|
|
|
|
|
this.autoDispose(commands.createGroup({
|
|
|
|
cancel() { isHelpPaneVisible(false); },
|
2022-02-26 15:50:15 +00:00
|
|
|
cursorDown() { helpDiv.scrollBy(0, 30); }, // 30 is height of the row in the help screen
|
|
|
|
cursorUp() { helpDiv.scrollBy(0, -30); },
|
|
|
|
pageUp() { helpDiv.scrollBy(0, -helpDiv.clientHeight); },
|
|
|
|
pageDown() { helpDiv.scrollBy(0, helpDiv.clientHeight); },
|
|
|
|
moveToFirstField() { helpDiv.scrollTo(0, 0); }, // home
|
|
|
|
moveToLastField() { helpDiv.scrollTo(0, helpDiv.scrollHeight); }, // end
|
|
|
|
find() { return true; }, // restore browser search
|
2020-10-02 15:10:00 +00:00
|
|
|
help() { isHelpPaneVisible(false); },
|
|
|
|
}, this, isHelpPaneVisible));
|
|
|
|
|
|
|
|
this.listenTo(this.comm, 'clientConnect', (message) => {
|
2022-06-13 03:30:07 +00:00
|
|
|
console.log(`App clientConnect event: needReload ${message.needReload} version ${message.serverVersion}`);
|
2020-10-02 15:10:00 +00:00
|
|
|
this._settings(message.settings);
|
|
|
|
if (message.serverVersion === 'dead' || (this._serverVersion && this._serverVersion !== message.serverVersion)) {
|
|
|
|
console.log("Upgrading...");
|
|
|
|
// Server has upgraded. Upgrade client. TODO: be gentle and polite.
|
|
|
|
return this.reload();
|
|
|
|
}
|
|
|
|
this._serverVersion = message.serverVersion;
|
2022-06-13 03:30:07 +00:00
|
|
|
// Reload any open documents if needed (if clientId changed, or client can't get all missed
|
|
|
|
// messages). We'll simply reload the active component of the App regardless of what it is.
|
|
|
|
if (message.needReload) {
|
2020-10-02 15:10:00 +00:00
|
|
|
this.reloadPane();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.listenTo(this.comm, 'connectState', (isConnected: boolean) => {
|
|
|
|
this.topAppModel.notifier.setConnectState(isConnected);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.listenTo(this.comm, 'docShutdown', () => {
|
|
|
|
console.log("Received docShutdown");
|
|
|
|
// Reload on next tick, to let other objects process 'docShutdown' before they get disposed.
|
|
|
|
setTimeout(() => this.reloadPane(), 0);
|
|
|
|
});
|
|
|
|
|
2022-06-04 04:12:30 +00:00
|
|
|
this.listenTo(this.comm, 'docError', (msg: CommDocError) => {
|
2022-05-18 16:05:37 +00:00
|
|
|
this._checkError(new Error(msg.data.message));
|
|
|
|
});
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// When the document is unloaded, dispose the app, allowing it to do any needed
|
|
|
|
// cleanup (e.g. Document on disposal triggers closeDoc message to the server). It needs to be
|
|
|
|
// in 'beforeunload' rather than 'unload', since websocket is closed by the time of 'unload'.
|
|
|
|
G.window.addEventListener('beforeunload', (ev: BeforeUnloadEvent) => {
|
|
|
|
if (unsavedChanges.haveUnsavedChanges()) {
|
|
|
|
// Following https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event
|
|
|
|
const msg = 'You have some unsaved changes';
|
|
|
|
ev.returnValue = msg;
|
|
|
|
ev.preventDefault();
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
this.dispose();
|
|
|
|
});
|
|
|
|
|
|
|
|
this.comm.initialize(null);
|
|
|
|
|
|
|
|
// Add the cssRootVars class to enable the variables in cssVars.
|
|
|
|
attachCssRootVars(this.topAppModel.productFlavor);
|
2021-01-21 19:12:24 +00:00
|
|
|
addViewportTag();
|
2020-10-02 15:10:00 +00:00
|
|
|
this.autoDispose(createAppUI(this.topAppModel, this));
|
|
|
|
}
|
|
|
|
|
2021-08-05 15:12:46 +00:00
|
|
|
// We want to test errors from Selenium, but errors we can trigger using driver.executeScript()
|
2020-10-02 15:10:00 +00:00
|
|
|
// will be impossible for the application to report properly (they seem to be considered not of
|
|
|
|
// "same-origin"). So this silly callback is for tests to generate a fake error.
|
|
|
|
public testTriggerError(msg: string) { throw new Error(msg); }
|
|
|
|
|
|
|
|
public reloadPane() {
|
|
|
|
console.log("reloadPane");
|
|
|
|
this.topAppModel.reload();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Intended to be used by tests to enable specific features.
|
|
|
|
public enableFeature(featureName: keyof ISupportedFeatures, onOff: boolean) {
|
|
|
|
const features = this.features();
|
|
|
|
features[featureName] = onOff;
|
|
|
|
this._settings(Object.assign(this._settings(), { features }));
|
|
|
|
}
|
|
|
|
|
|
|
|
public getServerVersion() {
|
|
|
|
return this._serverVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
public reload() {
|
|
|
|
G.window.location.reload(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-05-18 16:05:37 +00:00
|
|
|
public setDocPageModel(pageModel: DocPageModel) {
|
|
|
|
this._mostRecentDocPageModel = pageModel;
|
|
|
|
}
|
|
|
|
|
2023-03-13 15:47:22 +00:00
|
|
|
/**
|
|
|
|
* This method is not called anywhere, it is here just to introduce
|
|
|
|
* a special translation key. The purpose of this key is to let translators
|
|
|
|
* control whether a translation is ready to be offered to the user.
|
|
|
|
*
|
|
|
|
* If the key has not been translated for a language, and the language
|
|
|
|
* is not the default language, then the language should not be offered
|
|
|
|
* or used (unless some flag is set). TODO: implement this once key
|
|
|
|
* is available in weblate and good translations have been updated.
|
|
|
|
*/
|
|
|
|
public checkSpecialTranslationKey() {
|
|
|
|
return t('Translators: please translate this only when your language is ready to be offered to users');
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// Get the user profile for testing purposes
|
|
|
|
public async testGetProfile(): Promise<any> {
|
|
|
|
const resp = await fetchFromHome('/api/profile/user', {credentials: 'include'});
|
|
|
|
return resp.json();
|
|
|
|
}
|
|
|
|
|
|
|
|
public testNumPendingApiRequests(): number {
|
|
|
|
return BaseAPI.numPendingRequests();
|
|
|
|
}
|
2022-05-18 16:05:37 +00:00
|
|
|
|
|
|
|
private _checkError(err: Error) {
|
|
|
|
const message = String(err);
|
|
|
|
// Take special action on any error that suggests a memory problem.
|
|
|
|
if (message.match(/MemoryError|unmarshallable object/)) {
|
|
|
|
if (err.message.length > 30) {
|
|
|
|
// TLDR
|
2022-12-06 13:57:29 +00:00
|
|
|
err.message = t("Memory Error");
|
2022-05-18 16:05:37 +00:00
|
|
|
}
|
|
|
|
this._mostRecentDocPageModel?.offerRecovery(err);
|
|
|
|
}
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|