2020-10-02 15:10:00 +00:00
|
|
|
import { ColumnTransform } from 'app/client/components/ColumnTransform';
|
|
|
|
import { Cursor } from 'app/client/components/Cursor';
|
|
|
|
import { FormulaTransform } from 'app/client/components/FormulaTransform';
|
|
|
|
import { GristDoc } from 'app/client/components/GristDoc';
|
|
|
|
import { addColTypeSuffix } from 'app/client/components/TypeConversion';
|
|
|
|
import { TypeTransform } from 'app/client/components/TypeTransform';
|
|
|
|
import * as dom from 'app/client/lib/dom';
|
|
|
|
import { KoArray } from 'app/client/lib/koArray';
|
|
|
|
import * as kd from 'app/client/lib/koDom';
|
|
|
|
import * as kf from 'app/client/lib/koForm';
|
|
|
|
import * as koUtil from 'app/client/lib/koUtil';
|
|
|
|
import { reportError } from 'app/client/models/AppModel';
|
|
|
|
import { DataRowModel } from 'app/client/models/DataRowModel';
|
|
|
|
import { ColumnRec, DocModel, ViewFieldRec } from 'app/client/models/DocModel';
|
|
|
|
import { SaveableObjObservable, setSaveValue } from 'app/client/models/modelUtil';
|
|
|
|
import { FieldSettingsMenu } from 'app/client/ui/FieldMenus';
|
2022-01-25 09:38:21 +00:00
|
|
|
import { cssBlockedCursor, cssLabel, cssRow } from 'app/client/ui/RightPanel';
|
2020-10-02 15:10:00 +00:00
|
|
|
import { buttonSelect } from 'app/client/ui2018/buttonSelect';
|
|
|
|
import { IOptionFull, menu, select } from 'app/client/ui2018/menus';
|
|
|
|
import { DiffBox } from 'app/client/widgets/DiffBox';
|
|
|
|
import { buildErrorDom } from 'app/client/widgets/ErrorDom';
|
2021-11-09 20:03:12 +00:00
|
|
|
import { FieldEditor, openFormulaEditor, saveWithoutEditor, setupEditorCleanup } from 'app/client/widgets/FieldEditor';
|
2020-10-02 15:10:00 +00:00
|
|
|
import { NewAbstractWidget } from 'app/client/widgets/NewAbstractWidget';
|
2021-06-17 16:41:07 +00:00
|
|
|
import { NewBaseEditor } from "app/client/widgets/NewBaseEditor";
|
2020-10-02 15:10:00 +00:00
|
|
|
import * as UserType from 'app/client/widgets/UserType';
|
|
|
|
import * as UserTypeImpl from 'app/client/widgets/UserTypeImpl';
|
|
|
|
import * as gristTypes from 'app/common/gristTypes';
|
2021-07-23 15:29:35 +00:00
|
|
|
import { getReferencedTableId, isFullReferencingType } from 'app/common/gristTypes';
|
2020-10-02 15:10:00 +00:00
|
|
|
import { CellValue } from 'app/plugin/GristData';
|
2021-05-17 14:05:49 +00:00
|
|
|
import { Computed, Disposable, fromKo, dom as grainjsDom,
|
2021-08-12 18:06:40 +00:00
|
|
|
Holder, IDisposable, makeTestId, styled, toKo } from 'grainjs';
|
2020-10-02 15:10:00 +00:00
|
|
|
import * as ko from 'knockout';
|
|
|
|
import * as _ from 'underscore';
|
|
|
|
|
|
|
|
const testId = makeTestId('test-fbuilder-');
|
|
|
|
|
|
|
|
// Creates a FieldBuilder object for each field in viewFields
|
|
|
|
export function createAllFieldWidgets(gristDoc: GristDoc, viewFields: ko.Computed<KoArray<ViewFieldRec>>,
|
2021-11-19 05:35:01 +00:00
|
|
|
cursor: Cursor, options: { isPreview?: boolean } = {}) {
|
2020-10-02 15:10:00 +00:00
|
|
|
// TODO: Handle disposal from the map when fields are removed.
|
|
|
|
return viewFields().map(function(field) {
|
2021-11-19 05:35:01 +00:00
|
|
|
return new FieldBuilder(gristDoc, field, cursor, options);
|
2020-10-02 15:10:00 +00:00
|
|
|
}).setAutoDisposeValues();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the appropriate object from UserType.typeDefs, defaulting to Text for unknown types.
|
|
|
|
*/
|
|
|
|
function getTypeDefinition(type: string | false) {
|
|
|
|
if (!type) { return UserType.typeDefs.Text; }
|
|
|
|
return UserType.typeDefs[type] || UserType.typeDefs.Text;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an instance of FieldBuilder. Used to create all column configuration DOMs, cell DOMs,
|
|
|
|
* and cell editor DOMs for all Grist Types.
|
|
|
|
* @param {Object} field - The field for which the DOMs are to be created.
|
|
|
|
* @param {Object} cursor - The cursor object, used to get the cursor position while saving values.
|
|
|
|
*/
|
|
|
|
export class FieldBuilder extends Disposable {
|
|
|
|
public columnTransform: ColumnTransform | null;
|
|
|
|
public readonly origColumn: ColumnRec;
|
|
|
|
public readonly options: SaveableObjObservable<any>;
|
|
|
|
public readonly widget: ko.PureComputed<any>;
|
|
|
|
public readonly isCallPending: ko.Observable<boolean>;
|
|
|
|
public readonly widgetImpl: ko.Computed<NewAbstractWidget>;
|
|
|
|
public readonly diffImpl: NewAbstractWidget;
|
|
|
|
|
2021-05-23 17:43:11 +00:00
|
|
|
private readonly _availableTypes: Computed<Array<IOptionFull<string>>>;
|
|
|
|
private readonly _readOnlyPureType: ko.PureComputed<string>;
|
|
|
|
private readonly _isRightType: ko.PureComputed<(value: CellValue, options?: any) => boolean>;
|
|
|
|
private readonly _refTableId: ko.Computed<string | null>;
|
|
|
|
private readonly _isRef: ko.Computed<boolean>;
|
2020-10-02 15:10:00 +00:00
|
|
|
private readonly _rowMap: Map<DataRowModel, Element>;
|
2021-05-23 17:43:11 +00:00
|
|
|
private readonly _isTransformingFormula: ko.Computed<boolean>;
|
|
|
|
private readonly _isTransformingType: ko.Computed<boolean>;
|
2020-10-02 15:10:00 +00:00
|
|
|
private readonly _fieldEditorHolder: Holder<IDisposable>;
|
2021-05-23 17:43:11 +00:00
|
|
|
private readonly _widgetCons: ko.Computed<{create: (...args: any[]) => NewAbstractWidget}>;
|
|
|
|
private readonly _docModel: DocModel;
|
2021-06-17 16:41:07 +00:00
|
|
|
private readonly _readonly: Computed<boolean>;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2021-04-26 21:54:09 +00:00
|
|
|
public constructor(public readonly gristDoc: GristDoc, public readonly field: ViewFieldRec,
|
2021-11-19 05:35:01 +00:00
|
|
|
private _cursor: Cursor, private _options: { isPreview?: boolean } = {}) {
|
2020-10-02 15:10:00 +00:00
|
|
|
super();
|
|
|
|
|
2021-05-23 17:43:11 +00:00
|
|
|
this._docModel = gristDoc.docModel;
|
2020-10-02 15:10:00 +00:00
|
|
|
this.origColumn = field.column();
|
|
|
|
this.options = field.widgetOptionsJson;
|
|
|
|
|
2021-05-23 17:43:11 +00:00
|
|
|
this._readOnlyPureType = ko.pureComputed(() => this.field.column().pureType());
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2021-11-19 05:35:01 +00:00
|
|
|
this._readonly = Computed.create(this, (use) =>
|
|
|
|
use(gristDoc.isReadonly) || use(field.disableEditData) || Boolean(this._options.isPreview));
|
2021-06-17 16:41:07 +00:00
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// Observable with a list of available types.
|
2021-05-23 17:43:11 +00:00
|
|
|
this._availableTypes = Computed.create(this, (use) => {
|
2020-10-02 15:10:00 +00:00
|
|
|
const isFormula = use(this.origColumn.isFormula);
|
|
|
|
const types: Array<IOptionFull<string>> = [];
|
|
|
|
_.each(UserType.typeDefs, (def: any, key: string|number) => {
|
|
|
|
const o: IOptionFull<string> = {
|
|
|
|
value: key as string,
|
|
|
|
label: def.label,
|
|
|
|
icon: def.icon
|
|
|
|
};
|
|
|
|
if (key === 'Any') {
|
|
|
|
// User is unable to select the Any type in non-formula columns.
|
|
|
|
o.disabled = !isFormula;
|
|
|
|
}
|
|
|
|
types.push(o);
|
|
|
|
});
|
|
|
|
return types;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Observable which evaluates to a *function* that decides if a value is valid.
|
2021-05-23 17:43:11 +00:00
|
|
|
this._isRightType = ko.pureComputed(function() {
|
|
|
|
return gristTypes.isRightType(this._readOnlyPureType()) || _.constant(false);
|
2020-10-02 15:10:00 +00:00
|
|
|
}, this);
|
|
|
|
|
2021-07-23 15:29:35 +00:00
|
|
|
// Returns a boolean indicating whether the column is type Reference or ReferenceList.
|
2021-05-23 17:43:11 +00:00
|
|
|
this._isRef = this.autoDispose(ko.computed(() => {
|
2021-07-23 15:29:35 +00:00
|
|
|
return isFullReferencingType(this.field.column().type());
|
2020-10-02 15:10:00 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
// Gives the table ID to which the reference points.
|
2021-05-23 17:43:11 +00:00
|
|
|
this._refTableId = this.autoDispose(ko.computed({
|
2021-07-23 15:29:35 +00:00
|
|
|
read: () => getReferencedTableId(this.field.column().type()),
|
|
|
|
write: val => {
|
|
|
|
const type = this.field.column().type();
|
|
|
|
if (type.startsWith('Ref:')) {
|
|
|
|
void this._setType(`Ref:${val}`);
|
|
|
|
} else {
|
|
|
|
void this._setType(`RefList:${val}`);
|
|
|
|
}
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
this.widget = ko.pureComputed({
|
|
|
|
owner: this,
|
|
|
|
read() { return this.options().widget; },
|
|
|
|
write(widget) {
|
|
|
|
// Reset the entire JSON, so that all options revert to their defaults.
|
|
|
|
|
2021-02-09 10:12:53 +00:00
|
|
|
const previous = this.options.peek();
|
2020-10-02 15:10:00 +00:00
|
|
|
this.options.setAndSave({
|
|
|
|
widget,
|
2021-02-09 10:12:53 +00:00
|
|
|
// Persists color settings across widgets (note: we cannot use `field.fillColor` to get the
|
|
|
|
// current value because it returns a default value for `undefined`. Same for `field.textColor`.
|
|
|
|
fillColor: previous.fillColor,
|
|
|
|
textColor: previous.textColor,
|
2020-10-02 15:10:00 +00:00
|
|
|
}).catch(reportError);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Whether there is a pending call that transforms column.
|
|
|
|
this.isCallPending = ko.observable(false);
|
|
|
|
|
|
|
|
// Maintains an instance of the transform object if the field is currently being transformed,
|
|
|
|
// and null if not. Gets disposed along with the transform menu dom.
|
|
|
|
this.columnTransform = null;
|
|
|
|
|
|
|
|
// Returns a boolean indicating whether a formula transform is in progress.
|
2021-05-23 17:43:11 +00:00
|
|
|
this._isTransformingFormula = this.autoDispose(ko.computed(() => {
|
2020-10-02 15:10:00 +00:00
|
|
|
return this.field.column().isTransforming() && this.columnTransform instanceof FormulaTransform;
|
|
|
|
}));
|
|
|
|
// Returns a boolean indicating whether a type transform is in progress.
|
2021-05-23 17:43:11 +00:00
|
|
|
this._isTransformingType = this.autoDispose(ko.computed(() => {
|
2020-10-02 15:10:00 +00:00
|
|
|
return (this.field.column().isTransforming() || this.isCallPending()) &&
|
|
|
|
(this.columnTransform instanceof TypeTransform);
|
|
|
|
}));
|
|
|
|
|
|
|
|
// This holds a single FieldEditor. When a new FieldEditor is created (on edit), it replaces the
|
|
|
|
// previous one if any.
|
|
|
|
this._fieldEditorHolder = Holder.create(this);
|
|
|
|
|
|
|
|
// Map from rowModel to cell dom for the field to which this fieldBuilder applies.
|
|
|
|
this._rowMap = new Map();
|
|
|
|
|
|
|
|
// Returns the constructor for the widget, and only notifies subscribers on changes.
|
2021-05-23 17:43:11 +00:00
|
|
|
this._widgetCons = this.autoDispose(koUtil.withKoUtils(ko.computed(function() {
|
2020-10-02 15:10:00 +00:00
|
|
|
return UserTypeImpl.getWidgetConstructor(this.options().widget,
|
2021-05-23 17:43:11 +00:00
|
|
|
this._readOnlyPureType());
|
2020-10-02 15:10:00 +00:00
|
|
|
}, this)).onlyNotifyUnequal());
|
|
|
|
|
|
|
|
// Computed builder for the widget.
|
|
|
|
this.widgetImpl = this.autoDispose(koUtil.computedBuilder(() => {
|
2021-05-23 17:43:11 +00:00
|
|
|
const cons = this._widgetCons();
|
2020-10-02 15:10:00 +00:00
|
|
|
// Must subscribe to `colId` so that field.colId is rechecked on transform.
|
|
|
|
return cons.create.bind(cons, this.field, this.field.colId());
|
|
|
|
}, this).extend({ deferred: true }));
|
|
|
|
|
|
|
|
this.diffImpl = this.autoDispose(DiffBox.create(this.field));
|
|
|
|
}
|
|
|
|
|
|
|
|
public buildSelectWidgetDom() {
|
2021-05-23 17:43:11 +00:00
|
|
|
return grainjsDom.maybe((use) => !use(this._isTransformingType) && use(this._readOnlyPureType), type => {
|
2020-10-02 15:10:00 +00:00
|
|
|
const typeWidgets = getTypeDefinition(type).widgets;
|
|
|
|
const widgetOptions = Object.keys(typeWidgets).map(label => ({
|
|
|
|
label,
|
|
|
|
value: label,
|
|
|
|
icon: typeWidgets[label].icon
|
|
|
|
}));
|
|
|
|
return widgetOptions.length <= 1 ? null : [
|
|
|
|
cssLabel('CELL FORMAT'),
|
|
|
|
cssRow(
|
|
|
|
widgetOptions.length <= 2 ? buttonSelect(fromKo(this.widget), widgetOptions) :
|
|
|
|
select(fromKo(this.widget), widgetOptions),
|
|
|
|
testId('widget-select')
|
|
|
|
)
|
|
|
|
];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the type change dom.
|
|
|
|
*/
|
|
|
|
public buildSelectTypeDom() {
|
2021-05-23 17:43:11 +00:00
|
|
|
const selectType = Computed.create(null, (use) => use(fromKo(this._readOnlyPureType)));
|
|
|
|
selectType.onWrite(newType => newType === this._readOnlyPureType.peek() || this._setType(newType));
|
2020-10-02 15:10:00 +00:00
|
|
|
const onDispose = () => (this.isDisposed() || selectType.set(this.field.column().pureType()));
|
|
|
|
|
|
|
|
return [
|
|
|
|
cssRow(
|
|
|
|
grainjsDom.autoDispose(selectType),
|
2021-05-23 17:43:11 +00:00
|
|
|
select(selectType, this._availableTypes, {
|
|
|
|
disabled: (use) => use(this._isTransformingFormula) || use(this.origColumn.disableModifyBase) ||
|
2021-08-12 18:06:40 +00:00
|
|
|
use(this.isCallPending),
|
|
|
|
menuCssClass: cssTypeSelectMenu.className,
|
2020-10-02 15:10:00 +00:00
|
|
|
}),
|
2021-07-19 08:49:44 +00:00
|
|
|
testId('type-select'),
|
|
|
|
grainjsDom.cls('tour-type-selector'),
|
2022-01-25 09:38:21 +00:00
|
|
|
grainjsDom.cls(cssBlockedCursor.className, this.origColumn.disableModifyBase)
|
2020-10-02 15:10:00 +00:00
|
|
|
),
|
2021-05-23 17:43:11 +00:00
|
|
|
grainjsDom.maybe((use) => use(this._isRef) && !use(this._isTransformingType), () => this._buildRefTableSelect()),
|
|
|
|
grainjsDom.maybe(this._isTransformingType, () => {
|
2020-10-02 15:10:00 +00:00
|
|
|
// Editor dom must be built before preparing transform.
|
|
|
|
return dom('div.type_transform_prompt',
|
|
|
|
kf.prompt(
|
|
|
|
dom('div',
|
2021-05-23 17:43:11 +00:00
|
|
|
grainjsDom.maybe(this._isRef, () => this._buildRefTableSelect()),
|
2020-10-02 15:10:00 +00:00
|
|
|
grainjsDom.maybe((use) => use(this.field.column().isTransforming),
|
|
|
|
() => this.columnTransform!.buildDom())
|
2021-05-23 17:43:11 +00:00
|
|
|
)
|
2020-10-02 15:10:00 +00:00
|
|
|
),
|
|
|
|
grainjsDom.onDispose(onDispose)
|
2021-05-23 17:43:11 +00:00
|
|
|
);
|
2020-10-02 15:10:00 +00:00
|
|
|
})
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function to set the column type to newType.
|
|
|
|
public _setType(newType: string): Promise<unknown>|undefined {
|
|
|
|
if (this.origColumn.isFormula()) {
|
|
|
|
// Do not type transform a new/empty column or a formula column. Just make a best guess for
|
|
|
|
// the full type, and set it.
|
|
|
|
const column = this.field.column();
|
2021-05-23 17:43:11 +00:00
|
|
|
column.type.setAndSave(addColTypeSuffix(newType, column, this._docModel)).catch(reportError);
|
2020-10-02 15:10:00 +00:00
|
|
|
} else if (!this.columnTransform) {
|
|
|
|
this.columnTransform = TypeTransform.create(null, this.gristDoc, this);
|
|
|
|
return this.columnTransform.prepare(newType);
|
|
|
|
} else {
|
|
|
|
if (this.columnTransform instanceof TypeTransform) {
|
|
|
|
return this.columnTransform.setType(newType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Builds the reference type table selector. Built when the column is type reference.
|
|
|
|
public _buildRefTableSelect() {
|
|
|
|
const allTables = Computed.create(null, (use) =>
|
2021-05-23 17:43:11 +00:00
|
|
|
use(this._docModel.allTableIds.getObservable()).map(tableId => ({
|
2020-10-02 15:10:00 +00:00
|
|
|
value: tableId,
|
|
|
|
label: tableId,
|
|
|
|
icon: 'FieldTable' as const
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
return [
|
|
|
|
cssLabel('DATA FROM TABLE'),
|
|
|
|
cssRow(
|
|
|
|
dom.autoDispose(allTables),
|
2021-06-28 19:05:37 +00:00
|
|
|
select(fromKo(this._refTableId), allTables, {
|
|
|
|
// Disallow changing the destination table when the column should not be modified
|
|
|
|
// (specifically when it's a group-by column of a summary table).
|
|
|
|
disabled: this.origColumn.disableModifyBase,
|
|
|
|
}),
|
2020-10-02 15:10:00 +00:00
|
|
|
testId('ref-table-select')
|
|
|
|
)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the formula transform dom
|
|
|
|
*/
|
|
|
|
public buildTransformDom() {
|
|
|
|
const transformButton = ko.computed({
|
|
|
|
read: () => this.field.column().isTransforming(),
|
|
|
|
write: val => {
|
|
|
|
if (val) {
|
|
|
|
this.columnTransform = FormulaTransform.create(null, this.gristDoc, this);
|
|
|
|
return this.columnTransform.prepare();
|
|
|
|
} else {
|
|
|
|
return this.columnTransform && this.columnTransform.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return dom('div',
|
|
|
|
dom.autoDispose(transformButton),
|
|
|
|
dom.onDispose(() => {
|
|
|
|
// When losing focus, if there's an active column transform, finalize it.
|
|
|
|
if (this.columnTransform) {
|
2020-11-09 23:40:43 +00:00
|
|
|
this.columnTransform.finalize().catch(reportError);
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
kf.row(
|
|
|
|
15, kf.label('Apply Formula to Data'),
|
|
|
|
3, kf.buttonGroup(
|
|
|
|
kf.checkButton(transformButton,
|
|
|
|
dom('span.glyphicon.glyphicon-flash'),
|
|
|
|
dom.testId("FieldBuilder_editTransform"),
|
2021-05-23 17:43:11 +00:00
|
|
|
kd.toggleClass('disabled', () => this._isTransformingType() || this.origColumn.isFormula() ||
|
2021-03-17 03:45:44 +00:00
|
|
|
this.origColumn.disableModifyBase())
|
2020-10-02 15:10:00 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
2021-05-23 17:43:11 +00:00
|
|
|
kd.maybe(this._isTransformingFormula, () => {
|
2020-10-02 15:10:00 +00:00
|
|
|
return this.columnTransform!.buildDom();
|
|
|
|
})
|
2021-05-23 17:43:11 +00:00
|
|
|
);
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the FieldBuilder Options Config DOM. Calls the buildConfigDom function of its widgetImpl.
|
|
|
|
*/
|
|
|
|
public buildConfigDom() {
|
|
|
|
// NOTE: adding a grainjsDom .maybe here causes the disposable order of the widgetImpl and
|
|
|
|
// the dom created by the widgetImpl to get out of sync.
|
|
|
|
return dom('div',
|
2021-05-23 17:43:11 +00:00
|
|
|
kd.maybe(() => !this._isTransformingType() && this.widgetImpl(), (widget: NewAbstractWidget) =>
|
2020-10-02 15:10:00 +00:00
|
|
|
dom('div',
|
|
|
|
widget.buildConfigDom(),
|
|
|
|
widget.buildColorConfigDom(),
|
|
|
|
|
|
|
|
// If there is more than one field for this column (i.e. present in multiple views).
|
|
|
|
kd.maybe(() => this.origColumn.viewFields().all().length > 1, () =>
|
|
|
|
dom('div.fieldbuilder_settings',
|
|
|
|
kf.row(
|
|
|
|
kd.toggleClass('fieldbuilder_settings_header', true),
|
|
|
|
kf.label(
|
|
|
|
dom('div.fieldbuilder_settings_button',
|
|
|
|
dom.testId('FieldBuilder_settings'),
|
|
|
|
kd.text(() => this.field.useColOptions() ? 'Common' : 'Separate'), ' ▾',
|
2022-02-07 14:02:26 +00:00
|
|
|
menu(() => FieldSettingsMenu(
|
|
|
|
this.field.useColOptions(),
|
|
|
|
this.field.viewSection().isRaw(),
|
|
|
|
{
|
|
|
|
useSeparate: () => this.fieldSettingsUseSeparate(),
|
|
|
|
saveAsCommon: () => this.fieldSettingsSaveAsCommon(),
|
|
|
|
revertToCommon: () => this.fieldSettingsRevertToCommon(),
|
|
|
|
},
|
|
|
|
)),
|
2020-10-02 15:10:00 +00:00
|
|
|
),
|
|
|
|
'Field in ',
|
|
|
|
kd.text(() => this.origColumn.viewFields().all().length),
|
|
|
|
' views'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public fieldSettingsUseSeparate() {
|
|
|
|
return this.gristDoc.docData.bundleActions(
|
|
|
|
`Use separate field settings for ${this.origColumn.colId()}`, () => {
|
|
|
|
return Promise.all([
|
|
|
|
setSaveValue(this.field.widgetOptions, this.field.column().widgetOptions()),
|
|
|
|
setSaveValue(this.field.visibleCol, this.field.column().visibleCol()),
|
|
|
|
this.field.saveDisplayFormula(this.field.column()._displayColModel().formula() || '')
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public fieldSettingsSaveAsCommon() {
|
|
|
|
return this.gristDoc.docData.bundleActions(
|
|
|
|
`Save field settings for ${this.origColumn.colId()} as common`, () => {
|
|
|
|
return Promise.all([
|
|
|
|
setSaveValue(this.field.column().widgetOptions, this.field.widgetOptions()),
|
|
|
|
setSaveValue(this.field.column().visibleCol, this.field.visibleCol()),
|
|
|
|
this.field.column().saveDisplayFormula(this.field._displayColModel().formula() || ''),
|
|
|
|
setSaveValue(this.field.widgetOptions, ''),
|
|
|
|
setSaveValue(this.field.visibleCol, 0),
|
|
|
|
this.field.saveDisplayFormula('')
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public fieldSettingsRevertToCommon() {
|
|
|
|
return this.gristDoc.docData.bundleActions(
|
|
|
|
`Revert field settings for ${this.origColumn.colId()} to common`, () => {
|
|
|
|
return Promise.all([
|
|
|
|
setSaveValue(this.field.widgetOptions, ''),
|
|
|
|
setSaveValue(this.field.visibleCol, 0),
|
|
|
|
this.field.saveDisplayFormula('')
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the cell and editor DOM for the chosen UserType. Calls the buildDom and
|
|
|
|
* buildEditorDom functions of its widgetImpl.
|
|
|
|
*/
|
|
|
|
public buildDomWithCursor(row: DataRowModel, isActive: boolean, isSelected: boolean) {
|
|
|
|
const widgetObs = koUtil.withKoUtils(ko.computed(function() {
|
|
|
|
// TODO: Accessing row values like this doesn't always work (row and field might not be updated
|
|
|
|
// simultaneously).
|
|
|
|
if (this.isDisposed()) { return null; } // Work around JS errors during field removal.
|
|
|
|
const value = row.cells[this.field.colId()];
|
|
|
|
const cell = value && value();
|
2021-05-23 17:43:11 +00:00
|
|
|
if (value && this._isRightType()(cell, this.options) || row._isAddRow.peek()) {
|
2020-10-02 15:10:00 +00:00
|
|
|
return this.widgetImpl();
|
|
|
|
} else if (gristTypes.isVersions(cell)) {
|
|
|
|
return this.diffImpl;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}, this).extend({ deferred: true })).onlyNotifyUnequal();
|
|
|
|
|
2021-06-17 16:41:07 +00:00
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
return (elem: Element) => {
|
|
|
|
this._rowMap.set(row, elem);
|
|
|
|
dom(elem,
|
|
|
|
dom.autoDispose(widgetObs),
|
2021-11-19 05:35:01 +00:00
|
|
|
this._options.isPreview ? null : kd.cssClass(this.field.formulaCssClass),
|
2021-06-17 16:41:07 +00:00
|
|
|
kd.toggleClass("readonly", toKo(ko, this._readonly)),
|
2020-10-02 15:10:00 +00:00
|
|
|
kd.maybe(isSelected, () => dom('div.selected_cursor',
|
|
|
|
kd.toggleClass('active_cursor', isActive)
|
|
|
|
)),
|
|
|
|
kd.scope(widgetObs, (widget: NewAbstractWidget) => {
|
|
|
|
if (this.isDisposed()) { return null; } // Work around JS errors during field removal.
|
|
|
|
const cellDom = widget ? widget.buildDom(row) : buildErrorDom(row, this.field);
|
2021-02-09 10:12:53 +00:00
|
|
|
return dom(cellDom, kd.toggleClass('has_cursor', isActive),
|
2021-03-02 12:27:08 +00:00
|
|
|
kd.style('--grist-cell-color', () => this.field.textColor() || ''),
|
2021-02-09 10:12:53 +00:00
|
|
|
kd.style('--grist-cell-background-color', this.field.fillColor));
|
2020-10-02 15:10:00 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public buildEditorDom(editRow: DataRowModel, mainRowModel: DataRowModel, options: {
|
2021-05-17 14:05:49 +00:00
|
|
|
init?: string,
|
|
|
|
state?: any
|
2020-10-02 15:10:00 +00:00
|
|
|
}) {
|
|
|
|
// If the user attempts to edit a value during transform, finalize (i.e. cancel or execute)
|
|
|
|
// the transform.
|
|
|
|
if (this.columnTransform) {
|
2020-11-09 23:40:43 +00:00
|
|
|
this.columnTransform.finalize().catch(reportError);
|
2020-10-02 15:10:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-17 16:41:07 +00:00
|
|
|
// If this is censored value, don't open up the editor, unless it is a formula field.
|
|
|
|
const cell = editRow.cells[this.field.colId()];
|
|
|
|
const value = cell && cell();
|
|
|
|
if (gristTypes.isCensored(value) && !this.origColumn.isFormula.peek()) {
|
|
|
|
this._fieldEditorHolder.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const editorCtor: typeof NewBaseEditor =
|
|
|
|
UserTypeImpl.getEditorConstructor(this.options().widget, this._readOnlyPureType());
|
2020-10-02 15:10:00 +00:00
|
|
|
// constructor may be null for a read-only non-formula field, though not today.
|
|
|
|
if (!editorCtor) {
|
|
|
|
// Actually, we only expect buildEditorDom() to be called when isEditorActive() is false (i.e.
|
|
|
|
// _fieldEditorHolder is already clear), but clear here explicitly for clarity.
|
|
|
|
this._fieldEditorHolder.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-17 16:41:07 +00:00
|
|
|
// if editor doesn't support readonly mode, don't show it
|
|
|
|
if (this._readonly.get() && editorCtor.supportsReadonly && !editorCtor.supportsReadonly()) {
|
|
|
|
this._fieldEditorHolder.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._readonly.get() && saveWithoutEditor(editorCtor, editRow, this.field, options.init)) {
|
2020-10-02 15:10:00 +00:00
|
|
|
this._fieldEditorHolder.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const cellElem = this._rowMap.get(mainRowModel)!;
|
|
|
|
|
|
|
|
// The editor may dispose itself; the Holder will know to clear itself in this case.
|
|
|
|
const fieldEditor = FieldEditor.create(this._fieldEditorHolder, {
|
|
|
|
gristDoc: this.gristDoc,
|
|
|
|
field: this.field,
|
|
|
|
cursor: this._cursor,
|
|
|
|
editRow,
|
|
|
|
cellElem,
|
|
|
|
editorCtor,
|
2021-06-17 16:41:07 +00:00
|
|
|
state: options.state,
|
|
|
|
startVal: this._readonly.get() ? undefined : options.init, // don't start with initial value
|
|
|
|
readonly: this._readonly.get() // readonly for editor will not be observable
|
2020-10-02 15:10:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Put the FieldEditor into a holder in GristDoc too. This way any existing FieldEditor (perhaps
|
|
|
|
// for another field, or for another BaseView) will get disposed at this time. The reason to
|
|
|
|
// still maintain a Holder in this FieldBuilder is mainly to match older behavior; changing that
|
|
|
|
// will entail a number of other tweaks related to the order of creating and disposal.
|
|
|
|
this.gristDoc.fieldEditorHolder.autoDispose(fieldEditor);
|
2021-05-25 09:24:00 +00:00
|
|
|
|
|
|
|
// expose the active editor in a grist doc as an observable
|
|
|
|
fieldEditor.onDispose(() => this.gristDoc.activeEditor.set(null));
|
|
|
|
this.gristDoc.activeEditor.set(fieldEditor);
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public isEditorActive() {
|
|
|
|
return !this._fieldEditorHolder.isEmpty();
|
|
|
|
}
|
2021-03-17 03:45:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Open the formula editor in the side pane. It will be positioned over refElem.
|
|
|
|
*/
|
2021-11-05 10:25:05 +00:00
|
|
|
public openSideFormulaEditor(
|
|
|
|
editRow: DataRowModel,
|
|
|
|
refElem: Element,
|
|
|
|
editValue?: string,
|
2021-11-30 08:59:04 +00:00
|
|
|
onSave?: (column: ColumnRec, formula: string) => Promise<void>,
|
2021-11-05 10:25:05 +00:00
|
|
|
onCancel?: () => void) {
|
2021-11-09 20:03:12 +00:00
|
|
|
const editorHolder = openFormulaEditor({
|
2021-03-17 03:45:44 +00:00
|
|
|
gristDoc: this.gristDoc,
|
|
|
|
field: this.field,
|
2021-11-09 20:03:12 +00:00
|
|
|
setupCleanup: setupEditorCleanup,
|
2021-03-17 03:45:44 +00:00
|
|
|
editRow,
|
|
|
|
refElem,
|
2021-11-05 10:25:05 +00:00
|
|
|
editValue,
|
|
|
|
onSave,
|
|
|
|
onCancel
|
2021-03-17 03:45:44 +00:00
|
|
|
});
|
2021-11-05 10:25:05 +00:00
|
|
|
// Add editor to document holder - this will prevent multiple formula editor instances.
|
2021-03-17 03:45:44 +00:00
|
|
|
this.gristDoc.fieldEditorHolder.autoDispose(editorHolder);
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
2021-08-12 18:06:40 +00:00
|
|
|
|
|
|
|
const cssTypeSelectMenu = styled('div', `
|
|
|
|
max-height: 500px;
|
|
|
|
`);
|