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: "_abcdefikmnoprstuw",
19 var indexSectionNames =
30 function convertToId(search)
33 for (i=0;i<search.length;i++)
35 var c = search.charAt(i);
36 var cn = c.charCodeAt(0);
37 if (c.match(/[a-z0-9\u0080-\uFFFF]/))
43 result+="_0"+cn.toString(16);
47 result+="_"+cn.toString(16);
53 function getXPos(item)
58 while (item && item!=document.body)
61 item = item.offsetParent;
67 function getYPos(item)
72 while (item && item!=document.body)
75 item = item.offsetParent;
81 /* A class handling everything associated with the search panel.
84 name - The name of the global variable that will be
85 storing this instance. Is needed to be able to set timeouts.
86 resultPath - path to use for external files
88 function SearchBox(name, resultsPath, inFrame, label)
90 if (!name || !resultsPath) { alert("Missing parameters to SearchBox."); }
92 // ---------- Instance variables
94 this.resultsPath = resultsPath;
96 this.keyTimeoutLength = 500;
97 this.closeSelectionTimeout = 300;
98 this.lastSearchValue = "";
99 this.lastResultsPage = "";
100 this.hideTimeout = 0;
101 this.searchIndex = 0;
102 this.searchActive = false;
103 this.insideFrame = inFrame;
104 this.searchLabel = label;
106 // ----------- DOM Elements
108 this.DOMSearchField = function()
109 { return document.getElementById("MSearchField"); }
111 this.DOMSearchSelect = function()
112 { return document.getElementById("MSearchSelect"); }
114 this.DOMSearchSelectWindow = function()
115 { return document.getElementById("MSearchSelectWindow"); }
117 this.DOMPopupSearchResults = function()
118 { return document.getElementById("MSearchResults"); }
120 this.DOMPopupSearchResultsWindow = function()
121 { return document.getElementById("MSearchResultsWindow"); }
123 this.DOMSearchClose = function()
124 { return document.getElementById("MSearchClose"); }
126 this.DOMSearchBox = function()
127 { return document.getElementById("MSearchBox"); }
129 // ------------ Event Handlers
131 // Called when focus is added or removed from the search field.
132 this.OnSearchFieldFocus = function(isActive)
134 this.Activate(isActive);
137 this.OnSearchSelectShow = function()
139 var searchSelectWindow = this.DOMSearchSelectWindow();
140 var searchField = this.DOMSearchSelect();
142 if (this.insideFrame)
144 var left = getXPos(searchField);
145 var top = getYPos(searchField);
146 left += searchField.offsetWidth + 6;
147 top += searchField.offsetHeight;
149 // show search selection popup
150 searchSelectWindow.style.display='block';
151 left -= searchSelectWindow.offsetWidth;
152 searchSelectWindow.style.left = left + 'px';
153 searchSelectWindow.style.top = top + 'px';
157 var left = getXPos(searchField);
158 var top = getYPos(searchField);
159 top += searchField.offsetHeight;
161 // show search selection popup
162 searchSelectWindow.style.display='block';
163 searchSelectWindow.style.left = left + 'px';
164 searchSelectWindow.style.top = top + 'px';
167 // stop selection hide timer
168 if (this.hideTimeout)
170 clearTimeout(this.hideTimeout);
173 return false; // to avoid "image drag" default event
176 this.OnSearchSelectHide = function()
178 this.hideTimeout = setTimeout(this.name +".CloseSelectionWindow()",
179 this.closeSelectionTimeout);
182 // Called when the content of the search field is changed.
183 this.OnSearchFieldChange = function(evt)
185 if (this.keyTimeout) // kill running timer
187 clearTimeout(this.keyTimeout);
191 var e = (evt) ? evt : window.event; // for IE
192 if (e.keyCode==40 || e.keyCode==13)
196 this.OnSearchSelectShow();
197 var win=this.DOMSearchSelectWindow();
198 for (i=0;i<win.childNodes.length;i++)
200 var child = win.childNodes[i]; // get span within a
201 if (child.className=='SelectItem')
209 else if (window.frames.MSearchResults.searchResults)
211 var elem = window.frames.MSearchResults.searchResults.NavNext(0);
212 if (elem) elem.focus();
215 else if (e.keyCode==27) // Escape out of the search field
217 this.DOMSearchField().blur();
218 this.DOMPopupSearchResultsWindow().style.display = 'none';
219 this.DOMSearchClose().style.display = 'none';
220 this.lastSearchValue = '';
221 this.Activate(false);
226 var searchValue = this.DOMSearchField().value.replace(/ +/g, "");
228 if (searchValue != this.lastSearchValue) // search value has changed
230 if (searchValue != "") // non-empty search
232 // set timer for search update
233 this.keyTimeout = setTimeout(this.name + '.Search()',
234 this.keyTimeoutLength);
236 else // empty search field
238 this.DOMPopupSearchResultsWindow().style.display = 'none';
239 this.DOMSearchClose().style.display = 'none';
240 this.lastSearchValue = '';
245 this.SelectItemCount = function(id)
248 var win=this.DOMSearchSelectWindow();
249 for (i=0;i<win.childNodes.length;i++)
251 var child = win.childNodes[i]; // get span within a
252 if (child.className=='SelectItem')
260 this.SelectItemSet = function(id)
263 var win=this.DOMSearchSelectWindow();
264 for (i=0;i<win.childNodes.length;i++)
266 var child = win.childNodes[i]; // get span within a
267 if (child.className=='SelectItem')
269 var node = child.firstChild;
272 node.innerHTML='•';
276 node.innerHTML=' ';
283 // Called when an search filter selection is made.
284 // set item with index id as the active item
285 this.OnSelectItem = function(id)
287 this.searchIndex = id;
288 this.SelectItemSet(id);
289 var searchValue = this.DOMSearchField().value.replace(/ +/g, "");
290 if (searchValue!="" && this.searchActive) // something was found -> do a search
296 this.OnSearchSelectKey = function(evt)
298 var e = (evt) ? evt : window.event; // for IE
299 if (e.keyCode==40 && this.searchIndex<this.SelectItemCount()) // Down
302 this.OnSelectItem(this.searchIndex);
304 else if (e.keyCode==38 && this.searchIndex>0) // Up
307 this.OnSelectItem(this.searchIndex);
309 else if (e.keyCode==13 || e.keyCode==27)
311 this.OnSelectItem(this.searchIndex);
312 this.CloseSelectionWindow();
313 this.DOMSearchField().focus();
320 // Closes the results window.
321 this.CloseResultsWindow = function()
323 this.DOMPopupSearchResultsWindow().style.display = 'none';
324 this.DOMSearchClose().style.display = 'none';
325 this.Activate(false);
328 this.CloseSelectionWindow = function()
330 this.DOMSearchSelectWindow().style.display = 'none';
333 // Performs a search.
334 this.Search = function()
338 // strip leading whitespace
339 var searchValue = this.DOMSearchField().value.replace(/^ +/, "");
341 var code = searchValue.toLowerCase().charCodeAt(0);
342 var idxChar = searchValue.substr(0, 1).toLowerCase();
343 if ( 0xD800 <= code && code <= 0xDBFF && searchValue > 1) // surrogate pair
345 idxChar = searchValue.substr(0, 2);
349 var resultsPageWithSearch;
352 var idx = indexSectionsWithContent[this.searchIndex].indexOf(idxChar);
355 var hexCode=idx.toString(16);
356 resultsPage = this.resultsPath + '/' + indexSectionNames[this.searchIndex] + '_' + hexCode + '.html';
357 resultsPageWithSearch = resultsPage+'?'+escape(searchValue);
358 hasResultsPage = true;
360 else // nothing available for this search term
362 resultsPage = this.resultsPath + '/nomatches.html';
363 resultsPageWithSearch = resultsPage;
364 hasResultsPage = false;
367 window.frames.MSearchResults.location = resultsPageWithSearch;
368 var domPopupSearchResultsWindow = this.DOMPopupSearchResultsWindow();
370 if (domPopupSearchResultsWindow.style.display!='block')
372 var domSearchBox = this.DOMSearchBox();
373 this.DOMSearchClose().style.display = 'inline';
374 if (this.insideFrame)
376 var domPopupSearchResults = this.DOMPopupSearchResults();
377 domPopupSearchResultsWindow.style.position = 'relative';
378 domPopupSearchResultsWindow.style.display = 'block';
379 var width = document.body.clientWidth - 8; // the -8 is for IE :-(
380 domPopupSearchResultsWindow.style.width = width + 'px';
381 domPopupSearchResults.style.width = width + 'px';
385 var domPopupSearchResults = this.DOMPopupSearchResults();
386 var left = getXPos(domSearchBox) + 150; // domSearchBox.offsetWidth;
387 var top = getYPos(domSearchBox) + 20; // domSearchBox.offsetHeight + 1;
388 domPopupSearchResultsWindow.style.display = 'block';
389 left -= domPopupSearchResults.offsetWidth;
390 domPopupSearchResultsWindow.style.top = top + 'px';
391 domPopupSearchResultsWindow.style.left = left + 'px';
395 this.lastSearchValue = searchValue;
396 this.lastResultsPage = resultsPage;
399 // -------- Activation Functions
401 // Activates or deactivates the search panel, resetting things to
402 // their default values if necessary.
403 this.Activate = function(isActive)
405 if (isActive || // open it
406 this.DOMPopupSearchResultsWindow().style.display == 'block'
409 this.DOMSearchBox().className = 'MSearchBoxActive';
411 var searchField = this.DOMSearchField();
413 if (searchField.value == this.searchLabel) // clear "Search" term upon entry
415 searchField.value = '';
416 this.searchActive = true;
419 else if (!isActive) // directly remove the panel
421 this.DOMSearchBox().className = 'MSearchBoxInactive';
422 this.DOMSearchField().value = this.searchLabel;
423 this.searchActive = false;
424 this.lastSearchValue = ''
425 this.lastResultsPage = '';
430 // -----------------------------------------------------------------------
432 // The class that handles everything on the search results page.
433 function SearchResults(name)
435 // The number of matches from the last run of <Search()>.
436 this.lastMatchCount = 0;
438 this.repeatOn = false;
440 // Toggles the visibility of the passed element ID.
441 this.FindChildElement = function(id)
443 var parentElement = document.getElementById(id);
444 var element = parentElement.firstChild;
446 while (element && element!=parentElement)
448 if (element.nodeName == 'DIV' && element.className == 'SRChildren')
453 if (element.nodeName == 'DIV' && element.hasChildNodes())
455 element = element.firstChild;
457 else if (element.nextSibling)
459 element = element.nextSibling;
465 element = element.parentNode;
467 while (element && element!=parentElement && !element.nextSibling);
469 if (element && element!=parentElement)
471 element = element.nextSibling;
477 this.Toggle = function(id)
479 var element = this.FindChildElement(id);
482 if (element.style.display == 'block')
484 element.style.display = 'none';
488 element.style.display = 'block';
493 // Searches for the passed string. If there is no parameter,
494 // it takes it from the URL query.
496 // Always returns true, since other documents may try to call it
497 // and that may or may not be possible.
498 this.Search = function(search)
500 if (!search) // get search word from URL
502 search = window.location.search;
503 search = search.substring(1); // Remove the leading '?'
504 search = unescape(search);
507 search = search.replace(/^ +/, ""); // strip leading spaces
508 search = search.replace(/ +$/, ""); // strip trailing spaces
509 search = search.toLowerCase();
510 search = convertToId(search);
512 var resultRows = document.getElementsByTagName("div");
516 while (i < resultRows.length)
518 var row = resultRows.item(i);
519 if (row.className == "SRResult")
521 var rowMatchName = row.id.toLowerCase();
522 rowMatchName = rowMatchName.replace(/^sr\d*_/, ''); // strip 'sr123_'
524 if (search.length<=rowMatchName.length &&
525 rowMatchName.substr(0, search.length)==search)
527 row.style.display = 'block';
532 row.style.display = 'none';
537 document.getElementById("Searching").style.display='none';
538 if (matches == 0) // no results
540 document.getElementById("NoMatches").style.display='block';
542 else // at least one result
544 document.getElementById("NoMatches").style.display='none';
546 this.lastMatchCount = matches;
550 // return the first item with index index or higher that is visible
551 this.NavNext = function(index)
556 var focusName = 'Item'+index;
557 focusItem = document.getElementById(focusName);
558 if (focusItem && focusItem.parentNode.parentNode.style.display=='block')
562 else if (!focusItem) // last element
572 this.NavPrev = function(index)
577 var focusName = 'Item'+index;
578 focusItem = document.getElementById(focusName);
579 if (focusItem && focusItem.parentNode.parentNode.style.display=='block')
583 else if (!focusItem) // last element
593 this.ProcessKeys = function(e)
595 if (e.type == "keydown")
597 this.repeatOn = false;
598 this.lastKey = e.keyCode;
600 else if (e.type == "keypress")
604 if (this.lastKey) this.repeatOn = true;
605 return false; // ignore first keypress after keydown
608 else if (e.type == "keyup")
611 this.repeatOn = false;
613 return this.lastKey!=0;
616 this.Nav = function(evt,itemIndex)
618 var e = (evt) ? evt : window.event; // for IE
619 if (e.keyCode==13) return true;
620 if (!this.ProcessKeys(e)) return false;
622 if (this.lastKey==38) // Up
624 var newIndex = itemIndex-1;
625 var focusItem = this.NavPrev(newIndex);
628 var child = this.FindChildElement(focusItem.parentNode.parentNode.id);
629 if (child && child.style.display == 'block') // children visible
633 while (1) // search for last child
635 tmpElem = document.getElementById('Item'+newIndex+'_c'+n);
652 else // return focus to search field
654 parent.document.getElementById("MSearchField").focus();
657 else if (this.lastKey==40) // Down
659 var newIndex = itemIndex+1;
661 var item = document.getElementById('Item'+itemIndex);
662 var elem = this.FindChildElement(item.parentNode.parentNode.id);
663 if (elem && elem.style.display == 'block') // children visible
665 focusItem = document.getElementById('Item'+itemIndex+'_c0');
667 if (!focusItem) focusItem = this.NavNext(newIndex);
668 if (focusItem) focusItem.focus();
670 else if (this.lastKey==39) // Right
672 var item = document.getElementById('Item'+itemIndex);
673 var elem = this.FindChildElement(item.parentNode.parentNode.id);
674 if (elem) elem.style.display = 'block';
676 else if (this.lastKey==37) // Left
678 var item = document.getElementById('Item'+itemIndex);
679 var elem = this.FindChildElement(item.parentNode.parentNode.id);
680 if (elem) elem.style.display = 'none';
682 else if (this.lastKey==27) // Escape
684 parent.searchBox.CloseResultsWindow();
685 parent.document.getElementById("MSearchField").focus();
687 else if (this.lastKey==13) // Enter
694 this.NavChild = function(evt,itemIndex,childIndex)
696 var e = (evt) ? evt : window.event; // for IE
697 if (e.keyCode==13) return true;
698 if (!this.ProcessKeys(e)) return false;
700 if (this.lastKey==38) // Up
704 var newIndex = childIndex-1;
705 document.getElementById('Item'+itemIndex+'_c'+newIndex).focus();
707 else // already at first child, jump to parent
709 document.getElementById('Item'+itemIndex).focus();
712 else if (this.lastKey==40) // Down
714 var newIndex = childIndex+1;
715 var elem = document.getElementById('Item'+itemIndex+'_c'+newIndex);
716 if (!elem) // last child, jump to parent next parent
718 elem = this.NavNext(itemIndex+1);
725 else if (this.lastKey==27) // Escape
727 parent.searchBox.CloseResultsWindow();
728 parent.document.getElementById("MSearchField").focus();
730 else if (this.lastKey==13) // Enter
738 function setKeyActions(elem,action)
740 elem.setAttribute('onkeydown',action);
741 elem.setAttribute('onkeypress',action);
742 elem.setAttribute('onkeyup',action);
745 function setClassAttr(elem,attr)
747 elem.setAttribute('class',attr);
748 elem.setAttribute('className',attr);
751 function createResults()
753 var results = document.getElementById("SRResults");
754 for (var e=0; e<searchData.length; e++)
756 var id = searchData[e][0];
757 var srResult = document.createElement('div');
758 srResult.setAttribute('id','SR_'+id);
759 setClassAttr(srResult,'SRResult');
760 var srEntry = document.createElement('div');
761 setClassAttr(srEntry,'SREntry');
762 var srLink = document.createElement('a');
763 srLink.setAttribute('id','Item'+e);
764 setKeyActions(srLink,'return searchResults.Nav(event,'+e+')');
765 setClassAttr(srLink,'SRSymbol');
766 srLink.innerHTML = searchData[e][1][0];
767 srEntry.appendChild(srLink);
768 if (searchData[e][1].length==2) // single result
770 srLink.setAttribute('href',searchData[e][1][1][0]);
771 if (searchData[e][1][1][1])
773 srLink.setAttribute('target','_parent');
775 var srScope = document.createElement('span');
776 setClassAttr(srScope,'SRScope');
777 srScope.innerHTML = searchData[e][1][1][2];
778 srEntry.appendChild(srScope);
780 else // multiple results
782 srLink.setAttribute('href','javascript:searchResults.Toggle("SR_'+id+'")');
783 var srChildren = document.createElement('div');
784 setClassAttr(srChildren,'SRChildren');
785 for (var c=0; c<searchData[e][1].length-1; c++)
787 var srChild = document.createElement('a');
788 srChild.setAttribute('id','Item'+e+'_c'+c);
789 setKeyActions(srChild,'return searchResults.NavChild(event,'+e+','+c+')');
790 setClassAttr(srChild,'SRScope');
791 srChild.setAttribute('href',searchData[e][1][c+1][0]);
792 if (searchData[e][1][c+1][1])
794 srChild.setAttribute('target','_parent');
796 srChild.innerHTML = searchData[e][1][c+1][2];
797 srChildren.appendChild(srChild);
799 srEntry.appendChild(srChildren);
801 srResult.appendChild(srEntry);
802 results.appendChild(srResult);