-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathscript.js
113 lines (105 loc) · 4.96 KB
/
script.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
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
//Joel Malissa & Logan Stafman
//maps from acronym to meaning
var myMap = {};
$.getJSON('https://raw.githubusercontent.com/nasa/NASA-Acronyms/master/lists/acronyms.json', function getAcronymsJson(data) {
for(var i = 0; i < data.length; i++) {
var abbrev = data[i]['abbreviation'].toLowerCase();
if(!myMap[abbrev])
myMap[abbrev] = new Array();
myMap[abbrev].push(data[i]);
}
});
// places meaning(s) below selected acronym in a qtip tooltip
$(function renderTooltip() {
$('body').mouseup(function mouseUp(event) {
setTimeout(function setTimeout() {
var inner, range, rect, x, y, h, w;
var selection = window.getSelection();
if(selection.rangeCount > 0) // invoke when text is selected
{
inner = selection.anchorNode.innerHTML;
if(inner) // check if selection is in an input tag
{
if(inner.includes('input') || inner.includes('textarea')) // verify selection is in an input tag
{
x = event.clientX;
y = event.clientY + 10; // place qtip slightly below the mouse
h = 1;
w = 1;
}
} else { // for text that a user didn't type
range = selection.getRangeAt(0);
rect = range.getBoundingClientRect();
x = rect.left;
y = rect.top;
h = rect.height;
w = rect.width;
}
}
selection = selection.toString().trim();
var selText = selection.toLowerCase().replace(/\./g,'');
var meanings;
if(myMap[selText]) {
var meanings = new Array();
for(var i = 0; i < myMap[selText].length; i++) {
meanings.push(myMap[selText][i]['expansion']);
}
}
$('.qtip').remove(); // Does this line fix the top left corner duplicate qtip bug?
$('#nasa_tooltip').remove(); // removes the div created to anchor the first qtip
// Create tooltip with number of results displayed
if (selText.length > 1 && meanings) {
// Sort meanings here
// meanings.sort(); // i.e. sort alphabetically
// console.log('alphabetical order: ' + meanings);
// meanings.sort(function(a, b){return a.length - b.length}); // i.e. shortest first
// console.log('shortest first: ' + meanings);
// meanings.sort(function(a, b){return b.length - a.length}); // i.e. longest first
// console.log('longest first: ' + meanings);
meanings.sort(function(a, b){ // sort by appearances on page
var regA = new RegExp(a, 'gi');
var regB = new RegExp(b, 'gi');
return (document.body.innerText.match(regB) || []).length - (document.body.innerText.match(regA) || []).length;
})
if (meanings.length == 1) { // 1 result
var out = meanings.length + ' acronym found for ' + selection + ':<br>';
} else { // >1 result
var out = meanings.length + ' acronyms found for ' + selection + ':<br>';
}
for(var i = 0; i < meanings.length; i++) out += '\u2022 ' + meanings[i] + '<br>';
out = out.trim();
var div = document.createElement('div');
div.style.position = 'fixed';
div.style.left = x + 'px';
div.style.top = y + 'px';
div.style.height = h + 'px';
div.style.width = w + 'px';
div.setAttribute('id', 'nasa_tooltip');
document.body.appendChild(div);
$.fn.qtip.zindex = 999999;
$('#nasa_tooltip').qtip({
hide: {
event: 'unfocus'
}, // hides qtip on mousedown
content: out,
prerender: true,
show: {
ready: true
}, // prerendering and showing qtip immediately when ready fixes bug where selecting and quickly moving mouse away wouldn't get a qtip
position: {
my: 'top center',
at: 'bottom center',
viewport: true,
adjust: {
method: 'shift none',
scroll: false
}
},
style: {
classes: 'qtip-tipsy'
}
});
}
}, 10);
});
});