1/*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://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,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { BaseElement, element } from '../../../base-ui/BaseElement';
17import LitSwitch from '../../../base-ui/switch/lit-switch';
18import '../../../base-ui/select/LitAllocationSelect';
19
20import '../../../base-ui/switch/lit-switch';
21import { SpRecordTrace } from '../SpRecordTrace';
22import { Cmd } from '../../../command/Cmd';
23import { LitAllocationSelect } from '../../../base-ui/select/LitAllocationSelect';
24import { LitSelect } from '../../../base-ui/select/LitSelect';
25import { SpHiLogRecordHtml } from './SpHilogRecord.html';
26import { LitSelectV } from '../../../base-ui/select/LitSelectV';
27
28@element('sp-hi-log')
29export class SpHilogRecord extends BaseElement {
30  private vmTrackerSwitch: LitSwitch | undefined | null;
31  private processSelectEl: LitSelectV | undefined | null;
32  private logsSelectEl: LitSelect | undefined | null;
33
34  get recordHilog(): boolean {
35    return this.vmTrackerSwitch!.checked;
36  }
37
38  get appProcess(): string {
39    return this.processSelectEl!.value || '';
40  }
41
42  get appLogLevel(): string {
43    if (this.logsSelectEl!.value.trim() === '' || this.logsSelectEl!.value === 'ALL-Level') {
44      return 'LEVEL_UNSPECIFIED';
45    }
46    return this.logsSelectEl!.value || '';
47  }
48
49  initElements(): void {
50    this.vmTrackerSwitch = this.shadowRoot?.querySelector('.hilog-switch') as LitSwitch;
51    this.processSelectEl = this.shadowRoot?.querySelector('.record-process-select') as LitSelectV;
52    this.logsSelectEl = this.shadowRoot?.querySelector('.record-logs-select') as LitSelect;
53    let hiLogConfigList = this.shadowRoot?.querySelectorAll<HTMLDivElement>('.hilog-config-top');
54    this.vmTrackerSwitch.addEventListener('change', () => {
55      let configVisibility = 'none';
56      if (this.vmTrackerSwitch?.checked) {
57        configVisibility = 'block';
58      }
59      if (hiLogConfigList) {
60        console.log(configVisibility);
61        hiLogConfigList!.forEach((configEl) => {
62          configEl.style.display = configVisibility;
63        });
64      }
65    });
66    let processInputEl = this.processSelectEl.shadowRoot?.querySelector('input') as HTMLInputElement;
67    processInputEl.addEventListener('mousedown', () => {
68      if (this.recordHilog) {
69        if ((SpRecordTrace.serialNumber === '')) {
70          this.processSelectEl!.dataSource([], '');
71        } else {
72          Cmd.getProcess().then((processList) => {
73            if (processList.length > 0) {
74              this.processSelectEl!.dataSource(processList, 'ALL-Process', true);
75            } else {
76              this.processSelectEl!.dataSource([], '');
77            }
78          });
79        }
80        processInputEl!.removeAttribute('readonly');
81      } else {
82        processInputEl!.setAttribute('readonly', 'readonly');
83        return;
84      }
85    });
86  }
87
88  attributeChangedCallback(name: string, oldValue: string, newValue: string): void {
89    super.attributeChangedCallback(name, oldValue, newValue);
90  }
91
92  initHtml(): string {
93    return SpHiLogRecordHtml;
94  }
95}
96