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 '../../../base-ui/select/LitAllocationSelect';
18fb726d48Sopenharmony_ci
19fb726d48Sopenharmony_ciimport '../../../base-ui/switch/lit-switch';
20fb726d48Sopenharmony_ciimport { LitAllocationSelect } from '../../../base-ui/select/LitAllocationSelect';
21fb726d48Sopenharmony_ciimport { SpRecordTrace } from '../SpRecordTrace';
22fb726d48Sopenharmony_ciimport { Cmd } from '../../../command/Cmd';
23fb726d48Sopenharmony_ciimport { LitRadioBox } from '../../../base-ui/radiobox/LitRadioBox';
24fb726d48Sopenharmony_ciimport { SpCheckDesBox } from './SpCheckDesBox';
25fb726d48Sopenharmony_ciimport LitSwitch from '../../../base-ui/switch/lit-switch';
26fb726d48Sopenharmony_ciimport { SpApplication } from '../../SpApplication';
27fb726d48Sopenharmony_ciimport { SpArkTsHtml } from './SpArkTs.html';
28fb726d48Sopenharmony_ciimport { LitSelectV } from '../../../base-ui/select/LitSelectV';
29fb726d48Sopenharmony_ci
30fb726d48Sopenharmony_ci@element('sp-ark-ts')
31fb726d48Sopenharmony_ciexport class SpArkTs extends BaseElement {
32fb726d48Sopenharmony_ci  private processInput: LitSelectV | undefined | null;
33fb726d48Sopenharmony_ci  private spCheckDesBox: SpCheckDesBox | undefined | null;
34fb726d48Sopenharmony_ci  private radioBox: LitRadioBox | undefined | null;
35fb726d48Sopenharmony_ci  private interval: HTMLInputElement | undefined | null;
36fb726d48Sopenharmony_ci  private memorySwitch: LitSwitch | undefined | null;
37fb726d48Sopenharmony_ci  private cpuSwitch: LitSwitch | undefined | null;
38fb726d48Sopenharmony_ci  private litSwitch: LitSwitch | undefined | null;
39fb726d48Sopenharmony_ci
40fb726d48Sopenharmony_ci  set startSamp(jsHeapStart: boolean) {
41fb726d48Sopenharmony_ci    if (jsHeapStart) {
42fb726d48Sopenharmony_ci      this.setAttribute('startSamp', '');
43fb726d48Sopenharmony_ci    } else {
44fb726d48Sopenharmony_ci      this.removeAttribute('startSamp');
45fb726d48Sopenharmony_ci    }
46fb726d48Sopenharmony_ci  }
47fb726d48Sopenharmony_ci
48fb726d48Sopenharmony_ci  get startSamp(): boolean {
49fb726d48Sopenharmony_ci    return this.hasAttribute('startSamp');
50fb726d48Sopenharmony_ci  }
51fb726d48Sopenharmony_ci
52fb726d48Sopenharmony_ci  get process(): string {
53fb726d48Sopenharmony_ci    if (this.processInput!.value.length > 0) {
54fb726d48Sopenharmony_ci      return this.processInput!.value;
55fb726d48Sopenharmony_ci    }
56fb726d48Sopenharmony_ci    return '';
57fb726d48Sopenharmony_ci  }
58fb726d48Sopenharmony_ci
59fb726d48Sopenharmony_ci  get radioBoxType(): number {
60fb726d48Sopenharmony_ci    let memorySwitch = this.shadowRoot?.querySelector('#memory-switch');
61fb726d48Sopenharmony_ci    let type: string;
62fb726d48Sopenharmony_ci    if (memorySwitch!.getAttribute('checked') !== null) {
63fb726d48Sopenharmony_ci      this.radioBox = this.shadowRoot?.querySelector('lit-radio[checked]');
64fb726d48Sopenharmony_ci      type = this.radioBox?.getAttribute('type') || '';
65fb726d48Sopenharmony_ci    } else {
66fb726d48Sopenharmony_ci      type = '-1';
67fb726d48Sopenharmony_ci    }
68fb726d48Sopenharmony_ci    return Number(type);
69fb726d48Sopenharmony_ci  }
70fb726d48Sopenharmony_ci
71fb726d48Sopenharmony_ci  get grabNumeric(): boolean {
72fb726d48Sopenharmony_ci    if (this.radioBoxType === 0) {
73fb726d48Sopenharmony_ci      this.spCheckDesBox = this.shadowRoot?.querySelector('#snapshot');
74fb726d48Sopenharmony_ci      let isChecked = this.spCheckDesBox?.getAttribute('checked');
75fb726d48Sopenharmony_ci      return isChecked === 'true';
76fb726d48Sopenharmony_ci    } else {
77fb726d48Sopenharmony_ci      return false;
78fb726d48Sopenharmony_ci    }
79fb726d48Sopenharmony_ci  }
80fb726d48Sopenharmony_ci
81fb726d48Sopenharmony_ci  get grabAllocations(): boolean {
82fb726d48Sopenharmony_ci    if (this.radioBoxType === 1) {
83fb726d48Sopenharmony_ci      this.spCheckDesBox = this.shadowRoot?.querySelector('#timeline');
84fb726d48Sopenharmony_ci      let isChecked = this.spCheckDesBox?.getAttribute('checked');
85fb726d48Sopenharmony_ci      return isChecked === 'true';
86fb726d48Sopenharmony_ci    } else {
87fb726d48Sopenharmony_ci      return false;
88fb726d48Sopenharmony_ci    }
89fb726d48Sopenharmony_ci  }
90fb726d48Sopenharmony_ci
91fb726d48Sopenharmony_ci  get intervalValue(): number {
92fb726d48Sopenharmony_ci    if (this.radioBoxType === 0) {
93fb726d48Sopenharmony_ci      return Number(this.interval!.value);
94fb726d48Sopenharmony_ci    } else {
95fb726d48Sopenharmony_ci      return 0;
96fb726d48Sopenharmony_ci    }
97fb726d48Sopenharmony_ci  }
98fb726d48Sopenharmony_ci
99fb726d48Sopenharmony_ci  get grabCpuProfiler(): boolean {
100fb726d48Sopenharmony_ci    let isChecked = this.cpuSwitch?.getAttribute('checked');
101fb726d48Sopenharmony_ci    return isChecked !== null;
102fb726d48Sopenharmony_ci  }
103fb726d48Sopenharmony_ci
104fb726d48Sopenharmony_ci  get intervalCpuValue(): number {
105fb726d48Sopenharmony_ci    let interval = this.shadowRoot?.querySelector<HTMLInputElement>('#cpuInterval');
106fb726d48Sopenharmony_ci    if (interval) {
107fb726d48Sopenharmony_ci      return Number(interval!.value);
108fb726d48Sopenharmony_ci    } else {
109fb726d48Sopenharmony_ci      return 0;
110fb726d48Sopenharmony_ci    }
111fb726d48Sopenharmony_ci  }
112fb726d48Sopenharmony_ci
113fb726d48Sopenharmony_ci  initElements(): void {
114fb726d48Sopenharmony_ci    this.interval = this.shadowRoot?.querySelector('#interval');
115fb726d48Sopenharmony_ci    this.processInput = this.shadowRoot?.querySelector<LitSelectV>('lit-select-v');
116fb726d48Sopenharmony_ci    let processInput = this.processInput?.shadowRoot?.querySelector('input') as HTMLDivElement;
117fb726d48Sopenharmony_ci    this.cpuSwitch = this.shadowRoot?.querySelector('#cpu-switch') as LitSwitch;
118fb726d48Sopenharmony_ci    processInput!.addEventListener('mousedown', () => {
119fb726d48Sopenharmony_ci      if (this.startSamp && (SpRecordTrace.serialNumber === '')) {
120fb726d48Sopenharmony_ci        this.processInput!.dataSource([], '');
121fb726d48Sopenharmony_ci      }
122fb726d48Sopenharmony_ci    });
123fb726d48Sopenharmony_ci    processInput!.addEventListener('mouseup', () => {
124fb726d48Sopenharmony_ci      if (this.startSamp) {
125fb726d48Sopenharmony_ci        if (SpRecordTrace.serialNumber === '') {
126fb726d48Sopenharmony_ci          this.processInput!.dataSource([], '');
127fb726d48Sopenharmony_ci        } else {
128fb726d48Sopenharmony_ci          Cmd.getDebugProcess().then((processList) => {
129fb726d48Sopenharmony_ci            if (processList.length > 0) {
130fb726d48Sopenharmony_ci              this.processInput!.dataSource(processList, '', true);
131fb726d48Sopenharmony_ci            } else {
132fb726d48Sopenharmony_ci              this.processInput!.dataSource([], '');
133fb726d48Sopenharmony_ci            }
134fb726d48Sopenharmony_ci          });
135fb726d48Sopenharmony_ci        }
136fb726d48Sopenharmony_ci        processInput!.removeAttribute('readonly');
137fb726d48Sopenharmony_ci      } else {
138fb726d48Sopenharmony_ci        processInput!.setAttribute('readonly', 'readonly');
139fb726d48Sopenharmony_ci        return;
140fb726d48Sopenharmony_ci      }
141fb726d48Sopenharmony_ci    });
142fb726d48Sopenharmony_ci    this.litSwitch = this.shadowRoot?.querySelector('lit-switch') as LitSwitch;
143fb726d48Sopenharmony_ci    this.memorySwitch = this.shadowRoot?.querySelector('#memory-switch') as LitSwitch;
144fb726d48Sopenharmony_ci    this.cpuSwitch = this.shadowRoot?.querySelector('#cpu-switch') as LitSwitch;
145fb726d48Sopenharmony_ci    this.disable();
146fb726d48Sopenharmony_ci    this.memoryDisable();
147fb726d48Sopenharmony_ci  }
148fb726d48Sopenharmony_ci
149fb726d48Sopenharmony_ci  intervalFocusoutHandler = (): void => {
150fb726d48Sopenharmony_ci    if (this.interval!.value === '') {
151fb726d48Sopenharmony_ci      this.interval!.value = '10';
152fb726d48Sopenharmony_ci    }
153fb726d48Sopenharmony_ci  };
154fb726d48Sopenharmony_ci
155fb726d48Sopenharmony_ci  litSwitchChangeHandler = (event: Event): void => {
156fb726d48Sopenharmony_ci    // @ts-ignore
157fb726d48Sopenharmony_ci    let detail = event.detail;
158fb726d48Sopenharmony_ci    if (detail.checked) {
159fb726d48Sopenharmony_ci      this.unDisable();
160fb726d48Sopenharmony_ci      this.unMemoryDisable();
161fb726d48Sopenharmony_ci    } else {
162fb726d48Sopenharmony_ci      this.disable();
163fb726d48Sopenharmony_ci      this.memoryDisable();
164fb726d48Sopenharmony_ci    }
165fb726d48Sopenharmony_ci  };
166fb726d48Sopenharmony_ci
167fb726d48Sopenharmony_ci  memorySwitchChangeHandler = (event: Event): void => {
168fb726d48Sopenharmony_ci    // @ts-ignore
169fb726d48Sopenharmony_ci    let detail = event.detail;
170fb726d48Sopenharmony_ci    if (detail.checked) {
171fb726d48Sopenharmony_ci      this.unMemoryDisable();
172fb726d48Sopenharmony_ci    } else {
173fb726d48Sopenharmony_ci      if (!this.cpuSwitch?.checked) {
174fb726d48Sopenharmony_ci        this.litSwitch!.checked = false;
175fb726d48Sopenharmony_ci        this.disable();
176fb726d48Sopenharmony_ci      }
177fb726d48Sopenharmony_ci      this.memoryDisable();
178fb726d48Sopenharmony_ci    }
179fb726d48Sopenharmony_ci  };
180fb726d48Sopenharmony_ci
181fb726d48Sopenharmony_ci  cpuSwitchChangeHandler = (event: Event): void => {
182fb726d48Sopenharmony_ci    // @ts-ignore
183fb726d48Sopenharmony_ci    let detail = event.detail;
184fb726d48Sopenharmony_ci    let interval = this.shadowRoot?.querySelectorAll<HTMLInputElement>('#cpuInterval');
185fb726d48Sopenharmony_ci    if (!detail.checked && !this.memorySwitch?.checked) {
186fb726d48Sopenharmony_ci      this.litSwitch!.checked = false;
187fb726d48Sopenharmony_ci      this.disable();
188fb726d48Sopenharmony_ci    } else if (detail.checked) {
189fb726d48Sopenharmony_ci      interval!.forEach((item) => {
190fb726d48Sopenharmony_ci        item.disabled = false;
191fb726d48Sopenharmony_ci        item.style.background = 'var(--dark-background5,#FFFFFF)';
192fb726d48Sopenharmony_ci      });
193fb726d48Sopenharmony_ci    } else {
194fb726d48Sopenharmony_ci      interval!.forEach((item) => {
195fb726d48Sopenharmony_ci        item.disabled = true;
196fb726d48Sopenharmony_ci        item.style.color = '#b7b7b7';
197fb726d48Sopenharmony_ci        item.style.background = 'var(--dark-background1,#f5f5f5)';
198fb726d48Sopenharmony_ci      });
199fb726d48Sopenharmony_ci      this.litSwitch!.checked = true;
200fb726d48Sopenharmony_ci      this.startSamp = true;
201fb726d48Sopenharmony_ci    }
202fb726d48Sopenharmony_ci  };
203fb726d48Sopenharmony_ci
204fb726d48Sopenharmony_ci  private memoryDisable(): void {
205fb726d48Sopenharmony_ci    let interval = this.shadowRoot?.querySelectorAll<HTMLInputElement>('#interval');
206fb726d48Sopenharmony_ci    interval!.forEach((item) => {
207fb726d48Sopenharmony_ci      item.disabled = true;
208fb726d48Sopenharmony_ci      item.style.color = '#b7b7b7';
209fb726d48Sopenharmony_ci      item.style.background = 'var(--dark-background1,#f5f5f5)';
210fb726d48Sopenharmony_ci    });
211fb726d48Sopenharmony_ci    let radioBoxes = this.shadowRoot?.querySelectorAll<LitRadioBox>('lit-radio');
212fb726d48Sopenharmony_ci    radioBoxes!.forEach((item) => {
213fb726d48Sopenharmony_ci      item.disabled = true;
214fb726d48Sopenharmony_ci    });
215fb726d48Sopenharmony_ci    let checkBoxes = this.shadowRoot?.querySelectorAll<SpCheckDesBox>('check-des-box');
216fb726d48Sopenharmony_ci    checkBoxes!.forEach((item) => {
217fb726d48Sopenharmony_ci      item.disabled = true;
218fb726d48Sopenharmony_ci    });
219fb726d48Sopenharmony_ci  }
220fb726d48Sopenharmony_ci
221fb726d48Sopenharmony_ci  private unMemoryDisable(): void {
222fb726d48Sopenharmony_ci    let interval = this.shadowRoot?.querySelectorAll<HTMLInputElement>('#interval');
223fb726d48Sopenharmony_ci    interval!.forEach((item) => {
224fb726d48Sopenharmony_ci      item.disabled = false;
225fb726d48Sopenharmony_ci      item.style.background = 'var(--dark-background5,#FFFFFF)';
226fb726d48Sopenharmony_ci    });
227fb726d48Sopenharmony_ci    let radioBoxes = this.shadowRoot?.querySelectorAll<LitRadioBox>('lit-radio');
228fb726d48Sopenharmony_ci    radioBoxes!.forEach((item) => {
229fb726d48Sopenharmony_ci      item.disabled = false;
230fb726d48Sopenharmony_ci    });
231fb726d48Sopenharmony_ci    let checkBoxes = this.shadowRoot?.querySelectorAll<SpCheckDesBox>('check-des-box');
232fb726d48Sopenharmony_ci    checkBoxes!.forEach((item) => {
233fb726d48Sopenharmony_ci      item.disabled = false;
234fb726d48Sopenharmony_ci    });
235fb726d48Sopenharmony_ci  }
236fb726d48Sopenharmony_ci
237fb726d48Sopenharmony_ci  private disable(): void {
238fb726d48Sopenharmony_ci    this.startSamp = false;
239fb726d48Sopenharmony_ci    this.processInput!.setAttribute('disabled', '');
240fb726d48Sopenharmony_ci    let heapConfigs = this.shadowRoot?.querySelectorAll<HTMLInputElement>('.select');
241fb726d48Sopenharmony_ci    heapConfigs!.forEach((item) => {
242fb726d48Sopenharmony_ci      item.disabled = true;
243fb726d48Sopenharmony_ci    });
244fb726d48Sopenharmony_ci    let switches = this.shadowRoot?.querySelectorAll<LitSwitch>('.switch');
245fb726d48Sopenharmony_ci    switches!.forEach((item) => {
246fb726d48Sopenharmony_ci      item.disabled = true;
247fb726d48Sopenharmony_ci      item.checked = false;
248fb726d48Sopenharmony_ci    });
249fb726d48Sopenharmony_ci    let interval = this.shadowRoot?.querySelectorAll<HTMLInputElement>('.inputstyle');
250fb726d48Sopenharmony_ci    interval!.forEach((item) => {
251fb726d48Sopenharmony_ci      item.disabled = true;
252fb726d48Sopenharmony_ci      item.style.color = '#b7b7b7';
253fb726d48Sopenharmony_ci      item.style.background = 'var(--dark-background1,#f5f5f5)';
254fb726d48Sopenharmony_ci    });
255fb726d48Sopenharmony_ci  }
256fb726d48Sopenharmony_ci
257fb726d48Sopenharmony_ci  private unDisable(): void {
258fb726d48Sopenharmony_ci    this.startSamp = true;
259fb726d48Sopenharmony_ci    this.processInput!.removeAttribute('disabled');
260fb726d48Sopenharmony_ci    let heapConfigs = this.shadowRoot?.querySelectorAll<HTMLInputElement>('.select');
261fb726d48Sopenharmony_ci    heapConfigs!.forEach((item) => {
262fb726d48Sopenharmony_ci      item.disabled = false;
263fb726d48Sopenharmony_ci    });
264fb726d48Sopenharmony_ci    let switches = this.shadowRoot?.querySelectorAll<LitSwitch>('.switch');
265fb726d48Sopenharmony_ci    switches!.forEach((item) => {
266fb726d48Sopenharmony_ci      item.disabled = false;
267fb726d48Sopenharmony_ci      item.checked = true;
268fb726d48Sopenharmony_ci    });
269fb726d48Sopenharmony_ci    let interval = this.shadowRoot?.querySelectorAll<HTMLInputElement>('.inputstyle');
270fb726d48Sopenharmony_ci    interval!.forEach((item) => {
271fb726d48Sopenharmony_ci      item.disabled = false;
272fb726d48Sopenharmony_ci      item.style.background = 'var(--dark-background5,#FFFFFF)';
273fb726d48Sopenharmony_ci    });
274fb726d48Sopenharmony_ci  }
275fb726d48Sopenharmony_ci
276fb726d48Sopenharmony_ci  connectedCallback(): void {
277fb726d48Sopenharmony_ci    let traceMode = this.shadowRoot!.querySelector('#traceMode') as HTMLDivElement;
278fb726d48Sopenharmony_ci    let isLongTrace = SpApplication.isLongTrace;
279fb726d48Sopenharmony_ci    if (isLongTrace) {
280fb726d48Sopenharmony_ci      traceMode!.style.display = 'block';
281fb726d48Sopenharmony_ci    } else {
282fb726d48Sopenharmony_ci      traceMode!.style.display = 'none';
283fb726d48Sopenharmony_ci    }
284fb726d48Sopenharmony_ci    this.interval!.addEventListener('focusout', this.intervalFocusoutHandler);
285fb726d48Sopenharmony_ci    this.litSwitch!.addEventListener('change', this.litSwitchChangeHandler);
286fb726d48Sopenharmony_ci    this.memorySwitch!.addEventListener('change', this.memorySwitchChangeHandler);
287fb726d48Sopenharmony_ci    this.cpuSwitch!.addEventListener('change', this.cpuSwitchChangeHandler);
288fb726d48Sopenharmony_ci  }
289fb726d48Sopenharmony_ci
290fb726d48Sopenharmony_ci  disconnectedCallback(): void {
291fb726d48Sopenharmony_ci    super.disconnectedCallback();
292fb726d48Sopenharmony_ci    this.interval!.removeEventListener('focusout', this.intervalFocusoutHandler);
293fb726d48Sopenharmony_ci    this.litSwitch!.removeEventListener('change', this.litSwitchChangeHandler);
294fb726d48Sopenharmony_ci    this.memorySwitch!.removeEventListener('change', this.memorySwitchChangeHandler);
295fb726d48Sopenharmony_ci    this.cpuSwitch!.removeEventListener('change', this.cpuSwitchChangeHandler);
296fb726d48Sopenharmony_ci  }
297fb726d48Sopenharmony_ci
298fb726d48Sopenharmony_ci  initHtml(): string {
299fb726d48Sopenharmony_ci    return SpArkTsHtml;
300fb726d48Sopenharmony_ci  }
301fb726d48Sopenharmony_ci}
302