• 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 LitSwitch from '../../../base-ui/switch/lit-switch';
18import '../../../base-ui/select/LitAllocationSelect';
19import '../../../base-ui/switch/lit-switch';
20import { SpXPowerRecordHtml } from './SpXPowerRecord.html';
21import { LitSelectV } from '../../../base-ui/select/LitSelectV';
22import { SpApplication } from '../../SpApplication';
23import { Cmd } from '../../../command/Cmd';
24import { SpRecordTrace } from '../SpRecordTrace';
25import {
26  messageTypeAll,
27  realBattery,
28  thermalReport,
29  appDetail,
30  appStatistic,
31  componentTop,
32} from './utils/PluginConvertUtils';
33
34@element('sp-xpower')
35export class SpXPowerRecord extends BaseElement {
36  private xpowerSwitch: LitSwitch | undefined | null;
37  private configType: HTMLDivElement | undefined | null;
38  private typeSelect: LitSelectV | undefined | null;
39  private sp: SpApplication | undefined;
40  private inputEvent: HTMLInputElement | undefined;
41  private xPowerSelectV: LitSelectV | undefined | null;
42  get recordXPower(): boolean {
43    return this.xpowerSwitch!.checked;
44  }
45
46  get process(): string {
47    if (this.xPowerSelectV!.value.length > 0) {
48      if (this.xPowerSelectV!.value === 'none') {
49        return '';
50      } else {
51        return this.xPowerSelectV!.value;
52      }
53    }
54    return '';
55  }
56
57  initElements(): void {
58    this.initRecordXpowerConfig();
59    this.sp = document.querySelector('sp-application') as SpApplication;
60    this.typeSelect = this.shadowRoot?.querySelector<LitSelectV>("lit-select-v[title='MessageType']");
61    this.typeSelect!.showItems = [realBattery, thermalReport];
62    this.inputEvent = this.typeSelect!.shadowRoot?.querySelector('input') as HTMLInputElement;
63    this.xpowerSwitch = this.shadowRoot?.querySelector('.xpower-switch') as LitSwitch;
64    let xpowerConfigList = this.shadowRoot?.querySelectorAll<HTMLDivElement>('.xpower-config-top');
65    this.xPowerSelectV = this.shadowRoot?.querySelector<LitSelectV>('lit-select-v');
66    let xPowerInput = this.xPowerSelectV?.shadowRoot?.querySelector('input') as HTMLInputElement;
67    xPowerInput!.addEventListener('mousedown', () => {
68      if (this.xpowerSwitch!.checked && SpRecordTrace.serialNumber === '') {
69        this.xPowerSelectV!.dataSource([], '');
70      }
71    });
72    xPowerInput!.addEventListener('mouseup', () => {
73      if (this.xpowerSwitch!.checked) {
74        if (SpRecordTrace.serialNumber === '') {
75          this.xPowerSelectV!.dataSource([], '');
76          xPowerInput!.removeAttribute('readonly');
77          return;
78        } else {
79          Cmd.getPackage().then((packageList) => {
80            if (packageList.length > 0) {
81              packageList.unshift('none');
82              this.xPowerSelectV!.dataSource(packageList, '', true);
83              this.getSelectedOption();
84            } else {
85              this.xPowerSelectV!.dataSource([], '');
86            }
87          });
88          xPowerInput!.setAttribute('readonly', 'readonly');
89        }
90      }
91    });
92
93    this.xpowerSwitch.addEventListener('change', () => {
94      let configVisibility = 'none';
95      if (this.xpowerSwitch?.checked) {
96        configVisibility = 'block';
97      }
98      if (xpowerConfigList) {
99        xpowerConfigList!.forEach((configEl) => {
100          configEl.style.display = configVisibility;
101        });
102      }
103    });
104  }
105
106  private initRecordXpowerConfig(): void {
107    this.configType = this.shadowRoot?.querySelectorAll<HTMLDivElement>('.xpower-config-top')[1];
108    xpowerConfigList.forEach((config) => {
109      switch (config.type) {
110        case 'select-multiple':
111          this.configTypeBySelectMultiple(config, this.configType!);
112          break;
113        default:
114          break;
115      }
116    });
117  }
118
119  private configTypeBySelectMultiple(config: unknown, recordXpowerDiv: HTMLDivElement): void {
120    let html = '';
121    //@ts-ignore
122    let placeholder = config.selectArray[0];
123    //@ts-ignore
124    if (config.title === 'MessageType') {
125      placeholder = [realBattery, thermalReport].join(',');
126    }
127    html += `<lit-select-v default-value="" rounded="" class="record-type-select config"
128    mode="multiple" canInsert="" title="${
129      //@ts-ignore
130      config.title
131      }" rounded placement = "bottom" placeholder="${placeholder}">`;
132    //@ts-ignore
133    config.selectArray.forEach((value: string) => {
134      html += `<lit-select-option value="${value}">${value}</lit-select-option>`;
135    });
136    html += '</lit-select-v>';
137    recordXpowerDiv.innerHTML = recordXpowerDiv.innerHTML + html;
138  }
139
140  getXpowerConfig(): string | undefined {
141    let recordXpowerConfigVal = this.shadowRoot?.querySelectorAll<HTMLElement>('.config');
142    let xpowerConfig: string = '';
143    recordXpowerConfigVal!.forEach((value) => {
144      xpowerConfig = this.getXpowerConfigData(value, xpowerConfig);
145    });
146    return xpowerConfig;
147  }
148
149  private getXpowerConfigData(value: HTMLElement, xpowerConfig: string): string {
150    switch (value.title) {
151      case 'MessageType':
152        xpowerConfig = this.xpowerConfigByTypeList(xpowerConfig, value as LitSelectV);
153        break;
154    }
155    return xpowerConfig;
156  }
157
158  private xpowerConfigByTypeList(xpowerConfig: string, selectValue: LitSelectV): string {
159    xpowerConfig = selectValue.value;
160    return xpowerConfig;
161  }
162
163  private getSelectedOption(): void {
164    this.xPowerSelectV!.shadowRoot?.querySelectorAll('lit-select-option').forEach((a) => {
165      a.addEventListener('onSelected', (e: unknown) => {
166        if (a.hasAttribute('selected')) {
167          if (this.xPowerSelectV!.value === '' || this.xPowerSelectV!.value === 'none') {
168            let messageValue = this.typeSelect!.value || '';
169            if (messageValue.length > 0) {// @ts-ignore
170              let selectedOptions = messageValue.split(',').map((option: unknown) => option.trim());
171              let filteredOptions = selectedOptions.filter(// @ts-ignore
172                (option: unknown) => ![appStatistic, appDetail].includes(option)
173              );
174              messageValue = filteredOptions.join(',');
175              this.inputEvent!.value = messageValue;
176              this.typeSelect!.showItems = filteredOptions;
177            }
178          }
179        }
180      });
181    });
182  }
183
184  typeSelectMousedownHandler = (): void => {
185    this.typeSelect!.dataSource([], '');
186  };
187
188  typeSelectClickHandler = (): void => {
189    let messageType = [];
190    if (this.xPowerSelectV!.value === '' || this.xPowerSelectV!.value === 'none') {
191      messageType = [realBattery, thermalReport, componentTop];
192    } else {
193      messageType = messageTypeAll;
194    }
195    this.typeSelect?.dataSource(messageType, '');
196    this.inputEvent!.value = this.typeSelect!.showItems.join(',');
197    this.typeSelect?.shadowRoot?.querySelectorAll('lit-select-option').forEach((option) => {
198      if (this.inputEvent!.value.includes(option.getAttribute('value') || '')) {
199        option.setAttribute('selected', '');
200      } else {
201        option.removeAttribute('selected');
202        let number = this.typeSelect!.showItems.indexOf(option.getAttribute('value') || '');
203        if (number > -1) {
204          this.typeSelect!.showItems!.splice(number, 1);
205        }
206      }
207      option.addEventListener('onSelected', (e: unknown) => {
208        if (this.typeSelect!.showItems.length === 0) {
209          this.inputEvent!.placeholder = 'NONE';
210        }
211      });
212    });
213  };
214
215  connectedCallback(): void {
216    this.inputEvent?.addEventListener('mousedown', this.typeSelectMousedownHandler);
217    this.inputEvent?.addEventListener('click', this.typeSelectClickHandler);
218  }
219
220  disconnectedCallback(): void {
221    super.disconnectedCallback();
222    this.inputEvent?.removeEventListener('mousedown', this.typeSelectMousedownHandler);
223    this.inputEvent?.removeEventListener('click', this.typeSelectClickHandler);
224  }
225
226  attributeChangedCallback(name: string, oldValue: string, newValue: string): void {
227    super.attributeChangedCallback(name, oldValue, newValue);
228  }
229
230  initHtml(): string {
231    return SpXPowerRecordHtml;
232  }
233}
234
235const xpowerConfigList = [
236  {
237    title: 'MessageType',
238    des: 'Select MessageType',
239    hidden: true,
240    type: 'select-multiple',
241    selectArray: [''],
242  },
243];
244