2023-08-29 14:50:42 +00:00
|
|
|
import {BehavioralPromptsManager} from 'app/client/components/BehavioralPromptsManager';
|
|
|
|
import {GristDoc} from 'app/client/components/GristDoc';
|
|
|
|
import {makeT} from 'app/client/lib/localization';
|
|
|
|
import {reportError} from 'app/client/models/AppModel';
|
|
|
|
import {ColumnRec, TableRec, ViewSectionRec} from 'app/client/models/DocModel';
|
2023-12-12 09:58:20 +00:00
|
|
|
import {GRIST_FORMS_FEATURE, PERMITTED_CUSTOM_WIDGETS} from "app/client/models/features";
|
2023-08-29 14:50:42 +00:00
|
|
|
import {GristTooltips} from 'app/client/ui/GristTooltips';
|
|
|
|
import {linkId, NoLink} from 'app/client/ui/selectBy';
|
|
|
|
import {overflowTooltip, withInfoTooltip} from 'app/client/ui/tooltips';
|
|
|
|
import {getWidgetTypes} from "app/client/ui/widgetTypesMap";
|
|
|
|
import {bigPrimaryButton} from "app/client/ui2018/buttons";
|
|
|
|
import {theme, vars} from "app/client/ui2018/cssVars";
|
|
|
|
import {icon} from "app/client/ui2018/icons";
|
|
|
|
import {spinnerModal} from 'app/client/ui2018/modals';
|
|
|
|
import {isLongerThan, nativeCompare} from "app/common/gutil";
|
|
|
|
import {IAttachedCustomWidget, IWidgetType} from 'app/common/widgetTypes';
|
|
|
|
import {
|
|
|
|
computed,
|
|
|
|
Computed,
|
|
|
|
Disposable,
|
|
|
|
dom,
|
|
|
|
domComputed,
|
|
|
|
DomElementArg,
|
|
|
|
fromKo,
|
|
|
|
IOption,
|
|
|
|
makeTestId,
|
|
|
|
Observable,
|
|
|
|
onKeyDown,
|
|
|
|
select,
|
|
|
|
styled
|
|
|
|
} from "grainjs";
|
2020-10-02 15:10:00 +00:00
|
|
|
import Popper from 'popper.js';
|
2023-08-29 14:50:42 +00:00
|
|
|
import {IOpenController, popupOpen, setPopupToCreateDom} from 'popweasel';
|
|
|
|
import without = require('lodash/without');
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2022-10-28 16:11:08 +00:00
|
|
|
const t = makeT('PageWidgetPicker');
|
|
|
|
|
2023-12-12 09:58:20 +00:00
|
|
|
type TableRef = number|'New Table'|null;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// Describes a widget selection.
|
|
|
|
export interface IPageWidget {
|
|
|
|
|
|
|
|
// The widget type
|
|
|
|
type: IWidgetType;
|
|
|
|
|
|
|
|
// The table (one of the listed tables or 'New Table')
|
2023-12-12 09:58:20 +00:00
|
|
|
table: TableRef;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// Whether to summarize the table (not available for "New Table").
|
|
|
|
summarize: boolean;
|
|
|
|
|
|
|
|
// some of the listed columns to use to summarize the table.
|
|
|
|
columns: number[];
|
|
|
|
|
|
|
|
// link
|
|
|
|
link: string;
|
|
|
|
|
|
|
|
// the page widget section id (should be 0 for a to-be-saved new widget)
|
|
|
|
section: number;
|
|
|
|
}
|
|
|
|
|
2024-01-18 17:23:50 +00:00
|
|
|
export const DefaultPageWidget: () => IPageWidget = () => ({
|
|
|
|
type: 'record',
|
|
|
|
table: null,
|
|
|
|
summarize: false,
|
|
|
|
columns: [],
|
|
|
|
link: NoLink,
|
|
|
|
section: 0,
|
|
|
|
});
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// Creates a IPageWidget from a ViewSectionRec.
|
|
|
|
export function toPageWidget(section: ViewSectionRec): IPageWidget {
|
|
|
|
const link = linkId({
|
|
|
|
srcSectionRef: section.linkSrcSectionRef.peek(),
|
|
|
|
srcColRef: section.linkSrcColRef.peek(),
|
|
|
|
targetColRef: section.linkTargetColRef.peek()
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
type: section.parentKey.peek() as IWidgetType,
|
|
|
|
table: section.table.peek().summarySourceTable.peek() || section.tableRef.peek(),
|
|
|
|
summarize: Boolean(section.table.peek().summarySourceTable.peek()),
|
|
|
|
columns: section.table.peek().columns.peek().peek()
|
|
|
|
.filter((col) => col.summarySourceCol.peek())
|
|
|
|
.map((col) => col.summarySourceCol.peek()),
|
|
|
|
link, section: section.id.peek()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export interface IOptions extends ISelectOptions {
|
|
|
|
|
|
|
|
// the initial selected value, we call the function when the popup get triggered
|
|
|
|
value?: () => IPageWidget;
|
|
|
|
|
|
|
|
// placement, directly passed to the underlying Popper library.
|
|
|
|
placement?: Popper.Placement;
|
|
|
|
}
|
|
|
|
|
|
|
|
const testId = makeTestId('test-wselect-');
|
|
|
|
|
2023-12-12 09:58:20 +00:00
|
|
|
function maybeForms(): Array<'form'> {
|
|
|
|
return GRIST_FORMS_FEATURE() ? ['form'] : [];
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// The picker disables some choices that do not make much sense. This function return the list of
|
|
|
|
// compatible types given the tableId and whether user is creating a new page or not.
|
2023-12-12 09:58:20 +00:00
|
|
|
function getCompatibleTypes(tableId: TableRef, isNewPage: boolean|undefined): IWidgetType[] {
|
2020-10-02 15:10:00 +00:00
|
|
|
if (tableId !== 'New Table') {
|
2023-12-12 09:58:20 +00:00
|
|
|
return ['record', 'single', 'detail', 'chart', 'custom', 'custom.calendar', ...maybeForms()];
|
2020-10-02 15:10:00 +00:00
|
|
|
} else if (isNewPage) {
|
|
|
|
// New view + new table means we'll be switching to the primary view.
|
2023-12-12 09:58:20 +00:00
|
|
|
return ['record', ...maybeForms()];
|
2020-10-02 15:10:00 +00:00
|
|
|
} else {
|
|
|
|
// The type 'chart' makes little sense when creating a new table.
|
2023-12-12 09:58:20 +00:00
|
|
|
return ['record', 'single', 'detail', ...maybeForms()];
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Whether table and type make for a valid selection whether the user is creating a new page or not.
|
2023-12-12 09:58:20 +00:00
|
|
|
function isValidSelection(table: TableRef, type: IWidgetType, isNewPage: boolean|undefined) {
|
2020-10-02 15:10:00 +00:00
|
|
|
return table !== null && getCompatibleTypes(table, isNewPage).includes(type);
|
|
|
|
}
|
|
|
|
|
2022-04-07 10:10:58 +00:00
|
|
|
export type ISaveFunc = (val: IPageWidget) => Promise<any>;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// Delay in milliseconds, after a user click on the save btn, before we start showing a modal
|
|
|
|
// spinner. If saving completes before this time elapses (which is likely to happen for regular
|
|
|
|
// table) we don't show the modal spinner.
|
|
|
|
const DELAY_BEFORE_SPINNER_MS = 500;
|
|
|
|
|
|
|
|
// Attaches the page widget picker to elem to open on 'click' on the left.
|
2022-12-20 02:06:39 +00:00
|
|
|
export function attachPageWidgetPicker(elem: HTMLElement, gristDoc: GristDoc, onSave: ISaveFunc,
|
2020-10-02 15:10:00 +00:00
|
|
|
options: IOptions = {}) {
|
|
|
|
// Overrides .placement, this is needed to enable the page widget to update position when user
|
|
|
|
// expand the `Group By` panel.
|
|
|
|
// TODO: remove .placement from the options of this method (note: breaking buildPageWidgetPicker
|
|
|
|
// into two steps, one for model creation and the other for building UI, seems promising. In
|
|
|
|
// particular listening to value.summarize to update popup position could be done directly in
|
|
|
|
// code).
|
|
|
|
options.placement = 'left';
|
2022-12-20 02:06:39 +00:00
|
|
|
const domCreator = (ctl: IOpenController) => buildPageWidgetPicker(ctl, gristDoc, onSave, options);
|
2020-10-02 15:10:00 +00:00
|
|
|
setPopupToCreateDom(elem, domCreator, {
|
|
|
|
placement: 'left',
|
|
|
|
trigger: ['click'],
|
|
|
|
attach: 'body',
|
|
|
|
boundaries: 'viewport'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open page widget widget picker on the right of element.
|
2022-12-20 02:06:39 +00:00
|
|
|
export function openPageWidgetPicker(elem: HTMLElement, gristDoc: GristDoc, onSave: ISaveFunc,
|
2020-10-02 15:10:00 +00:00
|
|
|
options: IOptions = {}) {
|
|
|
|
popupOpen(elem, (ctl) => buildPageWidgetPicker(
|
2022-12-20 02:06:39 +00:00
|
|
|
ctl, gristDoc, onSave, options
|
2020-10-02 15:10:00 +00:00
|
|
|
), { placement: 'right' });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Builds a picker to stick into the popup. Takes care of setting up the initial selected value and
|
|
|
|
// bind various events to the popup behaviours: close popup on save, gives focus to the picker,
|
|
|
|
// binds cancel and save to Escape and Enter keydown events. Also takes care of preventing the popup
|
|
|
|
// to overlay the trigger element (which could happen when the 'Group By' panel is expanded for the
|
|
|
|
// first time). When saving is taking time, show a modal spinner (see DELAY_BEFORE_SPINNER_MS).
|
|
|
|
export function buildPageWidgetPicker(
|
2022-12-20 02:06:39 +00:00
|
|
|
ctl: IOpenController,
|
|
|
|
gristDoc: GristDoc,
|
|
|
|
onSave: ISaveFunc,
|
|
|
|
options: IOptions = {}
|
|
|
|
) {
|
2023-01-13 07:39:33 +00:00
|
|
|
const {behavioralPromptsManager, docModel} = gristDoc;
|
2022-07-06 07:41:09 +00:00
|
|
|
const tables = fromKo(docModel.visibleTables.getObservable());
|
2020-10-02 15:10:00 +00:00
|
|
|
const columns = fromKo(docModel.columns.createAllRowsModel('parentPos').getObservable());
|
|
|
|
|
|
|
|
// default value for when it is omitted
|
|
|
|
const defaultValue: IPageWidget = {
|
|
|
|
type: 'record',
|
|
|
|
table: null, // when creating a new widget, let's initially have no table selected
|
|
|
|
summarize: false,
|
|
|
|
columns: [],
|
|
|
|
link: NoLink,
|
|
|
|
section: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
// get initial value and setup state for the picker.
|
|
|
|
const initValue = options.value && options.value() || defaultValue;
|
|
|
|
const value: IWidgetValueObs = {
|
|
|
|
type: Observable.create(ctl, initValue.type),
|
|
|
|
table: Observable.create(ctl, initValue.table),
|
|
|
|
summarize: Observable.create(ctl, initValue.summarize),
|
|
|
|
columns: Observable.create(ctl, initValue.columns),
|
|
|
|
link: Observable.create(ctl, initValue.link),
|
|
|
|
section: Observable.create(ctl, initValue.section)
|
|
|
|
};
|
|
|
|
|
|
|
|
// calls onSave and closes the popup. Failure must be handled by the caller.
|
|
|
|
async function onSaveCB() {
|
|
|
|
ctl.close();
|
2021-06-14 21:58:22 +00:00
|
|
|
const type = value.type.get();
|
2020-10-02 15:10:00 +00:00
|
|
|
const savePromise = onSave({
|
2021-06-14 21:58:22 +00:00
|
|
|
type,
|
2020-10-02 15:10:00 +00:00
|
|
|
table: value.table.get(),
|
|
|
|
summarize: value.summarize.get(),
|
|
|
|
columns: sortedAs(value.columns.get(), columns.get().map((col) => col.id.peek())),
|
|
|
|
link: value.link.get(),
|
|
|
|
section: value.section.get(),
|
|
|
|
});
|
2022-05-04 09:54:30 +00:00
|
|
|
if (value.table.get() === 'New Table') {
|
|
|
|
// Adding empty table will show a prompt, so we don't want to wait for it.
|
|
|
|
await savePromise;
|
|
|
|
} else {
|
|
|
|
// If savePromise throws an error, before or after timeout, we let the error propagate as it
|
|
|
|
// should be handle by the caller.
|
|
|
|
if (await isLongerThan(savePromise, DELAY_BEFORE_SPINNER_MS)) {
|
|
|
|
const label = getWidgetTypes(type).label;
|
2022-12-06 13:57:29 +00:00
|
|
|
await spinnerModal(t("Building {{- label}} widget", { label }), savePromise);
|
2022-05-04 09:54:30 +00:00
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// whether the current selection is valid
|
|
|
|
function isValid() {
|
|
|
|
return isValidSelection(value.table.get(), value.type.get(), options.isNewPage);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Summarizing a table causes the 'Group By' panel to expand on the right. To prevent it from
|
|
|
|
// overlaying the trigger, we bind an update of the popup to it when it is on the left of the
|
|
|
|
// trigger.
|
|
|
|
// WARN: This does not work when the picker is triggered from a menu item because the trigger
|
|
|
|
// element does not exist anymore at this time so calling update will misplace the popup. However,
|
|
|
|
// this is not a problem at the time or writing because the picker is never placed at the left of
|
|
|
|
// a menu item (currently picker is only placed at the right of a menu item and at the left of a
|
|
|
|
// basic button).
|
|
|
|
if (options.placement && options.placement === 'left') {
|
|
|
|
ctl.autoDispose(value.summarize.addListener((val, old) => val && ctl.update()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// dom
|
|
|
|
return cssPopupWrapper(
|
2023-01-13 07:39:33 +00:00
|
|
|
dom.create(PageWidgetSelect,
|
|
|
|
value, tables, columns, onSaveCB, behavioralPromptsManager, options),
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// gives focus and binds keydown events
|
2021-05-23 17:43:11 +00:00
|
|
|
(elem: any) => { setTimeout(() => elem.focus(), 0); },
|
2020-10-02 15:10:00 +00:00
|
|
|
onKeyDown({
|
|
|
|
Escape: () => ctl.close(),
|
|
|
|
Enter: () => isValid() && onSaveCB()
|
|
|
|
})
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same as IWidgetValue but with observable values
|
|
|
|
export type IWidgetValueObs = {
|
|
|
|
[P in keyof IPageWidget]: Observable<IPageWidget[P]>;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export interface ISelectOptions {
|
|
|
|
// the button's label
|
|
|
|
buttonLabel?: string;
|
|
|
|
|
|
|
|
// Indicates whether the section builder is in a new view
|
|
|
|
isNewPage?: boolean;
|
|
|
|
|
|
|
|
// A callback to provides the links that are available to a page widget. It is called any time the
|
|
|
|
// user changes in the selected page widget (type, table, summary ...) and we update the "SELECT
|
|
|
|
// BY" dropdown with the result list of options. The "SELECT BY" dropdown is hidden if omitted.
|
|
|
|
selectBy?: (val: IPageWidget) => Array<IOption<string>>;
|
|
|
|
}
|
|
|
|
|
2023-08-29 14:50:42 +00:00
|
|
|
const registeredCustomWidgets: IAttachedCustomWidget[] = ['custom.calendar'];
|
|
|
|
|
|
|
|
const permittedCustomWidgets: IAttachedCustomWidget[] = PERMITTED_CUSTOM_WIDGETS().get().map((widget) =>
|
|
|
|
widget as IAttachedCustomWidget)??[];
|
2020-10-02 15:10:00 +00:00
|
|
|
// the list of widget types in the order they should be listed by the widget.
|
2023-08-29 14:50:42 +00:00
|
|
|
const finalListOfCustomWidgetToShow = permittedCustomWidgets.filter(a=>
|
|
|
|
registeredCustomWidgets.includes(a));
|
2020-10-02 15:10:00 +00:00
|
|
|
const sectionTypes: IWidgetType[] = [
|
2023-12-12 09:58:20 +00:00
|
|
|
'record', 'single', 'detail', ...maybeForms(), 'chart', ...finalListOfCustomWidgetToShow, 'custom'
|
2020-10-02 15:10:00 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
// Returns dom that let a user select a page widget. User can select a widget type (id: 'grid',
|
|
|
|
// 'card', ...), one of `tables` and optionally some of the `columns` of the selected table if she
|
|
|
|
// wants to generate a summary. Clicking the `Add ...` button trigger `onSave()`. Note: this is an
|
|
|
|
// internal method used by widgetPicker, it is only exposed for testing reason.
|
|
|
|
export class PageWidgetSelect extends Disposable {
|
|
|
|
|
|
|
|
// an observable holding the list of options of the `select by` dropdown
|
|
|
|
private _selectByOptions = this._options.selectBy ?
|
|
|
|
Computed.create(this, (use) => {
|
|
|
|
// TODO: it is unfortunate to have to convert from IWidgetValueObs to IWidgetValue. Maybe
|
|
|
|
// better to change this._value to be Observable<IWidgetValue> instead.
|
|
|
|
const val = {
|
|
|
|
type: use(this._value.type),
|
|
|
|
table: use(this._value.table),
|
|
|
|
summarize: use(this._value.summarize),
|
|
|
|
columns: use(this._value.columns),
|
|
|
|
// should not have a dependency on .link
|
|
|
|
link: this._value.link.get(),
|
|
|
|
section: use(this._value.section),
|
|
|
|
};
|
|
|
|
return this._options.selectBy!(val);
|
|
|
|
}) :
|
|
|
|
null;
|
|
|
|
|
2022-12-27 18:35:03 +00:00
|
|
|
private _isNewTableDisabled = Computed.create(this, this._value.type, (use, type) => !isValidSelection(
|
|
|
|
'New Table', type, this._options.isNewPage));
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
private _value: IWidgetValueObs,
|
|
|
|
private _tables: Observable<TableRec[]>,
|
|
|
|
private _columns: Observable<ColumnRec[]>,
|
|
|
|
private _onSave: () => Promise<void>,
|
2023-01-13 07:39:33 +00:00
|
|
|
private _behavioralPromptsManager: BehavioralPromptsManager,
|
2020-10-02 15:10:00 +00:00
|
|
|
private _options: ISelectOptions = {}
|
|
|
|
) { super(); }
|
|
|
|
|
|
|
|
public buildDom() {
|
|
|
|
return cssContainer(
|
|
|
|
testId('container'),
|
|
|
|
cssBody(
|
|
|
|
cssPanel(
|
2022-12-06 13:57:29 +00:00
|
|
|
header(t("Select Widget")),
|
2020-10-02 15:10:00 +00:00
|
|
|
sectionTypes.map((value) => {
|
|
|
|
const {label, icon: iconName} = getWidgetTypes(value);
|
|
|
|
const disabled = computed(this._value.table, (use, tid) => this._isTypeDisabled(value, tid));
|
|
|
|
return cssEntry(
|
|
|
|
dom.autoDispose(disabled),
|
|
|
|
cssTypeIcon(iconName),
|
|
|
|
label,
|
|
|
|
dom.on('click', () => !disabled.get() && this._selectType(value)),
|
|
|
|
cssEntry.cls('-selected', (use) => use(this._value.type) === value),
|
|
|
|
cssEntry.cls('-disabled', disabled),
|
|
|
|
testId('type'),
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
cssPanel(
|
|
|
|
testId('data'),
|
2022-12-06 13:57:29 +00:00
|
|
|
header(t("Select Data")),
|
2020-10-02 15:10:00 +00:00
|
|
|
cssEntry(
|
|
|
|
cssIcon('TypeTable'), 'New Table',
|
|
|
|
// prevent the selection of 'New Table' if it is disabled
|
|
|
|
dom.on('click', (ev) => !this._isNewTableDisabled.get() && this._selectTable('New Table')),
|
2023-01-13 07:39:33 +00:00
|
|
|
this._behavioralPromptsManager.attachTip('pageWidgetPicker', {
|
2022-12-20 02:06:39 +00:00
|
|
|
popupOptions: {
|
|
|
|
attach: null,
|
|
|
|
placement: 'right-start',
|
|
|
|
}
|
|
|
|
}),
|
2020-10-02 15:10:00 +00:00
|
|
|
cssEntry.cls('-selected', (use) => use(this._value.table) === 'New Table'),
|
|
|
|
cssEntry.cls('-disabled', this._isNewTableDisabled),
|
2022-12-20 02:06:39 +00:00
|
|
|
testId('table'),
|
2020-10-02 15:10:00 +00:00
|
|
|
),
|
|
|
|
dom.forEach(this._tables, (table) => dom('div',
|
|
|
|
cssEntryWrapper(
|
2022-04-27 17:46:24 +00:00
|
|
|
cssEntry(cssIcon('TypeTable'),
|
2023-03-13 07:03:59 +00:00
|
|
|
cssLabel(dom.text(table.tableNameDef), overflowTooltip()),
|
2020-10-02 15:10:00 +00:00
|
|
|
dom.on('click', () => this._selectTable(table.id())),
|
|
|
|
cssEntry.cls('-selected', (use) => use(this._value.table) === table.id()),
|
|
|
|
testId('table-label')
|
|
|
|
),
|
|
|
|
cssPivot(
|
|
|
|
cssBigIcon('Pivot'),
|
|
|
|
cssEntry.cls('-selected', (use) => use(this._value.summarize) && use(this._value.table) === table.id()),
|
|
|
|
dom.on('click', (ev, el) => this._selectPivot(table.id(), el as HTMLElement)),
|
|
|
|
testId('pivot'),
|
|
|
|
),
|
|
|
|
testId('table'),
|
|
|
|
)
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
cssPanel(
|
2022-12-06 13:57:29 +00:00
|
|
|
header(t("Group by")),
|
2020-10-02 15:10:00 +00:00
|
|
|
dom.hide((use) => !use(this._value.summarize)),
|
|
|
|
domComputed(
|
|
|
|
(use) => use(this._columns)
|
|
|
|
.filter((col) => !col.isHiddenCol() && col.parentId() === use(this._value.table)),
|
|
|
|
(cols) => cols ?
|
|
|
|
dom.forEach(cols, (col) =>
|
|
|
|
cssEntry(cssIcon('FieldColumn'), cssFieldLabel(dom.text(col.label)),
|
|
|
|
dom.on('click', () => this._toggleColumnId(col.id())),
|
|
|
|
cssEntry.cls('-selected', (use) => use(this._value.columns).includes(col.id())),
|
|
|
|
testId('column')
|
|
|
|
)
|
|
|
|
) :
|
|
|
|
null
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
cssFooter(
|
|
|
|
cssFooterContent(
|
|
|
|
// If _selectByOptions exists and has more than then "NoLinkOption", show the selector.
|
2022-10-19 23:06:05 +00:00
|
|
|
dom.maybe((use) => this._selectByOptions && use(this._selectByOptions).length > 1, () =>
|
|
|
|
withInfoTooltip(
|
|
|
|
cssSelectBy(
|
|
|
|
cssSmallLabel('SELECT BY'),
|
|
|
|
dom.update(cssSelect(this._value.link, this._selectByOptions!),
|
|
|
|
testId('selectby'))
|
|
|
|
),
|
|
|
|
GristTooltips.selectBy(),
|
2023-10-23 05:51:08 +00:00
|
|
|
{popupOptions: {attach: null}, domArgs: [
|
2023-01-13 07:39:33 +00:00
|
|
|
this._behavioralPromptsManager.attachTip('pageWidgetPickerSelectBy', {
|
2022-12-20 02:06:39 +00:00
|
|
|
popupOptions: {
|
|
|
|
attach: null,
|
|
|
|
placement: 'bottom',
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
]},
|
2022-10-19 23:06:05 +00:00
|
|
|
)
|
|
|
|
),
|
2020-10-02 15:10:00 +00:00
|
|
|
dom('div', {style: 'flex-grow: 1'}),
|
|
|
|
bigPrimaryButton(
|
|
|
|
// TODO: The button's label of the page widget picker should read 'Close' instead when
|
|
|
|
// there are no changes.
|
2022-12-06 13:57:29 +00:00
|
|
|
this._options.buttonLabel || t("Add to Page"),
|
2020-10-02 15:10:00 +00:00
|
|
|
dom.prop('disabled', (use) => !isValidSelection(
|
|
|
|
use(this._value.table), use(this._value.type), this._options.isNewPage)
|
|
|
|
),
|
|
|
|
dom.on('click', () => this._onSave().catch(reportError)),
|
|
|
|
testId('addBtn'),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private _closeSummarizePanel() {
|
|
|
|
this._value.summarize.set(false);
|
|
|
|
this._value.columns.set([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private _openSummarizePanel() {
|
|
|
|
this._value.summarize.set(true);
|
|
|
|
}
|
|
|
|
|
2022-12-27 18:35:03 +00:00
|
|
|
private _selectType(type: IWidgetType) {
|
|
|
|
this._value.type.set(type);
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
2023-12-12 09:58:20 +00:00
|
|
|
private _selectTable(tid: TableRef) {
|
(core) When changing a table for a page widget, unset widget-linking to avoid invalid values.
Summary:
Previously, using "Change Widget" allowed one to change the underlying table,
but would keep the linking settings. This could allow invalid settings which
would sometimes lead to JS errors. These manifested in production as
"UserError: Query error: n is not a function".
- Unset linking settings in this case, to avoid invalid values.
- In case invalid values are encountered (e.g. saved previously), treat them as
unset, to avoid JS errors.
- If an error does occur, report it with a stack trace.
Also, for testing, added 'selectBy' option to gristUtils helpers for using page-widget-picker.
Test Plan: Added test cases for resetting linking, and for ignoring invalid link settings.
Reviewers: alexmojaki
Reviewed By: alexmojaki
Differential Revision: https://phab.getgrist.com/D2993
2021-08-24 03:28:07 +00:00
|
|
|
if (tid !== this._value.table.get()) {
|
|
|
|
this._value.link.set(NoLink);
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
this._value.table.set(tid);
|
|
|
|
this._closeSummarizePanel();
|
|
|
|
}
|
|
|
|
|
|
|
|
private _isSelected(el: HTMLElement) {
|
|
|
|
return el.classList.contains(cssEntry.className + '-selected');
|
|
|
|
}
|
|
|
|
|
2023-12-12 09:58:20 +00:00
|
|
|
private _selectPivot(tid: TableRef, pivotEl: HTMLElement) {
|
2020-10-02 15:10:00 +00:00
|
|
|
if (this._isSelected(pivotEl)) {
|
|
|
|
this._closeSummarizePanel();
|
|
|
|
} else {
|
|
|
|
if (tid !== this._value.table.get()) {
|
|
|
|
this._value.columns.set([]);
|
|
|
|
this._value.table.set(tid);
|
(core) When changing a table for a page widget, unset widget-linking to avoid invalid values.
Summary:
Previously, using "Change Widget" allowed one to change the underlying table,
but would keep the linking settings. This could allow invalid settings which
would sometimes lead to JS errors. These manifested in production as
"UserError: Query error: n is not a function".
- Unset linking settings in this case, to avoid invalid values.
- In case invalid values are encountered (e.g. saved previously), treat them as
unset, to avoid JS errors.
- If an error does occur, report it with a stack trace.
Also, for testing, added 'selectBy' option to gristUtils helpers for using page-widget-picker.
Test Plan: Added test cases for resetting linking, and for ignoring invalid link settings.
Reviewers: alexmojaki
Reviewed By: alexmojaki
Differential Revision: https://phab.getgrist.com/D2993
2021-08-24 03:28:07 +00:00
|
|
|
this._value.link.set(NoLink);
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
this._openSummarizePanel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private _toggleColumnId(cid: number) {
|
|
|
|
const ids = this._value.columns.get();
|
|
|
|
const newIds = ids.includes(cid) ? without(ids, cid) : [...ids, cid];
|
|
|
|
this._value.columns.set(newIds);
|
|
|
|
}
|
|
|
|
|
2023-12-12 09:58:20 +00:00
|
|
|
private _isTypeDisabled(type: IWidgetType, table: TableRef) {
|
2020-10-02 15:10:00 +00:00
|
|
|
if (table === null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return !getCompatibleTypes(table, this._options.isNewPage).includes(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-12-20 02:06:39 +00:00
|
|
|
function header(label: string, ...args: DomElementArg[]) {
|
|
|
|
return cssHeader(dom('h4', label), ...args, testId('heading'));
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const cssContainer = styled('div', `
|
2022-09-06 01:51:57 +00:00
|
|
|
--outline: 1px solid ${theme.widgetPickerBorder};
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
max-height: 386px;
|
2022-09-06 01:51:57 +00:00
|
|
|
box-shadow: 0 2px 20px 0 ${theme.widgetPickerShadow};
|
2020-10-02 15:10:00 +00:00
|
|
|
border-radius: 2px;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
user-select: none;
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.widgetPickerPrimaryBg};
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssPopupWrapper = styled('div', `
|
|
|
|
&:focus {
|
|
|
|
outline: none;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssBody = styled('div', `
|
|
|
|
display: flex;
|
|
|
|
min-height: 0;
|
|
|
|
`);
|
|
|
|
|
|
|
|
// todo: try replace min-width / max-width
|
|
|
|
const cssPanel = styled('div', `
|
|
|
|
width: 224px;
|
|
|
|
font-size: ${vars.mediumFontSize};
|
|
|
|
overflow: auto;
|
|
|
|
padding-bottom: 18px;
|
|
|
|
&:nth-of-type(2n) {
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.widgetPickerSecondaryBg};
|
2020-10-02 15:10:00 +00:00
|
|
|
outline: var(--outline);
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssHeader = styled('div', `
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.text};
|
2020-10-02 15:10:00 +00:00
|
|
|
margin: 24px 0 24px 24px;
|
|
|
|
font-size: ${vars.mediumFontSize};
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssEntry = styled('div', `
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.widgetPickerItemFg};
|
2020-10-02 15:10:00 +00:00
|
|
|
padding: 0 0 0 24px;
|
|
|
|
height: 32px;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: row;
|
|
|
|
flex: 1 1 0px;
|
|
|
|
align-items: center;
|
|
|
|
white-space: nowrap;
|
|
|
|
overflow: hidden;
|
2022-08-23 12:30:52 +00:00
|
|
|
cursor: pointer;
|
2020-10-02 15:10:00 +00:00
|
|
|
&-selected {
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.widgetPickerItemSelectedBg};
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
&-disabled {
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.widgetPickerItemDisabledBg};
|
2022-08-24 07:47:47 +00:00
|
|
|
cursor: default;
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
&-disabled&-selected {
|
|
|
|
background-color: inherit;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssIcon = styled(icon, `
|
|
|
|
margin-right: 8px;
|
|
|
|
flex-shrink: 0;
|
2022-09-06 01:51:57 +00:00
|
|
|
--icon-color: ${theme.widgetPickerIcon};
|
2020-10-02 15:10:00 +00:00
|
|
|
.${cssEntry.className}-disabled > & {
|
2022-09-06 01:51:57 +00:00
|
|
|
opacity: 0.25;
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssTypeIcon = styled(cssIcon, `
|
2022-09-06 01:51:57 +00:00
|
|
|
--icon-color: ${theme.widgetPickerPrimaryIcon};
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssLabel = styled('span', `
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
overflow: hidden;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssFieldLabel = styled(cssLabel, `
|
|
|
|
padding-right: 8px;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssEntryWrapper = styled('div', `
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssPivot = styled(cssEntry, `
|
|
|
|
width: 48px;
|
|
|
|
padding-left: 8px;
|
|
|
|
flex: 0 0 auto;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssBigIcon = styled(icon, `
|
|
|
|
width: 24px;
|
|
|
|
height: 24px;
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.widgetPickerSummaryIcon};
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssFooter = styled('div', `
|
|
|
|
display: flex;
|
|
|
|
border-top: var(--outline);
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssFooterContent = styled('div', `
|
|
|
|
flex-grow: 1;
|
|
|
|
height: 65px;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: row;
|
|
|
|
align-items: center;
|
|
|
|
padding: 0 24px 0 24px;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssSmallLabel = styled('span', `
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.text};
|
2020-10-02 15:10:00 +00:00
|
|
|
font-size: ${vars.xsmallFontSize};
|
|
|
|
margin-right: 8px;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssSelect = styled(select, `
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${theme.selectButtonFg};
|
|
|
|
background-color: ${theme.selectButtonBg};
|
2020-10-02 15:10:00 +00:00
|
|
|
flex: 1 0 160px;
|
2022-06-06 17:42:51 +00:00
|
|
|
width: 160px;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssSelectBy = styled('div', `
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
2020-10-02 15:10:00 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
// Returns a copy of array with its items sorted in the same order as they appear in other.
|
|
|
|
function sortedAs(array: number[], other: number[]) {
|
|
|
|
const order: {[id: number]: number} = {};
|
|
|
|
for (const [index, item] of other.entries()) {
|
|
|
|
order[item] = index;
|
|
|
|
}
|
|
|
|
return array.slice().sort((a, b) => nativeCompare(order[a], order[b]));
|
|
|
|
}
|