• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  public 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  get isStartArkts(): boolean {
114    return this.litSwitch!.checked;
115  }
116
117  get isStartCpuProfiler(): boolean {
118    return this.cpuSwitch!.checked;
119  }
120
121  get isStartMemoryProfiler(): boolean {
122    return this.memorySwitch!.checked;
123  }
124
125  initElements(): void {
126    this.interval = this.shadowRoot?.querySelector('#interval');
127    this.processInput = this.shadowRoot?.querySelector<LitSelectV>('lit-select-v');
128    let processInput = this.processInput?.shadowRoot?.querySelector('input') as HTMLDivElement;
129    this.cpuSwitch = this.shadowRoot?.querySelector('#cpu-switch') as LitSwitch;
130    processInput!.addEventListener('mousedown', () => {
131      if (this.startSamp && (SpRecordTrace.serialNumber === '')) {
132        this.processInput!.dataSource([], '');
133      }
134    });
135    processInput!.addEventListener('mouseup', () => {
136      if (this.startSamp) {
137        if (SpRecordTrace.serialNumber === '') {
138          this.processInput!.dataSource([], '');
139        } else {
140          if (SpRecordTrace.useExtend) {
141            Cmd.getPackage().then((processList) => {
142              if (processList.length > 0) {
143                this.processInput!.dataSource(processList, '', true);
144              } else {
145                this.processInput!.dataSource([], '');
146              }
147            });
148          } else {
149            Cmd.getDebugProcess().then((processList) => {
150              if (processList.length > 0) {
151                this.processInput!.dataSource(processList, '', true);
152              } else {
153                this.processInput!.dataSource([], '');
154              }
155            });
156          }
157        }
158        processInput!.removeAttribute('readonly');
159      } else {
160        processInput!.setAttribute('readonly', 'readonly');
161        return;
162      }
163    });
164    this.litSwitch = this.shadowRoot?.querySelector('lit-switch') as LitSwitch;
165    this.memorySwitch = this.shadowRoot?.querySelector('#memory-switch') as LitSwitch;
166    this.cpuSwitch = this.shadowRoot?.querySelector('#cpu-switch') as LitSwitch;
167    this.disable();
168    this.memoryDisable();
169  }
170
171  intervalFocusoutHandler = (): void => {
172    if (this.interval!.value === '') {
173      this.interval!.value = '10';
174    }
175  };
176
177  litSwitchChangeHandler = (event: Event): void => {
178    // @ts-ignore
179    let detail = event.detail;
180    this.dispatchEvent(new CustomEvent('showTip', {}));
181    if (!SpRecordTrace.useExtend) {
182      setTimeout(() => {
183        this.litSwitch!.checked = false;
184        this.disable();
185        this.memoryDisable();
186      }, 500);
187      return;
188    }
189
190    if (detail.checked) {
191      this.unDisable();
192      this.unMemoryDisable();
193    } else {
194      this.disable();
195      this.memoryDisable();
196    }
197  };
198
199  memorySwitchChangeHandler = (event: Event): void => {
200    // @ts-ignore
201    let detail = event.detail;
202    if (detail.checked) {
203      this.unMemoryDisable();
204    } else {
205      if (!this.cpuSwitch?.checked) {
206        this.litSwitch!.checked = false;
207        this.disable();
208      }
209      this.memoryDisable();
210    }
211  };
212
213  cpuSwitchChangeHandler = (event: Event): void => {
214    // @ts-ignore
215    let detail = event.detail;
216    let interval = this.shadowRoot?.querySelectorAll<HTMLInputElement>('#cpuInterval');
217    if (!detail.checked && !this.memorySwitch?.checked) {
218      this.litSwitch!.checked = false;
219      this.disable();
220    } else if (detail.checked) {
221      interval!.forEach((item) => {
222        item.disabled = false;
223        item.style.background = 'var(--dark-background5,#FFFFFF)';
224      });
225    } else {
226      interval!.forEach((item) => {
227        item.disabled = true;
228        item.style.color = '#b7b7b7';
229        item.style.background = 'var(--dark-background1,#f5f5f5)';
230      });
231      this.litSwitch!.checked = true;
232      this.startSamp = true;
233    }
234  };
235
236  public memoryDisable(): void {
237    let interval = this.shadowRoot?.querySelectorAll<HTMLInputElement>('#interval');
238    interval!.forEach((item) => {
239      item.disabled = true;
240      item.style.color = '#b7b7b7';
241      item.style.background = 'var(--dark-background1,#f5f5f5)';
242    });
243    let radioBoxes = this.shadowRoot?.querySelectorAll<LitRadioBox>('lit-radio');
244    radioBoxes!.forEach((item) => {
245      item.disabled = true;
246    });
247    let checkBoxes = this.shadowRoot?.querySelectorAll<SpCheckDesBox>('check-des-box');
248    checkBoxes!.forEach((item) => {
249      item.disabled = true;
250    });
251  }
252
253  private unMemoryDisable(): void {
254    let interval = this.shadowRoot?.querySelectorAll<HTMLInputElement>('#interval');
255    interval!.forEach((item) => {
256      item.disabled = false;
257      item.style.background = 'var(--dark-background5,#FFFFFF)';
258    });
259    let radioBoxes = this.shadowRoot?.querySelectorAll<LitRadioBox>('lit-radio');
260    radioBoxes!.forEach((item) => {
261      item.disabled = false;
262    });
263    let checkBoxes = this.shadowRoot?.querySelectorAll<SpCheckDesBox>('check-des-box');
264    checkBoxes!.forEach((item) => {
265      item.disabled = false;
266    });
267  }
268
269  public disable(): void {
270    this.startSamp = false;
271    this.processInput!.setAttribute('disabled', '');
272    let heapConfigs = this.shadowRoot?.querySelectorAll<HTMLInputElement>('.select');
273    heapConfigs!.forEach((item) => {
274      item.disabled = true;
275    });
276    let switches = this.shadowRoot?.querySelectorAll<LitSwitch>('.switch');
277    switches!.forEach((item) => {
278      item.disabled = true;
279      item.checked = false;
280    });
281    let interval = this.shadowRoot?.querySelectorAll<HTMLInputElement>('.inputstyle');
282    interval!.forEach((item) => {
283      item.disabled = true;
284      item.style.color = '#b7b7b7';
285      item.style.background = 'var(--dark-background1,#f5f5f5)';
286    });
287  }
288
289  private unDisable(): void {
290    this.startSamp = true;
291    this.processInput!.removeAttribute('disabled');
292    let heapConfigs = this.shadowRoot?.querySelectorAll<HTMLInputElement>('.select');
293    heapConfigs!.forEach((item) => {
294      item.disabled = false;
295    });
296    let switches = this.shadowRoot?.querySelectorAll<LitSwitch>('.switch');
297    switches!.forEach((item) => {
298      item.disabled = false;
299      item.checked = true;
300    });
301    let interval = this.shadowRoot?.querySelectorAll<HTMLInputElement>('.inputstyle');
302    interval!.forEach((item) => {
303      item.disabled = false;
304      item.style.background = 'var(--dark-background5,#FFFFFF)';
305    });
306  }
307
308  connectedCallback(): void {
309    let traceMode = this.shadowRoot!.querySelector('#traceMode') as HTMLDivElement;
310    let isLongTrace = SpApplication.isLongTrace;
311    if (isLongTrace) {
312      traceMode!.style.display = 'block';
313    } else {
314      traceMode!.style.display = 'none';
315    }
316    this.interval!.addEventListener('focusout', this.intervalFocusoutHandler);
317    this.litSwitch!.addEventListener('change', this.litSwitchChangeHandler);
318    this.memorySwitch!.addEventListener('change', this.memorySwitchChangeHandler);
319    this.cpuSwitch!.addEventListener('change', this.cpuSwitchChangeHandler);
320  }
321
322  disconnectedCallback(): void {
323    super.disconnectedCallback();
324    this.interval!.removeEventListener('focusout', this.intervalFocusoutHandler);
325    this.litSwitch!.removeEventListener('change', this.litSwitchChangeHandler);
326    this.memorySwitch!.removeEventListener('change', this.memorySwitchChangeHandler);
327    this.cpuSwitch!.removeEventListener('change', this.cpuSwitchChangeHandler);
328  }
329
330  initHtml(): string {
331    return SpArkTsHtml;
332  }
333}
334