forked from tc39/ecmarkup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdoMap.js
82 lines (75 loc) · 2.46 KB
/
sdoMap.js
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
'use strict';
let sdoBox = {
init() {
this.$alternativeId = null;
this.$outer = document.createElement('div');
this.$outer.classList.add('toolbox-container');
this.$container = document.createElement('div');
this.$container.classList.add('toolbox');
this.$displayLink = document.createElement('a');
this.$displayLink.setAttribute('href', '#');
this.$displayLink.textContent = 'Syntax-Directed Operations';
this.$displayLink.addEventListener('click', e => {
e.preventDefault();
e.stopPropagation();
referencePane.showSDOs(sdoMap[this.$alternativeId] || {}, this.$alternativeId);
});
this.$container.appendChild(this.$displayLink);
this.$outer.appendChild(this.$container);
document.body.appendChild(this.$outer);
},
activate(el) {
clearTimeout(this.deactiveTimeout);
Toolbox.deactivate();
this.$alternativeId = el.id;
let numSdos = Object.keys(sdoMap[this.$alternativeId] || {}).length;
this.$displayLink.textContent = 'Syntax-Directed Operations (' + numSdos + ')';
this.$outer.classList.add('active');
let top = el.offsetTop - this.$outer.offsetHeight;
let left = el.offsetLeft + 50 - 10; // 50px = padding-left(=75px) + text-indent(=-25px)
this.$outer.setAttribute('style', 'left: ' + left + 'px; top: ' + top + 'px');
if (top < document.body.scrollTop) {
this.$container.scrollIntoView();
}
},
deactivate() {
clearTimeout(this.deactiveTimeout);
this.$outer.classList.remove('active');
},
};
document.addEventListener('DOMContentLoaded', () => {
if (typeof sdoMap == 'undefined') {
console.error('could not find sdo map');
return;
}
sdoBox.init();
let insideTooltip = false;
sdoBox.$outer.addEventListener('pointerenter', () => {
insideTooltip = true;
});
sdoBox.$outer.addEventListener('pointerleave', () => {
insideTooltip = false;
sdoBox.deactivate();
});
sdoBox.deactiveTimeout = null;
[].forEach.call(document.querySelectorAll('emu-grammar[type=definition] emu-rhs'), node => {
node.addEventListener('pointerenter', function () {
sdoBox.activate(this);
});
node.addEventListener('pointerleave', () => {
sdoBox.deactiveTimeout = setTimeout(() => {
if (!insideTooltip) {
sdoBox.deactivate();
}
}, 500);
});
});
document.addEventListener(
'keydown',
debounce(e => {
if (e.code === 'Escape') {
sdoBox.deactivate();
}
}),
);
});