2022-01-12 13:30:51 +00:00
|
|
|
import {allCommands} from 'app/client/components/commands';
|
|
|
|
import {GristDoc} from 'app/client/components/GristDoc';
|
2023-10-06 09:11:37 +00:00
|
|
|
import {makeTestId} from 'app/client/lib/domUtils';
|
2021-11-26 10:43:55 +00:00
|
|
|
import * as kf from 'app/client/lib/koForm';
|
2023-08-29 14:50:42 +00:00
|
|
|
import {makeT} from 'app/client/lib/localization';
|
2022-02-08 15:23:14 +00:00
|
|
|
import {ColumnToMapImpl} from 'app/client/models/ColumnToMap';
|
|
|
|
import {ColumnRec, ViewSectionRec} from 'app/client/models/DocModel';
|
2021-11-26 10:43:55 +00:00
|
|
|
import {reportError} from 'app/client/models/errors';
|
2022-08-26 12:25:34 +00:00
|
|
|
import {cssHelp, cssLabel, cssRow, cssSeparator} from 'app/client/ui/RightPanelStyles';
|
2023-10-06 09:11:37 +00:00
|
|
|
import {hoverTooltip} from 'app/client/ui/tooltips';
|
2022-02-08 15:23:14 +00:00
|
|
|
import {cssDragRow, cssFieldEntry, cssFieldLabel} from 'app/client/ui/VisibleFieldsConfig';
|
2022-01-12 13:30:51 +00:00
|
|
|
import {basicButton, primaryButton, textButton} from 'app/client/ui2018/buttons';
|
2023-04-11 05:00:28 +00:00
|
|
|
import {theme, vars} from 'app/client/ui2018/cssVars';
|
2022-02-08 15:23:14 +00:00
|
|
|
import {cssDragger} from 'app/client/ui2018/draggableList';
|
2022-08-08 13:32:50 +00:00
|
|
|
import {textInput} from 'app/client/ui2018/editableLabel';
|
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';
|
2023-10-06 09:11:37 +00:00
|
|
|
import {cssOptionLabel, IOption, IOptionFull, menu, menuItem, menuText, 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';
|
2023-10-06 09:11:37 +00:00
|
|
|
import {not, unwrap} from 'app/common/gutil';
|
2023-08-29 14:50:42 +00:00
|
|
|
import {
|
|
|
|
bundleChanges,
|
|
|
|
Computed,
|
|
|
|
Disposable,
|
|
|
|
dom,
|
|
|
|
fromKo,
|
|
|
|
MultiHolder,
|
|
|
|
Observable,
|
|
|
|
styled,
|
|
|
|
UseCBOwner
|
|
|
|
} from 'grainjs';
|
2022-10-28 16:11:08 +00:00
|
|
|
|
|
|
|
const t = makeT('CustomSectionConfig');
|
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
|
2022-02-08 15:23:14 +00:00
|
|
|
* allow to specify the custom URL.
|
2021-11-26 10:43:55 +00:00
|
|
|
*/
|
|
|
|
|
2022-02-08 15:23:14 +00:00
|
|
|
class ColumnPicker extends Disposable {
|
|
|
|
constructor(
|
|
|
|
private _value: Observable<number|number[]|null>,
|
|
|
|
private _column: ColumnToMapImpl,
|
|
|
|
private _section: ViewSectionRec){
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
public buildDom() {
|
|
|
|
// Rewrite value to ignore old configuration when allowMultiple is switched.
|
|
|
|
const properValue = Computed.create(this, use => {
|
|
|
|
const value = use(this._value);
|
|
|
|
return Array.isArray(value) ? null : value;
|
|
|
|
});
|
2023-10-06 09:11:37 +00:00
|
|
|
properValue.onWrite(value => this._value.set(value || null));
|
|
|
|
|
|
|
|
const canBeMapped = Computed.create(this, use => {
|
2022-02-08 15:23:14 +00:00
|
|
|
return use(this._section.columns)
|
2023-10-06 09:11:37 +00:00
|
|
|
.filter(col => this._column.canByMapped(use(col.pureType)));
|
|
|
|
});
|
|
|
|
|
|
|
|
// This is a HACK, to refresh options only when the menu is opened (or closed)
|
|
|
|
// and not to track down all the dependencies. Otherwise the select menu won't
|
|
|
|
// be hidden when option is selected - there is a bug that prevents it from closing
|
|
|
|
// when list of options is changed.
|
|
|
|
const refreshTrigger = Observable.create(this, false);
|
|
|
|
|
|
|
|
const options = Computed.create(this, use => {
|
|
|
|
void use(refreshTrigger);
|
|
|
|
|
|
|
|
const columnsAsOptions: IOption<number|null>[] = use(canBeMapped)
|
|
|
|
.map((col) => ({
|
|
|
|
value: col.getRowId(),
|
|
|
|
label: col.label.peek(),
|
|
|
|
icon: 'FieldColumn',
|
|
|
|
}));
|
|
|
|
|
|
|
|
// For optional mappings, add 'Blank' option but only if the value is set.
|
|
|
|
// This option will allow to clear the selection.
|
|
|
|
if (this._column.optional && properValue.get()) {
|
|
|
|
columnsAsOptions.push({
|
|
|
|
value: 0,
|
|
|
|
// Another hack. Select doesn't allow to have different label for blank option and the default text.
|
|
|
|
// So we will render this label ourselves later using `renderOptionArgs`.
|
|
|
|
label: '',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return columnsAsOptions;
|
|
|
|
});
|
|
|
|
|
|
|
|
const isDisabled = Computed.create(this, use => {
|
|
|
|
return use(canBeMapped).length === 0;
|
2022-02-08 15:23:14 +00:00
|
|
|
});
|
2023-10-06 09:11:37 +00:00
|
|
|
|
|
|
|
const defaultLabel = this._column.typeDesc != "any"
|
|
|
|
? t("Pick a {{columnType}} column", {"columnType": this._column.typeDesc})
|
|
|
|
: t("Pick a column");
|
|
|
|
|
2022-02-08 15:23:14 +00:00
|
|
|
return [
|
|
|
|
cssLabel(
|
|
|
|
this._column.title,
|
2022-12-06 13:57:29 +00:00
|
|
|
this._column.optional ? cssSubLabel(t(" (optional)")) : null,
|
2022-02-08 15:23:14 +00:00
|
|
|
testId('label-for-' + this._column.name),
|
|
|
|
),
|
2022-08-26 12:25:34 +00:00
|
|
|
this._column.description ? cssHelp(
|
|
|
|
this._column.description,
|
|
|
|
testId('help-for-' + this._column.name),
|
|
|
|
) : null,
|
2023-10-06 09:11:37 +00:00
|
|
|
dom.maybe(not(isDisabled), () => [
|
|
|
|
cssRow(
|
|
|
|
dom.update(
|
|
|
|
select(
|
|
|
|
properValue,
|
|
|
|
options,
|
|
|
|
{
|
|
|
|
defaultLabel,
|
|
|
|
renderOptionArgs : (opt) => {
|
|
|
|
// If there is a label, render it.
|
|
|
|
// Otherwise show the 'Clear selection' label as a greyed out text.
|
|
|
|
// This is the continuation of the hack from above - were we added an option
|
|
|
|
// without a label.
|
|
|
|
return (opt.label) ? null : [
|
|
|
|
cssBlank(t("Clear selection")),
|
|
|
|
testId('clear-selection'),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
),
|
|
|
|
dom.on('click', () => {
|
|
|
|
// When the menu is opened or closed, refresh the options.
|
|
|
|
refreshTrigger.set(!refreshTrigger.get());
|
|
|
|
})
|
|
|
|
),
|
|
|
|
testId('mapping-for-' + this._column.name),
|
|
|
|
testId('enabled'),
|
|
|
|
),
|
|
|
|
]),
|
|
|
|
dom.maybe(isDisabled, () => [
|
|
|
|
cssRow(
|
|
|
|
cssDisabledSelect(
|
|
|
|
Observable.create(this, null),
|
|
|
|
[], {
|
|
|
|
disabled: true,
|
|
|
|
defaultLabel: t("No {{columnType}} columns in table.", {"columnType": this._column.typeDesc})
|
|
|
|
}
|
|
|
|
),
|
|
|
|
hoverTooltip(t("No {{columnType}} columns in table.", {"columnType": this._column.typeDesc})),
|
|
|
|
testId('mapping-for-' + this._column.name),
|
|
|
|
testId('disabled'),
|
|
|
|
),
|
|
|
|
]),
|
2022-02-08 15:23:14 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ColumnListPicker extends Disposable {
|
|
|
|
constructor(
|
|
|
|
private _value: Observable<number|number[]|null>,
|
|
|
|
private _column: ColumnToMapImpl,
|
|
|
|
private _section: ViewSectionRec) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
public buildDom() {
|
|
|
|
return dom.domComputed((use) => {
|
|
|
|
return [
|
|
|
|
cssLabel(this._column.title,
|
|
|
|
cssLabel.cls("-required", !this._column.optional),
|
|
|
|
testId('label-for-' + this._column.name),
|
|
|
|
),
|
|
|
|
this._buildDraggableList(use),
|
|
|
|
this._buildAddColumn()
|
|
|
|
];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
private _buildAddColumn() {
|
2023-10-06 09:11:37 +00:00
|
|
|
|
|
|
|
const owner = MultiHolder.create(null);
|
|
|
|
|
|
|
|
const notMapped = Computed.create(owner, use => {
|
|
|
|
const value = use(this._value) || [];
|
|
|
|
const mapped = !Array.isArray(value) ? [] : value;
|
|
|
|
return this._section.columns().filter(col => !mapped.includes(use(col.id)));
|
|
|
|
});
|
|
|
|
|
|
|
|
const typedColumns = Computed.create(owner, use => {
|
|
|
|
return use(notMapped).filter(this._typeFilter(use));
|
|
|
|
});
|
|
|
|
|
2022-02-08 15:23:14 +00:00
|
|
|
return [
|
|
|
|
cssRow(
|
2023-10-06 09:11:37 +00:00
|
|
|
dom.autoDispose(owner),
|
2022-02-08 15:23:14 +00:00
|
|
|
cssAddMapping(
|
2022-12-06 13:57:29 +00:00
|
|
|
cssAddIcon('Plus'), t("Add") + ' ' + this._column.title,
|
2023-10-06 09:11:37 +00:00
|
|
|
dom.cls('disabled', use => use(notMapped).length === 0),
|
|
|
|
testId('disabled', use => use(notMapped).length === 0),
|
2022-02-08 15:23:14 +00:00
|
|
|
menu(() => {
|
2023-10-06 09:11:37 +00:00
|
|
|
const wrongTypeCount = notMapped.get().length - typedColumns.get().length;
|
2022-02-08 15:23:14 +00:00
|
|
|
return [
|
2023-10-06 09:11:37 +00:00
|
|
|
...typedColumns.get()
|
2022-02-08 15:23:14 +00:00
|
|
|
.map((col) => menuItem(
|
|
|
|
() => this._addColumn(col),
|
|
|
|
col.label.peek(),
|
|
|
|
)),
|
|
|
|
wrongTypeCount > 0 ? menuText(
|
2022-12-27 18:35:03 +00:00
|
|
|
t("{{wrongTypeCount}} non-{{columnType}} columns are not shown", {
|
|
|
|
wrongTypeCount,
|
|
|
|
columnType: this._column.type.toLowerCase(),
|
|
|
|
count: wrongTypeCount
|
|
|
|
}),
|
2022-02-08 15:23:14 +00:00
|
|
|
testId('map-message-' + this._column.name)
|
|
|
|
) : null
|
|
|
|
];
|
|
|
|
}),
|
|
|
|
testId('add-column-for-' + this._column.name),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper method for filtering columns that can be picked by the widget.
|
2023-10-06 09:11:37 +00:00
|
|
|
private _typeFilter = (use = unwrap) => (col: ColumnRec|null) =>
|
|
|
|
!col ? false : this._column.canByMapped(use(col.pureType));
|
2022-02-08 15:23:14 +00:00
|
|
|
|
|
|
|
private _buildDraggableList(use: UseCBOwner) {
|
|
|
|
return dom.update(kf.draggableList(
|
|
|
|
this._readItems(use),
|
|
|
|
this._renderItem.bind(this, use),
|
|
|
|
{
|
|
|
|
itemClass: cssDragRow.className,
|
|
|
|
reorder: this._reorder.bind(this),
|
|
|
|
receive: this._addColumn.bind(this),
|
|
|
|
drag_indicator: cssDragger,
|
|
|
|
}
|
|
|
|
), testId('map-list-for-' + this._column.name));
|
|
|
|
}
|
2023-10-06 09:11:37 +00:00
|
|
|
|
2022-02-08 15:23:14 +00:00
|
|
|
private _readItems(use: UseCBOwner): ColumnRec[] {
|
|
|
|
let selectedRefs = (use(this._value) || []) as number[];
|
|
|
|
// Ignore if configuration was changed from what it was saved.
|
|
|
|
if (!Array.isArray(selectedRefs)) {
|
|
|
|
selectedRefs = [];
|
|
|
|
}
|
|
|
|
// Filter columns by type - when column type has changed since mapping.
|
|
|
|
const columns = use(this._section.columns).filter(this._typeFilter(use));
|
|
|
|
const columnMap = new Map(columns.map(c => [c.id.peek(), c]));
|
|
|
|
// Remove any columns that are no longer there.
|
2023-08-29 14:50:42 +00:00
|
|
|
return selectedRefs.map(s => columnMap.get(s)!).filter(c => Boolean(c));
|
2022-02-08 15:23:14 +00:00
|
|
|
}
|
2023-10-06 09:11:37 +00:00
|
|
|
|
2022-02-08 15:23:14 +00:00
|
|
|
private _renderItem(use: UseCBOwner, field: ColumnRec): any {
|
|
|
|
return cssFieldEntry(
|
|
|
|
cssFieldLabel(
|
|
|
|
dom.text(field.label),
|
|
|
|
testId('ref-select-label'),
|
|
|
|
),
|
|
|
|
cssRemoveIcon(
|
|
|
|
'Remove',
|
|
|
|
dom.on('click', () => this._remove(field)),
|
|
|
|
testId('ref-select-remove'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper method that for accessing mapped columns. Can be used to set and retrieve the value.
|
|
|
|
private _list(value: number[]): void
|
|
|
|
private _list(): number[]
|
|
|
|
private _list(value?: number[]) {
|
|
|
|
if (value) {
|
|
|
|
this._value.set(value);
|
|
|
|
} else {
|
|
|
|
let current = (this._value.get() || []) as number[];
|
2023-10-06 09:11:37 +00:00
|
|
|
// Ignore if the saved value is not a number list.
|
2022-02-08 15:23:14 +00:00
|
|
|
if (!Array.isArray(current)) {
|
|
|
|
current = [];
|
|
|
|
}
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private _reorder(column: ColumnRec, nextColumn: ColumnRec|null): any {
|
|
|
|
const id = column.id.peek();
|
|
|
|
const nextId = nextColumn?.id.peek();
|
|
|
|
const currentList = this._list();
|
|
|
|
const indexOfId = currentList.indexOf(id);
|
|
|
|
// Remove element from the list.
|
|
|
|
currentList.splice(indexOfId, 1);
|
|
|
|
const indexOfNext = nextId ? currentList.indexOf(nextId) : currentList.length;
|
|
|
|
// Insert before next element or at the end.
|
|
|
|
currentList.splice(indexOfNext, 0, id);
|
|
|
|
this._list(currentList);
|
|
|
|
}
|
|
|
|
private _remove(column: ColumnRec): any {
|
|
|
|
const current = this._list();
|
|
|
|
this._value.set(current.filter(c => c != column.id.peek()));
|
|
|
|
}
|
|
|
|
private _addColumn(col: ColumnRec): any {
|
2023-10-06 09:11:37 +00:00
|
|
|
// Helper to find column model.
|
|
|
|
const model = (id: number) => this._section.columns().find(c => c.id.peek() === id) || null;
|
|
|
|
// Get the list of currently mapped columns.
|
|
|
|
let current = this._list();
|
|
|
|
// Add new column.
|
2022-02-08 15:23:14 +00:00
|
|
|
current.push(col.id.peek());
|
2023-10-06 09:11:37 +00:00
|
|
|
// Remove those that don't exists anymore.
|
|
|
|
current = current.filter(c => model(c));
|
|
|
|
// And those with wrong type.
|
|
|
|
current = current.filter(c => this._typeFilter()(model(c)));
|
2022-02-08 15:23:14 +00:00
|
|
|
this._value.set(current);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-29 14:50:42 +00:00
|
|
|
class CustomSectionConfigurationConfig extends Disposable{
|
|
|
|
// Does widget has custom configuration.
|
|
|
|
private readonly _hasConfiguration: Computed<boolean>;
|
2023-09-19 02:48:56 +00:00
|
|
|
constructor(private _section: ViewSectionRec, private _gristDoc: GristDoc) {
|
2023-08-29 14:50:42 +00:00
|
|
|
super();
|
|
|
|
this._hasConfiguration = Computed.create(this, use => use(_section.hasCustomOptions));
|
|
|
|
}
|
|
|
|
public buildDom() {
|
|
|
|
// Show prompt, when desired access level is different from actual one.
|
|
|
|
return dom(
|
|
|
|
'div',
|
|
|
|
dom.maybe(this._hasConfiguration, () =>
|
|
|
|
cssSection(
|
|
|
|
textButton(
|
|
|
|
t("Open configuration"),
|
|
|
|
dom.on('click', () => this._openConfiguration()),
|
|
|
|
testId('open-configuration')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
dom.maybeOwned(use => use(this._section.columnsToMap), (owner, columns) => {
|
|
|
|
const createObs = (column: ColumnToMapImpl) => {
|
|
|
|
const obs = Computed.create(owner, use => {
|
|
|
|
const savedDefinition = use(this._section.customDef.columnsMapping) || {};
|
|
|
|
return savedDefinition[column.name];
|
|
|
|
});
|
|
|
|
obs.onWrite(async (value) => {
|
|
|
|
const savedDefinition = this._section.customDef.columnsMapping.peek() || {};
|
|
|
|
savedDefinition[column.name] = value;
|
|
|
|
await this._section.customDef.columnsMapping.setAndSave(savedDefinition);
|
|
|
|
});
|
|
|
|
return obs;
|
|
|
|
};
|
|
|
|
// Create observables for all columns to pick.
|
|
|
|
const mappings = columns.map(c => new ColumnToMapImpl(c)).map((column) => ({
|
|
|
|
value: createObs(column),
|
|
|
|
column
|
|
|
|
}));
|
2023-09-19 02:48:56 +00:00
|
|
|
return dom('div',
|
|
|
|
this._attachColumnMappingTip(this._section.customDef.url()),
|
2023-08-29 14:50:42 +00:00
|
|
|
...mappings.map(m => m.column.allowMultiple
|
|
|
|
? dom.create(ColumnListPicker, m.value, m.column, this._section)
|
2023-09-19 02:48:56 +00:00
|
|
|
: dom.create(ColumnPicker, m.value, m.column, this._section)),
|
|
|
|
);
|
2023-08-29 14:50:42 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
private _openConfiguration(): void {
|
|
|
|
allCommands.openWidgetConfiguration.run();
|
|
|
|
}
|
|
|
|
|
2023-09-19 02:48:56 +00:00
|
|
|
private _attachColumnMappingTip(widgetUrl: string | null) {
|
|
|
|
switch (widgetUrl) {
|
|
|
|
// TODO: come up with a way to attach tips without hardcoding widget URLs.
|
|
|
|
case 'https://gristlabs.github.io/grist-widget/calendar/index.html': {
|
|
|
|
return this._gristDoc.behavioralPromptsManager.attachTip('calendarConfig', {
|
|
|
|
popupOptions: {placement: 'left-start'},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-29 14:50:42 +00:00
|
|
|
}
|
|
|
|
|
2021-11-26 10:43:55 +00:00
|
|
|
export class CustomSectionConfig extends Disposable {
|
2023-08-29 14:50:42 +00:00
|
|
|
|
|
|
|
protected _customSectionConfigurationConfig: CustomSectionConfigurationConfig;
|
2021-11-26 10:43:55 +00:00
|
|
|
// 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).
|
2023-08-29 14:50:42 +00:00
|
|
|
private readonly _selectedId: Computed<string | null>;
|
2021-11-26 10:43:55 +00:00
|
|
|
// Holds custom widget URL.
|
2023-08-29 14:50:42 +00:00
|
|
|
private readonly _url: Computed<string>;
|
2021-11-26 10:43:55 +00:00
|
|
|
// Enable or disable widget repository.
|
2023-08-29 14:50:42 +00:00
|
|
|
private readonly _canSelect: boolean = true;
|
2021-11-26 10:43:55 +00:00
|
|
|
// When widget is changed, it sets its desired access level. We will prompt
|
|
|
|
// user to approve or reject it.
|
2023-08-29 14:50:42 +00:00
|
|
|
private readonly _desiredAccess: Observable<AccessLevel|null>;
|
2021-11-26 10:43:55 +00:00
|
|
|
// Current access level (stored inside a section).
|
2023-08-29 14:50:42 +00:00
|
|
|
private readonly _currentAccess: Computed<AccessLevel>;
|
2021-11-26 10:43:55 +00:00
|
|
|
|
|
|
|
|
2023-08-29 14:50:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
constructor(protected _section: ViewSectionRec, private _gristDoc: GristDoc) {
|
|
|
|
super();
|
2023-09-19 02:48:56 +00:00
|
|
|
this._customSectionConfigurationConfig = new CustomSectionConfigurationConfig(_section, _gristDoc);
|
2022-01-12 13:30:51 +00:00
|
|
|
|
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, []);
|
2023-08-29 14:50:42 +00:00
|
|
|
this._getWidgets().catch(reportError);
|
|
|
|
// Request for rest of the widgets.
|
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-02-08 15:23:14 +00:00
|
|
|
return CUSTOM_ID;
|
2021-11-26 10:43:55 +00:00
|
|
|
});
|
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(() => {
|
2023-09-19 18:44:22 +00:00
|
|
|
// Reset whether widget should render after `grist.ready()`.
|
|
|
|
_section.customDef.renderAfterReady(false);
|
2021-11-26 10:43:55 +00:00
|
|
|
// 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
|
|
|
// 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);
|
2022-02-08 15:23:14 +00:00
|
|
|
// Clear column mappings.
|
|
|
|
_section.customDef.columnsMapping(null);
|
|
|
|
_section.columnsToMap(null);
|
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(() => {
|
2023-09-19 18:44:22 +00:00
|
|
|
// Reset whether widget should render after `grist.ready()`.
|
2023-10-06 13:17:39 +00:00
|
|
|
_section.customDef.renderAfterReady(selectedWidget.renderAfterReady ?? false);
|
2021-11-26 10:43:55 +00:00
|
|
|
// 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);
|
2022-02-08 15:23:14 +00:00
|
|
|
// Clear column mappings.
|
|
|
|
_section.customDef.columnsMapping(null);
|
|
|
|
_section.columnsToMap(null);
|
2021-11-26 10:43:55 +00:00
|
|
|
});
|
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) || '');
|
2023-09-19 18:44:22 +00:00
|
|
|
this._url.onWrite(async newUrl => {
|
|
|
|
bundleChanges(() => {
|
|
|
|
_section.customDef.renderAfterReady(false);
|
|
|
|
_section.customDef.url(newUrl);
|
|
|
|
});
|
|
|
|
await _section.saveCustomDef();
|
|
|
|
});
|
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(() => this._reject()));
|
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) {
|
2022-12-06 13:57:29 +00:00
|
|
|
case AccessLevel.none: return cssConfirmLine(t("Widget does not require any permissions."));
|
2022-12-27 18:35:03 +00:00
|
|
|
case AccessLevel.read_table:
|
|
|
|
return cssConfirmLine(t("Widget needs to {{read}} the current table.", {read: dom("b", "read")}));
|
|
|
|
case AccessLevel.full:
|
|
|
|
return cssConfirmLine(t("Widget needs {{fullAccess}} to this document.", {
|
|
|
|
fullAccess: dom("b", "full access")
|
|
|
|
}));
|
2022-01-12 13:30:51 +00:00
|
|
|
default: throw new Error(`Unsupported ${level} access level`);
|
|
|
|
}
|
|
|
|
}
|
2021-11-26 10:43:55 +00:00
|
|
|
// Options for access level.
|
|
|
|
const levels: IOptionFull<string>[] = [
|
2022-12-06 13:57:29 +00:00
|
|
|
{label: t("No document access"), value: AccessLevel.none},
|
|
|
|
{label: t("Read selected table"), value: AccessLevel.read_table},
|
|
|
|
{label: t("Full document access"), value: AccessLevel.full},
|
2021-11-26 10:43:55 +00:00
|
|
|
];
|
|
|
|
return dom(
|
|
|
|
'div',
|
|
|
|
dom.autoDispose(holder),
|
2023-08-29 14:50:42 +00:00
|
|
|
this.shouldRenderWidgetSelector() &&
|
2022-01-12 13:30:51 +00:00
|
|
|
this._canSelect
|
|
|
|
? cssRow(
|
2023-08-29 14:50:42 +00:00
|
|
|
select(this._selectedId, options, {
|
|
|
|
defaultLabel: t("Select Custom Widget"),
|
|
|
|
menuCssClass: cssMenu.className,
|
|
|
|
}),
|
|
|
|
testId('select')
|
|
|
|
)
|
2022-01-12 13:30:51 +00:00
|
|
|
: null,
|
2023-08-29 14:50:42 +00:00
|
|
|
dom.maybe(isCustom && this.shouldRenderWidgetSelector(), () => [
|
2021-11-26 10:43:55 +00:00
|
|
|
cssRow(
|
|
|
|
cssTextInput(
|
|
|
|
this._url,
|
|
|
|
async value => this._url.set(value),
|
2022-12-06 13:57:29 +00:00
|
|
|
dom.attr('placeholder', t("Enter Custom URL")),
|
2021-11-26 10:43:55 +00:00
|
|
|
testId('url')
|
2023-04-14 10:09:50 +00:00
|
|
|
),
|
|
|
|
this._gristDoc.behavioralPromptsManager.attachTip('customURL', {
|
|
|
|
popupOptions: {
|
|
|
|
placement: 'left-start',
|
2023-09-19 02:48:56 +00:00
|
|
|
},
|
2023-09-27 00:40:34 +00:00
|
|
|
isDisabled: () => {
|
|
|
|
// Disable tip if a custom widget is already selected.
|
|
|
|
return Boolean(this._selectedId.get() && !(isCustom.get() && this._url.get().trim() === ''));
|
2023-09-19 02:48:56 +00:00
|
|
|
},
|
2023-04-14 10:09:50 +00:00
|
|
|
})
|
2021-11-26 10:43:55 +00:00
|
|
|
),
|
|
|
|
]),
|
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')),
|
|
|
|
]
|
|
|
|
),
|
2023-08-29 14:50:42 +00:00
|
|
|
cssSection(
|
|
|
|
cssLink(
|
|
|
|
dom.attr('href', 'https://support.getgrist.com/widget-custom'),
|
|
|
|
dom.attr('target', '_blank'),
|
|
|
|
t("Learn more about custom widgets")
|
2022-01-12 13:30:51 +00:00
|
|
|
)
|
|
|
|
),
|
2023-08-29 14:50:42 +00:00
|
|
|
cssSeparator(),
|
|
|
|
this._customSectionConfigurationConfig.buildDom(),
|
2021-11-26 10:43:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-29 14:50:42 +00:00
|
|
|
protected shouldRenderWidgetSelector(): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected async _getWidgets() {
|
|
|
|
const api = this._gristDoc.app.topAppModel.api;
|
2023-10-06 13:17:39 +00:00
|
|
|
const widgets = await api.getWidgets();
|
2023-10-11 21:03:02 +00:00
|
|
|
if (this.isDisposed()) { return; }
|
|
|
|
|
2023-08-29 14:50:42 +00:00
|
|
|
// Request for rest of the widgets.
|
|
|
|
if (this._canSelect) {
|
|
|
|
// From the start we will provide single widget definition
|
|
|
|
// that was chosen previously.
|
|
|
|
if (this._section.customDef.widgetDef.peek()) {
|
2023-10-06 13:17:39 +00:00
|
|
|
widgets.push(this._section.customDef.widgetDef.peek()!);
|
2023-08-29 14:50:42 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-06 13:17:39 +00:00
|
|
|
this._widgets.set(widgets);
|
2022-01-12 13:30:51 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2023-04-11 05:00:28 +00:00
|
|
|
--icon-color: ${theme.iconError}
|
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 {
|
2023-04-11 05:00:28 +00:00
|
|
|
border-bottom: 1px solid ${theme.menuBorder};
|
2021-11-26 10:43:55 +00:00
|
|
|
}
|
|
|
|
`);
|
2022-02-08 15:23:14 +00:00
|
|
|
|
|
|
|
const cssAddIcon = styled(icon, `
|
|
|
|
margin-right: 4px;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssRemoveIcon = styled(icon, `
|
|
|
|
display: none;
|
|
|
|
cursor: pointer;
|
|
|
|
flex: none;
|
|
|
|
margin-left: 8px;
|
|
|
|
.${cssFieldEntry.className}:hover & {
|
|
|
|
display: block;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
2022-08-08 13:32:50 +00:00
|
|
|
// Additional text in label (greyed out)
|
|
|
|
const cssSubLabel = styled('span', `
|
|
|
|
text-transform: none;
|
|
|
|
font-size: ${vars.xsmallFontSize};
|
2023-04-11 05:00:28 +00:00
|
|
|
color: ${theme.lightText};
|
2022-08-08 13:32:50 +00:00
|
|
|
`);
|
|
|
|
|
2022-02-08 15:23:14 +00:00
|
|
|
const cssAddMapping = styled('div', `
|
|
|
|
display: flex;
|
|
|
|
cursor: pointer;
|
2023-04-11 05:00:28 +00:00
|
|
|
color: ${theme.controlFg};
|
|
|
|
--icon-color: ${theme.controlFg};
|
2022-02-08 15:23:14 +00:00
|
|
|
|
|
|
|
&:not(:first-child) {
|
|
|
|
margin-top: 8px;
|
|
|
|
}
|
|
|
|
&:hover, &:focus, &:active {
|
2023-04-11 05:00:28 +00:00
|
|
|
color: ${theme.controlHoverFg};
|
|
|
|
--icon-color: ${theme.controlHoverFg};
|
2022-02-08 15:23:14 +00:00
|
|
|
}
|
2023-10-06 09:11:37 +00:00
|
|
|
&.disabled {
|
|
|
|
color: ${theme.lightText};
|
|
|
|
--icon-color: ${theme.lightText};
|
|
|
|
pointer-events: none;
|
|
|
|
}
|
2022-02-08 15:23:14 +00:00
|
|
|
`);
|
2022-08-08 13:32:50 +00:00
|
|
|
|
|
|
|
const cssTextInput = styled(textInput, `
|
|
|
|
flex: 1 0 auto;
|
|
|
|
|
2023-04-11 05:00:28 +00:00
|
|
|
color: ${theme.inputFg};
|
|
|
|
background-color: ${theme.inputBg};
|
|
|
|
|
2022-08-08 13:32:50 +00:00
|
|
|
&:disabled {
|
2023-04-11 05:00:28 +00:00
|
|
|
color: ${theme.inputDisabledFg};
|
|
|
|
background-color: ${theme.inputDisabledBg};
|
2022-08-08 13:32:50 +00:00
|
|
|
pointer-events: none;
|
|
|
|
}
|
2023-04-11 05:00:28 +00:00
|
|
|
|
|
|
|
&::placeholder {
|
|
|
|
color: ${theme.inputPlaceholderFg};
|
|
|
|
}
|
2022-08-08 13:32:50 +00:00
|
|
|
`);
|
2023-10-06 09:11:37 +00:00
|
|
|
|
|
|
|
const cssDisabledSelect = styled(select, `
|
|
|
|
opacity: unset !important;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssBlank = styled(cssOptionLabel, `
|
|
|
|
--grist-option-label-color: ${theme.lightText};
|
|
|
|
`);
|