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 '../BaseElement';
17fb726d48Sopenharmony_ci
18fb726d48Sopenharmony_ciconst initHtmlStyle: string = `
19fb726d48Sopenharmony_ci    <style>
20fb726d48Sopenharmony_ci            :host{
21fb726d48Sopenharmony_ci                user-select: none;
22fb726d48Sopenharmony_ci                display: flex;
23fb726d48Sopenharmony_ci                font-family: Helvetica;
24fb726d48Sopenharmony_ci                font-size: 14px;
25fb726d48Sopenharmony_ci                color: #000;
26fb726d48Sopenharmony_ci                text-align: left;
27fb726d48Sopenharmony_ci                line-height: 20px;
28fb726d48Sopenharmony_ci                font-weight: 400
29fb726d48Sopenharmony_ci                background-color: #FFFFFF;
30fb726d48Sopenharmony_ci                transition: background-color .3s;
31fb726d48Sopenharmony_ci            }
32fb726d48Sopenharmony_ci            :host(:not([disabled]):hover){
33fb726d48Sopenharmony_ci                display: flex;
34fb726d48Sopenharmony_ci                background-color: var(--dark-background8,#6C9BFA);
35fb726d48Sopenharmony_ci                color: #FFFFFF;
36fb726d48Sopenharmony_ci                cursor: pointer;
37fb726d48Sopenharmony_ci            }
38fb726d48Sopenharmony_ci            :host([disabled]:hover){
39fb726d48Sopenharmony_ci                display: flex;
40fb726d48Sopenharmony_ci                /*background-color:#3391FF;*/
41fb726d48Sopenharmony_ci                /*color: #FFFFFF;*/
42fb726d48Sopenharmony_ci                cursor:not-allowed;
43fb726d48Sopenharmony_ci            }
44fb726d48Sopenharmony_ci            :host([disabled]) .root{
45fb726d48Sopenharmony_ci                cursor:not-allowed;
46fb726d48Sopenharmony_ci                display: flex;
47fb726d48Sopenharmony_ci                align-items: center;
48fb726d48Sopenharmony_ci                padding: 10px 24px;
49fb726d48Sopenharmony_ci                width: 100%;
50fb726d48Sopenharmony_ci            }
51fb726d48Sopenharmony_ci            :host(:not([disabled])) .root{
52fb726d48Sopenharmony_ci                cursor:pointer;
53fb726d48Sopenharmony_ci                display: flex;
54fb726d48Sopenharmony_ci                align-items: center;
55fb726d48Sopenharmony_ci                padding: 10px 24px;
56fb726d48Sopenharmony_ci                width: 100%;
57fb726d48Sopenharmony_ci            }
58fb726d48Sopenharmony_ci            .name{
59fb726d48Sopenharmony_ci                padding-left: 10px;
60fb726d48Sopenharmony_ci                cursor: pointer;
61fb726d48Sopenharmony_ci                overflow-wrap: anywhere;
62fb726d48Sopenharmony_ci            }
63fb726d48Sopenharmony_ci            .icon{
64fb726d48Sopenharmony_ci                pointer-events: none;
65fb726d48Sopenharmony_ci            }
66fb726d48Sopenharmony_ci            :host(:not([file])) .name{
67fb726d48Sopenharmony_ci                pointer-events: none;
68fb726d48Sopenharmony_ci            }
69fb726d48Sopenharmony_ci            :host(:not([file])) .root{
70fb726d48Sopenharmony_ci                pointer-events: none;
71fb726d48Sopenharmony_ci            }
72fb726d48Sopenharmony_ci            :host([file]) .name{
73fb726d48Sopenharmony_ci                pointer-events: none;
74fb726d48Sopenharmony_ci            }
75fb726d48Sopenharmony_ci            :host([file]) .icon{
76fb726d48Sopenharmony_ci                pointer-events: none;
77fb726d48Sopenharmony_ci            }
78fb726d48Sopenharmony_ci            
79fb726d48Sopenharmony_ci            :host([back]) {
80fb726d48Sopenharmony_ci                background-color: var(--dark-background8,#6C9BFA);
81fb726d48Sopenharmony_ci            } 
82fb726d48Sopenharmony_ci            
83fb726d48Sopenharmony_ci        </style>
84fb726d48Sopenharmony_ci    `;
85fb726d48Sopenharmony_ci
86fb726d48Sopenharmony_ci@element('lit-main-menu-item')
87fb726d48Sopenharmony_ciexport class LitMainMenuItem extends BaseElement {
88fb726d48Sopenharmony_ci  private titleEl: HTMLElement | null | undefined;
89fb726d48Sopenharmony_ci  private rootEL: HTMLElement | null | undefined;
90fb726d48Sopenharmony_ci  private iconEl: HTMLElement | null | undefined;
91fb726d48Sopenharmony_ci  private fileEL: HTMLInputElement | undefined | null;
92fb726d48Sopenharmony_ci
93fb726d48Sopenharmony_ci  static get observedAttributes(): string[] {
94fb726d48Sopenharmony_ci    return ['title', 'icon', 'file', 'multi', 'disabled'];
95fb726d48Sopenharmony_ci  }
96fb726d48Sopenharmony_ci
97fb726d48Sopenharmony_ci  get title(): string {
98fb726d48Sopenharmony_ci    return this.getAttribute('title') || '';
99fb726d48Sopenharmony_ci  }
100fb726d48Sopenharmony_ci
101fb726d48Sopenharmony_ci  set title(val: string) {
102fb726d48Sopenharmony_ci    this.setAttribute('title', val);
103fb726d48Sopenharmony_ci  }
104fb726d48Sopenharmony_ci
105fb726d48Sopenharmony_ci  get multi(): boolean {
106fb726d48Sopenharmony_ci    return this.hasAttribute('multi');
107fb726d48Sopenharmony_ci  }
108fb726d48Sopenharmony_ci
109fb726d48Sopenharmony_ci  set multi(val: boolean) {
110fb726d48Sopenharmony_ci    if (val) {
111fb726d48Sopenharmony_ci      this.setAttribute('multi', '');
112fb726d48Sopenharmony_ci    } else {
113fb726d48Sopenharmony_ci      this.removeAttribute('multi');
114fb726d48Sopenharmony_ci    }
115fb726d48Sopenharmony_ci  }
116fb726d48Sopenharmony_ci
117fb726d48Sopenharmony_ci  get disabled(): boolean {
118fb726d48Sopenharmony_ci    return this.hasAttribute('disabled');
119fb726d48Sopenharmony_ci  }
120fb726d48Sopenharmony_ci
121fb726d48Sopenharmony_ci  set disabled(val: boolean) {
122fb726d48Sopenharmony_ci    if (val) {
123fb726d48Sopenharmony_ci      this.setAttribute('disabled', val.toString());
124fb726d48Sopenharmony_ci      this.fileEL?.setAttribute('disabled', val.toString());
125fb726d48Sopenharmony_ci    } else {
126fb726d48Sopenharmony_ci      this.removeAttribute('disabled');
127fb726d48Sopenharmony_ci      this.fileEL?.removeAttribute('disabled');
128fb726d48Sopenharmony_ci    }
129fb726d48Sopenharmony_ci  }
130fb726d48Sopenharmony_ci
131fb726d48Sopenharmony_ci  get back(): boolean {
132fb726d48Sopenharmony_ci    return this.hasAttribute('back');
133fb726d48Sopenharmony_ci  }
134fb726d48Sopenharmony_ci
135fb726d48Sopenharmony_ci  set back(isShowBack: boolean) {
136fb726d48Sopenharmony_ci    if (isShowBack) {
137fb726d48Sopenharmony_ci      this.setAttribute('back', '');
138fb726d48Sopenharmony_ci    } else {
139fb726d48Sopenharmony_ci      this.removeAttribute('back');
140fb726d48Sopenharmony_ci    }
141fb726d48Sopenharmony_ci  }
142fb726d48Sopenharmony_ci
143fb726d48Sopenharmony_ci  initElements(): void {
144fb726d48Sopenharmony_ci    this.rootEL = this.shadowRoot?.querySelector('.root');
145fb726d48Sopenharmony_ci    this.titleEl = this.shadowRoot?.querySelector('.name');
146fb726d48Sopenharmony_ci    this.iconEl = this.shadowRoot?.querySelector('.icon');
147fb726d48Sopenharmony_ci    this.fileEL = this.shadowRoot?.querySelector('.file');
148fb726d48Sopenharmony_ci  }
149fb726d48Sopenharmony_ci
150fb726d48Sopenharmony_ci  isFile(): boolean {
151fb726d48Sopenharmony_ci    if (this.hasAttribute('file')) {
152fb726d48Sopenharmony_ci      if (this.fileEL) {
153fb726d48Sopenharmony_ci        return true;
154fb726d48Sopenharmony_ci      }
155fb726d48Sopenharmony_ci    }
156fb726d48Sopenharmony_ci    return false;
157fb726d48Sopenharmony_ci  }
158fb726d48Sopenharmony_ci
159fb726d48Sopenharmony_ci  connectedCallback(): void {
160fb726d48Sopenharmony_ci    if (this.hasAttribute('file')) {
161fb726d48Sopenharmony_ci      if (this.fileEL) {
162fb726d48Sopenharmony_ci        this.fileEL!.addEventListener('change', (event) => {
163fb726d48Sopenharmony_ci          let files = this.fileEL!.files;
164fb726d48Sopenharmony_ci          if (files && files.length > 0) {
165fb726d48Sopenharmony_ci            if (this.titleEl!.textContent!.includes('long trace') || this.multi) {
166fb726d48Sopenharmony_ci              this.dispatchEvent(
167fb726d48Sopenharmony_ci                new CustomEvent('file-change', {
168fb726d48Sopenharmony_ci                  // @ts-ignore
169fb726d48Sopenharmony_ci                  target: this,
170fb726d48Sopenharmony_ci                  // @ts-ignore
171fb726d48Sopenharmony_ci                  detail: event.target!.files,
172fb726d48Sopenharmony_ci                })
173fb726d48Sopenharmony_ci              );
174fb726d48Sopenharmony_ci            } else {
175fb726d48Sopenharmony_ci              this.dispatchEvent(
176fb726d48Sopenharmony_ci                new CustomEvent('file-change', {
177fb726d48Sopenharmony_ci                  // @ts-ignore
178fb726d48Sopenharmony_ci                  target: this,
179fb726d48Sopenharmony_ci                  detail: files[0],
180fb726d48Sopenharmony_ci                })
181fb726d48Sopenharmony_ci              );
182fb726d48Sopenharmony_ci            }
183fb726d48Sopenharmony_ci            if (this.fileEL) {
184fb726d48Sopenharmony_ci              this.fileEL.value = '';
185fb726d48Sopenharmony_ci            }
186fb726d48Sopenharmony_ci            if (this.fileEL) {
187fb726d48Sopenharmony_ci              this.fileEL.value = '';
188fb726d48Sopenharmony_ci            }
189fb726d48Sopenharmony_ci          }
190fb726d48Sopenharmony_ci        });
191fb726d48Sopenharmony_ci      }
192fb726d48Sopenharmony_ci      this.addEventListener('click', (e) => {
193fb726d48Sopenharmony_ci        e.stopPropagation();
194fb726d48Sopenharmony_ci      });
195fb726d48Sopenharmony_ci    }
196fb726d48Sopenharmony_ci  }
197fb726d48Sopenharmony_ci
198fb726d48Sopenharmony_ci  initHtml(): string {
199fb726d48Sopenharmony_ci    return `
200fb726d48Sopenharmony_ci        ${initHtmlStyle}
201fb726d48Sopenharmony_ci        <input id="file" class="file" type="file" style="display:none;pointer-events: none" />
202fb726d48Sopenharmony_ci        <label class="root" for="file">
203fb726d48Sopenharmony_ci            <lit-icon class="icon" name="user" size="20"></lit-icon>
204fb726d48Sopenharmony_ci            <label class="name"></label>
205fb726d48Sopenharmony_ci        </label>
206fb726d48Sopenharmony_ci        `;
207fb726d48Sopenharmony_ci  }
208fb726d48Sopenharmony_ci
209fb726d48Sopenharmony_ci  attributeChangedCallback(name: string, oldValue: string, newValue: string): void {
210fb726d48Sopenharmony_ci    switch (name) {
211fb726d48Sopenharmony_ci      case 'title':
212fb726d48Sopenharmony_ci        if (this.titleEl) {
213fb726d48Sopenharmony_ci          this.titleEl.textContent = newValue;
214fb726d48Sopenharmony_ci          if (newValue.includes('long trace')) {
215fb726d48Sopenharmony_ci            this.fileEL!.setAttribute('multiple', '');
216fb726d48Sopenharmony_ci            this.fileEL!.setAttribute('webkitdirectory', '');
217fb726d48Sopenharmony_ci            this.fileEL!.setAttribute('directory', '');
218fb726d48Sopenharmony_ci          } else {
219fb726d48Sopenharmony_ci            if (this.fileEL!.hasAttribute('multiple')) {
220fb726d48Sopenharmony_ci              this.fileEL!.removeAttribute('multiple');
221fb726d48Sopenharmony_ci              this.fileEL!.removeAttribute('webkitdirectory');
222fb726d48Sopenharmony_ci              this.fileEL!.removeAttribute('directory');
223fb726d48Sopenharmony_ci            }
224fb726d48Sopenharmony_ci          }
225fb726d48Sopenharmony_ci        }
226fb726d48Sopenharmony_ci        break;
227fb726d48Sopenharmony_ci      case 'multi':
228fb726d48Sopenharmony_ci        if (this.hasAttribute('multi')) {
229fb726d48Sopenharmony_ci          this.fileEL!.setAttribute('multiple', '');
230fb726d48Sopenharmony_ci          this.fileEL!.setAttribute('webkitdirectory', '');
231fb726d48Sopenharmony_ci          this.fileEL!.setAttribute('directory', '');
232fb726d48Sopenharmony_ci        } else {
233fb726d48Sopenharmony_ci          this.fileEL!.removeAttribute('multiple');
234fb726d48Sopenharmony_ci          this.fileEL!.removeAttribute('webkitdirectory');
235fb726d48Sopenharmony_ci          this.fileEL!.removeAttribute('directory');
236fb726d48Sopenharmony_ci        }
237fb726d48Sopenharmony_ci        break;
238fb726d48Sopenharmony_ci      case 'icon':
239fb726d48Sopenharmony_ci        if (this.iconEl) {
240fb726d48Sopenharmony_ci          this.iconEl.setAttribute('name', newValue);
241fb726d48Sopenharmony_ci        }
242fb726d48Sopenharmony_ci        break;
243fb726d48Sopenharmony_ci    }
244fb726d48Sopenharmony_ci  }
245fb726d48Sopenharmony_ci}
246