-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmenu.js
More file actions
131 lines (128 loc) · 4.5 KB
/
menu.js
File metadata and controls
131 lines (128 loc) · 4.5 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
const menu = (() => {
let initialized, menuOpen, wrapper, body, cooked, toast;
const buttons = [];
function init() {
if (initialized || initialized === false) return initialized;
// jQuery must be initialized by now
if (typeof jQuery === 'undefined') return initialized = false;
$('head').append(`<style type="text/css">
.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; }
</style>`);
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${GM_info.script.version} <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) => {
const button = $('<li>');
button.attr({
role: 'button',
tabindex: 0,
});
if (data.url) {
// Make text a url
} else {
button.text(data.text);
}
if (typeof data.action === 'function') {
function callable(e) {
if (typeof data.enabled !== 'function' || data.enabled()) {
data.action(e);
if (data.close) {
close();
}
}
}
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.hover((e) => {
const note = data.note();
if (note) {
hover.show(note)(e);
}
}, hover.close);
} else {
button.hover(hover.show(data.note));
}
}
body.append(button);
});
cooked = true;
}
wrapper.css({ display: 'block' }).focus();
menuOpen = true;
if (toast) {
toast.close();
}
}
function close() {
if (!menuOpen) return;
wrapper.css({ display: '' });
menuOpen = false;
}
function isOpen() {
return menuOpen;
}
function addButton(button = {}) {
if (!button || !button.text) return fn.debug('Missing button information');
cooked = false;
const { text, action, url, note, enabled } = button;
const close = button.keep !== true;
const safeButton = {
text, action, url, close, note, enabled
};
if (button.top) {
buttons.splice(0, 0, safeButton);
} else {
buttons.push(safeButton);
}
}
toast = fn.infoToast({
text: 'UnderScript has a menu, press ESC to open it!',
onClose: () => {
toast = null;
},
}, 'underscript.notice.menu', '1');
return {
open, close, isOpen, addButton,
};
})();