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 { LitSelectV } from '../../../base-ui/select/LitSelectV';
18fb726d48Sopenharmony_ciimport LitSwitch, { LitSwitchChangeEvent } from '../../../base-ui/switch/lit-switch';
19fb726d48Sopenharmony_ciimport '../../../base-ui/select/LitSelectV';
20fb726d48Sopenharmony_ciimport '../../../base-ui/select/LitSelect';
21fb726d48Sopenharmony_ci
22fb726d48Sopenharmony_ciimport '../../../base-ui/switch/lit-switch';
23fb726d48Sopenharmony_ciimport { SpRecordTrace } from '../SpRecordTrace';
24fb726d48Sopenharmony_ciimport { Cmd } from '../../../command/Cmd';
25fb726d48Sopenharmony_ciimport { SpApplication } from '../../SpApplication';
26fb726d48Sopenharmony_ciimport { SpFIleSystemHtml } from './SpFIleSystem.html';
27fb726d48Sopenharmony_ci
28fb726d48Sopenharmony_ci@element('sp-file-system')
29fb726d48Sopenharmony_ciexport class SpFileSystem extends BaseElement {
30fb726d48Sopenharmony_ci  private processInput: LitSelectV | undefined | null;
31fb726d48Sopenharmony_ci  private maximum: HTMLInputElement | undefined | null;
32fb726d48Sopenharmony_ci  private selectProcess: HTMLInputElement | undefined | null;
33fb726d48Sopenharmony_ci
34fb726d48Sopenharmony_ci  set startRecord(start: boolean) {
35fb726d48Sopenharmony_ci    if (start) {
36fb726d48Sopenharmony_ci      this.unDisable();
37fb726d48Sopenharmony_ci      this.setAttribute('startRecord', '');
38fb726d48Sopenharmony_ci      this.selectProcess!.removeAttribute('readonly');
39fb726d48Sopenharmony_ci    } else {
40fb726d48Sopenharmony_ci      if (!this.startFileSystem && !this.startVirtualMemory && !this.startIo) {
41fb726d48Sopenharmony_ci        this.removeAttribute('startRecord');
42fb726d48Sopenharmony_ci        this.disable();
43fb726d48Sopenharmony_ci        this.selectProcess!.setAttribute('readonly', 'readonly');
44fb726d48Sopenharmony_ci      }
45fb726d48Sopenharmony_ci    }
46fb726d48Sopenharmony_ci  }
47fb726d48Sopenharmony_ci
48fb726d48Sopenharmony_ci  get startRecord(): boolean {
49fb726d48Sopenharmony_ci    return this.hasAttribute('startRecord');
50fb726d48Sopenharmony_ci  }
51fb726d48Sopenharmony_ci
52fb726d48Sopenharmony_ci  set startFileSystem(start: boolean) {
53fb726d48Sopenharmony_ci    if (start) {
54fb726d48Sopenharmony_ci      this.setAttribute('startSamp', '');
55fb726d48Sopenharmony_ci    } else {
56fb726d48Sopenharmony_ci      this.removeAttribute('startSamp');
57fb726d48Sopenharmony_ci    }
58fb726d48Sopenharmony_ci    this.startRecord = start;
59fb726d48Sopenharmony_ci  }
60fb726d48Sopenharmony_ci
61fb726d48Sopenharmony_ci  get startFileSystem(): boolean {
62fb726d48Sopenharmony_ci    return this.hasAttribute('startSamp');
63fb726d48Sopenharmony_ci  }
64fb726d48Sopenharmony_ci
65fb726d48Sopenharmony_ci  set startVirtualMemory(start: boolean) {
66fb726d48Sopenharmony_ci    if (start) {
67fb726d48Sopenharmony_ci      this.setAttribute('virtual', '');
68fb726d48Sopenharmony_ci    } else {
69fb726d48Sopenharmony_ci      this.removeAttribute('virtual');
70fb726d48Sopenharmony_ci    }
71fb726d48Sopenharmony_ci    this.startRecord = start;
72fb726d48Sopenharmony_ci  }
73fb726d48Sopenharmony_ci
74fb726d48Sopenharmony_ci  get startVirtualMemory(): boolean {
75fb726d48Sopenharmony_ci    return this.hasAttribute('virtual');
76fb726d48Sopenharmony_ci  }
77fb726d48Sopenharmony_ci
78fb726d48Sopenharmony_ci  set startIo(start: boolean) {
79fb726d48Sopenharmony_ci    if (start) {
80fb726d48Sopenharmony_ci      this.setAttribute('io', '');
81fb726d48Sopenharmony_ci    } else {
82fb726d48Sopenharmony_ci      this.removeAttribute('io');
83fb726d48Sopenharmony_ci    }
84fb726d48Sopenharmony_ci    this.startRecord = start;
85fb726d48Sopenharmony_ci  }
86fb726d48Sopenharmony_ci
87fb726d48Sopenharmony_ci  get startIo(): boolean {
88fb726d48Sopenharmony_ci    return this.hasAttribute('io');
89fb726d48Sopenharmony_ci  }
90fb726d48Sopenharmony_ci
91fb726d48Sopenharmony_ci  getSystemConfig(): SystemConfig | undefined {
92fb726d48Sopenharmony_ci    let configVal = this.shadowRoot?.querySelectorAll<HTMLElement>('.config');
93fb726d48Sopenharmony_ci    let systemConfig: SystemConfig = {
94fb726d48Sopenharmony_ci      process: '',
95fb726d48Sopenharmony_ci      unWindLevel: 0,
96fb726d48Sopenharmony_ci    };
97fb726d48Sopenharmony_ci    configVal!.forEach((value) => {
98fb726d48Sopenharmony_ci      switch (value.title) {
99fb726d48Sopenharmony_ci        case 'Process':
100fb726d48Sopenharmony_ci          let processSelect = value as LitSelectV;
101fb726d48Sopenharmony_ci          if (processSelect.all) {
102fb726d48Sopenharmony_ci            systemConfig.process = 'ALL';
103fb726d48Sopenharmony_ci            break;
104fb726d48Sopenharmony_ci          }
105fb726d48Sopenharmony_ci          if (processSelect.value.length > 0) {
106fb726d48Sopenharmony_ci            let result = processSelect.value.match(/\((.+?)\)/g);
107fb726d48Sopenharmony_ci            if (result) {
108fb726d48Sopenharmony_ci              systemConfig.process = result.toString().replace(/[()]/g, '');
109fb726d48Sopenharmony_ci            } else {
110fb726d48Sopenharmony_ci              systemConfig.process = processSelect.value;
111fb726d48Sopenharmony_ci            }
112fb726d48Sopenharmony_ci          }
113fb726d48Sopenharmony_ci          break;
114fb726d48Sopenharmony_ci        case 'Max Unwind Level':
115fb726d48Sopenharmony_ci          let maxUnwindLevel = value as HTMLInputElement;
116fb726d48Sopenharmony_ci          if (maxUnwindLevel.value !== '') {
117fb726d48Sopenharmony_ci            systemConfig.unWindLevel = Number(maxUnwindLevel.value);
118fb726d48Sopenharmony_ci          }
119fb726d48Sopenharmony_ci      }
120fb726d48Sopenharmony_ci    });
121fb726d48Sopenharmony_ci    return systemConfig;
122fb726d48Sopenharmony_ci  }
123fb726d48Sopenharmony_ci
124fb726d48Sopenharmony_ci  initElements(): void {
125fb726d48Sopenharmony_ci    this.switchChange();
126fb726d48Sopenharmony_ci    this.processInput = this.shadowRoot?.querySelector<LitSelectV>('lit-select-v');
127fb726d48Sopenharmony_ci    this.maximum = this.shadowRoot?.querySelector<HTMLInputElement>('#maxUnwindLevel');
128fb726d48Sopenharmony_ci    this.maximum?.addEventListener('keyup', () => {
129fb726d48Sopenharmony_ci      this.maximum!.value = this.maximum!.value.replace(/\D/g, '');
130fb726d48Sopenharmony_ci      if (this.maximum!.value !== '') {
131fb726d48Sopenharmony_ci        let mun = parseInt(this.maximum!.value);
132fb726d48Sopenharmony_ci        if (mun > 64 || mun < 0) {
133fb726d48Sopenharmony_ci          this.maximum!.value = '10';
134fb726d48Sopenharmony_ci        }
135fb726d48Sopenharmony_ci      }
136fb726d48Sopenharmony_ci    });
137fb726d48Sopenharmony_ci    this.selectProcess = this.processInput!.shadowRoot?.querySelector('input') as HTMLInputElement;
138fb726d48Sopenharmony_ci    this.selectProcess!.addEventListener('mousedown', () => {
139fb726d48Sopenharmony_ci      if (SpRecordTrace.serialNumber === '') {
140fb726d48Sopenharmony_ci        this.processInput!.dataSource([], '');
141fb726d48Sopenharmony_ci      } else {
142fb726d48Sopenharmony_ci        Cmd.getProcess().then((processList) => {
143fb726d48Sopenharmony_ci          if (processList.length > 0 && this.startRecord) {
144fb726d48Sopenharmony_ci            this.selectProcess!.setAttribute('readonly', 'readonly');
145fb726d48Sopenharmony_ci          }
146fb726d48Sopenharmony_ci          this.processInput?.dataSource(processList, 'ALL-Process');
147fb726d48Sopenharmony_ci        });
148fb726d48Sopenharmony_ci      }
149fb726d48Sopenharmony_ci    });
150fb726d48Sopenharmony_ci    this.disable();
151fb726d48Sopenharmony_ci  }
152fb726d48Sopenharmony_ci
153fb726d48Sopenharmony_ci  private switchChange(): void {
154fb726d48Sopenharmony_ci    let fileSystemSwitch = this.shadowRoot?.querySelector<LitSwitch>('#fileSystem');
155fb726d48Sopenharmony_ci    fileSystemSwitch!.addEventListener('change', (event: CustomEventInit<LitSwitchChangeEvent>) => {
156fb726d48Sopenharmony_ci      let detail = event.detail;
157fb726d48Sopenharmony_ci      this.startFileSystem = detail!.checked;
158fb726d48Sopenharmony_ci    });
159fb726d48Sopenharmony_ci    let pageFaultSwitch = this.shadowRoot?.querySelector<LitSwitch>('#pageFault');
160fb726d48Sopenharmony_ci    pageFaultSwitch!.addEventListener('change', (event: CustomEventInit<LitSwitchChangeEvent>) => {
161fb726d48Sopenharmony_ci      let detail = event.detail;
162fb726d48Sopenharmony_ci      this.startVirtualMemory = detail!.checked;
163fb726d48Sopenharmony_ci    });
164fb726d48Sopenharmony_ci    let bioLatencySwitch = this.shadowRoot?.querySelector<LitSwitch>('#bioLatency');
165fb726d48Sopenharmony_ci    bioLatencySwitch!.addEventListener('change', (event: CustomEventInit<LitSwitchChangeEvent>) => {
166fb726d48Sopenharmony_ci      let detail = event.detail;
167fb726d48Sopenharmony_ci      this.startIo = detail!.checked;
168fb726d48Sopenharmony_ci    });
169fb726d48Sopenharmony_ci  }
170fb726d48Sopenharmony_ci
171fb726d48Sopenharmony_ci  private unDisable(): void {
172fb726d48Sopenharmony_ci    let fileSystemConfigVals = this.shadowRoot?.querySelectorAll<HTMLElement>('.config');
173fb726d48Sopenharmony_ci    fileSystemConfigVals!.forEach((fileSystemConfigVal) => {
174fb726d48Sopenharmony_ci      fileSystemConfigVal.removeAttribute('disabled');
175fb726d48Sopenharmony_ci    });
176fb726d48Sopenharmony_ci  }
177fb726d48Sopenharmony_ci
178fb726d48Sopenharmony_ci  private disable(): void {
179fb726d48Sopenharmony_ci    let fileSystemConfigVals = this.shadowRoot?.querySelectorAll<HTMLElement>('.config');
180fb726d48Sopenharmony_ci    fileSystemConfigVals!.forEach((fileSystemConfigVal) => {
181fb726d48Sopenharmony_ci      if (
182fb726d48Sopenharmony_ci        fileSystemConfigVal.title === 'Start FileSystem Record' ||
183fb726d48Sopenharmony_ci        fileSystemConfigVal.title === 'Start Page Fault Record' ||
184fb726d48Sopenharmony_ci        fileSystemConfigVal.title === 'Start BIO Latency Record'
185fb726d48Sopenharmony_ci      ) {
186fb726d48Sopenharmony_ci      } else {
187fb726d48Sopenharmony_ci        fileSystemConfigVal.setAttribute('disabled', '');
188fb726d48Sopenharmony_ci      }
189fb726d48Sopenharmony_ci    });
190fb726d48Sopenharmony_ci  }
191fb726d48Sopenharmony_ci
192fb726d48Sopenharmony_ci  connectedCallback(): void {
193fb726d48Sopenharmony_ci    let traceMode = this.shadowRoot!.querySelector('#traceMode') as HTMLDivElement;
194fb726d48Sopenharmony_ci    this.maximum!.onkeydown = (ev): void => {
195fb726d48Sopenharmony_ci      // @ts-ignore
196fb726d48Sopenharmony_ci      if (ev.key === '0' && ev.target.value.length === 1 && ev.target.value === '0') {
197fb726d48Sopenharmony_ci        ev.preventDefault();
198fb726d48Sopenharmony_ci      }
199fb726d48Sopenharmony_ci    };
200fb726d48Sopenharmony_ci    let isLongTrace = SpApplication.isLongTrace;
201fb726d48Sopenharmony_ci    if (isLongTrace) {
202fb726d48Sopenharmony_ci      traceMode!.style.display = 'block';
203fb726d48Sopenharmony_ci    } else {
204fb726d48Sopenharmony_ci      traceMode!.style.display = 'none';
205fb726d48Sopenharmony_ci    }
206fb726d48Sopenharmony_ci  }
207fb726d48Sopenharmony_ci
208fb726d48Sopenharmony_ci  initHtml(): string {
209fb726d48Sopenharmony_ci    return SpFIleSystemHtml;
210fb726d48Sopenharmony_ci  }
211fb726d48Sopenharmony_ci}
212fb726d48Sopenharmony_ci
213fb726d48Sopenharmony_ciexport interface SystemConfig {
214fb726d48Sopenharmony_ci  process: string;
215fb726d48Sopenharmony_ci  unWindLevel: number;
216fb726d48Sopenharmony_ci}
217