xref: /aosp_15_r20/external/pigweed/pw_web/log-viewer/src/shared/view-node.ts (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1// Copyright 2024 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15import { TableColumn } from './interfaces';
16import { LogViewState } from './state';
17
18export enum NodeType {
19  View = 'view',
20  Split = 'split',
21}
22
23export enum Orientation {
24  Horizontal = 'horizontal',
25  Vertical = 'vertical',
26}
27
28export interface ViewNodeOptions {
29  logViewId?: string;
30  type?: NodeType;
31  orientation?: Orientation;
32  children?: ViewNode[];
33  columnData?: TableColumn[];
34  searchText?: string;
35  viewTitle?: string;
36  wordWrap?: boolean;
37}
38
39/**
40 * Represents a log view node in a dynamic layout. This class can function as either
41 * a single log view or a container that manages a split between two child views.
42 *
43 * @class ViewContainer
44 * @property {string} logViewId - A unique identifier for the log view.
45 * @property {NodeType} type - Specifies whether the node is a single view (NodeType.View) or a container for split views (NodeType.Split). Defaults to NodeType.View.
46 * @property {Orientation} orientation - Defines the orientation of the split; defaults to undefined.
47 * @property {ViewNode[]} children - An array of child nodes if this is a split type; empty for single view types.
48 * @property {LogViewState} logViewState - State properties for the corresponding log view.
49 */
50export class ViewNode {
51  logViewId?: string;
52  type?: NodeType;
53  orientation?: Orientation | undefined;
54  children: ViewNode[];
55  logViewState?: LogViewState;
56
57  constructor(options?: ViewNodeOptions) {
58    this.type = options?.type || NodeType.View;
59    this.orientation = options?.orientation || undefined;
60    this.children = options?.children || [];
61
62    if (this.type === NodeType.View) {
63      this.logViewId = options?.logViewId || crypto.randomUUID();
64      this.logViewState = {
65        columnData: options?.columnData || [],
66        searchText: options?.searchText || '',
67        viewTitle: options?.viewTitle || '',
68        wordWrap: options?.wordWrap !== undefined ? options.wordWrap : true,
69      };
70    }
71  }
72}
73