import { isArrayEqual } from '@pbkware/js-utils'; import { RevTextFormattableValue } from '../../../../../cell-content/client'; /** @public */ export abstract class RevTableValue { private _textFormattableValue: RevTextFormattableValue | undefined; private _renderAttributes: RevTextFormattableValue.Attribute[] = []; get textFormattableValue() { if (this._textFormattableValue === undefined) { this._textFormattableValue = this.createTextFormattableValue(); this._textFormattableValue.setAttributes(this._renderAttributes); } return this._textFormattableValue; } clearRendering() { this._textFormattableValue = undefined; } hasRenderAttribute(value: RevTextFormattableValue.Attribute): boolean { const attributes = this._renderAttributes; const attributeCount = attributes.length; if (attributeCount === 0) { return false; } else { for (let i = 0; i < attributeCount; i++) { const attribute = attributes[i]; if (attribute === value) { return true; } } return false; } } addRenderAttribute(value: RevTextFormattableValue.Attribute) { if (!this.hasRenderAttribute(value)) { this._renderAttributes.push(value); this._textFormattableValue = undefined; } } removeRenderAttribute(value: RevTextFormattableValue.Attribute): void { const attributes = this._renderAttributes; const attributeCount = attributes.length; if (attributeCount > 0) { for (let i = attributeCount - 1; i >= 0; i--) { const attribute = attributes[i]; if (attribute === value) { attributes.splice(i, 1); this._textFormattableValue = undefined; break; } } } } addOrRemoveRenderAttribute(value: RevTextFormattableValue.Attribute, add: boolean): void { if (add) { this.addRenderAttribute(value); } else { this.removeRenderAttribute(value); } } setRenderAttributes(value: readonly RevTextFormattableValue.Attribute[]): void { if (!isArrayEqual(value, this._renderAttributes)) { this._renderAttributes = value.slice(); this._textFormattableValue = undefined; } } abstract isUndefined(): boolean; protected abstract createTextFormattableValue(): RevTextFormattableValue; } /** @public */ export namespace RevTableValue { export type Constructor = new() => RevTableValue; }