meta.js
8.76 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
;(function(){
var root;
if(typeof window !== "undefined"){ root = window }
if(typeof global !== "undefined"){ root = global }
root = root || {};
var console = root.console || {log: function(){}};
function USE(arg, req){
return req? require(arg) : arg.slice? USE[R(arg)] : function(mod, path){
arg(mod = {exports: {}});
USE[R(path)] = mod.exports;
}
function R(p){
return p.split('/').slice(-1).toString().replace('.js','');
}
}
if(typeof module !== "undefined"){ var common = module }
/* UNBUILD */
;USE(function(module){
var noop = function(){}, u;
$.fn.or = function(s){ return this.length ? this : $(s||'body') };
var m = window.meta = {edit:[]};
var k = m.key = {};
k.meta = {17:17, 91:17, 93:17, 224:17, 18: 17}; // ALT added
function withMeta(eve){ return eve.metaKey || eve.ctrlKey || eve.altKey } // ALT added
var defaults = {
8: { // backspace: close root or go back on submenu
on: () => k.at == m.edit ? m.flip(false) : m.check('down', 'back')
},
27: { // esc: close and reset menu
up: () => k.wipe()
}
}
k.down = function(eve){
var key = (k.eve = m.eve = eve).which = eve.which || eve.fake || eve.keyCode;
if(eve.repeat){ return }
if(!k.meta[key] && withMeta(eve) && !k.at[key]) {
return m.flip(false)
} // cancel and close when no action and "meta key" held down (e.g. ctrl+c)
if(!eve.fake && key === k.last){ return }; k.last = key; // jussi: polyfilling eve.repeat?
if(!eve.fake && $(eve.target).closest('input, textarea, [contenteditable=true]').length && !$(eve.target).closest('#meta').length){
if(meta.flip.is() && !withMeta(eve)) eve.preventDefault()
}
m.check('on', key, k.at || (k.at = m.edit));
if(k.meta[key]){ m.flip() }
}
k.down.keys = {} // currently pressed keys
k.up = function(eve){ var tmp;
var key = (k.eve = m.eve = eve).which = eve.which || eve.fake || eve.keyCode;
k.last = null;
m.check('up', key);
if(k.meta[key] && m.check.fired){
m.close()
}
}
m.flip = function(tmp){
((tmp === false) || (!tmp && m.ui.board.is(':visible')))?
m.close() : m.open();
}
m.open = function(){
m.check.fired = null;
m.ui.board.removeClass('meta-none');
}
m.close = function(){
Object.keys(k.down.keys).forEach((keyDown) => {
m.check('up', keyDown);
})
m.ui.board.addClass('meta-none')
}
m.flip.is = function(){
return m.ui.board.is(':visible');
}
m.flip.wait = 500;
m.check = function(how, key, at){
if(!m.flip.is() && !k.meta[key]){ return } // TEMP: cancel non-open events when closed TODO make optional
at = k.at || m.edit;
var next = at[key];
if(!next){ return }
var tmp = k.eve || noop;
if(tmp.preventDefault){ tmp.preventDefault()} // prevent typing (etc) when action found
if(next[how]){
next[how](m.eve);
meta.ui.blink();
m.check.fired = true;
if(how == 'up') delete k.down.keys[key]
else k.down.keys[key] = 1;
}
if('up' == how){ return }
if(at != next){ next.back = at }
(k.combo || (k.combo = [])).push(key);
m.list(next, true);
}
function defaultSort(a,b){
a = a.combo.slice(-1)[0] || 0;
if(a.length){ a = a.toUpperCase().charCodeAt(0) }
b = b.combo.slice(-1)[0] || 0;
if(b.length){ b = b.toUpperCase().charCodeAt(0) }
return (a < b)? -1 : 1;
}
m.list = function(at, opt){
if(!at){ return m.flip(false) }
var l = [];
$.each(at, function(i,k){ 'back' != i && k && k.combo && k.name && l.push(k) });
if(!l.length){ return }
k.at = at;
if(at.sort !== null){ l = l.sort(at.sort || defaultSort) }
var $ul = $('#meta .meta-menu ul')
$ul.children('li').addClass('meta-none').hide(); setTimeout(function(){ $ul.children('.meta-none').remove() },250); // necessary fix for weird bug glitch
$.each(l, function(i, k){
var $li = $('<li>').text(k.name).data(k)
$ul.append($li);
if(k.styles) meta.ui.iniline($li[0], k.styles);
});
if(opt){ m.flip(true) }
$ul.append($('<li>').html('←').on('click', function(){
m.list(at.back);
}));
}
m.ask = function(help, cb){
var $ul = $('#meta .meta-menu ul').empty();
var $put = $('<input>').attr('id', 'meta-ask').attr('placeholder', help);
var $form = $('<form>').append($put).on('submit', function(eve){
eve.preventDefault();
cb($put.val());
$li.remove();
//k.wipe();
m.list(k.at);
});
var $li = $('<li>').append($form);
$ul.append($li);
m.flip(true);
$put.focus();
}
k.wipe = function(opt){
k.combo = [];
if(!opt){ m.flip(false) }
m.list(k.at = m.edit);
};
m.tap = function(){
var on = $('.meta-on')
.or($($(document.querySelectorAll(':hover')).get().reverse()).first())
.or($(document.elementFromPoint(meta.tap.x||0, meta.tap.y||0)));
return on;
}
meta.edit = function(e){
var path = [];
$.each(e.combo || (e.combo = []), function(i,k){
if(!k || !k.length){ if('number' == typeof k){ path.push(k) } return }
path.push(k.toUpperCase().charCodeAt(0));
});
var at = meta.edit, l = e.combo.length;
$.each(path, function(i,k){ at = at[k] = at[k] || Object.create(defaults) });
$.extend(at, e) // fixes overwriting when sub action is defined before parent
e.combow = path.join(','); // deprecate?
m.list(k.at || meta.edit);
}
$.extend(meta.edit, defaults)
})(USE, './metaCore');
;USE(function(module){
/* UI */
meta.ui = {
blink: function(){ // hint visually that action has happened
$('#meta').css('transition', 'none').css('background', 'none')
setTimeout(function(){
$('#meta')[0].style.transition = null
$('#meta')[0].style.background = null
})
},
depth: function(n){
if (n) {
$('#meta').css('background', 'hsl(60, 100%,'+(85-(n*10))+'%)');
} else {
$('#meta')[0].style.background = null
}
}
}
var $m = $('<div>').attr('id', 'meta');
$m.append($('<span>').html('☰').addClass('meta-start'));
$m.append($('<div>').addClass('meta-menu meta-none').append('<ul>'));
$(document.body).append($m);
meta.ui.board = $('.meta-menu', $m);
css({
'#meta': {
display: 'block',
position: 'fixed',
bottom: '2em',
right: '2em',
background: 'white',
'font-size': '18pt',
'font-family': 'Tahoma, arial',
'border-radius': '1em',
'text-align': 'center',
'z-index': 999999,
margin: 0,
padding: 0,
width: '2em',
height: '2em',
outline: 'none',
color: '#000044',
overflow: 'visible',
transition: 'all 0.2s ease-in'
},
'#meta *': {outline: 'none'},
'#meta .meta-none': {display: 'none'},
'#meta span': {'line-height': '2em'},
'#meta .meta-menu': {
background: 'rgba(0,0,0,0.1)',
width: '12em',
right: '-2em',
bottom: '-2em',
overflow: 'visible',
position: 'absolute',
'overflow-y': 'scroll',
'text-align': 'right',
'min-height': '20em',
height: '100vh'
},
'#meta .meta-menu ul': {
padding: 0,
margin: '1em 1em 2em 0',
'list-style-type': 'none'
},
'#meta .meta-menu ul li': {
display: 'block',
'float': 'right',
background: 'white',
opacity: 0.7,
padding: '0.5em 1em',
'border-radius': '1em',
'margin-left': '0.25em',
'margin-top': '0.25em',
'cursor': 'pointer'
},
'#meta .meta-menu ul li:hover': {
opacity: 1
},
'#meta a': {color: 'black'},
'#meta .meta-menu ul:before': {
content: "' '",
display: 'block',
'min-height': '15em',
height: '50vh'
},
'#meta .meta-start': {
cursor: 'pointer'
}
});
function css(css){
var tmp = '';
$.each(css, function(c,r){
tmp += c + ' {\n';
$.each(r, function(k,v){
tmp += '\t'+ k +': '+ v +';\n';
});
tmp += '}\n';
});
var tag = document.createElement('style');
tag.innerHTML = tmp;
$m.append(tag)
}
meta.ui.iniline = function(el, cssObj){
for(var k in cssObj) { el.style[k] = cssObj[k]; }
}
})(USE, './metaUI');
;USE(function(module){
var m = meta, k = m.key;
$(window).on('focus', k.wipe.bind(null, false)); // .on('blur', k.wipe.bind(null, false))
$(document).on('mousedown mousemove mouseup', function(eve){
m.tap.eve = eve;
m.tap.x = eve.pageX||0;
m.tap.y = eve.pageY||0;
m.tap.on = $(eve.target);
})
var [start, end] = 'ontouchstart' in window
? ['touchstart', 'touchend']
: ['mousedown', 'mouseup']
$(document).on(start, '#meta .meta-menu li', function(eve){
var combo = $(this).data().combo;
eve.fake = eve.which = combo && combo.slice(-1)[0].toUpperCase().charCodeAt(0);
eve.tap = true;
k.down(eve);
$(document).one(end, () => k.up(eve))
return;
});
$(document).on('keydown', k.down).on('keyup', k.up);
$('#meta').on(start, function(ev) {
if (ev.target.tagName == 'LI' || ev.target.tagName == 'UL') return
meta.flip()
})
})(USE, './metaEvents');
}());