-
Notifications
You must be signed in to change notification settings - Fork 0
/
nasIataIcaoTooltips.js
446 lines (412 loc) · 17.9 KB
/
nasIataIcaoTooltips.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/**
* LICENSE
* =======
*
* Copyright 2018 netAirspace.com, LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
* IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*
*/
var nasIataIcaoTooltips={
/**
* Initiate highlighting of IATA/ICAO tooltips.
* @param cssSelector Specifies the HTML elements to search for IATA/ICAO codes to highlight.
* @param {string} ajaxUri The URI to which a GET request is made to get possible tooltip values.
* @param {object} overrides Optional parameter. An object containing key-value pairs to override the
* defaults in nasTooltips.
*/
init: function(cssSelector, ajaxUri, overrides){
nasTooltips.init(cssSelector, nasIataIcaoTooltips.regex, ajaxUri, overrides);
},
/**
* Matches word break, code, word break. Codes recognised:
*
* - IATA operator: 2 characters, of which zero or one (but not both) may be numbers
* - IATA airport: 3 letters
* - ICAO operator: 3 letters
* - ICAO airfield: 4 letters
*
* All codes are assumed to be uppercase.
*/
regex:/\b[A-Z]{2,4}\b|\b[0-9][A-Z]\b|\b[A-Z][0-9]\b/g
}
var nasTooltips={
/**
* Initiate highlighting of tooltips. Sets up any overrides and adds an event listener on DOMContentLoaded to
* trigger matching of codes, AJAX lookup, and highlighting of found codes.
* @param {string} cssSelector Specifies the HTML elements to search for tooltips to highlight.
* @param {RegExp} regex A regular expression defining the possible tooltips (for example, IATA/ICAO codes).
* @param {string} ajaxUri The URI to which a GET request is made to get possible tooltip values.
* @param {array} overrides Optional parameter. An object containing key-value pairs to override the
* defaults in nasTooltips. You can override tagsToExclude, wrapperTagClass and tooltipTagId.
*/
init: function(cssSelector, regex, ajaxUri, overrides){
if(!cssSelector || !regex || !ajaxUri){ return false; } //called badly
if(!XMLHttpRequest || !JSON || !document.querySelectorAll){ return false; } //browser not capable
nasTooltips.cssSelector=cssSelector;
nasTooltips.regex=regex;
nasTooltips.ajaxUri=ajaxUri;
if(overrides){
var keys=Object.keys(overrides);
for(i=0;i<keys.length;i++){
var k=keys[i];
nasTooltips[k]=overrides[k];
}
}
if(typeof nasTooltips.renderer==="string"){
//Look up the function in the renderers
nasTooltips.renderer=nasTooltips.renderers[nasTooltips.renderer];
}
if(!nasTooltips.renderer){
//was either "off" or a non-existent item
return false;
}
document.addEventListener("DOMContentLoaded", nasTooltips.doHighlight);
},
/* *******************************************************************************
* Default settings for tooltip highlighting. Any of these can be overridden by *
* specifying them in the overrides parameter when calling highlight(). *
* ******************************************************************************* */
/**
* The renderer to apply to each tooltip for which a result is found. If overriding this,
* you must specify one of the built-in text values in "renderers" below, or supply a
* function whose only parameter is the span around the IATA/ICAO code.
*/
renderer:'tooltip',
/**
* These tags will be excluded from parsing for matches, even if they appear inside
* the elements that should be searched. Modifying the text content of these tags
* may cause errors on your page.
*/
tagsToExclude:['script','style','iframe','acronym','abbr','canvas'],
/**
* The CSS class of the element that is wrapped around matched text, and the ID of the tooltip
* that appears when the wrapper element is moused over (if using the default tooltip rendering).
*
* Note that we use <span> tags for this because the tags intended for this task are not fit
* for our purpose: <acronym> is removed in HTML5, and <abbr> fails to handle multiple possible
* matches, as can happen with IATA airline codes, re-used airfield codes (think Hong Kong), etc.
*
* Toolitps are assumed to be styled so that they sit above and to the right of the acronym/code that
* they explain. Additional CSS classes are applied if they run over the top or right-hand edge, but they
* are assumed not to run over the bottom or left-hand edge in their initial state.
*/
wrapperTagClass:'nastt_acronym',
inlineExplanationTagClass:'nastt_explain',
tooltipTagId:'nastt_tooltip',
tooltipOffRightEdgeClass:'nastt_offrightedge',
tooltipOffTopEdgeClass:'nastt_offtopedge',
/* *******************************************************************************
* Supporting functions and properties. *
* You don't need to worry about anything below here. *
* ******************************************************************************* */
/**
* Mapping of built-in rendering options to their functions.
*/
renderers:{
//Default - mouseover tooltips
'tooltip': function(node){ nasTooltips.renderNodeAsTooltip(node); },
//Append the explanation after the acronym
'inline': function(node){ nasTooltips.renderNodeAsInline(node); },
//Don't render the tooltips at all
'off': null
},
/**************************************************************************************************************
* IATA/ICAO CODE TOOLTIP STYLES *
**************************************************************************************************************/
defaultAcronymStyle:'position:relative; top:0; border-bottom:1px dotted black; cursor:help;',
defaultTooltipStyle:'font-family:verdana,arial,helvetica,sans-serif; position:absolute; z-index:99999; left:0; bottom:1.5em; padding:0.5em; white-space:nowrap; background-color:#ffd; color:#333; border:1px solid #333; border-radius:0.5em;',
defaultTooltipStyleOffRightEdge:'left:auto; right:0;',
defaultTooltipStyleOffTopEdge:'bottom:auto; top:1.5em',
possibleMatchClass:'nastt_match',
/**
* This object will hold the results found on the server. Each matched result will appear
* as an object key, with the object value being an array of one or more possible explanations.
* See "Server-side code" in the documentation at the top of this file.
*/
results:{},
/**
* Strips the HTML tags used to identify possible or actual tooltip matches within the supplied text.
* This can be useful with phpBB quoting, where the raw HTML of the tooltip ends up in the post editing box.
* @oaram {string} txt The raw text to clean
* @return {string} The cleaned text
*/
stripTooltipTags:function(txt){
//Strip inline explanations, in case they were added
var re1='<span class="'+nasTooltips.inlineExplanationTagClass+'">(.+?)</span>';
var regex1=new RegExp(re1,'g');
txt=txt.replace(regex1,'');
//Then strip the span tags around the IATA/ICAO ode
var re2='<span class="(?:'+nasTooltips.wrapperTagClass+'|'+nasTooltips.possibleMatchClass+').*?">(.+?)</span>';
var regex2=new RegExp(re2,'g');
txt=txt.replace(regex2,'$1');
return txt;
},
/**
* Initiates highlighting. Finds matches for regex, wraps each in a span tag, and starts the AJAX request
* to retrieve definitions.
*/
doHighlight:function(){
if(!nasTooltips.ajaxUri || !nasTooltips.cssSelector){
//init() has not been called.
return false;
}
var nodes=document.querySelectorAll(nasTooltips.cssSelector);
var numNodes=nodes.length;
for(var i=0;i<numNodes;i++){
nasTooltips.markPossibleMatches(nodes[i]);
}
var possibleMatches=document.querySelectorAll("."+nasTooltips.possibleMatchClass);
if(!possibleMatches ||0==possibleMatches.length){
return false;
}
var found=[];
var numPossibleMatches=possibleMatches.length;
for(var j=0;j<numPossibleMatches;j++){
var node=possibleMatches[j];
var code=node.textContent;
if(-1==found.indexOf(code) && code.match(nasTooltips.regex)){
found.push(code);
}
}
nasTooltips.getDefinitionsFromServer(found);
},
/**
* Marks possible text matches found within the supplied element.
* Possible matches are surrounded with a span whose classname is defined at nasTooltips.possibleMatchClass
* @param {HTMLElement} elem The element to search
* @return {array} An array of matches. If none are found, returns an empty array.
*/
markPossibleMatches: function(elem){
var textNodes=nasTooltips.getChildTextNodes(elem);
if(!textNodes || 0==textNodes.length){ return []; }
var numTextNodes=textNodes.length;
for(var i=0; i<numTextNodes; i++){
var txtNode=textNodes[i];
var oldContent=txtNode.textContent;
var newContent=oldContent.replace(nasTooltips.regex, '!!nastt!!$&!!nastt!!');
if(newContent!=oldContent){
elem.innerHTML=elem.innerHTML.replace(oldContent,newContent);
}
}
elem.innerHTML=elem.innerHTML.replace(/!!nastt!!(.+?)!!nastt!!/g,'<span class="'+nasTooltips.possibleMatchClass+'">$1</span>');
},
/**
* Initiates an AJAX request to look up the found matches
* @param {array} found An array of strings found matching the search conditions
*/
getDefinitionsFromServer: function(found){
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=function(){
if(XMLHttpRequest.DONE==xhr.readyState){
if(200==xhr.status){
nasTooltips.handleResponse(xhr.responseText);
} else {
//Something went wrong. Just eat it, leave the user's page alone.
}
}
};
xhr.open('GET', nasTooltips.ajaxUri+'?codes='+found.join(','), true);
xhr.send();
},
/**
* Handles the AJAX response from the server, including some basic error checking.
* @param {string} responseText The response from the server.
*/
handleResponse:function(responseText){
var matches=JSON.parse(responseText);
if(!matches || matches.error){
//just eat it, leave the user's page alone
return false;
}
nasTooltips.results=matches;
nasTooltips.highlightFoundCodes();
},
/**
* For each previously-found acronym span for which a definition was found,
* applies the acronym CSS class and attaching the events.
*/
highlightFoundCodes:function(){
nasTooltips.renderedAsTooltips=false;
var nodes=document.querySelectorAll("."+nasTooltips.possibleMatchClass);
for(i=0;i<nodes.length;i++){
var node=nodes[i];
var content=node.textContent;
if(nasTooltips.results[content]){
nasTooltips.renderer(node);
}
if(nasTooltips.renderedAsTooltips){
window.setTimeout(nasTooltips.applyDefaultStylesIfNeeded, 50);
}
}
},
renderNodeAsTooltip:function(node){
node.className=nasTooltips.wrapperTagClass;
node.onmouseover=nasTooltips.showCodeTooltip;
node.ontouchstart=nasTooltips.showCodeTooltip;
node.onmouseout=nasTooltips.hideCodeTooltip;
node.ontouchend=nasTooltips.hideCodeTooltip;
nasTooltips.renderedAsTooltips=true;
},
/**
* Renders the IATA/ICAO code as inline text, e.g., "F9 (Frontier Airlines - United States)".
* Multiple matches are separated within the parentheses by semicolons.
*/
renderNodeAsInline:function(node){
var key=node.innerHTML;
var values=nasTooltips.results[key];
if(!values || 0==values.length){ return false; }
var explanation="";
if("string"===typeof values[0]){
explanation+=" ("+values.join("; ")+")";
} else {
var parts=new Array();
for(var i=0; i<values.length; i++){
var val=values[i];
if(!val.name || !val.country){ continue; }
parts.push(val.name+", "+val.country);
}
if(0!=parts.length){
explanation+=' ('+parts.join("; ")+')';
}
node.innerHTML+='<span class="'+nasTooltips.inlineExplanationTagClass+'">'+explanation+'</span>';
}
},
/**
* Shows the tooltip explaining the moused-over acronym.
* @param {Event} event The triggering mouseover/touchstart event
*/
showCodeTooltip:function(evt){
var span=evt.target;
var key=span.innerHTML;
var values=nasTooltips.results[key];
if(!values || 0==values.length){ return false; }
if(document.getElementById(nasTooltips.tooltipTagId)){
document.getElementById(nasTooltips.tooltipTagId).remove();
}
var tooltip=document.createElement("span");
tooltip.style.whitespace="pre";
tooltip.id=nasTooltips.tooltipTagId;
if("string"===typeof values[0]){
tooltip.innerHTML=values.join("<br/>");
} else {
var parts=new Array();
for(var i=0; i<values.length; i++){
var val=values[i];
if(!val.name || !val.country){ continue; }
var extraClasses='';
if(val.kind){ extraClasses+=' nastt_kind_'+val.kind; }
if(undefined!==val.isCurrent){
if(1==1*(val.isCurrent)){
extraClasses+=' nastt_current_yes';
} else {
extraClasses+=' nastt_current_no';
}
}
if(""!=extraClasses){ extraClasses=' class="'+extraClasses.trim()+'"'; }
parts.push('<span'+extraClasses+'>'+val.name+" ("+val.country+")</span>");
}
tooltip.innerHTML=parts.join("<br/>");
}
span.appendChild(tooltip);
nasTooltips.forceTooltipIntoWindow();
},
/**
* Ensures that the tooltip does not run off the top or right-hand edge of the screen.
*/
forceTooltipIntoWindow:function(){
var tt=document.getElementById(nasTooltips.tooltipTagId);
if(!tt){ return; }
var box=tt.getBoundingClientRect();
var displayWidth = document.documentElement.clientWidth;
if(box.top<0){ tt.style.top=tt.className=tt.className+" nastt_offtopedge"; }
if(box.right>displayWidth){ tt.className=tt.className+" nastt_offrightedge"; }
},
/**
* Hides the tooltip by removing the element.
* @param {Event} event The triggering mouseout/touchend event
*/
hideCodeTooltip:function(evt){
var span=evt.target;
if(document.getElementById(nasTooltips.tooltipTagId)){
document.getElementById(nasTooltips.tooltipTagId).remove();
}
},
/**
* Recursively searches the DOM structure of the given node and returns an array of text nodes, excluding those
* contained in elements whose tagName occurs in the tagsToExclude array.
* @param elem a DOM element such as a div
* @return An array of textNode objects. If none are found, an empty array is returned.
*/
getChildTextNodes:function(elem){
var found=[];
if(!elem.hasChildNodes()){ return found; }
var children=elem.childNodes;
var numChildren=children.length;
for(var i=0;i<numChildren;i++){
var child=children[i];
if(child.tagName && nasTooltips.tagsToExclude.indexOf(child.tagName.toLowerCase())>=0){
//ignore
} else if(Node.TEXT_NODE===child.nodeType){
found.push(child);
} else if(Node.ELEMENT_NODE===child.nodeType){
found=found.concat(nasTooltips.getChildTextNodes(child));
}
}
return found;
},
/**
* Checks for the existence of the supplied CSS selector in all included stylesheets.
* @return true if the CSS selector is found in at least one stylesheet, otherwise false.
*/
selectorHasStyle:function(selector){
for(var i=0; i<document.styleSheets.length; i++){
var styleSheet = document.styleSheets[i];
var cssRules=styleSheet.rules ? styleSheet.rules : styleSheet.cssRules;
for (var j=0; j<cssRules.length; ++j) {
if(cssRules[j].selectorText==selector) return true;
}
}
return false;
},
/**
* If the acronym and tooltip elements are not styled, adds a new stylesheet with the default styles.
* @see defaultAcronymStyle
* @see defaultTooltipStyle
*/
applyDefaultStylesIfNeeded:function(){
var acronymSelector='.'+nasTooltips.wrapperTagClass;
var tooltipSelector='#'+nasTooltips.tooltipTagId;
var topEdgeSelector='.'+nasTooltips.tooltipOffTopEdgeClass;
var rightEdgeSelector='.'+nasTooltips.tooltipOffLeftEdgeClass;
var stylesheetContent='';
if(!nasTooltips.selectorHasStyle(acronymSelector) && !nasTooltips.selectorHasStyle("span"+acronymSelector)){
stylesheetContent+="span"+acronymSelector+" { "+ nasTooltips.defaultAcronymStyle +" }\n";
}
if(!nasTooltips.selectorHasStyle(tooltipSelector) && !nasTooltips.selectorHasStyle("span"+tooltipSelector)){
stylesheetContent+="span"+tooltipSelector+" { "+ nasTooltips.defaultTooltipStyle +" }\n";
}
if(!nasTooltips.selectorHasStyle(topEdgeSelector) && !nasTooltips.selectorHasStyle("span"+topEdgeSelector)){
stylesheetContent+="span"+tooltipSelector+"."+nasTooltips.tooltipOffTopEdgeClass+"{ "+ nasTooltips.defaultTooltipStyleOffTopEdge +" }\n";
}
if(!nasTooltips.selectorHasStyle(rightEdgeSelector) && !nasTooltips.selectorHasStyle("span"+rightEdgeSelector)){
stylesheetContent+="span"+tooltipSelector+"."+nasTooltips.tooltipOffRightEdgeClass+"{ "+ nasTooltips.defaultTooltipStyleOffRightEdge +" }\n";
}
if(""!=stylesheetContent){
var sheet=document.createElement("style");
sheet.innerHTML=stylesheetContent;
document.body.appendChild(sheet);
}
},
}