2022-10-28 16:11:08 +00:00
|
|
|
import {makeT} from 'app/client/lib/localization';
|
2021-11-05 10:25:05 +00:00
|
|
|
import {GristDoc} from 'app/client/components/GristDoc';
|
2022-10-14 10:07:19 +00:00
|
|
|
import {BEHAVIOR, ColumnRec} from 'app/client/models/entities/ColumnRec';
|
2021-03-17 03:45:44 +00:00
|
|
|
import {buildHighlightedCode, cssCodeBlock} from 'app/client/ui/CodeHighlight';
|
2022-10-19 23:06:05 +00:00
|
|
|
import {GristTooltips} from 'app/client/ui/GristTooltips';
|
2022-08-08 13:32:50 +00:00
|
|
|
import {cssBlockedCursor, cssLabel, cssRow} from 'app/client/ui/RightPanelStyles';
|
2023-03-01 15:21:19 +00:00
|
|
|
import {withInfoTooltip} from 'app/client/ui/tooltips';
|
2021-06-29 04:47:59 +00:00
|
|
|
import {buildFormulaTriggers} from 'app/client/ui/TriggerFormulas';
|
2021-11-05 10:25:05 +00:00
|
|
|
import {textButton} from 'app/client/ui2018/buttons';
|
2023-03-01 15:21:19 +00:00
|
|
|
import {testId, theme} from 'app/client/ui2018/cssVars';
|
2021-03-17 03:45:44 +00:00
|
|
|
import {textInput} from 'app/client/ui2018/editableLabel';
|
|
|
|
import {cssIconButton, icon} from 'app/client/ui2018/icons';
|
2022-03-22 13:41:11 +00:00
|
|
|
import {IconName} from 'app/client/ui2018/IconList';
|
2021-11-05 10:25:05 +00:00
|
|
|
import {selectMenu, selectOption, selectTitle} from 'app/client/ui2018/menus';
|
2022-03-22 13:41:11 +00:00
|
|
|
import {createFormulaErrorObs, cssError} from 'app/client/widgets/FormulaEditor';
|
2021-03-17 03:45:44 +00:00
|
|
|
import {sanitizeIdent} from 'app/common/gutil';
|
2023-04-11 05:00:28 +00:00
|
|
|
import {Theme} from 'app/common/ThemePrefs';
|
2023-08-28 09:16:17 +00:00
|
|
|
import {CursorPos} from 'app/plugin/GristAPI';
|
2022-01-25 09:38:21 +00:00
|
|
|
import {bundleChanges, Computed, dom, DomContents, DomElementArg, fromKo, MultiHolder,
|
2022-03-22 13:41:11 +00:00
|
|
|
Observable, styled} from 'grainjs';
|
2021-06-08 07:07:20 +00:00
|
|
|
import * as ko from 'knockout';
|
2021-03-17 03:45:44 +00:00
|
|
|
|
2022-10-28 16:11:08 +00:00
|
|
|
const t = makeT('FieldConfig');
|
|
|
|
|
2022-10-14 10:07:19 +00:00
|
|
|
export function buildNameConfig(
|
|
|
|
owner: MultiHolder,
|
|
|
|
origColumn: ColumnRec,
|
|
|
|
cursor: ko.Computed<CursorPos>,
|
|
|
|
disabled: ko.Computed<boolean> // Whether the name is editable (it's not editable for multiple selected columns).
|
|
|
|
) {
|
2021-03-17 03:45:44 +00:00
|
|
|
const untieColId = origColumn.untieColIdFromLabel;
|
|
|
|
|
|
|
|
const editedLabel = Observable.create(owner, '');
|
|
|
|
const editableColId = Computed.create(owner, editedLabel, (use, edited) =>
|
|
|
|
'$' + (edited ? sanitizeIdent(edited) : use(origColumn.colId)));
|
|
|
|
const saveColId = (val: string) => origColumn.colId.saveOnly(val.startsWith('$') ? val.slice(1) : val);
|
|
|
|
|
2022-01-25 09:38:21 +00:00
|
|
|
const isSummaryTable = Computed.create(owner, use => Boolean(use(use(origColumn.table).summarySourceTable)));
|
2021-06-08 07:07:20 +00:00
|
|
|
// We will listen to cursor position and force a blur event on
|
|
|
|
// the text input, which will trigger save before the column observable
|
|
|
|
// will change its value.
|
|
|
|
// Otherwise, blur will be invoked after column change and save handler will
|
|
|
|
// update a different column.
|
|
|
|
let editor: HTMLInputElement | undefined;
|
|
|
|
owner.autoDispose(
|
|
|
|
cursor.subscribe(() => {
|
|
|
|
editor?.blur();
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2022-10-14 10:07:19 +00:00
|
|
|
const toggleUntieColId = () => {
|
|
|
|
if (!origColumn.disableModify.peek() && !disabled.peek()) {
|
|
|
|
untieColId.saveOnly(!untieColId.peek()).catch(reportError);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-03-17 03:45:44 +00:00
|
|
|
return [
|
2022-12-06 13:57:29 +00:00
|
|
|
cssLabel(t("COLUMN LABEL AND ID")),
|
2021-03-17 03:45:44 +00:00
|
|
|
cssRow(
|
2022-01-25 09:38:21 +00:00
|
|
|
dom.cls(cssBlockedCursor.className, origColumn.disableModify),
|
2021-03-17 03:45:44 +00:00
|
|
|
cssColLabelBlock(
|
2022-09-06 01:51:57 +00:00
|
|
|
editor = cssInput(fromKo(origColumn.label),
|
2021-03-17 03:45:44 +00:00
|
|
|
async val => { await origColumn.label.saveOnly(val); editedLabel.set(''); },
|
|
|
|
dom.on('input', (ev, elem) => { if (!untieColId.peek()) { editedLabel.set(elem.value); } }),
|
2022-10-14 10:07:19 +00:00
|
|
|
dom.boolAttr('readonly', use => use(origColumn.disableModify) || use(disabled)),
|
2021-03-17 03:45:44 +00:00
|
|
|
testId('field-label'),
|
|
|
|
),
|
2022-09-06 01:51:57 +00:00
|
|
|
cssInput(editableColId,
|
2021-03-17 03:45:44 +00:00
|
|
|
saveColId,
|
2022-10-14 10:07:19 +00:00
|
|
|
dom.boolAttr('readonly',
|
|
|
|
use => use(disabled) || use(origColumn.disableModify) || !use(origColumn.untieColIdFromLabel)),
|
2021-03-17 03:45:44 +00:00
|
|
|
cssCodeBlock.cls(''),
|
|
|
|
{style: 'margin-top: 8px'},
|
|
|
|
testId('field-col-id'),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
cssColTieBlock(
|
|
|
|
cssColTieConnectors(),
|
|
|
|
cssToggleButton(icon('FieldReference'),
|
|
|
|
cssToggleButton.cls('-selected', (use) => !use(untieColId)),
|
2022-10-14 10:07:19 +00:00
|
|
|
dom.on('click', toggleUntieColId),
|
|
|
|
cssToggleButton.cls("-disabled", use => use(origColumn.disableModify) || use(disabled)),
|
2021-03-17 03:45:44 +00:00
|
|
|
testId('field-derive-id')
|
|
|
|
),
|
|
|
|
)
|
|
|
|
),
|
2022-01-25 09:38:21 +00:00
|
|
|
dom.maybe(isSummaryTable,
|
2022-12-06 13:57:29 +00:00
|
|
|
() => cssRow(t("Column options are limited in summary tables.")))
|
2021-03-17 03:45:44 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-07-13 14:00:56 +00:00
|
|
|
export interface BuildEditorOptions {
|
|
|
|
// Element to attach to.
|
|
|
|
refElem: Element;
|
|
|
|
// Should the detach button be shown?
|
|
|
|
canDetach: boolean;
|
|
|
|
// Simulate user typing on the cell - open editor with an initial value.
|
|
|
|
editValue?: string;
|
|
|
|
// Custom save handler.
|
|
|
|
onSave?: SaveHandler;
|
|
|
|
// Custom cancel handler.
|
|
|
|
onCancel?: () => void;
|
|
|
|
}
|
|
|
|
|
2021-11-30 08:59:04 +00:00
|
|
|
type SaveHandler = (column: ColumnRec, formula: string) => Promise<void>;
|
2023-07-13 14:00:56 +00:00
|
|
|
|
|
|
|
type BuildEditor = (options: BuildEditorOptions) => void;
|
2021-11-05 10:25:05 +00:00
|
|
|
|
2021-03-17 03:45:44 +00:00
|
|
|
export function buildFormulaConfig(
|
2021-04-20 21:57:45 +00:00
|
|
|
owner: MultiHolder, origColumn: ColumnRec, gristDoc: GristDoc, buildEditor: BuildEditor
|
2021-03-17 03:45:44 +00:00
|
|
|
) {
|
|
|
|
|
2022-10-14 10:07:19 +00:00
|
|
|
// If we can't modify anything about the column.
|
|
|
|
const disableModify = Computed.create(owner, use => use(origColumn.disableModify));
|
|
|
|
|
2021-11-05 10:25:05 +00:00
|
|
|
// Intermediate state - user wants to specify formula, but haven't done yet
|
|
|
|
const maybeFormula = Observable.create(owner, false);
|
|
|
|
|
|
|
|
// Intermediate state - user wants to specify formula, but haven't done yet
|
|
|
|
const maybeTrigger = Observable.create(owner, false);
|
|
|
|
|
2022-01-25 09:38:21 +00:00
|
|
|
// If this column belongs to a summary table.
|
|
|
|
const isSummaryTable = Computed.create(owner, use => Boolean(use(use(origColumn.table).summarySourceTable)));
|
|
|
|
|
2022-10-14 10:07:19 +00:00
|
|
|
// Column behavior. There are 3 types of behaviors:
|
2021-11-05 10:25:05 +00:00
|
|
|
// - empty: isFormula and formula == ''
|
|
|
|
// - formula: isFormula and formula != ''
|
|
|
|
// - data: not isFormula nd formula == ''
|
|
|
|
const behavior = Computed.create<BEHAVIOR|null>(owner, (use) => {
|
|
|
|
// When no id column is invalid, show nothing.
|
|
|
|
if (!use(origColumn.id)) { return null; }
|
|
|
|
// Column is a formula column, when it is a formula column with valid formula or will be a formula.
|
|
|
|
if (use(origColumn.isRealFormula) || use(maybeFormula)) { return "formula"; }
|
|
|
|
// If column is not empty, or empty but wants to be a trigger
|
|
|
|
if (use(maybeTrigger) || !use(origColumn.isEmpty)) { return "data"; }
|
|
|
|
return "empty";
|
|
|
|
});
|
|
|
|
|
|
|
|
// Reference to current editor, we will open it when user wants to specify a formula or trigger.
|
|
|
|
// And close it dispose it when user opens up behavior menu.
|
|
|
|
let formulaField: HTMLElement|null = null;
|
|
|
|
|
|
|
|
// Helper function to clear temporary state (will be called when column changes or formula editor closes)
|
|
|
|
const clearState = () => bundleChanges(() => {
|
2023-06-02 11:25:14 +00:00
|
|
|
// For a detached editor, we may have already been disposed when user switched page.
|
|
|
|
if (owner.isDisposed()) { return; }
|
2021-11-05 10:25:05 +00:00
|
|
|
maybeFormula.set(false);
|
|
|
|
maybeTrigger.set(false);
|
|
|
|
formulaField = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Clear state when column has changed
|
|
|
|
owner.autoDispose(origColumn.id.subscribe(clearState));
|
2021-11-30 08:59:04 +00:00
|
|
|
owner.autoDispose(origColumn.formula.subscribe(clearState));
|
|
|
|
owner.autoDispose(origColumn.isFormula.subscribe(clearState));
|
2021-11-05 10:25:05 +00:00
|
|
|
|
2022-10-14 10:07:19 +00:00
|
|
|
// User might have selected multiple columns, in that case all elements will be disabled, except the menu.
|
|
|
|
// If user has selected only empty or formula columns, we offer to reset all or to convert to data.
|
|
|
|
// If user has selected any data column, we offer only to reset all.
|
|
|
|
const viewSection = Computed.create(owner, use => {
|
|
|
|
return use(gristDoc.currentView)?.viewSection;
|
|
|
|
});
|
|
|
|
const isMultiSelect = Computed.create(owner, use => {
|
|
|
|
const vs = use(viewSection);
|
|
|
|
return !!vs && use(vs.selectedFields).length > 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
// If all columns are empty or have formulas.
|
|
|
|
const multiType = Computed.create(owner, use => {
|
|
|
|
if (!use(isMultiSelect)) { return false; }
|
|
|
|
const vs = use(viewSection);
|
|
|
|
if (!vs) { return false; }
|
|
|
|
return use(vs.columnsBehavior);
|
|
|
|
});
|
|
|
|
|
|
|
|
// If all columns are empty or have formulas.
|
|
|
|
const isFormulaLike = Computed.create(owner, use => {
|
|
|
|
if (!use(isMultiSelect)) { return false; }
|
|
|
|
const vs = use(viewSection);
|
|
|
|
if (!vs) { return false; }
|
|
|
|
return use(vs.columnsAllIsFormula);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Helper to get all selected columns refs.
|
|
|
|
const selectedColumns = () => viewSection.get()?.selectedFields.peek().map(f => f.column.peek()) || [];
|
|
|
|
const selectedColumnIds = () => selectedColumns().map(f => f.id.peek()) || [];
|
|
|
|
|
|
|
|
// Clear and reset all option for multiple selected columns.
|
|
|
|
const clearAndResetAll = () => selectOption(
|
|
|
|
() => Promise.all([
|
|
|
|
gristDoc.clearColumns(selectedColumnIds())
|
|
|
|
]),
|
|
|
|
'Clear and reset', 'CrossSmall'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Convert to data option for multiple selected columns.
|
|
|
|
const convertToDataAll = () => selectOption(
|
|
|
|
() => gristDoc.convertIsFormula(selectedColumnIds(), {toFormula: false, noRecalc: true}),
|
|
|
|
'Convert columns to data', 'Database',
|
|
|
|
dom.cls('disabled', isSummaryTable)
|
|
|
|
);
|
|
|
|
|
2021-11-05 10:25:05 +00:00
|
|
|
// Menu helper that will show normal menu with some default options
|
|
|
|
const menu = (label: DomContents, options: DomElementArg[]) =>
|
|
|
|
cssRow(
|
|
|
|
selectMenu(
|
|
|
|
label,
|
2022-10-14 10:07:19 +00:00
|
|
|
() => !isMultiSelect.get() ? options : [
|
|
|
|
isFormulaLike.get() ? convertToDataAll() : null,
|
|
|
|
clearAndResetAll(),
|
|
|
|
],
|
2021-11-05 10:25:05 +00:00
|
|
|
testId("field-behaviour"),
|
|
|
|
// HACK: Menu helper will add tabindex to this element, which will make
|
|
|
|
// this element focusable and will steal focus from clipboard. This in turn,
|
|
|
|
// will not dispose the formula editor when menu is clicked.
|
|
|
|
(el) => el.removeAttribute("tabindex"),
|
2022-10-14 10:07:19 +00:00
|
|
|
dom.cls(cssBlockedCursor.className, disableModify),
|
|
|
|
dom.cls("disabled", disableModify)),
|
2021-11-05 10:25:05 +00:00
|
|
|
);
|
|
|
|
|
2022-10-14 10:07:19 +00:00
|
|
|
|
|
|
|
// Behavior label
|
2021-11-05 10:25:05 +00:00
|
|
|
const behaviorName = Computed.create(owner, behavior, (use, type) => {
|
2022-10-14 10:07:19 +00:00
|
|
|
if (use(isMultiSelect)) {
|
|
|
|
const commonType = use(multiType);
|
2022-12-06 16:43:35 +00:00
|
|
|
if (commonType === 'formula') { return t('Formula Columns', {count: 2}); }
|
|
|
|
if (commonType === 'data') { return t('Data Columns', {count: 2}); }
|
2022-12-06 16:23:29 +00:00
|
|
|
if (commonType === 'mixed') { return t('Mixed Behavior'); }
|
2022-12-06 16:43:35 +00:00
|
|
|
return t('Empty Columns', {count: 2});
|
2022-10-14 10:07:19 +00:00
|
|
|
} else {
|
2022-12-06 16:43:35 +00:00
|
|
|
if (type === 'formula') { return t('Formula Columns', {count: 1}); }
|
|
|
|
if (type === 'data') { return t('Data Columns', {count: 1}); }
|
|
|
|
return t('Empty Columns', {count: 1});
|
2022-10-14 10:07:19 +00:00
|
|
|
}
|
2021-11-05 10:25:05 +00:00
|
|
|
});
|
|
|
|
const behaviorIcon = Computed.create<IconName>(owner, (use) => {
|
2022-12-06 16:43:35 +00:00
|
|
|
return use(behaviorName) === t('Data Columns', {count: 2}) ||
|
|
|
|
use(behaviorName) === t('Data Columns', {count: 1}) ? "Database" : "Script";
|
2021-11-05 10:25:05 +00:00
|
|
|
});
|
2022-10-14 10:07:19 +00:00
|
|
|
const behaviorLabel = () => selectTitle(behaviorName, behaviorIcon);
|
2021-11-05 10:25:05 +00:00
|
|
|
|
|
|
|
// Actions on select menu:
|
|
|
|
|
|
|
|
// Converts data column to formula column.
|
|
|
|
const convertDataColumnToFormulaOption = () => selectOption(
|
|
|
|
() => (maybeFormula.set(true), formulaField?.focus()),
|
2022-12-06 15:36:14 +00:00
|
|
|
t("Clear and make into formula"), 'Script');
|
2021-11-05 10:25:05 +00:00
|
|
|
|
|
|
|
// Converts to empty column and opens up the editor. (label is the same, but this is used when we have no formula)
|
|
|
|
const convertTriggerToFormulaOption = () => selectOption(
|
|
|
|
() => gristDoc.convertIsFormula([origColumn.id.peek()], {toFormula: true, noRecalc: true}),
|
2022-12-06 15:36:14 +00:00
|
|
|
t("Clear and make into formula"), 'Script');
|
2021-11-05 10:25:05 +00:00
|
|
|
|
|
|
|
// Convert column to data.
|
|
|
|
// This method is also available through a text button.
|
|
|
|
const convertToData = () => gristDoc.convertIsFormula([origColumn.id.peek()], {toFormula: false, noRecalc: true});
|
|
|
|
const convertToDataOption = () => selectOption(
|
|
|
|
convertToData,
|
2022-12-06 15:36:14 +00:00
|
|
|
t("Convert column to data"), 'Database',
|
2022-01-25 09:38:21 +00:00
|
|
|
dom.cls('disabled', isSummaryTable)
|
|
|
|
);
|
2021-11-05 10:25:05 +00:00
|
|
|
|
|
|
|
// Clears the column
|
|
|
|
const clearAndResetOption = () => selectOption(
|
|
|
|
() => gristDoc.clearColumns([origColumn.id.peek()]),
|
2022-12-06 13:57:29 +00:00
|
|
|
t("Clear and reset"), 'CrossSmall');
|
2021-11-05 10:25:05 +00:00
|
|
|
|
|
|
|
// Actions on text buttons:
|
|
|
|
|
|
|
|
// Tries to convert data column to a trigger column.
|
|
|
|
const convertDataColumnToTriggerColumn = () => {
|
|
|
|
maybeTrigger.set(true);
|
|
|
|
// Open the formula editor.
|
|
|
|
formulaField?.focus();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Converts formula column to trigger formula column.
|
|
|
|
const convertFormulaToTrigger = () =>
|
|
|
|
gristDoc.convertIsFormula([origColumn.id.peek()], {toFormula: false, noRecalc: false});
|
|
|
|
|
|
|
|
const setFormula = () => (maybeFormula.set(true), formulaField?.focus());
|
|
|
|
const setTrigger = () => (maybeTrigger.set(true), formulaField?.focus());
|
|
|
|
|
2021-11-30 08:59:04 +00:00
|
|
|
// Actions on save formula. Those actions are using column that comes from FormulaEditor.
|
|
|
|
// Formula editor scope is broader then RightPanel, it can be disposed after RightPanel is closed,
|
|
|
|
// and in some cases, when window is in background, it won't be disposed at all when panel is closed.
|
2021-11-05 10:25:05 +00:00
|
|
|
|
2021-11-30 08:59:04 +00:00
|
|
|
// Converts column to formula column.
|
|
|
|
const onSaveConvertToFormula = async (column: ColumnRec, formula: string) => {
|
2023-06-02 11:25:14 +00:00
|
|
|
// For a detached editor, we may have already been disposed when user switched page.
|
|
|
|
if (owner.isDisposed()) { return; }
|
2021-11-30 08:59:04 +00:00
|
|
|
// For non formula column, we will not convert it to formula column when expression is empty,
|
|
|
|
// as it means we were trying to convert data column to formula column, but changed our mind.
|
2021-11-05 10:25:05 +00:00
|
|
|
const notBlank = Boolean(formula);
|
2021-11-30 08:59:04 +00:00
|
|
|
// But when the column is a formula column, empty formula expression is acceptable (it will
|
|
|
|
// convert column to empty column).
|
|
|
|
const trueFormula = column.formula.peek();
|
|
|
|
if (notBlank || trueFormula) { await gristDoc.convertToFormula(column.id.peek(), formula); }
|
|
|
|
// Clear state only when owner was not disposed
|
|
|
|
if (!owner.isDisposed()) {
|
|
|
|
clearState();
|
|
|
|
}
|
2021-11-05 10:25:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Updates formula or convert column to trigger formula column if necessary.
|
2021-11-30 08:59:04 +00:00
|
|
|
const onSaveConvertToTrigger = async (column: ColumnRec, formula: string) => {
|
|
|
|
// If formula expression is not empty, and column was plain data column (without a formula)
|
|
|
|
if (formula && !column.hasTriggerFormula.peek()) {
|
|
|
|
// then convert column to a trigger formula column
|
|
|
|
await gristDoc.convertToTrigger(column.id.peek(), formula);
|
|
|
|
} else if (column.hasTriggerFormula.peek()) {
|
|
|
|
// else, if it was already a trigger formula column, just update formula.
|
|
|
|
await gristDoc.updateFormula(column.id.peek(), formula);
|
|
|
|
}
|
|
|
|
// Clear state only when owner was not disposed
|
|
|
|
if (!owner.isDisposed()) {
|
|
|
|
clearState();
|
2021-03-17 03:45:44 +00:00
|
|
|
}
|
2021-11-05 10:25:05 +00:00
|
|
|
};
|
|
|
|
|
2022-10-14 10:07:19 +00:00
|
|
|
// Should we disable all other action buttons and formula editor. For now
|
|
|
|
// we will disable them when multiple columns are selected, or any of the column selected
|
|
|
|
// can't be modified.
|
|
|
|
const disableOtherActions = Computed.create(owner, use => use(disableModify) || use(isMultiSelect));
|
|
|
|
|
2021-11-05 10:25:05 +00:00
|
|
|
const errorMessage = createFormulaErrorObs(owner, gristDoc, origColumn);
|
|
|
|
// Helper that will create different flavors for formula builder.
|
2023-07-13 14:00:56 +00:00
|
|
|
const formulaBuilder = (onSave: SaveHandler, canDetach?: boolean) => [
|
2021-11-05 10:25:05 +00:00
|
|
|
cssRow(formulaField = buildFormula(
|
|
|
|
origColumn,
|
|
|
|
buildEditor,
|
2023-04-11 05:00:28 +00:00
|
|
|
{
|
|
|
|
gristTheme: gristDoc.currentTheme,
|
|
|
|
disabled: disableOtherActions,
|
2023-07-13 14:00:56 +00:00
|
|
|
canDetach,
|
2023-04-11 05:00:28 +00:00
|
|
|
onSave,
|
|
|
|
onCancel: clearState,
|
|
|
|
})),
|
2021-11-05 10:25:05 +00:00
|
|
|
dom.maybe(errorMessage, errMsg => cssRow(cssError(errMsg), testId('field-error-count'))),
|
|
|
|
];
|
|
|
|
|
|
|
|
return dom.maybe(behavior, (type: BEHAVIOR) => [
|
2022-12-06 13:57:29 +00:00
|
|
|
cssLabel(t("COLUMN BEHAVIOR")),
|
2021-11-05 10:25:05 +00:00
|
|
|
...(type === "empty" ? [
|
2022-10-14 10:07:19 +00:00
|
|
|
menu(behaviorLabel(), [
|
2022-01-25 09:38:21 +00:00
|
|
|
convertToDataOption(),
|
2021-11-05 10:25:05 +00:00
|
|
|
]),
|
|
|
|
cssEmptySeparator(),
|
|
|
|
cssRow(textButton(
|
2022-12-06 13:57:29 +00:00
|
|
|
t("Set formula"),
|
2021-11-05 10:25:05 +00:00
|
|
|
dom.on("click", setFormula),
|
2022-10-14 10:07:19 +00:00
|
|
|
dom.prop("disabled", disableOtherActions),
|
2021-11-05 10:25:05 +00:00
|
|
|
testId("field-set-formula")
|
|
|
|
)),
|
2023-02-01 12:55:41 +00:00
|
|
|
cssRow(withInfoTooltip(
|
2022-10-19 23:06:05 +00:00
|
|
|
textButton(
|
2022-12-06 13:57:29 +00:00
|
|
|
t("Set trigger formula"),
|
2022-10-19 23:06:05 +00:00
|
|
|
dom.on("click", setTrigger),
|
|
|
|
dom.prop("disabled", use => use(isSummaryTable) || use(disableOtherActions)),
|
|
|
|
testId("field-set-trigger")
|
|
|
|
),
|
|
|
|
GristTooltips.setTriggerFormula(),
|
2021-11-05 10:25:05 +00:00
|
|
|
)),
|
|
|
|
cssRow(textButton(
|
2022-12-06 13:57:29 +00:00
|
|
|
t("Make into data column"),
|
2021-11-05 10:25:05 +00:00
|
|
|
dom.on("click", convertToData),
|
2022-10-14 10:07:19 +00:00
|
|
|
dom.prop("disabled", use => use(isSummaryTable) || use(disableOtherActions)),
|
2021-11-05 10:25:05 +00:00
|
|
|
testId("field-set-data")
|
|
|
|
))
|
|
|
|
] : type === "formula" ? [
|
2022-10-14 10:07:19 +00:00
|
|
|
menu(behaviorLabel(), [
|
2021-11-05 10:25:05 +00:00
|
|
|
convertToDataOption(),
|
|
|
|
clearAndResetOption(),
|
|
|
|
]),
|
|
|
|
formulaBuilder(onSaveConvertToFormula),
|
|
|
|
cssEmptySeparator(),
|
|
|
|
cssRow(textButton(
|
2022-12-06 15:36:14 +00:00
|
|
|
t("Convert to trigger formula"),
|
2021-11-05 10:25:05 +00:00
|
|
|
dom.on("click", convertFormulaToTrigger),
|
|
|
|
dom.hide(maybeFormula),
|
2022-10-14 10:07:19 +00:00
|
|
|
dom.prop("disabled", use => use(isSummaryTable) || use(disableOtherActions)),
|
2021-11-05 10:25:05 +00:00
|
|
|
testId("field-set-trigger")
|
|
|
|
))
|
|
|
|
] : /* type == 'data' */ [
|
2022-10-14 10:07:19 +00:00
|
|
|
menu(behaviorLabel(),
|
2021-11-05 10:25:05 +00:00
|
|
|
[
|
|
|
|
dom.domComputed(origColumn.hasTriggerFormula, (hasTrigger) => hasTrigger ?
|
|
|
|
// If we have trigger, we will convert it directly to a formula column
|
|
|
|
convertTriggerToFormulaOption() :
|
|
|
|
// else we will convert to empty column and open up the editor
|
|
|
|
convertDataColumnToFormulaOption()
|
|
|
|
),
|
|
|
|
clearAndResetOption(),
|
|
|
|
]
|
|
|
|
),
|
|
|
|
// If data column is or wants to be a trigger formula:
|
|
|
|
dom.maybe((use) => use(maybeTrigger) || use(origColumn.hasTriggerFormula), () => [
|
2022-12-06 13:57:29 +00:00
|
|
|
cssLabel(t("TRIGGER FORMULA")),
|
2023-07-13 14:00:56 +00:00
|
|
|
formulaBuilder(onSaveConvertToTrigger, false),
|
2022-10-14 10:07:19 +00:00
|
|
|
dom.create(buildFormulaTriggers, origColumn, {
|
|
|
|
disabled: disableOtherActions,
|
|
|
|
notTrigger: maybeTrigger,
|
|
|
|
})
|
2021-11-05 10:25:05 +00:00
|
|
|
]),
|
|
|
|
// Else offer a way to convert to trigger formula.
|
|
|
|
dom.maybe((use) => !(use(maybeTrigger) || use(origColumn.hasTriggerFormula)), () => [
|
|
|
|
cssEmptySeparator(),
|
2023-02-01 12:55:41 +00:00
|
|
|
cssRow(withInfoTooltip(
|
2022-10-19 23:06:05 +00:00
|
|
|
textButton(
|
2022-12-06 13:57:29 +00:00
|
|
|
t("Set trigger formula"),
|
2022-10-19 23:06:05 +00:00
|
|
|
dom.on("click", convertDataColumnToTriggerColumn),
|
|
|
|
dom.prop("disabled", disableOtherActions),
|
|
|
|
testId("field-set-trigger")
|
|
|
|
),
|
|
|
|
GristTooltips.setTriggerFormula()
|
|
|
|
)),
|
2021-11-05 10:25:05 +00:00
|
|
|
])
|
|
|
|
])
|
|
|
|
]);
|
2021-03-17 03:45:44 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 05:00:28 +00:00
|
|
|
interface BuildFormulaOptions {
|
|
|
|
gristTheme: Computed<Theme>;
|
|
|
|
disabled: Observable<boolean>;
|
2023-07-13 14:00:56 +00:00
|
|
|
canDetach?: boolean;
|
2023-04-11 05:00:28 +00:00
|
|
|
onSave?: SaveHandler;
|
|
|
|
onCancel?: () => void;
|
|
|
|
}
|
|
|
|
|
2021-11-05 10:25:05 +00:00
|
|
|
function buildFormula(
|
2023-04-11 05:00:28 +00:00
|
|
|
column: ColumnRec,
|
|
|
|
buildEditor: BuildEditor,
|
|
|
|
options: BuildFormulaOptions
|
|
|
|
) {
|
2023-07-13 14:00:56 +00:00
|
|
|
const {gristTheme, disabled, canDetach = true, onSave, onCancel} = options;
|
|
|
|
return cssFieldFormula(column.formula, {gristTheme, maxLines: 2},
|
2021-03-17 03:45:44 +00:00
|
|
|
dom.cls('formula_field_sidepane'),
|
2022-10-14 10:07:19 +00:00
|
|
|
cssFieldFormula.cls('-disabled', disabled),
|
2021-03-17 03:45:44 +00:00
|
|
|
cssFieldFormula.cls('-disabled-icon', use => !use(column.formula)),
|
|
|
|
dom.cls('disabled'),
|
|
|
|
{tabIndex: '-1'},
|
2021-11-05 10:25:05 +00:00
|
|
|
// Focus event use used by a user to edit an existing formula.
|
|
|
|
// It can also be triggered manually to open up the editor.
|
2023-07-13 14:00:56 +00:00
|
|
|
dom.on('focus', (_, refElem) => buildEditor({
|
|
|
|
refElem,
|
|
|
|
editValue: undefined,
|
|
|
|
canDetach,
|
|
|
|
onSave,
|
|
|
|
onCancel,
|
|
|
|
})),
|
2021-03-17 03:45:44 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-22 13:41:11 +00:00
|
|
|
export const cssFieldFormula = styled(buildHighlightedCode, `
|
2021-03-17 03:45:44 +00:00
|
|
|
flex: auto;
|
|
|
|
cursor: pointer;
|
|
|
|
margin-top: 4px;
|
|
|
|
padding-left: 24px;
|
2022-09-06 01:51:57 +00:00
|
|
|
--icon-color: ${theme.accentIcon};
|
2021-03-17 03:45:44 +00:00
|
|
|
|
|
|
|
&-disabled-icon.formula_field_sidepane::before {
|
2022-09-06 01:51:57 +00:00
|
|
|
--icon-color: ${theme.lightText};
|
2021-03-17 03:45:44 +00:00
|
|
|
}
|
|
|
|
&-disabled {
|
|
|
|
pointer-events: none;
|
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssToggleButton = styled(cssIconButton, `
|
|
|
|
margin-left: 8px;
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.rightPanelToggleButtonDisabledBg};
|
|
|
|
box-shadow: inset 0 0 0 1px ${theme.inputBorder};
|
2023-09-21 16:57:58 +00:00
|
|
|
cursor: pointer;
|
2021-03-17 03:45:44 +00:00
|
|
|
|
|
|
|
&-selected, &-selected:hover {
|
|
|
|
box-shadow: none;
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.rightPanelToggleButtonEnabledBg};
|
|
|
|
--icon-color: ${theme.rightPanelToggleButtonEnabledFg};
|
2021-03-17 03:45:44 +00:00
|
|
|
}
|
2022-01-18 11:48:57 +00:00
|
|
|
&-disabled, &-disabled:hover {
|
2023-09-21 16:57:58 +00:00
|
|
|
cursor: not-allowed;
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.rightPanelToggleButtonDisabledBg};
|
2023-09-21 16:57:58 +00:00
|
|
|
--icon-color: ${theme.iconDisabled};
|
2022-01-18 11:48:57 +00:00
|
|
|
}
|
2021-03-17 03:45:44 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssColLabelBlock = styled('div', `
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
2021-12-03 16:59:05 +00:00
|
|
|
flex: auto;
|
|
|
|
min-width: 80px;
|
2021-03-17 03:45:44 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssColTieBlock = styled('div', `
|
|
|
|
position: relative;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssColTieConnectors = styled('div', `
|
|
|
|
position: absolute;
|
2022-09-06 01:51:57 +00:00
|
|
|
border: 2px solid ${theme.inputBorder};
|
2021-03-17 03:45:44 +00:00
|
|
|
top: -9px;
|
|
|
|
bottom: -9px;
|
|
|
|
right: 11px;
|
|
|
|
left: 0px;
|
|
|
|
border-left: none;
|
|
|
|
z-index: -1;
|
|
|
|
`);
|
2022-08-08 13:32:50 +00:00
|
|
|
|
|
|
|
const cssEmptySeparator = styled('div', `
|
|
|
|
margin-top: 16px;
|
|
|
|
`);
|
2022-09-06 01:51:57 +00:00
|
|
|
|
|
|
|
const cssInput = styled(textInput, `
|
|
|
|
color: ${theme.inputFg};
|
|
|
|
background-color: ${theme.mainPanelBg};
|
|
|
|
border: 1px solid ${theme.inputBorder};
|
|
|
|
|
|
|
|
&::placeholder {
|
|
|
|
color: ${theme.inputPlaceholderFg};
|
|
|
|
}
|
|
|
|
|
2022-09-16 12:44:39 +00:00
|
|
|
&[readonly] {
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.inputDisabledBg};
|
|
|
|
color: ${theme.inputDisabledFg};
|
|
|
|
}
|
|
|
|
`);
|