import * as ko from "knockout"; import * as Constants from "../Constants"; import ClauseGroup from "./ClauseGroup"; import QueryBuilderViewModel from "./QueryBuilderViewModel"; /** * View model for showing group indicators on UI, contains information such as group color and border styles. */ export default class ClauseGroupViewModel { public ungroupClausesLabel = "Ungroup clauses"; // localize public backgroundColor: ko.Observable; public canUngroup: ko.Observable; public showTopBorder: ko.Observable; public showLeftBorder: ko.Observable; public showBottomBorder: ko.Observable; public depth: ko.Observable; // for debugging purpose only, now showing the number on UI. public borderBackgroundColor: ko.Observable; private _clauseGroup: ClauseGroup; private _queryBuilderViewModel: QueryBuilderViewModel; constructor(clauseGroup: ClauseGroup, canUngroup: boolean, queryBuilderViewModel: QueryBuilderViewModel) { this._clauseGroup = clauseGroup; this._queryBuilderViewModel = queryBuilderViewModel; this.backgroundColor = ko.observable(this.getGroupBackgroundColor(clauseGroup)); this.canUngroup = ko.observable(canUngroup); this.showTopBorder = ko.observable(false); this.showLeftBorder = ko.observable(false); this.showBottomBorder = ko.observable(false); this.depth = ko.observable(clauseGroup.getCurrentGroupDepth()); this.borderBackgroundColor = ko.observable("solid thin " + this.getGroupBackgroundColor(clauseGroup)); } public ungroupClauses = (): void => { this._clauseGroup.ungroup(); this._queryBuilderViewModel.updateClauseArray(); }; private getGroupBackgroundColor(group: ClauseGroup): string { const colorCount = Constants.clauseGroupColors.length; if (group.isRootGroup) { return Constants.transparentColor; } else { return Constants.clauseGroupColors[group.getCurrentGroupDepth() % colorCount]; } } }