/*
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { BaseElement, element } from '../BaseElement';
import './LitMainMenuItem';
import './LitMainMenuGroup';
import { LitMainMenuGroup } from './LitMainMenuGroup';
import { LitMainMenuItem } from './LitMainMenuItem';
const initHtmlStyle: string = `
`;
@element('lit-main-menu')
export class LitMainMenu extends BaseElement {
private slotElements: Element[] | undefined;
private _menus: Array | undefined;
static get observedAttributes(): string[] {
return [];
}
get menus(): Array | undefined {
return this._menus;
}
set menus(value: Array | undefined) {
this._menus = value;
this.shadowRoot?.querySelectorAll('lit-main-menu-group').forEach((a) => a.remove());
let menuBody = this.shadowRoot?.querySelector('.menu-body');
if (this.getAttribute('main_menu') === '1' && window.localStorage.getItem('Theme') === 'dark') {
this.style.backgroundColor = '#262f3c';
} else {
this.style.backgroundColor = '#fff';
}
value?.forEach((it) => {
let group: LitMainMenuGroup = new LitMainMenuGroup();
group.setAttribute('title', it.title || '');
if (it.describe !== '') {
group.setAttribute('describe', it.describe || '');
} else {
group.removeAttribute('describe');
}
group.setAttribute('icon', it.icon || '');
if (it.collapsed) {
group.setAttribute('collapsed', '');
} else {
group.removeAttribute('collapsed');
}
let groupName: LitMainMenuGroup = group!.shadowRoot!.querySelector('.group-name') as LitMainMenuGroup;
let groupDescribe: LitMainMenuGroup = group!.shadowRoot!.querySelector('.group-describe') as LitMainMenuGroup;
menuBody?.appendChild(group); // @ts-ignore
it.children?.forEach((item: unknown) => {
// @ts-ignore
if (item.fileModel !== undefined && item.fileModel === 'db') {
return;
} // @ts-ignore
if (item.children && item.children.length > 0) {
let secondGroup: LitMainMenuGroup = new LitMainMenuGroup(); // @ts-ignore
secondGroup.setAttribute('title', item.title || ''); // @ts-ignore
if (item.describe !== '') {
// @ts-ignore
secondGroup.setAttribute('describe', item.describe || '');
} else {
secondGroup.removeAttribute('describe');
}
this.setChildren(item, group, groupName, groupDescribe, secondGroup);
} else {
this.notChildren(item, group, groupName, groupDescribe);
}
});
});
}
setChildren(
item: unknown,
group: LitMainMenuGroup,
groupName: LitMainMenuGroup,
groupDescribe: LitMainMenuGroup,
secondGroup: LitMainMenuGroup
): void {
// @ts-ignore
secondGroup.setAttribute('icon', item.icon || ''); // @ts-ignore
if (item.second) {
secondGroup.setAttribute('second', '');
} else {
secondGroup.removeAttribute('second');
} // @ts-ignore
if (item.collapsed) {
secondGroup.setAttribute('collapsed', '');
} else {
secondGroup.removeAttribute('collapsed');
}
group?.appendChild(secondGroup); // @ts-ignore
item.children?.forEach((v: unknown) => {
let th = new LitMainMenuItem(); // @ts-ignore
th.setAttribute('icon', v.icon || ''); // @ts-ignore
th.setAttribute('title', v.title || '');
if (this.getAttribute('main_menu') === '1' && window.localStorage.getItem('Theme') === 'dark') {
groupName.style.color = 'white';
groupDescribe.style.color = 'white';
th!.style.color = 'white';
} else {
groupName.style.color = 'black';
groupDescribe.style.color = 'black';
th!.style.color = 'black';
} // @ts-ignore
if (v.fileChoose) {
th.setAttribute('file', '');
th.addEventListener('file-change', (e): void => {
// @ts-ignore
if (v.fileHandler && !th.disabled) { // @ts-ignore
v.fileHandler(e);
}
});
} else {
th.removeAttribute('file');
th.addEventListener('click', (e): void => {
// @ts-ignore
if (v.clickHandler && !th.disabled) { // @ts-ignore
v.clickHandler(v);
}
});
} // @ts-ignore
if (v.disabled !== undefined) { // @ts-ignore
th.disabled = v.disabled;
}
secondGroup.appendChild(th);
});
}
notChildren(
item: unknown,
group: LitMainMenuGroup,
groupName: LitMainMenuGroup,
groupDescribe: LitMainMenuGroup
): void {
let th = new LitMainMenuItem(); // @ts-ignore
th.setAttribute('icon', item.icon || ''); // @ts-ignore
th.setAttribute('title', item.title || '');
if (this.getAttribute('main_menu') === '1' && window.localStorage.getItem('Theme') === 'dark') {
groupName.style.color = 'white';
groupDescribe.style.color = 'white';
th!.style.color = 'white';
} else {
groupName.style.color = 'black';
groupDescribe.style.color = 'black';
th!.style.color = 'black';
} // @ts-ignore
if (item.fileChoose) {
th.setAttribute('file', '');
th.addEventListener('file-change', (e) => {
// @ts-ignore
if (item.fileHandler && !th.disabled) {
// @ts-ignore
item.fileHandler(e);
}
});
} else {
th.removeAttribute('file');
th.addEventListener('click', (e) => {
// @ts-ignore
if (item.clickHandler && !th.disabled) {
// @ts-ignore
item.clickHandler(item);
}
});
}
// @ts-ignore
if (item.multi) {
th.multi = true;
}
// @ts-ignore
if (item.disabled !== undefined) {
// @ts-ignore
th.disabled = item.disabled;
}
group?.appendChild(th);
}
initElements(): void {
let st: HTMLSlotElement | null | undefined = this.shadowRoot?.querySelector('#st');
st?.addEventListener('slotchange', (e) => {
this.slotElements = st?.assignedElements();
this.slotElements?.forEach((it) => {
it.querySelectorAll('lit-main-menu-item').forEach((cell) => {});
});
});
let versionDiv: HTMLElement | null | undefined = this.shadowRoot?.querySelector('.version');
//@ts-ignore
versionDiv!.innerText = window.version || '';
}
initHtml(): string {
return `
${initHtmlStyle}
`;
}
}
export interface MenuGroup {
title: string;
describe: string;
second: boolean;
collapsed: boolean;
children: MenuItem[];
icon: string;
}
export interface MenuItem {
icon: string;
title: string;
fileModel?: string;
multi?: boolean;
disabled?: boolean;
fileChoose?: boolean;
clickHandler?: Function;
fileHandler?: Function;
}