1fb726d48Sopenharmony_ci/*
2fb726d48Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3fb726d48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb726d48Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb726d48Sopenharmony_ci * You may obtain a copy of the License at
6fb726d48Sopenharmony_ci *
7fb726d48Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb726d48Sopenharmony_ci *
9fb726d48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb726d48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb726d48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb726d48Sopenharmony_ci * See the License for the specific language governing permissions and
13fb726d48Sopenharmony_ci * limitations under the License.
14fb726d48Sopenharmony_ci */
15fb726d48Sopenharmony_ci
16fb726d48Sopenharmony_ciimport { BaseElement, element } from '../../../base-ui/BaseElement';
17fb726d48Sopenharmony_ciimport LitSwitch, { LitSwitchChangeEvent } from '../../../base-ui/switch/lit-switch';
18fb726d48Sopenharmony_ciimport '../../../base-ui/select/LitAllocationSelect';
19fb726d48Sopenharmony_ci
20fb726d48Sopenharmony_ciimport '../../../base-ui/switch/lit-switch';
21fb726d48Sopenharmony_ciimport { LitAllocationSelect } from '../../../base-ui/select/LitAllocationSelect';
22fb726d48Sopenharmony_ciimport { SpRecordTrace } from '../SpRecordTrace';
23fb726d48Sopenharmony_ciimport { Cmd } from '../../../command/Cmd';
24fb726d48Sopenharmony_ciimport { SpVmTrackerHtml } from './SpVmTracker.html';
25fb726d48Sopenharmony_ciimport { LitSelectV } from '../../../base-ui/select/LitSelectV';
26fb726d48Sopenharmony_ci
27fb726d48Sopenharmony_ci@element('sp-vm-tracker')
28fb726d48Sopenharmony_ciexport class SpVmTracker extends BaseElement {
29fb726d48Sopenharmony_ci  private vmTrackerProcessInput: LitSelectV | undefined | null;
30fb726d48Sopenharmony_ci
31fb726d48Sopenharmony_ci  set startSamp(start: boolean) {
32fb726d48Sopenharmony_ci    if (start) {
33fb726d48Sopenharmony_ci      this.setAttribute('startSamp', '');
34fb726d48Sopenharmony_ci    } else {
35fb726d48Sopenharmony_ci      this.removeAttribute('startSamp');
36fb726d48Sopenharmony_ci      let input = this.vmTrackerProcessInput?.shadowRoot?.querySelector<HTMLInputElement>('#singleInput');
37fb726d48Sopenharmony_ci      input!.value = '';
38fb726d48Sopenharmony_ci    }
39fb726d48Sopenharmony_ci  }
40fb726d48Sopenharmony_ci
41fb726d48Sopenharmony_ci  get process(): string {
42fb726d48Sopenharmony_ci    if (this.vmTrackerProcessInput!.value.length > 0) {
43fb726d48Sopenharmony_ci      let result = this.vmTrackerProcessInput!.value.match(/\((.+?)\)/g);
44fb726d48Sopenharmony_ci      if (result) {
45fb726d48Sopenharmony_ci        return result.toString().replace('(', '').replace(')', '');
46fb726d48Sopenharmony_ci      } else {
47fb726d48Sopenharmony_ci        return this.vmTrackerProcessInput!.value;
48fb726d48Sopenharmony_ci      }
49fb726d48Sopenharmony_ci    }
50fb726d48Sopenharmony_ci    return '';
51fb726d48Sopenharmony_ci  }
52fb726d48Sopenharmony_ci
53fb726d48Sopenharmony_ci  get startSamp(): boolean {
54fb726d48Sopenharmony_ci    return this.hasAttribute('startSamp');
55fb726d48Sopenharmony_ci  }
56fb726d48Sopenharmony_ci
57fb726d48Sopenharmony_ci  initElements(): void {
58fb726d48Sopenharmony_ci    let vmTrackerSwitch = this.shadowRoot?.querySelector('lit-switch') as LitSwitch;
59fb726d48Sopenharmony_ci    vmTrackerSwitch.addEventListener('change', (event: CustomEventInit<LitSwitchChangeEvent>) => {
60fb726d48Sopenharmony_ci      let detail = event.detail;
61fb726d48Sopenharmony_ci      if (detail!.checked) {
62fb726d48Sopenharmony_ci        this.startSamp = true;
63fb726d48Sopenharmony_ci        this.unDisable();
64fb726d48Sopenharmony_ci      } else {
65fb726d48Sopenharmony_ci        this.startSamp = false;
66fb726d48Sopenharmony_ci        this.disable();
67fb726d48Sopenharmony_ci      }
68fb726d48Sopenharmony_ci    });
69fb726d48Sopenharmony_ci    this.vmTrackerProcessInput = this.shadowRoot?.querySelector<LitSelectV>('lit-select-v');
70fb726d48Sopenharmony_ci    let vmTrackerMul = this.vmTrackerProcessInput?.shadowRoot?.querySelector('input') as HTMLDivElement;
71fb726d48Sopenharmony_ci    vmTrackerMul!.addEventListener('mousedown', () => {
72fb726d48Sopenharmony_ci      if (this.startSamp && (SpRecordTrace.serialNumber === '')) {
73fb726d48Sopenharmony_ci        this.vmTrackerProcessInput!.dataSource([], '');
74fb726d48Sopenharmony_ci      }
75fb726d48Sopenharmony_ci    });
76fb726d48Sopenharmony_ci    vmTrackerMul!.addEventListener('mouseup', () => {
77fb726d48Sopenharmony_ci      if (this.startSamp) {
78fb726d48Sopenharmony_ci        if (SpRecordTrace.serialNumber === '') {
79fb726d48Sopenharmony_ci          this.vmTrackerProcessInput!.dataSource([], '');
80fb726d48Sopenharmony_ci        } else {
81fb726d48Sopenharmony_ci          Cmd.getProcess().then((processList) => {
82fb726d48Sopenharmony_ci            if (processList.length > 0) {
83fb726d48Sopenharmony_ci              this.vmTrackerProcessInput!.dataSource(processList, '', true);
84fb726d48Sopenharmony_ci            } else {
85fb726d48Sopenharmony_ci              this.vmTrackerProcessInput!.dataSource([], '');
86fb726d48Sopenharmony_ci            }
87fb726d48Sopenharmony_ci          });
88fb726d48Sopenharmony_ci        }
89fb726d48Sopenharmony_ci        vmTrackerMul!.removeAttribute('readonly');
90fb726d48Sopenharmony_ci      } else {
91fb726d48Sopenharmony_ci        vmTrackerMul!.setAttribute('readonly', 'readonly');
92fb726d48Sopenharmony_ci        return;
93fb726d48Sopenharmony_ci      }
94fb726d48Sopenharmony_ci    });
95fb726d48Sopenharmony_ci    this.disable();
96fb726d48Sopenharmony_ci  }
97fb726d48Sopenharmony_ci
98fb726d48Sopenharmony_ci  private unDisable(): void {
99fb726d48Sopenharmony_ci    let configVal = this.shadowRoot?.querySelectorAll<HTMLElement>('.config');
100fb726d48Sopenharmony_ci    configVal!.forEach((configVal1) => {
101fb726d48Sopenharmony_ci      configVal1.removeAttribute('disabled');
102fb726d48Sopenharmony_ci    });
103fb726d48Sopenharmony_ci  }
104fb726d48Sopenharmony_ci
105fb726d48Sopenharmony_ci  private disable(): void {
106fb726d48Sopenharmony_ci    let configVal = this.shadowRoot?.querySelectorAll<HTMLElement>('.config');
107fb726d48Sopenharmony_ci    configVal!.forEach((configVal1) => {
108fb726d48Sopenharmony_ci      if (configVal1.title !== 'Start VM Tracker Record') {
109fb726d48Sopenharmony_ci        configVal1.setAttribute('disabled', '');
110fb726d48Sopenharmony_ci      }
111fb726d48Sopenharmony_ci    });
112fb726d48Sopenharmony_ci  }
113fb726d48Sopenharmony_ci
114fb726d48Sopenharmony_ci  initHtml(): string {
115fb726d48Sopenharmony_ci    return SpVmTrackerHtml;
116fb726d48Sopenharmony_ci  }
117fb726d48Sopenharmony_ci}
118