1 // Search script generated by doxygen
2 // Copyright (C) 2009 by Dimitri van Heesch.
4 // The code in this file is loosly based on main.js, part of Natural Docs,
5 // which is Copyright (C) 2003-2008 Greg Valure
6 // Natural Docs is licensed under the GPL.
8 var indexSectionsWithContent =
10 0: "_abcdefghijklmnopqrstuvwxyz~",
13 3: "acdefghjmnprstuv",
14 4: "_abcdefghijklmnopqrstuvwxyz~",
15 5: "_abcdefghijklmnoprstuvwxyz",
18 8: "abcdeghilmnoprstvwxyz",
20 10: "_bdefgilmnopqrstuvw",
25 var indexSectionNames =
42 function convertToId(search)
45 for (i=0;i<search.length;i++)
47 var c = search.charAt(i);
48 var cn = c.charCodeAt(0);
49 if (c.match(/[a-z0-9\u0080-\uFFFF]/))
55 result+="_0"+cn.toString(16);
59 result+="_"+cn.toString(16);
65 function getXPos(item)
70 while (item && item!=document.body)
73 item = item.offsetParent;
79 function getYPos(item)
84 while (item && item!=document.body)
87 item = item.offsetParent;
93 /* A class handling everything associated with the search panel.
96 name - The name of the global variable that will be
97 storing this instance. Is needed to be able to set timeouts.
98 resultPath - path to use for external files
100 function SearchBox(name, resultsPath, inFrame, label)
102 if (!name || !resultsPath) { alert("Missing parameters to SearchBox."); }
104 // ---------- Instance variables
106 this.resultsPath = resultsPath;
108 this.keyTimeoutLength = 500;
109 this.closeSelectionTimeout = 300;
110 this.lastSearchValue = "";
111 this.lastResultsPage = "";
112 this.hideTimeout = 0;
113 this.searchIndex = 0;
114 this.searchActive = false;
115 this.insideFrame = inFrame;
116 this.searchLabel = label;
118 // ----------- DOM Elements
120 this.DOMSearchField = function()
121 { return document.getElementById("MSearchField"); }
123 this.DOMSearchSelect = function()
124 { return document.getElementById("MSearchSelect"); }
126 this.DOMSearchSelectWindow = function()
127 { return document.getElementById("MSearchSelectWindow"); }
129 this.DOMPopupSearchResults = function()
130 { return document.getElementById("MSearchResults"); }
132 this.DOMPopupSearchResultsWindow = function()
133 { return document.getElementById("MSearchResultsWindow"); }
135 this.DOMSearchClose = function()
136 { return document.getElementById("MSearchClose"); }
138 this.DOMSearchBox = function()
139 { return document.getElementById("MSearchBox"); }
141 // ------------ Event Handlers
143 // Called when focus is added or removed from the search field.
144 this.OnSearchFieldFocus = function(isActive)
146 this.Activate(isActive);
149 this.OnSearchSelectShow = function()
151 var searchSelectWindow = this.DOMSearchSelectWindow();
152 var searchField = this.DOMSearchSelect();
154 if (this.insideFrame)
156 var left = getXPos(searchField);
157 var top = getYPos(searchField);
158 left += searchField.offsetWidth + 6;
159 top += searchField.offsetHeight;
161 // show search selection popup
162 searchSelectWindow.style.display='block';
163 left -= searchSelectWindow.offsetWidth;
164 searchSelectWindow.style.left = left + 'px';
165 searchSelectWindow.style.top = top + 'px';
169 var left = getXPos(searchField);
170 var top = getYPos(searchField);
171 top += searchField.offsetHeight;
173 // show search selection popup
174 searchSelectWindow.style.display='block';
175 searchSelectWindow.style.left = left + 'px';
176 searchSelectWindow.style.top = top + 'px';
179 // stop selection hide timer
180 if (this.hideTimeout)
182 clearTimeout(this.hideTimeout);
185 return false; // to avoid "image drag" default event
188 this.OnSearchSelectHide = function()
190 this.hideTimeout = setTimeout(this.name +".CloseSelectionWindow()",
191 this.closeSelectionTimeout);
194 // Called when the content of the search field is changed.
195 this.OnSearchFieldChange = function(evt)
197 if (this.keyTimeout) // kill running timer
199 clearTimeout(this.keyTimeout);
203 var e = (evt) ? evt : window.event; // for IE
204 if (e.keyCode==40 || e.keyCode==13)
208 this.OnSearchSelectShow();
209 var win=this.DOMSearchSelectWindow();
210 for (i=0;i<win.childNodes.length;i++)
212 var child = win.childNodes[i]; // get span within a
213 if (child.className=='SelectItem')
221 else if (window.frames.MSearchResults.searchResults)
223 var elem = window.frames.MSearchResults.searchResults.NavNext(0);
224 if (elem) elem.focus();
227 else if (e.keyCode==27) // Escape out of the search field
229 this.DOMSearchField().blur();
230 this.DOMPopupSearchResultsWindow().style.display = 'none';
231 this.DOMSearchClose().style.display = 'none';
232 this.lastSearchValue = '';
233 this.Activate(false);
238 var searchValue = this.DOMSearchField().value.replace(/ +/g, "");
240 if (searchValue != this.lastSearchValue) // search value has changed
242 if (searchValue != "") // non-empty search
244 // set timer for search update
245 this.keyTimeout = setTimeout(this.name + '.Search()',
246 this.keyTimeoutLength);
248 else // empty search field
250 this.DOMPopupSearchResultsWindow().style.display = 'none';
251 this.DOMSearchClose().style.display = 'none';
252 this.lastSearchValue = '';
257 this.SelectItemCount = function(id)
260 var win=this.DOMSearchSelectWindow();
261 for (i=0;i<win.childNodes.length;i++)
263 var child = win.childNodes[i]; // get span within a
264 if (child.className=='SelectItem')
272 this.SelectItemSet = function(id)
275 var win=this.DOMSearchSelectWindow();
276 for (i=0;i<win.childNodes.length;i++)
278 var child = win.childNodes[i]; // get span within a
279 if (child.className=='SelectItem')
281 var node = child.firstChild;
284 node.innerHTML='•';
288 node.innerHTML=' ';
295 // Called when an search filter selection is made.
296 // set item with index id as the active item
297 this.OnSelectItem = function(id)
299 this.searchIndex = id;
300 this.SelectItemSet(id);
301 var searchValue = this.DOMSearchField().value.replace(/ +/g, "");
302 if (searchValue!="" && this.searchActive) // something was found -> do a search
308 this.OnSearchSelectKey = function(evt)
310 var e = (evt) ? evt : window.event; // for IE
311 if (e.keyCode==40 && this.searchIndex<this.SelectItemCount()) // Down
314 this.OnSelectItem(this.searchIndex);
316 else if (e.keyCode==38 && this.searchIndex>0) // Up
319 this.OnSelectItem(this.searchIndex);
321 else if (e.keyCode==13 || e.keyCode==27)
323 this.OnSelectItem(this.searchIndex);
324 this.CloseSelectionWindow();
325 this.DOMSearchField().focus();
332 // Closes the results window.
333 this.CloseResultsWindow = function()
335 this.DOMPopupSearchResultsWindow().style.display = 'none';
336 this.DOMSearchClose().style.display = 'none';
337 this.Activate(false);
340 this.CloseSelectionWindow = function()
342 this.DOMSearchSelectWindow().style.display = 'none';
345 // Performs a search.
346 this.Search = function()
350 // strip leading whitespace
351 var searchValue = this.DOMSearchField().value.replace(/^ +/, "");
353 var code = searchValue.toLowerCase().charCodeAt(0);
354 var idxChar = searchValue.substr(0, 1).toLowerCase();
355 if ( 0xD800 <= code && code <= 0xDBFF && searchValue > 1) // surrogate pair
357 idxChar = searchValue.substr(0, 2);
361 var resultsPageWithSearch;
364 var idx = indexSectionsWithContent[this.searchIndex].indexOf(idxChar);
367 var hexCode=idx.toString(16);
368 resultsPage = this.resultsPath + '/' + indexSectionNames[this.searchIndex] + '_' + hexCode + '.html';
369 resultsPageWithSearch = resultsPage+'?'+escape(searchValue);
370 hasResultsPage = true;
372 else // nothing available for this search term
374 resultsPage = this.resultsPath + '/nomatches.html';
375 resultsPageWithSearch = resultsPage;
376 hasResultsPage = false;
379 window.frames.MSearchResults.location = resultsPageWithSearch;
380 var domPopupSearchResultsWindow = this.DOMPopupSearchResultsWindow();
382 if (domPopupSearchResultsWindow.style.display!='block')
384 var domSearchBox = this.DOMSearchBox();
385 this.DOMSearchClose().style.display = 'inline';
386 if (this.insideFrame)
388 var domPopupSearchResults = this.DOMPopupSearchResults();
389 domPopupSearchResultsWindow.style.position = 'relative';
390 domPopupSearchResultsWindow.style.display = 'block';
391 var width = document.body.clientWidth - 8; // the -8 is for IE :-(
392 domPopupSearchResultsWindow.style.width = width + 'px';
393 domPopupSearchResults.style.width = width + 'px';
397 var domPopupSearchResults = this.DOMPopupSearchResults();
398 var left = getXPos(domSearchBox) + 150; // domSearchBox.offsetWidth;
399 var top = getYPos(domSearchBox) + 20; // domSearchBox.offsetHeight + 1;
400 domPopupSearchResultsWindow.style.display = 'block';
401 left -= domPopupSearchResults.offsetWidth;
402 domPopupSearchResultsWindow.style.top = top + 'px';
403 domPopupSearchResultsWindow.style.left = left + 'px';
407 this.lastSearchValue = searchValue;
408 this.lastResultsPage = resultsPage;
411 // -------- Activation Functions
413 // Activates or deactivates the search panel, resetting things to
414 // their default values if necessary.
415 this.Activate = function(isActive)
417 if (isActive || // open it
418 this.DOMPopupSearchResultsWindow().style.display == 'block'
421 this.DOMSearchBox().className = 'MSearchBoxActive';
423 var searchField = this.DOMSearchField();
425 if (searchField.value == this.searchLabel) // clear "Search" term upon entry
427 searchField.value = '';
428 this.searchActive = true;
431 else if (!isActive) // directly remove the panel
433 this.DOMSearchBox().className = 'MSearchBoxInactive';
434 this.DOMSearchField().value = this.searchLabel;
435 this.searchActive = false;
436 this.lastSearchValue = ''
437 this.lastResultsPage = '';
442 // -----------------------------------------------------------------------
444 // The class that handles everything on the search results page.
445 function SearchResults(name)
447 // The number of matches from the last run of <Search()>.
448 this.lastMatchCount = 0;
450 this.repeatOn = false;
452 // Toggles the visibility of the passed element ID.
453 this.FindChildElement = function(id)
455 var parentElement = document.getElementById(id);
456 var element = parentElement.firstChild;
458 while (element && element!=parentElement)
460 if (element.nodeName == 'DIV' && element.className == 'SRChildren')
465 if (element.nodeName == 'DIV' && element.hasChildNodes())
467 element = element.firstChild;
469 else if (element.nextSibling)
471 element = element.nextSibling;
477 element = element.parentNode;
479 while (element && element!=parentElement && !element.nextSibling);
481 if (element && element!=parentElement)
483 element = element.nextSibling;
489 this.Toggle = function(id)
491 var element = this.FindChildElement(id);
494 if (element.style.display == 'block')
496 element.style.display = 'none';
500 element.style.display = 'block';
505 // Searches for the passed string. If there is no parameter,
506 // it takes it from the URL query.
508 // Always returns true, since other documents may try to call it
509 // and that may or may not be possible.
510 this.Search = function(search)
512 if (!search) // get search word from URL
514 search = window.location.search;
515 search = search.substring(1); // Remove the leading '?'
516 search = unescape(search);
519 search = search.replace(/^ +/, ""); // strip leading spaces
520 search = search.replace(/ +$/, ""); // strip trailing spaces
521 search = search.toLowerCase();
522 search = convertToId(search);
524 var resultRows = document.getElementsByTagName("div");
528 while (i < resultRows.length)
530 var row = resultRows.item(i);
531 if (row.className == "SRResult")
533 var rowMatchName = row.id.toLowerCase();
534 rowMatchName = rowMatchName.replace(/^sr\d*_/, ''); // strip 'sr123_'
536 if (search.length<=rowMatchName.length &&
537 rowMatchName.substr(0, search.length)==search)
539 row.style.display = 'block';
544 row.style.display = 'none';
549 document.getElementById("Searching").style.display='none';
550 if (matches == 0) // no results
552 document.getElementById("NoMatches").style.display='block';
554 else // at least one result
556 document.getElementById("NoMatches").style.display='none';
558 this.lastMatchCount = matches;
562 // return the first item with index index or higher that is visible
563 this.NavNext = function(index)
568 var focusName = 'Item'+index;
569 focusItem = document.getElementById(focusName);
570 if (focusItem && focusItem.parentNode.parentNode.style.display=='block')
574 else if (!focusItem) // last element
584 this.NavPrev = function(index)
589 var focusName = 'Item'+index;
590 focusItem = document.getElementById(focusName);
591 if (focusItem && focusItem.parentNode.parentNode.style.display=='block')
595 else if (!focusItem) // last element
605 this.ProcessKeys = function(e)
607 if (e.type == "keydown")
609 this.repeatOn = false;
610 this.lastKey = e.keyCode;
612 else if (e.type == "keypress")
616 if (this.lastKey) this.repeatOn = true;
617 return false; // ignore first keypress after keydown
620 else if (e.type == "keyup")
623 this.repeatOn = false;
625 return this.lastKey!=0;
628 this.Nav = function(evt,itemIndex)
630 var e = (evt) ? evt : window.event; // for IE
631 if (e.keyCode==13) return true;
632 if (!this.ProcessKeys(e)) return false;
634 if (this.lastKey==38) // Up
636 var newIndex = itemIndex-1;
637 var focusItem = this.NavPrev(newIndex);
640 var child = this.FindChildElement(focusItem.parentNode.parentNode.id);
641 if (child && child.style.display == 'block') // children visible
645 while (1) // search for last child
647 tmpElem = document.getElementById('Item'+newIndex+'_c'+n);
664 else // return focus to search field
666 parent.document.getElementById("MSearchField").focus();
669 else if (this.lastKey==40) // Down
671 var newIndex = itemIndex+1;
673 var item = document.getElementById('Item'+itemIndex);
674 var elem = this.FindChildElement(item.parentNode.parentNode.id);
675 if (elem && elem.style.display == 'block') // children visible
677 focusItem = document.getElementById('Item'+itemIndex+'_c0');
679 if (!focusItem) focusItem = this.NavNext(newIndex);
680 if (focusItem) focusItem.focus();
682 else if (this.lastKey==39) // Right
684 var item = document.getElementById('Item'+itemIndex);
685 var elem = this.FindChildElement(item.parentNode.parentNode.id);
686 if (elem) elem.style.display = 'block';
688 else if (this.lastKey==37) // Left
690 var item = document.getElementById('Item'+itemIndex);
691 var elem = this.FindChildElement(item.parentNode.parentNode.id);
692 if (elem) elem.style.display = 'none';
694 else if (this.lastKey==27) // Escape
696 parent.searchBox.CloseResultsWindow();
697 parent.document.getElementById("MSearchField").focus();
699 else if (this.lastKey==13) // Enter
706 this.NavChild = function(evt,itemIndex,childIndex)
708 var e = (evt) ? evt : window.event; // for IE
709 if (e.keyCode==13) return true;
710 if (!this.ProcessKeys(e)) return false;
712 if (this.lastKey==38) // Up
716 var newIndex = childIndex-1;
717 document.getElementById('Item'+itemIndex+'_c'+newIndex).focus();
719 else // already at first child, jump to parent
721 document.getElementById('Item'+itemIndex).focus();
724 else if (this.lastKey==40) // Down
726 var newIndex = childIndex+1;
727 var elem = document.getElementById('Item'+itemIndex+'_c'+newIndex);
728 if (!elem) // last child, jump to parent next parent
730 elem = this.NavNext(itemIndex+1);
737 else if (this.lastKey==27) // Escape
739 parent.searchBox.CloseResultsWindow();
740 parent.document.getElementById("MSearchField").focus();
742 else if (this.lastKey==13) // Enter
750 function setKeyActions(elem,action)
752 elem.setAttribute('onkeydown',action);
753 elem.setAttribute('onkeypress',action);
754 elem.setAttribute('onkeyup',action);
757 function setClassAttr(elem,attr)
759 elem.setAttribute('class',attr);
760 elem.setAttribute('className',attr);
763 function createResults()
765 var results = document.getElementById("SRResults");
766 for (var e=0; e<searchData.length; e++)
768 var id = searchData[e][0];
769 var srResult = document.createElement('div');
770 srResult.setAttribute('id','SR_'+id);
771 setClassAttr(srResult,'SRResult');
772 var srEntry = document.createElement('div');
773 setClassAttr(srEntry,'SREntry');
774 var srLink = document.createElement('a');
775 srLink.setAttribute('id','Item'+e);
776 setKeyActions(srLink,'return searchResults.Nav(event,'+e+')');
777 setClassAttr(srLink,'SRSymbol');
778 srLink.innerHTML = searchData[e][1][0];
779 srEntry.appendChild(srLink);
780 if (searchData[e][1].length==2) // single result
782 srLink.setAttribute('href',searchData[e][1][1][0]);
783 if (searchData[e][1][1][1])
785 srLink.setAttribute('target','_parent');
787 var srScope = document.createElement('span');
788 setClassAttr(srScope,'SRScope');
789 srScope.innerHTML = searchData[e][1][1][2];
790 srEntry.appendChild(srScope);
792 else // multiple results
794 srLink.setAttribute('href','javascript:searchResults.Toggle("SR_'+id+'")');
795 var srChildren = document.createElement('div');
796 setClassAttr(srChildren,'SRChildren');
797 for (var c=0; c<searchData[e][1].length-1; c++)
799 var srChild = document.createElement('a');
800 srChild.setAttribute('id','Item'+e+'_c'+c);
801 setKeyActions(srChild,'return searchResults.NavChild(event,'+e+','+c+')');
802 setClassAttr(srChild,'SRScope');
803 srChild.setAttribute('href',searchData[e][1][c+1][0]);
804 if (searchData[e][1][c+1][1])
806 srChild.setAttribute('target','_parent');
808 srChild.innerHTML = searchData[e][1][c+1][2];
809 srChildren.appendChild(srChild);
811 srEntry.appendChild(srChildren);
813 srResult.appendChild(srEntry);
814 results.appendChild(srResult);