2022-01-12 13:30:51 +00:00
|
|
|
import {allCommands} from 'app/client/components/commands';
|
|
|
|
import {GristDoc} from 'app/client/components/GristDoc';
|
2021-11-26 10:43:55 +00:00
|
|
|
import * as kf from 'app/client/lib/koForm';
|
|
|
|
import {ViewSectionRec} from 'app/client/models/DocModel';
|
|
|
|
import {reportError} from 'app/client/models/errors';
|
|
|
|
import {cssLabel, cssRow, cssTextInput} from 'app/client/ui/RightPanel';
|
2022-01-12 13:30:51 +00:00
|
|
|
import {basicButton, primaryButton, textButton} from 'app/client/ui2018/buttons';
|
2021-11-26 10:43:55 +00:00
|
|
|
import {colors} from 'app/client/ui2018/cssVars';
|
2022-01-12 13:30:51 +00:00
|
|
|
import {icon} from 'app/client/ui2018/icons';
|
2021-11-26 10:43:55 +00:00
|
|
|
import {cssLink} from 'app/client/ui2018/links';
|
|
|
|
import {IOptionFull, select} from 'app/client/ui2018/menus';
|
2022-01-12 13:30:51 +00:00
|
|
|
import {AccessLevel, ICustomWidget, isSatisfied} from 'app/common/CustomWidget';
|
2021-11-26 10:43:55 +00:00
|
|
|
import {GristLoadConfig} from 'app/common/gristUrls';
|
|
|
|
import {nativeCompare} from 'app/common/gutil';
|
2022-01-12 13:30:51 +00:00
|
|
|
import {bundleChanges, Computed, Disposable, dom, fromKo, makeTestId, MultiHolder, Observable, styled} from 'grainjs';
|
2021-11-26 10:43:55 +00:00
|
|
|
|
|
|
|
// Custom URL widget id - used as mock id for selectbox.
|
2022-01-12 13:30:51 +00:00
|
|
|
const CUSTOM_ID = 'custom';
|
2021-11-26 10:43:55 +00:00
|
|
|
const testId = makeTestId('test-config-widget-');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom Widget section.
|
|
|
|
* Allows to select custom widget from the list of available widgets
|
|
|
|
* (taken from /widgets endpoint), or enter a Custom URL.
|
|
|
|
* When Custom Widget has a desired access level (in accessLevel field),
|
|
|
|
* will prompt user to approve it. "None" access level is auto approved,
|
|
|
|
* so prompt won't be shown.
|
|
|
|
*
|
|
|
|
* When gristConfig.enableWidgetRepository is set to false, it will only
|
|
|
|
* allow to specify Custom URL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
export class CustomSectionConfig extends Disposable {
|
|
|
|
// Holds all available widget definitions.
|
|
|
|
private _widgets: Observable<ICustomWidget[]>;
|
2022-01-12 13:30:51 +00:00
|
|
|
// Holds selected option (either custom string or a widgetId).
|
|
|
|
private _selectedId: Computed<string | null>;
|
2021-11-26 10:43:55 +00:00
|
|
|
// Holds custom widget URL.
|
|
|
|
private _url: Computed<string>;
|
|
|
|
// Enable or disable widget repository.
|
|
|
|
private _canSelect = true;
|
|
|
|
// When widget is changed, it sets its desired access level. We will prompt
|
|
|
|
// user to approve or reject it.
|
2022-01-12 13:30:51 +00:00
|
|
|
private _desiredAccess: Observable<AccessLevel|null>;
|
2021-11-26 10:43:55 +00:00
|
|
|
// Current access level (stored inside a section).
|
|
|
|
private _currentAccess: Computed<AccessLevel>;
|
2022-01-12 13:30:51 +00:00
|
|
|
// Does widget has custom configuration.
|
|
|
|
private _hasConfiguration: Computed<boolean>;
|
2021-11-26 10:43:55 +00:00
|
|
|
|
2022-01-12 13:30:51 +00:00
|
|
|
constructor(_section: ViewSectionRec, _gristDoc: GristDoc) {
|
2021-11-26 10:43:55 +00:00
|
|
|
super();
|
|
|
|
|
2022-01-12 13:30:51 +00:00
|
|
|
const api = _gristDoc.app.topAppModel.api;
|
|
|
|
|
2021-11-26 10:43:55 +00:00
|
|
|
// Test if we can offer widget list.
|
|
|
|
const gristConfig: GristLoadConfig = (window as any).gristConfig || {};
|
|
|
|
this._canSelect = gristConfig.enableWidgetRepository ?? true;
|
|
|
|
|
|
|
|
// Array of available widgets - will be updated asynchronously.
|
|
|
|
this._widgets = Observable.create(this, []);
|
|
|
|
|
|
|
|
if (this._canSelect) {
|
|
|
|
// From the start we will provide single widget definition
|
|
|
|
// that was chosen previously.
|
2022-01-12 13:30:51 +00:00
|
|
|
if (_section.customDef.widgetDef.peek()) {
|
|
|
|
this._widgets.set([_section.customDef.widgetDef.peek()!]);
|
2021-11-26 10:43:55 +00:00
|
|
|
}
|
|
|
|
// Request for rest of the widgets.
|
2022-01-12 13:30:51 +00:00
|
|
|
api
|
|
|
|
.getWidgets()
|
|
|
|
.then(widgets => {
|
|
|
|
if (this.isDisposed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const existing = _section.customDef.widgetDef.peek();
|
|
|
|
// Make sure we have current widget in place.
|
|
|
|
if (existing && !widgets.some(w => w.widgetId === existing.widgetId)) {
|
|
|
|
widgets.push(existing);
|
|
|
|
}
|
|
|
|
this._widgets.set(widgets.sort((a, b) => nativeCompare(a.name.toLowerCase(), b.name.toLowerCase())));
|
|
|
|
})
|
|
|
|
.catch(reportError);
|
2021-11-26 10:43:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create temporary variable that will hold blank Custom Url state. When url is blank and widgetDef is not stored
|
|
|
|
// we can either show "Select Custom Widget" or a Custom Url with a blank url.
|
|
|
|
// To distinguish those states, we will mark Custom Url state at start (by checking that url is not blank and
|
|
|
|
// widgetDef is not set). And then switch it during selectbox manipulation.
|
2022-01-12 13:30:51 +00:00
|
|
|
const wantsToBeCustom = Observable.create(
|
|
|
|
this,
|
|
|
|
Boolean(_section.customDef.url.peek() && !_section.customDef.widgetDef.peek())
|
2021-11-26 10:43:55 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Selected value from the dropdown (contains widgetId or "custom" string for Custom URL)
|
2022-01-12 13:30:51 +00:00
|
|
|
this._selectedId = Computed.create(this, use => {
|
|
|
|
if (use(_section.customDef.widgetDef)) {
|
|
|
|
return _section.customDef.widgetDef.peek()!.widgetId;
|
2021-11-26 10:43:55 +00:00
|
|
|
}
|
2022-01-12 13:30:51 +00:00
|
|
|
if (use(_section.customDef.url) || use(wantsToBeCustom)) {
|
2021-11-26 10:43:55 +00:00
|
|
|
return CUSTOM_ID;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
2022-01-12 13:30:51 +00:00
|
|
|
this._selectedId.onWrite(async value => {
|
2021-11-26 10:43:55 +00:00
|
|
|
if (value === CUSTOM_ID) {
|
|
|
|
// Select Custom URL
|
|
|
|
bundleChanges(() => {
|
|
|
|
// Clear url.
|
2022-01-12 13:30:51 +00:00
|
|
|
_section.customDef.url(null);
|
2021-11-26 10:43:55 +00:00
|
|
|
// Clear widget definition.
|
2022-01-12 13:30:51 +00:00
|
|
|
_section.customDef.widgetDef(null);
|
2021-11-26 10:43:55 +00:00
|
|
|
// Set intermediate state
|
|
|
|
wantsToBeCustom.set(true);
|
|
|
|
// Reset access level to none.
|
2022-01-12 13:30:51 +00:00
|
|
|
_section.customDef.access(AccessLevel.none);
|
|
|
|
// Clear all saved options.
|
|
|
|
_section.customDef.widgetOptions(null);
|
|
|
|
// Reset custom configuration flag.
|
|
|
|
_section.hasCustomOptions(false);
|
2021-11-26 10:43:55 +00:00
|
|
|
this._desiredAccess.set(AccessLevel.none);
|
|
|
|
});
|
2022-01-12 13:30:51 +00:00
|
|
|
await _section.saveCustomDef();
|
2021-11-26 10:43:55 +00:00
|
|
|
} else {
|
|
|
|
// Select Widget
|
|
|
|
const selectedWidget = this._widgets.get().find(w => w.widgetId === value);
|
|
|
|
if (!selectedWidget) {
|
|
|
|
// should not happen
|
2022-01-12 13:30:51 +00:00
|
|
|
throw new Error('Error accessing widget from the list');
|
2021-11-26 10:43:55 +00:00
|
|
|
}
|
|
|
|
// If user selected the same one, do nothing.
|
2022-01-12 13:30:51 +00:00
|
|
|
if (_section.customDef.widgetDef.peek()?.widgetId === value) {
|
2021-11-26 10:43:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
bundleChanges(() => {
|
|
|
|
// Clear access level
|
2022-01-12 13:30:51 +00:00
|
|
|
_section.customDef.access(AccessLevel.none);
|
2021-11-26 10:43:55 +00:00
|
|
|
// When widget wants some access, set desired access level.
|
|
|
|
this._desiredAccess.set(selectedWidget.accessLevel || AccessLevel.none);
|
|
|
|
// Update widget definition.
|
2022-01-12 13:30:51 +00:00
|
|
|
_section.customDef.widgetDef(selectedWidget);
|
2021-11-26 10:43:55 +00:00
|
|
|
// Update widget URL.
|
2022-01-12 13:30:51 +00:00
|
|
|
_section.customDef.url(selectedWidget.url);
|
|
|
|
// Clear options.
|
|
|
|
_section.customDef.widgetOptions(null);
|
|
|
|
// Clear has custom configuration.
|
|
|
|
_section.hasCustomOptions(false);
|
2021-11-26 10:43:55 +00:00
|
|
|
// Clear intermediate state.
|
|
|
|
wantsToBeCustom.set(false);
|
|
|
|
});
|
2022-01-12 13:30:51 +00:00
|
|
|
await _section.saveCustomDef();
|
2021-11-26 10:43:55 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Url for the widget, taken either from widget definition, or provided by hand for Custom URL.
|
|
|
|
// For custom widget, we will store url also in section definition.
|
2022-01-12 13:30:51 +00:00
|
|
|
this._url = Computed.create(this, use => use(_section.customDef.url) || '');
|
|
|
|
this._url.onWrite(newUrl => _section.customDef.url.setAndSave(newUrl));
|
2021-11-26 10:43:55 +00:00
|
|
|
|
|
|
|
// Compute current access level.
|
2022-01-12 13:30:51 +00:00
|
|
|
this._currentAccess = Computed.create(
|
|
|
|
this,
|
|
|
|
use => (use(_section.customDef.access) as AccessLevel) || AccessLevel.none
|
|
|
|
);
|
|
|
|
this._currentAccess.onWrite(async newAccess => {
|
|
|
|
await _section.customDef.access.setAndSave(newAccess);
|
2021-11-26 10:43:55 +00:00
|
|
|
});
|
2022-01-12 13:30:51 +00:00
|
|
|
// From the start desired access level is the same as current one.
|
|
|
|
this._desiredAccess = fromKo(_section.desiredAccessLevel);
|
2021-11-26 10:43:55 +00:00
|
|
|
|
|
|
|
// Clear intermediate state when section changes.
|
2022-01-12 13:30:51 +00:00
|
|
|
this.autoDispose(_section.id.subscribe(() => wantsToBeCustom.set(false)));
|
|
|
|
this.autoDispose(_section.id.subscribe(() => this._reject()));
|
|
|
|
|
|
|
|
this._hasConfiguration = Computed.create(this, use => use(_section.hasCustomOptions));
|
2021-11-26 10:43:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public buildDom() {
|
|
|
|
// UI observables holder.
|
|
|
|
const holder = new MultiHolder();
|
|
|
|
|
|
|
|
// Show prompt, when desired access level is different from actual one.
|
2022-01-12 13:30:51 +00:00
|
|
|
const prompt = Computed.create(holder, use =>
|
|
|
|
use(this._desiredAccess)
|
|
|
|
&& !isSatisfied(use(this._currentAccess), use(this._desiredAccess)!));
|
2021-11-26 10:43:55 +00:00
|
|
|
// If this is empty section or not.
|
2022-01-12 13:30:51 +00:00
|
|
|
const isSelected = Computed.create(holder, use => Boolean(use(this._selectedId)));
|
2021-11-26 10:43:55 +00:00
|
|
|
// If user is using custom url.
|
2022-01-12 13:30:51 +00:00
|
|
|
const isCustom = Computed.create(holder, use => use(this._selectedId) === CUSTOM_ID || !this._canSelect);
|
|
|
|
// Options for the select-box (all widgets definitions and Custom URL)
|
2021-11-26 10:43:55 +00:00
|
|
|
const options = Computed.create(holder, use => [
|
|
|
|
{label: 'Custom URL', value: 'custom'},
|
|
|
|
...use(this._widgets).map(w => ({label: w.name, value: w.widgetId})),
|
|
|
|
]);
|
2022-01-12 13:30:51 +00:00
|
|
|
function buildPrompt(level: AccessLevel|null) {
|
|
|
|
if (!level) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
switch(level) {
|
|
|
|
case AccessLevel.none: return cssConfirmLine("Widget does not require any permissions.");
|
|
|
|
case AccessLevel.read_table: return cssConfirmLine("Widget needs to ", dom("b", "read"), " the current table.");
|
|
|
|
case AccessLevel.full: return cssConfirmLine("Widget needs a ", dom("b", "full access"), " to this document.");
|
|
|
|
default: throw new Error(`Unsupported ${level} access level`);
|
|
|
|
}
|
|
|
|
}
|
2021-11-26 10:43:55 +00:00
|
|
|
// Options for access level.
|
|
|
|
const levels: IOptionFull<string>[] = [
|
|
|
|
{label: 'No document access', value: AccessLevel.none},
|
|
|
|
{label: 'Read selected table', value: AccessLevel.read_table},
|
|
|
|
{label: 'Full document access', value: AccessLevel.full},
|
|
|
|
];
|
|
|
|
return dom(
|
|
|
|
'div',
|
|
|
|
dom.autoDispose(holder),
|
2022-01-12 13:30:51 +00:00
|
|
|
this._canSelect
|
|
|
|
? cssRow(
|
|
|
|
select(this._selectedId, options, {
|
|
|
|
defaultLabel: 'Select Custom Widget',
|
|
|
|
menuCssClass: cssMenu.className,
|
|
|
|
}),
|
|
|
|
testId('select')
|
|
|
|
)
|
|
|
|
: null,
|
2021-11-26 10:43:55 +00:00
|
|
|
dom.maybe(isCustom, () => [
|
|
|
|
cssRow(
|
|
|
|
cssTextInput(
|
|
|
|
this._url,
|
|
|
|
async value => this._url.set(value),
|
|
|
|
dom.attr('placeholder', 'Enter Custom URL'),
|
|
|
|
testId('url')
|
|
|
|
)
|
|
|
|
),
|
|
|
|
]),
|
2022-01-12 13:30:51 +00:00
|
|
|
dom.maybe(prompt, () =>
|
|
|
|
kf.prompt(
|
|
|
|
{tabindex: '-1'},
|
|
|
|
cssColumns(
|
|
|
|
cssWarningWrapper(icon('Lock')),
|
|
|
|
dom(
|
|
|
|
'div',
|
|
|
|
cssConfirmRow(
|
|
|
|
dom.domComputed(this._desiredAccess, (level) => buildPrompt(level))
|
2021-11-26 10:43:55 +00:00
|
|
|
),
|
2022-01-12 13:30:51 +00:00
|
|
|
cssConfirmRow(
|
|
|
|
primaryButton(
|
|
|
|
'Accept',
|
|
|
|
testId('access-accept'),
|
|
|
|
dom.on('click', () => this._accept())
|
2021-11-26 10:43:55 +00:00
|
|
|
),
|
2022-01-12 13:30:51 +00:00
|
|
|
basicButton(
|
|
|
|
'Reject',
|
|
|
|
testId('access-reject'),
|
|
|
|
dom.on('click', () => this._reject())
|
2021-11-26 10:43:55 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2022-01-12 13:30:51 +00:00
|
|
|
),
|
|
|
|
dom.maybe(
|
|
|
|
use => use(isSelected) || !this._canSelect,
|
|
|
|
() => [
|
|
|
|
cssLabel('ACCESS LEVEL'),
|
|
|
|
cssRow(select(this._currentAccess, levels), testId('access')),
|
|
|
|
]
|
|
|
|
),
|
|
|
|
dom.maybe(this._hasConfiguration, () =>
|
|
|
|
cssSection(
|
|
|
|
textButton(
|
|
|
|
'Open configuration',
|
|
|
|
dom.on('click', () => this._openConfiguration()),
|
|
|
|
testId('open-configuration')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
cssSection(
|
|
|
|
cssLink(
|
|
|
|
dom.attr('href', 'https://support.getgrist.com/widget-custom'),
|
|
|
|
dom.attr('target', '_blank'),
|
|
|
|
'Learn more about custom widgets'
|
|
|
|
)
|
|
|
|
),
|
2021-11-26 10:43:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-12 13:30:51 +00:00
|
|
|
private _openConfiguration(): void {
|
|
|
|
allCommands.openWidgetConfiguration.run();
|
|
|
|
}
|
|
|
|
|
2021-11-26 10:43:55 +00:00
|
|
|
private _accept() {
|
2022-01-12 13:30:51 +00:00
|
|
|
if (this._desiredAccess.get()) {
|
|
|
|
this._currentAccess.set(this._desiredAccess.get()!);
|
|
|
|
}
|
2021-11-26 10:43:55 +00:00
|
|
|
this._reject();
|
|
|
|
}
|
|
|
|
|
|
|
|
private _reject() {
|
2022-01-12 13:30:51 +00:00
|
|
|
this._desiredAccess.set(null);
|
2021-11-26 10:43:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const cssWarningWrapper = styled('div', `
|
|
|
|
padding-left: 8px;
|
|
|
|
padding-top: 6px;
|
2022-01-12 13:30:51 +00:00
|
|
|
--icon-color: ${colors.error}
|
2021-11-26 10:43:55 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssColumns = styled('div', `
|
|
|
|
display: flex;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssConfirmRow = styled('div', `
|
|
|
|
display: flex;
|
|
|
|
padding: 8px;
|
|
|
|
gap: 8px;
|
|
|
|
`);
|
|
|
|
|
2022-01-12 13:30:51 +00:00
|
|
|
const cssConfirmLine = styled('span', `
|
|
|
|
white-space: pre-wrap;
|
|
|
|
`);
|
|
|
|
|
2021-11-26 10:43:55 +00:00
|
|
|
const cssSection = styled('div', `
|
|
|
|
margin: 16px 16px 12px 16px;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssMenu = styled('div', `
|
|
|
|
& > li:first-child {
|
|
|
|
border-bottom: 1px solid ${colors.mediumGrey};
|
|
|
|
}
|
|
|
|
`);
|