-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmenu.js
More file actions
145 lines (141 loc) · 5.21 KB
/
menu.js
File metadata and controls
145 lines (141 loc) · 5.21 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
const menu = wrap(() => {
let initialized, menuOpen, wrapper, body, cooked, 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-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; }',
'.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>MENU</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;
}
function open() {
if (menuOpen || !init()) return;
// 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(data.getText());
}
if (typeof data.action === 'function') {
function 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.which !== 32 && e.which !== 13) 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');
}
}
function close() {
if (!menuOpen) return;
wrapper.css({ display: '' });
menuOpen = false;
}
function isOpen() {
return menuOpen;
}
function addButton(button = {}) {
if (!button || !button.text) return fn.debug('Menu: Missing button information');
const { text, action, url, note, enabled, hidden } = button;
const close = button.keep !== true;
const safeButton = {
action, url, close, note, enabled,
getText: () => typeof text === 'function' ? text() : text,
hidden: () => typeof hidden === 'function' && hidden() || false,
};
if (button.top) {
buttons.splice(0, 0, safeButton);
} else {
buttons.push(safeButton);
}
dirty();
}
eventManager.on(':ready', () => {
toast = fn.infoToast({
text: 'UnderScript has a menu, press ESC to open it!',
onClose: (reason) => {
toast = null;
//return reason !== 'opened';
},
}, 'underscript.notice.menu', '1');
});
function dirty() {
cooked = false;
}
return {
open, close, isOpen, addButton, dirty,
};
});