2022-07-04 14:14:55 +00:00
|
|
|
import BaseView from 'app/client/components/BaseView';
|
2022-01-12 13:30:51 +00:00
|
|
|
import * as commands from 'app/client/components/commands';
|
2023-08-29 14:50:42 +00:00
|
|
|
import {Cursor} from 'app/client/components/Cursor';
|
2022-01-12 13:30:51 +00:00
|
|
|
import {GristDoc} from 'app/client/components/GristDoc';
|
2023-08-29 14:50:42 +00:00
|
|
|
import {
|
2023-09-26 20:37:27 +00:00
|
|
|
CommandAPI,
|
2023-08-29 14:50:42 +00:00
|
|
|
ConfigNotifier,
|
|
|
|
CustomSectionAPIImpl,
|
|
|
|
GristDocAPIImpl,
|
|
|
|
GristViewImpl,
|
|
|
|
MinimumLevel,
|
|
|
|
RecordNotifier,
|
|
|
|
TableNotifier,
|
2023-10-06 13:17:39 +00:00
|
|
|
ThemeNotifier,
|
2023-08-29 14:50:42 +00:00
|
|
|
WidgetAPIImpl,
|
|
|
|
WidgetFrame
|
|
|
|
} from 'app/client/components/WidgetFrame';
|
2022-01-12 13:30:51 +00:00
|
|
|
import {CustomSectionElement, ViewProcess} from 'app/client/lib/CustomSectionElement';
|
|
|
|
import {Disposable} from 'app/client/lib/dispose';
|
2022-07-04 14:14:55 +00:00
|
|
|
import dom from 'app/client/lib/dom';
|
2020-10-02 15:10:00 +00:00
|
|
|
import * as kd from 'app/client/lib/koDom';
|
2022-07-04 14:14:55 +00:00
|
|
|
import DataTableModel from 'app/client/models/DataTableModel';
|
2022-01-12 13:30:51 +00:00
|
|
|
import {ViewSectionRec} from 'app/client/models/DocModel';
|
|
|
|
import {CustomViewSectionDef} from 'app/client/models/entities/ViewSectionRec';
|
|
|
|
import {UserError} from 'app/client/models/errors';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {SortedRowSet} from 'app/client/models/rowset';
|
2022-03-07 12:58:06 +00:00
|
|
|
import {closeRegisteredMenu} from 'app/client/ui2018/menus';
|
2023-08-29 14:50:42 +00:00
|
|
|
import {AccessLevel} from 'app/common/CustomWidget';
|
2023-10-02 13:57:20 +00:00
|
|
|
import {defaultLocale} from 'app/common/gutil';
|
2023-08-29 14:50:42 +00:00
|
|
|
import {PluginInstance} from 'app/common/PluginInstance';
|
2022-03-07 12:58:06 +00:00
|
|
|
import {getGristConfig} from 'app/common/urlUtils';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {Events as BackboneEvents} from 'backbone';
|
2022-01-12 13:30:51 +00:00
|
|
|
import {dom as grains} from 'grainjs';
|
2020-10-02 15:10:00 +00:00
|
|
|
import * as ko from 'knockout';
|
|
|
|
import defaults = require('lodash/defaults');
|
|
|
|
|
2023-10-27 19:34:42 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Built in settings for a custom widget. Used when the custom
|
|
|
|
* widget is the implementation of a native-looking widget,
|
|
|
|
* for example the calendar widget.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export interface CustomViewSettings {
|
|
|
|
widgetId?: string;
|
|
|
|
accessLevel?: AccessLevel;
|
|
|
|
}
|
2023-08-29 14:50:42 +00:00
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* CustomView components displays arbitrary html. There are two modes available, in the "url" mode
|
|
|
|
* the content is hosted by a third-party (for instance a github page), as opposed to the "plugin"
|
|
|
|
* mode where the contents is provided by a plugin. In both cases the content is rendered safely
|
|
|
|
* within an iframe (or webview if running electron). Configuration of the component is done within
|
|
|
|
* the view config tab in the side pane. In "plugin" mode, shows notification if either the plugin
|
|
|
|
* of the section could not be found.
|
|
|
|
*/
|
|
|
|
export class CustomView extends Disposable {
|
|
|
|
|
2022-01-12 13:30:51 +00:00
|
|
|
private static _commands = {
|
|
|
|
async openWidgetConfiguration(this: CustomView) {
|
|
|
|
if (!this.isDisposed() && !this._frame?.isDisposed()) {
|
|
|
|
try {
|
|
|
|
await this._frame.editOptions();
|
|
|
|
} catch(err) {
|
|
|
|
if (err.message === "Unknown interface") {
|
|
|
|
throw new UserError("Custom widget doesn't expose configuration screen.");
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* The HTMLElement embedding the content.
|
|
|
|
*/
|
|
|
|
public viewPane: HTMLElement;
|
|
|
|
|
|
|
|
// viewSection, sortedRows, tableModel, gristDoc, and cursor are inherited from BaseView
|
|
|
|
protected viewSection: ViewSectionRec;
|
|
|
|
protected sortedRows: SortedRowSet;
|
|
|
|
protected tableModel: DataTableModel;
|
|
|
|
protected gristDoc: GristDoc;
|
|
|
|
protected cursor: Cursor;
|
|
|
|
|
2023-08-29 14:50:42 +00:00
|
|
|
protected customDef: CustomViewSectionDef;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// state of the component
|
|
|
|
private _foundPlugin: ko.Observable<boolean>;
|
|
|
|
private _foundSection: ko.Observable<boolean>;
|
|
|
|
// Note the invariant: this._customSection != undefined if this._foundSection() == true
|
|
|
|
private _customSection: ViewProcess|undefined;
|
|
|
|
private _pluginInstance: PluginInstance|undefined;
|
|
|
|
|
2022-01-12 13:30:51 +00:00
|
|
|
private _frame: WidgetFrame; // plugin frame (holding external page)
|
2023-08-29 14:50:42 +00:00
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
public create(gristDoc: GristDoc, viewSectionModel: ViewSectionRec) {
|
2022-04-11 19:33:26 +00:00
|
|
|
BaseView.call(this as any, gristDoc, viewSectionModel, { 'addNewRow': true });
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2023-10-27 19:34:42 +00:00
|
|
|
this.customDef = this.viewSection.customDef;
|
2021-08-10 11:18:37 +00:00
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
this.autoDisposeCallback(() => {
|
|
|
|
if (this._customSection) {
|
|
|
|
this._customSection.dispose();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this._foundPlugin = ko.observable(false);
|
|
|
|
this._foundSection = ko.observable(false);
|
|
|
|
// Ensure that selecting another section in same plugin update the view.
|
|
|
|
this._foundSection.extend({notify: 'always'});
|
|
|
|
|
2023-08-29 14:50:42 +00:00
|
|
|
this.autoDispose(this.customDef.pluginId.subscribe(this._updatePluginInstance, this));
|
|
|
|
this.autoDispose(this.customDef.sectionId.subscribe(this._updateCustomSection, this));
|
2022-01-12 13:30:51 +00:00
|
|
|
this.autoDispose(commands.createGroup(CustomView._commands, this, this.viewSection.hasFocus));
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
this.viewPane = this.autoDispose(this._buildDom());
|
|
|
|
this._updatePluginInstance();
|
|
|
|
}
|
|
|
|
|
2020-10-09 21:39:13 +00:00
|
|
|
public async triggerPrint() {
|
2022-01-12 13:30:51 +00:00
|
|
|
if (!this.isDisposed() && this._frame) {
|
|
|
|
return await this._frame.callRemote('print');
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-27 19:34:42 +00:00
|
|
|
protected getBuiltInSettings(): CustomViewSettings {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-08-29 14:50:42 +00:00
|
|
|
protected getEmptyWidgetPage(): string {
|
|
|
|
return new URL("custom-widget.html", getGristConfig().homeUrl!).href;
|
|
|
|
}
|
2023-10-27 19:34:42 +00:00
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
/**
|
2022-01-12 13:30:51 +00:00
|
|
|
* Find a plugin instance that matches the plugin id, update the `found` observables, then tries to
|
2020-10-02 15:10:00 +00:00
|
|
|
* find a matching section.
|
|
|
|
*/
|
|
|
|
private _updatePluginInstance() {
|
|
|
|
|
2023-08-29 14:50:42 +00:00
|
|
|
const pluginId = this.customDef.pluginId();
|
2020-10-02 15:10:00 +00:00
|
|
|
this._pluginInstance = this.gristDoc.docPluginManager.pluginsList.find(p => p.definition.id === pluginId);
|
|
|
|
|
|
|
|
if (this._pluginInstance) {
|
|
|
|
this._foundPlugin(true);
|
|
|
|
} else {
|
|
|
|
this._foundPlugin(false);
|
|
|
|
this._foundSection(false);
|
|
|
|
}
|
|
|
|
this._updateCustomSection();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If a plugin was found, find a custom section matching the section id and update the `found`
|
|
|
|
* observables.
|
|
|
|
*/
|
|
|
|
private _updateCustomSection() {
|
|
|
|
|
|
|
|
if (!this._pluginInstance) { return; }
|
|
|
|
|
2023-08-29 14:50:42 +00:00
|
|
|
const sectionId = this.customDef.sectionId();
|
2020-10-02 15:10:00 +00:00
|
|
|
this._customSection = CustomSectionElement.find(this._pluginInstance, sectionId);
|
|
|
|
|
|
|
|
if (this._customSection) {
|
|
|
|
const el = this._customSection.element;
|
|
|
|
el.classList.add("flexitem");
|
|
|
|
this._foundSection(true);
|
|
|
|
} else {
|
|
|
|
this._foundSection(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private _buildDom() {
|
2023-10-27 19:34:42 +00:00
|
|
|
const {mode, url, access, renderAfterReady, widgetDef, widgetId, pluginId} = this.customDef;
|
2023-08-29 14:50:42 +00:00
|
|
|
const showPlugin = ko.pureComputed(() => this.customDef.mode() === "plugin");
|
2023-09-19 18:44:22 +00:00
|
|
|
const showAfterReady = () => {
|
|
|
|
// The empty widget page calls `grist.ready()`.
|
2023-10-27 19:34:42 +00:00
|
|
|
if (!url() && !widgetId()) { return true; }
|
2023-09-19 18:44:22 +00:00
|
|
|
|
2023-10-27 19:34:42 +00:00
|
|
|
return renderAfterReady();
|
2023-09-19 18:44:22 +00:00
|
|
|
};
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// When both plugin and section are not found, let's show only plugin notification.
|
|
|
|
const showPluginNotification = ko.pureComputed(() => showPlugin() && !this._foundPlugin());
|
|
|
|
const showSectionNotification = ko.pureComputed(() => showPlugin() && this._foundPlugin() && !this._foundSection());
|
|
|
|
const showPluginContent = ko.pureComputed(() => showPlugin() && this._foundSection())
|
|
|
|
// For the view to update when switching from one section to another one, the computed
|
|
|
|
// observable must always notify.
|
|
|
|
.extend({notify: 'always'});
|
2023-10-27 19:34:42 +00:00
|
|
|
// Some widgets have built-in settings that should override anything
|
|
|
|
// that is in the rest of the view options. Ideally, everything would
|
|
|
|
// be consistent. We could fix inconsistencies if we find them, but
|
|
|
|
// we are not guaranteed to have write privileges at this point.
|
|
|
|
const builtInSettings = this.getBuiltInSettings();
|
2020-10-02 15:10:00 +00:00
|
|
|
return dom('div.flexauto.flexvbox.custom_view_container',
|
|
|
|
dom.autoDispose(showPlugin),
|
|
|
|
dom.autoDispose(showPluginNotification),
|
|
|
|
dom.autoDispose(showSectionNotification),
|
|
|
|
dom.autoDispose(showPluginContent),
|
|
|
|
// todo: should display content in webview when running electron
|
2023-10-27 19:34:42 +00:00
|
|
|
// prefer widgetId; spelunk in widgetDef for older docs
|
|
|
|
kd.scope(() => [mode(), url(), access(), widgetId() || widgetDef()?.widgetId || '', pluginId()],
|
|
|
|
([_mode, _url, _access, _widgetId, _pluginId]: string[]) =>
|
2023-09-19 18:44:22 +00:00
|
|
|
_mode === "url" ?
|
|
|
|
this._buildIFrame({
|
|
|
|
baseUrl: _url,
|
2023-10-27 19:34:42 +00:00
|
|
|
access: builtInSettings.accessLevel || (_access as AccessLevel || AccessLevel.none),
|
2023-09-19 18:44:22 +00:00
|
|
|
showAfterReady: showAfterReady(),
|
2023-10-27 19:34:42 +00:00
|
|
|
widgetId: builtInSettings.widgetId || _widgetId,
|
|
|
|
pluginId: _pluginId,
|
2023-09-19 18:44:22 +00:00
|
|
|
})
|
|
|
|
: null
|
|
|
|
),
|
2020-10-02 15:10:00 +00:00
|
|
|
kd.maybe(showPluginNotification, () => buildNotification('Plugin ',
|
2023-08-29 14:50:42 +00:00
|
|
|
dom('strong', kd.text(this.customDef.pluginId)), ' was not found',
|
2020-10-02 15:10:00 +00:00
|
|
|
dom.testId('customView_notification_plugin')
|
|
|
|
)),
|
|
|
|
kd.maybe(showSectionNotification, () => buildNotification('Section ',
|
2023-08-29 14:50:42 +00:00
|
|
|
dom('strong', kd.text(this.customDef.sectionId)), ' was not found in plugin ',
|
|
|
|
dom('strong', kd.text(this.customDef.pluginId)),
|
2020-10-02 15:10:00 +00:00
|
|
|
dom.testId('customView_notification_section')
|
|
|
|
)),
|
|
|
|
// When showPluginContent() is true then _foundSection() is also and _customSection is not
|
|
|
|
// undefined (invariant).
|
|
|
|
kd.maybe(showPluginContent, () => this._customSection!.element)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-12 13:30:51 +00:00
|
|
|
private _promptAccess(access: AccessLevel) {
|
|
|
|
if (this.gristDoc.isReadonly.get()) {
|
|
|
|
return;
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
2022-01-12 13:30:51 +00:00
|
|
|
this.viewSection.desiredAccessLevel(access);
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2023-09-19 18:44:22 +00:00
|
|
|
private _buildIFrame(options: {
|
|
|
|
baseUrl: string|null,
|
|
|
|
access: AccessLevel,
|
|
|
|
showAfterReady?: boolean,
|
2023-10-27 19:34:42 +00:00
|
|
|
widgetId?: string|null,
|
|
|
|
pluginId?: string
|
2023-09-19 18:44:22 +00:00
|
|
|
}) {
|
2023-10-27 19:34:42 +00:00
|
|
|
const {baseUrl, access, showAfterReady, widgetId, pluginId} = options;
|
2023-10-02 13:57:20 +00:00
|
|
|
const documentSettings = this.gristDoc.docData.docSettings();
|
2023-10-06 13:17:39 +00:00
|
|
|
const readonly = this.gristDoc.isReadonly.get();
|
2023-11-06 15:28:14 +00:00
|
|
|
const frame = WidgetFrame.create(null, {
|
2023-08-29 14:50:42 +00:00
|
|
|
url: baseUrl || this.getEmptyWidgetPage(),
|
2023-10-27 19:34:42 +00:00
|
|
|
widgetId,
|
|
|
|
pluginId,
|
2022-01-12 13:30:51 +00:00
|
|
|
access,
|
2023-10-02 13:57:20 +00:00
|
|
|
preferences:
|
|
|
|
{
|
|
|
|
culture: documentSettings.locale?? defaultLocale,
|
|
|
|
language: this.gristDoc.appModel.currentUser?.locale ?? defaultLocale,
|
|
|
|
timeZone: this.gristDoc.docInfo.timezone() ?? "UTC",
|
|
|
|
currency: documentSettings.currency?? "USD",
|
|
|
|
},
|
2023-10-06 13:17:39 +00:00
|
|
|
readonly,
|
2023-09-19 18:44:22 +00:00
|
|
|
showAfterReady,
|
2022-01-12 13:30:51 +00:00
|
|
|
configure: (frame) => {
|
|
|
|
this._frame = frame;
|
|
|
|
// Need to cast myself to a BaseView
|
|
|
|
const view = this as unknown as BaseView;
|
|
|
|
frame.exposeAPI(
|
|
|
|
"GristDocAPI",
|
|
|
|
new GristDocAPIImpl(this.gristDoc),
|
|
|
|
GristDocAPIImpl.defaultAccess);
|
|
|
|
frame.exposeAPI(
|
|
|
|
"GristView",
|
2023-10-26 21:44:47 +00:00
|
|
|
new GristViewImpl(view, access), new MinimumLevel(AccessLevel.read_table));
|
2022-01-12 13:30:51 +00:00
|
|
|
frame.exposeAPI(
|
|
|
|
"CustomSectionAPI",
|
|
|
|
new CustomSectionAPIImpl(
|
|
|
|
this.viewSection,
|
|
|
|
access,
|
|
|
|
this._promptAccess.bind(this)),
|
|
|
|
new MinimumLevel(AccessLevel.none));
|
2023-09-26 20:37:27 +00:00
|
|
|
frame.exposeAPI(
|
|
|
|
"CommandAPI",
|
|
|
|
new CommandAPI(access),
|
|
|
|
new MinimumLevel(AccessLevel.none));
|
2022-01-12 13:30:51 +00:00
|
|
|
frame.useEvents(RecordNotifier.create(frame, view), new MinimumLevel(AccessLevel.read_table));
|
|
|
|
frame.useEvents(TableNotifier.create(frame, view), new MinimumLevel(AccessLevel.read_table));
|
|
|
|
frame.exposeAPI(
|
|
|
|
"WidgetAPI",
|
|
|
|
new WidgetAPIImpl(this.viewSection),
|
|
|
|
new MinimumLevel(AccessLevel.none)); // none access is enough
|
|
|
|
frame.useEvents(
|
2023-09-19 18:44:22 +00:00
|
|
|
ConfigNotifier.create(frame, this.viewSection, {
|
|
|
|
access,
|
|
|
|
}),
|
2022-01-12 13:30:51 +00:00
|
|
|
new MinimumLevel(AccessLevel.none)); // none access is enough
|
2023-10-06 13:17:39 +00:00
|
|
|
frame.useEvents(
|
|
|
|
ThemeNotifier.create(frame, this.gristDoc.currentTheme),
|
|
|
|
new MinimumLevel(AccessLevel.none));
|
2022-01-12 13:30:51 +00:00
|
|
|
},
|
|
|
|
onElem: (iframe) => onFrameFocus(iframe, () => {
|
|
|
|
if (this.isDisposed()) { return; }
|
|
|
|
if (!this.viewSection.isDisposed() && !this.viewSection.hasFocus()) {
|
|
|
|
this.viewSection.hasFocus(true);
|
2021-07-30 14:10:54 +00:00
|
|
|
}
|
2022-02-16 14:15:27 +00:00
|
|
|
// allow menus to close if any
|
|
|
|
closeRegisteredMenu();
|
2023-10-27 19:34:42 +00:00
|
|
|
}),
|
|
|
|
gristDoc: this.gristDoc,
|
2022-01-12 13:30:51 +00:00
|
|
|
});
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2023-11-06 15:28:14 +00:00
|
|
|
// Can't use dom.create() because it seems buggy in this context. This dom will be detached
|
|
|
|
// and attached several times, and dom.create() doesn't seem to handle that well as it returns an
|
|
|
|
// array of nodes (comment, node, comment) and it somehow breaks the dispose order. Collapsed widgets
|
|
|
|
// relay on a correct order of dispose, and are detaching nodes just before they are disposed, so if
|
|
|
|
// the order is wrong, the node is disposed without being detached first.
|
|
|
|
return grains.update(frame.buildDom(), dom.autoDispose(frame));
|
2022-01-12 13:30:51 +00:00
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Getting an ES6 class to work with old-style multiple base classes takes a little hacking. Credits: ./ChartView.ts
|
|
|
|
defaults(CustomView.prototype, BaseView.prototype);
|
|
|
|
Object.assign(CustomView.prototype, BackboneEvents);
|
|
|
|
|
|
|
|
|
|
|
|
// helper to build the notification's frame.
|
|
|
|
function buildNotification(...args: any[]) {
|
|
|
|
return dom('div.custom_view_notification.bg-warning', dom('p', ...args));
|
|
|
|
}
|
2022-01-12 13:30:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* There is no way to detect if the frame was clicked. This causes a bug, when
|
|
|
|
* there are 2 custom widgets on a page then user can't switch focus from 1 section
|
|
|
|
* to another. The only solution is too pool and test if the iframe is an active element
|
|
|
|
* in the dom.
|
|
|
|
* (See https://stackoverflow.com/questions/2381336/detect-click-into-iframe-using-javascript).
|
|
|
|
*
|
|
|
|
* For a single iframe, it will gain focus through a hack in ViewLayout.ts.
|
|
|
|
*/
|
|
|
|
function onFrameFocus(frame: HTMLIFrameElement, handler: () => void) {
|
|
|
|
let timer: NodeJS.Timeout|null = null;
|
|
|
|
// Flag that will prevent mouseenter event to be fired
|
|
|
|
// after dom is disposed. This shouldn't happen.
|
|
|
|
let disposed = false;
|
|
|
|
// Stops pooling.
|
|
|
|
function stop() {
|
|
|
|
if (timer) {
|
|
|
|
clearInterval(timer);
|
|
|
|
timer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return grains.update(frame,
|
|
|
|
grains.on("mouseenter", () => {
|
|
|
|
// Make sure we weren't dispose (should not happen)
|
|
|
|
if (disposed) { return; }
|
|
|
|
// If frame already has focus, do nothing.
|
|
|
|
// NOTE: Frame will always be an active element from our perspective,
|
|
|
|
// even if the focus is somewhere inside the iframe.
|
|
|
|
if (document.activeElement === frame) { return; }
|
|
|
|
// Start pooling for frame focus.
|
|
|
|
timer = setInterval(() => {
|
|
|
|
if (document.activeElement === frame) {
|
|
|
|
try {
|
|
|
|
handler();
|
|
|
|
} finally {
|
|
|
|
// Stop checking, we will start again after next mouseenter.
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 70); // 70 is enough to make it look like a click.
|
|
|
|
}),
|
|
|
|
grains.on("mouseleave", stop),
|
|
|
|
grains.onDispose(() => {
|
|
|
|
stop();
|
|
|
|
disposed = true;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|