2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* NewAbstractWidget is equivalent to AbstractWidget for outside code, but is in typescript, and
|
|
|
|
* so is friendlier and clearer to derive TypeScript classes from.
|
|
|
|
*/
|
|
|
|
import {DocComm} from 'app/client/components/DocComm';
|
|
|
|
import {DocData} from 'app/client/models/DocData';
|
|
|
|
import {ViewFieldRec} from 'app/client/models/entities/ViewFieldRec';
|
|
|
|
import {SaveableObjObservable} from 'app/client/models/modelUtil';
|
|
|
|
import {cssLabel, cssRow} from 'app/client/ui/RightPanel';
|
2021-03-02 12:27:08 +00:00
|
|
|
import {colorSelect} from 'app/client/ui2018/ColorSelect';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {BaseFormatter, createFormatter} from 'app/common/ValueFormatter';
|
2021-03-02 12:27:08 +00:00
|
|
|
import {Computed, Disposable, DomContents, fromKo, Observable} from 'grainjs';
|
2020-10-02 15:10:00 +00:00
|
|
|
import * as ko from 'knockout';
|
|
|
|
|
|
|
|
|
2021-03-02 12:27:08 +00:00
|
|
|
export interface Options {
|
|
|
|
// A hex value to set the default widget text color. Default to '#000000' if omitted.
|
|
|
|
defaultTextColor?: string;
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* NewAbstractWidget - The base of the inheritance tree for widgets.
|
|
|
|
* @param {Function} field - The RowModel for this view field.
|
|
|
|
*/
|
|
|
|
export abstract class NewAbstractWidget extends Disposable {
|
|
|
|
/**
|
|
|
|
* Override the create() method to match the parameters of create() expected by FieldBuilder.
|
|
|
|
*/
|
|
|
|
public static create(field: ViewFieldRec) {
|
|
|
|
return Disposable.create.call(this as any, null, field);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected options: SaveableObjObservable<any>;
|
|
|
|
protected valueFormatter: Observable<BaseFormatter>;
|
2021-02-09 10:12:53 +00:00
|
|
|
protected textColor: Observable<string>;
|
|
|
|
protected fillColor: Observable<string>;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2021-03-02 12:27:08 +00:00
|
|
|
constructor(protected field: ViewFieldRec, opts: Options = {}) {
|
2020-10-02 15:10:00 +00:00
|
|
|
super();
|
2021-03-02 12:27:08 +00:00
|
|
|
const {defaultTextColor = '#000000'} = opts;
|
2020-10-02 15:10:00 +00:00
|
|
|
this.options = field.widgetOptionsJson;
|
2021-03-02 12:27:08 +00:00
|
|
|
this.textColor = Computed.create(this, (use) => (
|
|
|
|
use(this.field.textColor) || defaultTextColor
|
|
|
|
)).onWrite((val) => this.field.textColor(val === defaultTextColor ? undefined : val));
|
2021-02-09 10:12:53 +00:00
|
|
|
this.fillColor = fromKo(this.field.fillColor);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// Note that its easier to create a knockout computed from the several knockout observables,
|
|
|
|
// but then we turn it into a grainjs observable.
|
|
|
|
const formatter = this.autoDispose(ko.computed(() =>
|
2021-08-26 16:35:11 +00:00
|
|
|
createFormatter(field.displayColModel().type(), this.options(), field.documentSettings())));
|
2020-10-02 15:10:00 +00:00
|
|
|
this.valueFormatter = fromKo(formatter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the DOM showing configuration buttons and fields in the sidebar.
|
|
|
|
*/
|
2020-10-07 21:58:43 +00:00
|
|
|
public buildConfigDom(): DomContents { return null; }
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the transform prompt config DOM in the few cases where it is necessary.
|
|
|
|
* Child classes need not override this function if they do not require transform config options.
|
|
|
|
*/
|
2020-10-07 21:58:43 +00:00
|
|
|
public buildTransformConfigDom(): DomContents { return null; }
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
public buildColorConfigDom(): Element[] {
|
|
|
|
return [
|
|
|
|
cssLabel('CELL COLOR'),
|
|
|
|
cssRow(
|
2021-03-02 12:27:08 +00:00
|
|
|
colorSelect(
|
|
|
|
this.textColor,
|
|
|
|
this.fillColor,
|
|
|
|
// Calling `field.widgetOptionsJson.save()` saves both fill and text color settings.
|
|
|
|
() => this.field.widgetOptionsJson.save()
|
2020-10-02 15:10:00 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds the data cell DOM.
|
|
|
|
* @param {DataRowModel} row - The rowModel object.
|
|
|
|
*/
|
|
|
|
public abstract buildDom(row: any): Element;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the DocData object to which this field belongs.
|
|
|
|
*/
|
|
|
|
protected _getDocData(): DocData {
|
|
|
|
// TODO: There should be a better way to access docData and docComm, or better yet GristDoc.
|
|
|
|
return this.field._table.tableData.docData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the docComm object for communicating with the server.
|
|
|
|
*/
|
|
|
|
protected _getDocComm(): DocComm { return this._getDocData().docComm; }
|
|
|
|
}
|