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