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 '../BaseElement.js'; 17import './LitMainMenuItem.js'; 18import './LitMainMenuGroup.js'; 19import { LitMainMenuGroup } from './LitMainMenuGroup.js'; 20import { LitMainMenuItem } from './LitMainMenuItem.js'; 21let backgroundColor = sessionStorage.getItem('backgroundColor'); 22 23@element('lit-main-menu') 24export class LitMainMenu extends BaseElement { 25 private slotElements: Element[] | undefined; 26 private _menus: Array<MenuGroup> | undefined; 27 28 static get observedAttributes() { 29 return []; 30 } 31 32 get menus(): Array<MenuGroup> | undefined { 33 return this._menus; 34 } 35 36 set menus(value: Array<MenuGroup> | undefined) { 37 this._menus = value; 38 this.shadowRoot?.querySelectorAll('lit-main-menu-group').forEach((a) => a.remove()); 39 let menuBody = this.shadowRoot?.querySelector('.menu-body'); 40 value?.forEach((it) => { 41 let group = new LitMainMenuGroup(); 42 group.setAttribute('title', it.title || ''); 43 group.setAttribute('describe', it.describe || ''); 44 if (it.collapsed) { 45 group.setAttribute('collapsed', ''); 46 } else { 47 group.removeAttribute('collapsed'); 48 } 49 menuBody?.appendChild(group); 50 it.children?.forEach((item: any) => { 51 let th = new LitMainMenuItem(); 52 th.setAttribute('icon', item.icon || ''); 53 th.setAttribute('title', item.title || ''); 54 if (item.fileChoose) { 55 th.setAttribute('file', ''); 56 th.addEventListener('file-change', (e) => { 57 if (item.fileHandler && !th.disabled) { 58 item.fileHandler(e); 59 } 60 }); 61 } else { 62 th.removeAttribute('file'); 63 th.addEventListener('click', (e) => { 64 if (item.clickHandler && !th.disabled) { 65 item.clickHandler(item); 66 } 67 }); 68 } 69 if (item.disabled != undefined) { 70 th.disabled = item.disabled; 71 } 72 group?.appendChild(th); 73 }); 74 }); 75 } 76 77 initElements(): void { 78 let st: HTMLSlotElement | null | undefined = this.shadowRoot?.querySelector('#st'); 79 st?.addEventListener('slotchange', (e) => { 80 this.slotElements = st?.assignedElements(); 81 this.slotElements?.forEach((it) => { 82 it.querySelectorAll('lit-main-menu-item').forEach((cell) => {}); 83 }); 84 }); 85 let versionDiv: HTMLElement | null | undefined = this.shadowRoot?.querySelector<HTMLElement>('.version'); 86 versionDiv!.innerText = (window as any).version || ''; 87 } 88 89 initHtml(): string { 90 return ` 91 <style> 92 :host{ 93 width: 248px; 94 height: 100vh; 95 display: flex; 96 flex-direction: column; 97 background-color: ${backgroundColor}; 98 } 99 .menu-body ::-webkit-scrollbar-thumb 100 { 101 background-color: var(--dark-background,#FFFFFF); 102 border-radius:10px; 103 104 } 105 .menu-body ::-webkit-scrollbar-track 106 { 107 border-radius:10px; 108 background-color:#F5F5F5; 109 110 } 111 .header{ 112 display: grid; 113 width: 100%; 114 height: 56px; 115 font-size: 1.4rem; 116 padding-left: 20px; 117 gap: 0 20px; 118 box-sizing: border-box; 119 grid-template-columns: min-content 1fr min-content; 120 grid-template-rows: auto; 121 color: #47A7E0; 122 background-color: var(--dark-background1); 123 border-bottom: 1px solid var(--dark-background1,#EFEFEF); 124 } 125 .bottom{ 126 width: 100%; 127 display: flex; 128 justify-content: space-between; 129 } 130 .header *{ 131 user-select: none; 132 align-self: center; 133 } 134 .version{ 135 width: 15rem; 136 padding: 20px 0; 137 text-align: center; 138 color: #94979d; 139 font-size: 0.6rem; 140 } 141 .color{ 142 cursor: pointer; 143 font-size: 0.6rem; 144 padding: 20px; 145 } 146 *{ 147 box-sizing: border-box; 148 } 149 .menu-button{ 150 display: flex; 151 align-content: center; 152 justify-content: right; 153 cursor: pointer; 154 height: 47px; 155 width: 48px; 156 } 157 </style> 158 <div class="header" name="header"> 159 <img src="img/logo.png"/> 160 <div class="menu-button"> 161 <lit-icon name="menu" size="20" color="var(blue,#4D4D4D)"></lit-icon> 162 </div> 163 </div> 164 <div class="menu-body" style="overflow: auto;overflow-x:hidden;height: 100%"> 165 <slot id="st" ></slot> 166 </div> 167 <div class="bottom"> 168 <div class="color" style=""> 169 <lit-icon name="bg-colors" size="20" color="gray"></lit-icon> 170 </div> 171 <div class="version" style=""> 172 </div> 173 </div>`; 174 } 175} 176 177export interface MenuGroup { 178 title: string; 179 describe: string; 180 collapsed: boolean; 181 children: Array<MenuItem>; 182} 183 184export interface MenuItem { 185 icon: string; 186 title: string; 187 fileChoose?: boolean; 188 clickHandler?: Function; 189 fileHandler?: Function; 190} 191