<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Adding a Google Search to a Site</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://www.google.com/uds/api?file=uds.js&v=1.0&key=uNiQuE_kEy"> </script>
<script type="text/javascript">
//<![CDATA[
/**
* This function, removeChildren, is a utility function that walks
* the DOM tree and removes childNodes from the passed /parent/
* element.
*
* @param {Node} parent The node to delete children from.
*/
function removeChildren(parent) {
/* Walk the parent to get to all childNodes */
while (parent.firstChild)
parent.removeChild(parent.firstChild);
}
/**
* This function, createDiv, creates a new <div> element, giving it
* content if any is passed in /opt_text/ and setting a className if
* one is passed in /opt_className/.
*
* @param {String} opt_text Optional text that will be placed in the newly created <div> element.
* @param {String} opt_className Optional /className/ to give the newly created <div> element.
* @return Returns the newly created <div> element.
* @type Node
*/
function createDiv(opt_text, opt_className) {
var div = document.createElement('div');
/* Was any optional text passed? */
if (opt_text)
div.innerHTML = opt_text;
/* Was an optional /className/ passed? */
if (opt_className)
div.className = opt_className;
return (div);
}
/**
* This function, body_onload, creates a new /RawSearchControl/
* object when the page loads.
*/
function body_onload( ) {
new RawSearchControl( );
}
/**
* This object, RawSearchControl, creates and wires up an instance
* of GwebSearch and one of GlocalSearch. HTML generation is
* disabled in the object so that manual creation of search
* results can be shown.
*
* @constructor
* @see GwebSearch
* @see GwebSearch#setNoHtmlGeneration
* @see GwebSearch#setSearchCompleteCallback
* @see #searchComplete
* @see GlocalSearch
* @see GlocalSearch#setNoHtmlGeneration
* @see GlocalSearch#setCenterPoint
* @see GlocalSearch#setSearchCompleteCallback
* @see GSearchForm
* @see GSearchForm#setOnSubmitCallback
* @see GSearchForm#setOnClearCallback
* @see #onSubmit
* @see #onClear
*/
function RawSearchControl( ) {
/* Latch on to key portions of the document */
this.searcherform = document.getElementById('searcher');
this.results = document.getElementById('results');
this.searchform = document.getElementById('searchform');
/* Create map of searchers as well as note the active searcher */
this.activeSearcher = 'web';
this.searchers = new Array( );
/* Wire up a raw GwebSearch searcher */
var searcher = new GwebSearch( );
searcher.setNoHtmlGeneration( );
searcher.setSearchCompleteCallback(this, RawSearchControl.prototype.searchComplete, [searcher]);
this.searchers['web'] = searcher;
/* Wire up the raw GlocalSearch searcher */
searcher = new GlocalSearch( );
searcher.setNoHtmlGeneration( );
searcher.setCenterPoint('62221');
searcher.setSearchCompleteCallback(this, RawSearchControl.prototype.searchComplete, [searcher]);
this.searchers['local'] = searcher;
/*
* Now, create a search form and wire up a submit and clear
* handler
*/
this.searchForm = new GSearchForm(true, this.searchform);
this.searchForm.setOnSubmitCallback(this, RawSearchControl.prototype.onSubmit);
this.searchForm.setOnClearCallback(this, RawSearchControl.prototype.onClear);
}
/**
* This method, computeActiveSearcher, figures out which searcher is
* active by looking at the radio button array.
*
* @member RawSearchControl
* @see #onSubmit
*/
RawSearchControl.prototype.computeActiveSearcher = function( ) {
/* Loop through the searcher types available */
for (var i = 0; i < this.searcherform['searcherType'].length; i++)
/* Is the searcher checked? */
if (this.searcherform['searcherType'][i].checked) {
this.activeSearcher = this.searcherform['searcherType'][i].value;
return;
}
}
/**
* This method, onSubmit, is called when the search form is
* 'submitted,' meaning that someone clicked the Search button or
* pressed Enter. The form is passed as an argument.
*
* @member RawSearchControl
* @param {Node} form The form that called this method.
* @return Returns false to let the caller know everything is good.
* @type Boolean
* @see #computeActiveSearch
* @see GwebSearch#execute
* @see GlocalSearch#execute
*/
RawSearchControl.prototype.onSubmit = function(form) {
this.computeActiveSearcher( );
/* Is there something to search on? */
if (form.input.value)
this.searchers[this.activeSearcher].execute(form.input.value);
return (false);
}
/**
* This method, onClear, is called when someone clicks on the Clear
* button (the little x on the form).
*
* @member RawSearchControl
* @param {Node} form The form that called this method.
* @see #clearResults
*/
RawSearchControl.prototype.onClear = function(form) {
this.clearResults( );
}
/**
* This method, searchComplete, is called when a search completes.
* Note that the searcher that is completing is passed as an argument
* because that is what we arranged when we called
* /setSearchCompleteCallback/.
*
* @member RawSearchControl
* @param {object} searcher The active searcher for the completed results.
* @see #clearResults
* @see GwebSearch#createResultHtml
* @see GlocalSearch#createResultHtml
*/
RawSearchControl.prototype.searchComplete = function(searcher) {
/* Always clear old results from the page */
this.clearResults( );
/* Does the searcher have results? */
if (searcher.results && searcher.results.length > 0) {
var div = createDiv('Result Titles', 'header');
this.results.appendChild(div);
/* Loop through the search results */
for (var i = 0; i < searcher.results.length; i++) {
var result = searcher.results[i];
var titleLine = result.title;
/* Are there HTML results */
if (result.html)
titleLine += ' ** html is present **';
div = createDiv(titleLine);
this.results.appendChild(div);
}
/*
* Now manually generate the HTML that we disabled initially
* and display it
*/
var div = createDiv('Result Html", "header');
this.results.appendChild(div);
/* Loop through the search results */
for (var i = 0; i < searcher.results.length; i++) {
var result = searcher.results[i];
searcher.createResultHtml(result);
/* Are there HTML results */
if (result.html)
div = result.html.cloneNode(true);
else
div = createDiv('** failure to create html **');
this.results.appendChild(div);
}
}
}
/**
* This method, clearResults, clears out any old search results.
*
* @member RawSearchControl
* @see #onClear
* @see #searchComplete
*/
RawSearchControl.prototype.clearResults = function( ) {
removeChildren(this.results);
}
/* Register to call body_onload when the page loads */
GSearch.setOnLoadCallback(body_onload);
//]]>
</script>
</head>
<body>
<h1>Adding a Google Search to a Site</h1>
<form id="searcher">
<div id="searchform">Loading...</div>
<div>
<input name="searcherType" value="web" type="radio" checked="checked">
<label>web</label>
<input name="searcherType" value="local" type="radio">
<label>local</label>
</div>
</form>
<div id="results"></div>
</body>
</html>