-
Notifications
You must be signed in to change notification settings - Fork 14
/
menus.ts
135 lines (116 loc) · 3.63 KB
/
menus.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { isArray, isFunction, mergeDeep } from 'tianjie';
import { LoadOhMyLive2D } from './load-oml2d.js';
import { DEFAULT_OPTIONS, ELEMENT_ID } from '../config/index.js';
import { WindowSizeType } from '../constants/index.js';
import { Item } from '../types/common.js';
import type { CSSProperties, DefaultOptions, MenusOptions } from '../types/index.js';
import { createElement, getWindowSizeType, handleCommonStyle, setStyleForElement } from '../utils/index.js';
export class Menus {
element?: HTMLElement;
private style: CSSProperties = {};
private itemStyle: CSSProperties = {};
private menuItemList: HTMLElement[] = [];
constructor(
private options: DefaultOptions,
private publicOml2d: LoadOhMyLive2D
) {}
private get menuOptions(): MenusOptions {
return this.options.menus;
}
createMenuItemElements(items: Item[]): void {
this.menuItemList = items.map((item) => {
const el = createElement({
id: item.id,
tagName: 'div',
dataName: item.id,
className: 'oml2d-menus-item',
innerHtml: `<svg class="oml2d-icon"><use xlink:href="#${item.icon}"></use></svg>`
});
el.title = item.title;
el.onclick = (): void => {
void item.onClick?.(this.publicOml2d);
};
return el;
});
}
createMenuItem(): void {
if (isArray(this.menuOptions.items)) {
this.createMenuItemElements(this.menuOptions.items);
} else if (isFunction(this.menuOptions.items)) {
const items = this.menuOptions.items(DEFAULT_OPTIONS.menus.items);
this.createMenuItemElements(items);
}
}
/**
* 创建
*/
create(): void {
if (this.menuOptions.items) {
this.element = createElement({ id: ELEMENT_ID.menus, tagName: 'div', className: ELEMENT_ID.menus });
this.createMenuItem();
}
}
/**
* 挂载
* @param stageElement
*/
mount(stageElement: HTMLElement): void {
if (this.element) {
stageElement.append(this.element);
this.element.append(...this.menuItemList);
stageElement.onmouseover = (): void => {
this.setStyle({ opacity: 1, visibility: 'visible' });
};
stageElement.onmouseout = (): void => {
this.setStyle({ opacity: 0, visibility: 'hidden' });
};
// 适配移动端交互
let timer: number;
stageElement.ontouchend = (): void => {
clearTimeout(timer);
this.setStyle({ opacity: 1, visibility: 'visible' });
timer = setTimeout(() => {
this.setStyle({ opacity: 0, visibility: 'hidden' });
}, 3000);
};
}
}
/**
* 重载样式
*/
reloadStyle(): void {
switch (getWindowSizeType()) {
case WindowSizeType.pc:
this.setStyle(handleCommonStyle(this.options.menus.style || {}));
this.setItemStyle(handleCommonStyle(this.options.menus.itemStyle || {}));
break;
case WindowSizeType.mobile:
this.setStyle(handleCommonStyle(this.options.menus.mobileStyle || {}));
this.setItemStyle(handleCommonStyle(this.options.menus.mobileItemStyle || {}));
break;
}
}
/**
* 初始化样式
*/
initializeStyle(): void {
this.reloadStyle();
}
// 更新
update(options: DefaultOptions): void {
this.options = options;
this.reloadStyle();
}
setStyle(style: CSSProperties): void {
if (this.element) {
this.style = mergeDeep(this.style, style);
setStyleForElement(this.style, this.element);
}
}
setItemStyle(style: CSSProperties): void {
this.itemStyle = mergeDeep(this.itemStyle, style);
this.menuItemList.forEach((item) => {
setStyleForElement(this.itemStyle, item);
});
}
}