2 @licstart The following is the entire license notice for the
3 JavaScript code in this file.
5 Copyright (C) 1997-2017 by Dimitri van Heesch
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 @licend The above is the entire license notice
22 for the JavaScript code in this file
24 function convertToId(search)
27 for (i=0;i<search.length;i++)
29 var c = search.charAt(i);
30 var cn = c.charCodeAt(0);
31 if (c.match(/[a-z0-9\u0080-\uFFFF]/))
37 result+="_0"+cn.toString(16);
41 result+="_"+cn.toString(16);
47 function getXPos(item)
52 while (item && item!=document.body)
55 item = item.offsetParent;
61 function getYPos(item)
66 while (item && item!=document.body)
69 item = item.offsetParent;
75 /* A class handling everything associated with the search panel.
78 name - The name of the global variable that will be
79 storing this instance. Is needed to be able to set timeouts.
80 resultPath - path to use for external files
82 function SearchBox(name, resultsPath, inFrame, label)
84 if (!name || !resultsPath) { alert("Missing parameters to SearchBox."); }
86 // ---------- Instance variables
88 this.resultsPath = resultsPath;
90 this.keyTimeoutLength = 500;
91 this.closeSelectionTimeout = 300;
92 this.lastSearchValue = "";
93 this.lastResultsPage = "";
96 this.searchActive = false;
97 this.insideFrame = inFrame;
98 this.searchLabel = label;
100 // ----------- DOM Elements
102 this.DOMSearchField = function()
103 { return document.getElementById("MSearchField"); }
105 this.DOMSearchSelect = function()
106 { return document.getElementById("MSearchSelect"); }
108 this.DOMSearchSelectWindow = function()
109 { return document.getElementById("MSearchSelectWindow"); }
111 this.DOMPopupSearchResults = function()
112 { return document.getElementById("MSearchResults"); }
114 this.DOMPopupSearchResultsWindow = function()
115 { return document.getElementById("MSearchResultsWindow"); }
117 this.DOMSearchClose = function()
118 { return document.getElementById("MSearchClose"); }
120 this.DOMSearchBox = function()
121 { return document.getElementById("MSearchBox"); }
123 // ------------ Event Handlers
125 // Called when focus is added or removed from the search field.
126 this.OnSearchFieldFocus = function(isActive)
128 this.Activate(isActive);
131 this.OnSearchSelectShow = function()
133 var searchSelectWindow = this.DOMSearchSelectWindow();
134 var searchField = this.DOMSearchSelect();
136 if (this.insideFrame)
138 var left = getXPos(searchField);
139 var top = getYPos(searchField);
140 left += searchField.offsetWidth + 6;
141 top += searchField.offsetHeight;
143 // show search selection popup
144 searchSelectWindow.style.display='block';
145 left -= searchSelectWindow.offsetWidth;
146 searchSelectWindow.style.left = left + 'px';
147 searchSelectWindow.style.top = top + 'px';
151 var left = getXPos(searchField);
152 var top = getYPos(searchField);
153 top += searchField.offsetHeight;
155 // show search selection popup
156 searchSelectWindow.style.display='block';
157 searchSelectWindow.style.left = left + 'px';
158 searchSelectWindow.style.top = top + 'px';
161 // stop selection hide timer
162 if (this.hideTimeout)
164 clearTimeout(this.hideTimeout);
167 return false; // to avoid "image drag" default event
170 this.OnSearchSelectHide = function()
172 this.hideTimeout = setTimeout(this.name +".CloseSelectionWindow()",
173 this.closeSelectionTimeout);
176 // Called when the content of the search field is changed.
177 this.OnSearchFieldChange = function(evt)
179 if (this.keyTimeout) // kill running timer
181 clearTimeout(this.keyTimeout);
185 var e = (evt) ? evt : window.event; // for IE
186 if (e.keyCode==40 || e.keyCode==13)
190 this.OnSearchSelectShow();
191 var win=this.DOMSearchSelectWindow();
192 for (i=0;i<win.childNodes.length;i++)
194 var child = win.childNodes[i]; // get span within a
195 if (child.className=='SelectItem')
203 else if (window.frames.MSearchResults.searchResults)
205 var elem = window.frames.MSearchResults.searchResults.NavNext(0);
206 if (elem) elem.focus();
209 else if (e.keyCode==27) // Escape out of the search field
211 this.DOMSearchField().blur();
212 this.DOMPopupSearchResultsWindow().style.display = 'none';
213 this.DOMSearchClose().style.display = 'none';
214 this.lastSearchValue = '';
215 this.Activate(false);
220 var searchValue = this.DOMSearchField().value.replace(/ +/g, "");
222 if (searchValue != this.lastSearchValue) // search value has changed
224 if (searchValue != "") // non-empty search
226 // set timer for search update
227 this.keyTimeout = setTimeout(this.name + '.Search()',
228 this.keyTimeoutLength);
230 else // empty search field
232 this.DOMPopupSearchResultsWindow().style.display = 'none';
233 this.DOMSearchClose().style.display = 'none';
234 this.lastSearchValue = '';
239 this.SelectItemCount = function(id)
242 var win=this.DOMSearchSelectWindow();
243 for (i=0;i<win.childNodes.length;i++)
245 var child = win.childNodes[i]; // get span within a
246 if (child.className=='SelectItem')
254 this.SelectItemSet = function(id)
257 var win=this.DOMSearchSelectWindow();
258 for (i=0;i<win.childNodes.length;i++)
260 var child = win.childNodes[i]; // get span within a
261 if (child.className=='SelectItem')
263 var node = child.firstChild;
266 node.innerHTML='•';
270 node.innerHTML=' ';
277 // Called when an search filter selection is made.
278 // set item with index id as the active item
279 this.OnSelectItem = function(id)
281 this.searchIndex = id;
282 this.SelectItemSet(id);
283 var searchValue = this.DOMSearchField().value.replace(/ +/g, "");
284 if (searchValue!="" && this.searchActive) // something was found -> do a search
290 this.OnSearchSelectKey = function(evt)
292 var e = (evt) ? evt : window.event; // for IE
293 if (e.keyCode==40 && this.searchIndex<this.SelectItemCount()) // Down
296 this.OnSelectItem(this.searchIndex);
298 else if (e.keyCode==38 && this.searchIndex>0) // Up
301 this.OnSelectItem(this.searchIndex);
303 else if (e.keyCode==13 || e.keyCode==27)
305 this.OnSelectItem(this.searchIndex);
306 this.CloseSelectionWindow();
307 this.DOMSearchField().focus();
314 // Closes the results window.
315 this.CloseResultsWindow = function()
317 this.DOMPopupSearchResultsWindow().style.display = 'none';
318 this.DOMSearchClose().style.display = 'none';
319 this.Activate(false);
322 this.CloseSelectionWindow = function()
324 this.DOMSearchSelectWindow().style.display = 'none';
327 // Performs a search.
328 this.Search = function()
332 // strip leading whitespace
333 var searchValue = this.DOMSearchField().value.replace(/^ +/, "");
335 var code = searchValue.toLowerCase().charCodeAt(0);
336 var idxChar = searchValue.substr(0, 1).toLowerCase();
337 if ( 0xD800 <= code && code <= 0xDBFF && searchValue > 1) // surrogate pair
339 idxChar = searchValue.substr(0, 2);
343 var resultsPageWithSearch;
346 var idx = indexSectionsWithContent[this.searchIndex].indexOf(idxChar);
349 var hexCode=idx.toString(16);
350 resultsPage = this.resultsPath + '/' + indexSectionNames[this.searchIndex] + '_' + hexCode + '.html';
351 resultsPageWithSearch = resultsPage+'?'+escape(searchValue);
352 hasResultsPage = true;
354 else // nothing available for this search term
356 resultsPage = this.resultsPath + '/nomatches.html';
357 resultsPageWithSearch = resultsPage;
358 hasResultsPage = false;
361 window.frames.MSearchResults.location = resultsPageWithSearch;
362 var domPopupSearchResultsWindow = this.DOMPopupSearchResultsWindow();
364 if (domPopupSearchResultsWindow.style.display!='block')
366 var domSearchBox = this.DOMSearchBox();
367 this.DOMSearchClose().style.display = 'inline';
368 if (this.insideFrame)
370 var domPopupSearchResults = this.DOMPopupSearchResults();
371 domPopupSearchResultsWindow.style.position = 'relative';
372 domPopupSearchResultsWindow.style.display = 'block';
373 var width = document.body.clientWidth - 8; // the -8 is for IE :-(
374 domPopupSearchResultsWindow.style.width = width + 'px';
375 domPopupSearchResults.style.width = width + 'px';
379 var domPopupSearchResults = this.DOMPopupSearchResults();
380 var left = getXPos(domSearchBox) + 150; // domSearchBox.offsetWidth;
381 var top = getYPos(domSearchBox) + 20; // domSearchBox.offsetHeight + 1;
382 domPopupSearchResultsWindow.style.display = 'block';
383 left -= domPopupSearchResults.offsetWidth;
384 domPopupSearchResultsWindow.style.top = top + 'px';
385 domPopupSearchResultsWindow.style.left = left + 'px';
389 this.lastSearchValue = searchValue;
390 this.lastResultsPage = resultsPage;
393 // -------- Activation Functions
395 // Activates or deactivates the search panel, resetting things to
396 // their default values if necessary.
397 this.Activate = function(isActive)
399 if (isActive || // open it
400 this.DOMPopupSearchResultsWindow().style.display == 'block'
403 this.DOMSearchBox().className = 'MSearchBoxActive';
405 var searchField = this.DOMSearchField();
407 if (searchField.value == this.searchLabel) // clear "Search" term upon entry
409 searchField.value = '';
410 this.searchActive = true;
413 else if (!isActive) // directly remove the panel
415 this.DOMSearchBox().className = 'MSearchBoxInactive';
416 this.DOMSearchField().value = this.searchLabel;
417 this.searchActive = false;
418 this.lastSearchValue = ''
419 this.lastResultsPage = '';
424 // -----------------------------------------------------------------------
426 // The class that handles everything on the search results page.
427 function SearchResults(name)
429 // The number of matches from the last run of <Search()>.
430 this.lastMatchCount = 0;
432 this.repeatOn = false;
434 // Toggles the visibility of the passed element ID.
435 this.FindChildElement = function(id)
437 var parentElement = document.getElementById(id);
438 var element = parentElement.firstChild;
440 while (element && element!=parentElement)
442 if (element.nodeName == 'DIV' && element.className == 'SRChildren')
447 if (element.nodeName == 'DIV' && element.hasChildNodes())
449 element = element.firstChild;
451 else if (element.nextSibling)
453 element = element.nextSibling;
459 element = element.parentNode;
461 while (element && element!=parentElement && !element.nextSibling);
463 if (element && element!=parentElement)
465 element = element.nextSibling;
471 this.Toggle = function(id)
473 var element = this.FindChildElement(id);
476 if (element.style.display == 'block')
478 element.style.display = 'none';
482 element.style.display = 'block';
487 // Searches for the passed string. If there is no parameter,
488 // it takes it from the URL query.
490 // Always returns true, since other documents may try to call it
491 // and that may or may not be possible.
492 this.Search = function(search)
494 if (!search) // get search word from URL
496 search = window.location.search;
497 search = search.substring(1); // Remove the leading '?'
498 search = unescape(search);
501 search = search.replace(/^ +/, ""); // strip leading spaces
502 search = search.replace(/ +$/, ""); // strip trailing spaces
503 search = search.toLowerCase();
504 search = convertToId(search);
506 var resultRows = document.getElementsByTagName("div");
510 while (i < resultRows.length)
512 var row = resultRows.item(i);
513 if (row.className == "SRResult")
515 var rowMatchName = row.id.toLowerCase();
516 rowMatchName = rowMatchName.replace(/^sr\d*_/, ''); // strip 'sr123_'
518 if (search.length<=rowMatchName.length &&
519 rowMatchName.substr(0, search.length)==search)
521 row.style.display = 'block';
526 row.style.display = 'none';
531 document.getElementById("Searching").style.display='none';
532 if (matches == 0) // no results
534 document.getElementById("NoMatches").style.display='block';
536 else // at least one result
538 document.getElementById("NoMatches").style.display='none';
540 this.lastMatchCount = matches;
544 // return the first item with index index or higher that is visible
545 this.NavNext = function(index)
550 var focusName = 'Item'+index;
551 focusItem = document.getElementById(focusName);
552 if (focusItem && focusItem.parentNode.parentNode.style.display=='block')
556 else if (!focusItem) // last element
566 this.NavPrev = function(index)
571 var focusName = 'Item'+index;
572 focusItem = document.getElementById(focusName);
573 if (focusItem && focusItem.parentNode.parentNode.style.display=='block')
577 else if (!focusItem) // last element
587 this.ProcessKeys = function(e)
589 if (e.type == "keydown")
591 this.repeatOn = false;
592 this.lastKey = e.keyCode;
594 else if (e.type == "keypress")
598 if (this.lastKey) this.repeatOn = true;
599 return false; // ignore first keypress after keydown
602 else if (e.type == "keyup")
605 this.repeatOn = false;
607 return this.lastKey!=0;
610 this.Nav = function(evt,itemIndex)
612 var e = (evt) ? evt : window.event; // for IE
613 if (e.keyCode==13) return true;
614 if (!this.ProcessKeys(e)) return false;
616 if (this.lastKey==38) // Up
618 var newIndex = itemIndex-1;
619 var focusItem = this.NavPrev(newIndex);
622 var child = this.FindChildElement(focusItem.parentNode.parentNode.id);
623 if (child && child.style.display == 'block') // children visible
627 while (1) // search for last child
629 tmpElem = document.getElementById('Item'+newIndex+'_c'+n);
646 else // return focus to search field
648 parent.document.getElementById("MSearchField").focus();
651 else if (this.lastKey==40) // Down
653 var newIndex = itemIndex+1;
655 var item = document.getElementById('Item'+itemIndex);
656 var elem = this.FindChildElement(item.parentNode.parentNode.id);
657 if (elem && elem.style.display == 'block') // children visible
659 focusItem = document.getElementById('Item'+itemIndex+'_c0');
661 if (!focusItem) focusItem = this.NavNext(newIndex);
662 if (focusItem) focusItem.focus();
664 else if (this.lastKey==39) // Right
666 var item = document.getElementById('Item'+itemIndex);
667 var elem = this.FindChildElement(item.parentNode.parentNode.id);
668 if (elem) elem.style.display = 'block';
670 else if (this.lastKey==37) // Left
672 var item = document.getElementById('Item'+itemIndex);
673 var elem = this.FindChildElement(item.parentNode.parentNode.id);
674 if (elem) elem.style.display = 'none';
676 else if (this.lastKey==27) // Escape
678 parent.searchBox.CloseResultsWindow();
679 parent.document.getElementById("MSearchField").focus();
681 else if (this.lastKey==13) // Enter
688 this.NavChild = function(evt,itemIndex,childIndex)
690 var e = (evt) ? evt : window.event; // for IE
691 if (e.keyCode==13) return true;
692 if (!this.ProcessKeys(e)) return false;
694 if (this.lastKey==38) // Up
698 var newIndex = childIndex-1;
699 document.getElementById('Item'+itemIndex+'_c'+newIndex).focus();
701 else // already at first child, jump to parent
703 document.getElementById('Item'+itemIndex).focus();
706 else if (this.lastKey==40) // Down
708 var newIndex = childIndex+1;
709 var elem = document.getElementById('Item'+itemIndex+'_c'+newIndex);
710 if (!elem) // last child, jump to parent next parent
712 elem = this.NavNext(itemIndex+1);
719 else if (this.lastKey==27) // Escape
721 parent.searchBox.CloseResultsWindow();
722 parent.document.getElementById("MSearchField").focus();
724 else if (this.lastKey==13) // Enter
732 function setKeyActions(elem,action)
734 elem.setAttribute('onkeydown',action);
735 elem.setAttribute('onkeypress',action);
736 elem.setAttribute('onkeyup',action);
739 function setClassAttr(elem,attr)
741 elem.setAttribute('class',attr);
742 elem.setAttribute('className',attr);
745 function createResults()
747 var results = document.getElementById("SRResults");
748 for (var e=0; e<searchData.length; e++)
750 var id = searchData[e][0];
751 var srResult = document.createElement('div');
752 srResult.setAttribute('id','SR_'+id);
753 setClassAttr(srResult,'SRResult');
754 var srEntry = document.createElement('div');
755 setClassAttr(srEntry,'SREntry');
756 var srLink = document.createElement('a');
757 srLink.setAttribute('id','Item'+e);
758 setKeyActions(srLink,'return searchResults.Nav(event,'+e+')');
759 setClassAttr(srLink,'SRSymbol');
760 srLink.innerHTML = searchData[e][1][0];
761 srEntry.appendChild(srLink);
762 if (searchData[e][1].length==2) // single result
764 srLink.setAttribute('href',searchData[e][1][1][0]);
765 if (searchData[e][1][1][1])
767 srLink.setAttribute('target','_parent');
769 var srScope = document.createElement('span');
770 setClassAttr(srScope,'SRScope');
771 srScope.innerHTML = searchData[e][1][1][2];
772 srEntry.appendChild(srScope);
774 else // multiple results
776 srLink.setAttribute('href','javascript:searchResults.Toggle("SR_'+id+'")');
777 var srChildren = document.createElement('div');
778 setClassAttr(srChildren,'SRChildren');
779 for (var c=0; c<searchData[e][1].length-1; c++)
781 var srChild = document.createElement('a');
782 srChild.setAttribute('id','Item'+e+'_c'+c);
783 setKeyActions(srChild,'return searchResults.NavChild(event,'+e+','+c+')');
784 setClassAttr(srChild,'SRScope');
785 srChild.setAttribute('href',searchData[e][1][c+1][0]);
786 if (searchData[e][1][c+1][1])
788 srChild.setAttribute('target','_parent');
790 srChild.innerHTML = searchData[e][1][c+1][2];
791 srChildren.appendChild(srChild);
793 srEntry.appendChild(srChildren);
795 srResult.appendChild(srEntry);
796 results.appendChild(srResult);
800 function init_search()
802 var results = document.getElementById("MSearchSelectWindow");
803 for (var key in indexSectionLabels)
805 var link = document.createElement('a');
806 link.setAttribute('class','SelectItem');
807 link.setAttribute('onclick','searchBox.OnSelectItem('+key+')');
808 link.href='javascript:void(0)';
809 link.innerHTML='<span class="SelectionMark"> </span>'+indexSectionLabels[key];
810 results.appendChild(link);
812 searchBox.OnSelectItem(0);