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