-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmenu.js
More file actions
167 lines (161 loc) · 5.42 KB
/
menu.js
File metadata and controls
167 lines (161 loc) · 5.42 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import Translation from 'src/structures/constants/translation.js';
import style from './style.js';
import { scriptVersion } from './1.variables.js';
import * as hover from './hover.js';
import eventManager from './eventManager.js';
import { infoToast } from './2.toasts.js';
import { debug } from './debug.js';
import addMenuButton from './menubuttons.js';
import { translateText } from './translate.js';
import compound from './compoundEvent.js';
let initialized;
let menuOpen;
let wrapper;
let body;
let cooked;
let toast;
const buttons = [];
function init() {
if (typeof initialized === 'boolean') return initialized;
// jQuery must be initialized by now
if (typeof jQuery === 'undefined') return initialized = false;
style.add(
'.menu-backdrop { display: none; position: fixed; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: #000; background-color: rgba(0,0,0,0.4); z-index: 1010; }',
'.menu-close { float: right; color: #aaa; font-size: 28px; font-weight: bold; margin: -5px; }',
'.menu-close:hover, .menu-close:focus { color: #FFF; text-decoration: none; cursor: pointer; }',
'.menu-content { color: #fff; margin: 4% auto; padding: 0; border: 2px solid #888; width: 280px; background: #000 url(../images/backgrounds/2.png) center 632px; }',
'.menu-content > * { padding: 2px 16px; }',
'.menu-content a { color: #fff; }',
'.menu-header { text-align: center; font-size: 30px; }',
'.menu-footer img { height: 16px; vertical-align: middle; }',
'.menu-body { background-color: rgba(0,0,0,0.6); min-height: 250px; max-height: 600px; overflow-y: auto; }',
'.menu-body ul { list-style: none; padding: 0; }',
'.menu-body li { list-style-type: none; border: 1px solid #fff; width: 80%; text-align: center; margin: 5px auto; opacity: 1; }',
'.menu-body li:hover, .menu-body li:focus { text-decoration: underline; opacity: 0.4; }',
);
body = $('<div class="menu-body">');
wrapper = $('<div class="menu-backdrop" tabindex="-1">')
.append($('<div class="menu-content">')
.attr({
role: 'Menu',
})
.append(
`<div class="menu-header"><span class="menu-close right">×</span>${Translation.Menu('menu.title')}</div>`,
body,
`<div class="menu-footer"><a href="https://git.io/fxysg" target="_blank">UnderScript</a> v${scriptVersion} <a href="https://discord.gg/D8DFvrU" target="_blank"><img id="usdiscord" src="images/social/discord.png" alt="discord"></a></div>`,
))
.on('click', (e) => {
if (e.target === wrapper[0]) {
close();
}
});
$('body').append(wrapper);
$('span.menu-close').on('click', close);
return initialized = true;
}
export function open() {
if (menuOpen || !init()) return;
eventManager.emit(':menu:opening');
// Generate buttons
if (!cooked) {
body.html(''); // Clear current buttons
buttons.forEach((data) => {
if (data.hidden()) return;
const button = $('<li>');
button.attr({
role: 'button',
tabindex: 0,
});
if (data.url) {
// Make text a url
} else {
button.text(translateText(data.getText()));
}
if (typeof data.action === 'function') {
const callable = (e) => {
if (typeof data.enabled !== 'function' || data.enabled()) {
const result = data.action(e);
if (result !== undefined ? result : data.close) {
close();
}
} else {
button.blur();
}
};
button.on('click', callable)
.on('keydown', (e) => {
if (e.key !== ' ' && e.key !== 'Enter') return;
e.preventDefault();
callable(e);
}).css({
cursor: 'pointer',
});
}
if (data.note) {
if (typeof data.note === 'function') {
button.on('mouseenter focus', (e) => {
const note = data.note();
if (note) {
hover.show(note)(e);
}
}).on('mouseleave blur', hover.hide);
} else if (typeof data.note === 'string') {
button.on('mouseenter focus', hover.show(data.note))
.on('mouseleave blur', hover.hide);
}
}
body.append(button);
});
cooked = true;
}
wrapper.css({ display: 'block' }).focus();
menuOpen = true;
if (toast) {
toast.close('opened');
}
}
export function close() {
if (!menuOpen) return;
wrapper.css({ display: '' });
menuOpen = false;
}
export function isOpen() {
return menuOpen;
}
export function addButton(button = {}) {
if (!button || !button.text) {
debug('Menu: Missing button information');
return;
}
const { text, action, url, note, enabled, hidden } = button;
const safeButton = {
action,
url,
note,
enabled,
close: button.keep !== true,
getText: () => (typeof text === 'function' ? text() : text),
hidden: () => typeof hidden === 'function' && hidden() || false,
};
if (button.top) {
buttons.unshift(safeButton);
} else {
buttons.push(safeButton);
}
dirty();
}
compound(':load', 'underscript:ready', () => {
const btn = addMenuButton(Translation.Menu('menu'));
if (btn) btn.addEventListener('click', () => open());
dirty();
toast = infoToast({
text: Translation.Toast('menu'),
onClose: (reason) => {
toast = null;
// return reason !== 'opened';
},
}, 'underscript.notice.menu', '1');
});
export function dirty() {
cooked = false;
}