import {ColumnRec, DocModel} from 'app/client/models/DocModel'; import {Style} from 'app/client/models/Styles'; import * as modelUtil from 'app/client/models/modelUtil'; export interface RuleOwner { // Field or Section can have a list of conditional styling rules. Each style is a combination of a formula and options // that must by applied. Style is persisted as a new hidden formula column and the list of such // columns is stored as Reference List property ('rules') in a field or column. tableId: ko.Computed; // If this field (or column) has a list of conditional styling rules. hasRules: ko.Computed; // List of columns that are used as rules for conditional styles. rulesCols: ko.Computed; // List of columns ids that are used as rules for conditional styles. rulesColsIds: ko.Computed; // List of styles used by conditional rules. rulesStyles: modelUtil.KoSaveableObservable; // Adds empty conditional style rule. Sets before sending to the server. addEmptyRule(): Promise; // Removes one rule from the collection. Removes before sending update to the server. removeRule(index: number): Promise; } export async function removeRule(docModel: DocModel, owner: RuleOwner, index: number) { const col = owner.rulesCols.peek()[index]; if (!col) { throw new Error(`There is no rule at index ${index}`); } const newStyles = owner.rulesStyles.peek()?.slice() ?? []; if (newStyles.length >= index) { newStyles.splice(index, 1); } else { console.debug(`There are not style options at index ${index}`); } await docModel.docData.bundleActions("Remove conditional rule", () => Promise.all([ owner.rulesStyles.setAndSave(newStyles), docModel.docData.sendAction(['RemoveColumn', owner.tableId.peek(), col.colId.peek()]) ]) ); }