bubble的备忘录

本blog主要用于常用资料的备忘、备份、备搜索

源码注释:Raw Search Objects (部分)

google.load('search', '1.0');
function OnLoad() {
new RawSearchControl();
}

/**
* The RawSearchControl demonstrates how to use Searcher Objects
* outside of the standard GSearchControl. This includes calling
* searcher .execute() methods, reacting to search completions,
* and if you had previously disabled html generation, how to generate
* an html representation of the result.
* 本例子没有使用标准GSearchControl,而是 包括调用查询.execute()
* 在搜索完成时的处理,以及结果显示
*/
function RawSearchControl() {
// latch on to key portions of the document
this.searcherform = document.getElementById("searcher");//搜索框,包括搜索输入框和选择按钮
this.results = document.getElementById("results");//结果框
this.cursor = document.getElementById("cursor");//结果分页信息链接
this.searchform = document.getElementById("searchform");//搜索输入框

// create map of searchers as well as note the active searcher
this.activeSearcher = "web";//默认选择,Web搜索
this.searchers = new Array();//新建数组

// create and wire up an instance of GwebSearch and one of
// GlocalSearch. Note, we disable html generation. We are doing
// this so that we can demonstrate how to manually create it if
// needed. Note that we register to handle search completion notifications
// when searches complete, they are called in the context of this instance
// of RawSearchControl and they are passed the searcher that just completed

// wire up a raw GwebSearch searcher Web搜索
var searcher = new google.search.WebSearch();
searcher.setNoHtmlGeneration();//不直接显示HTML格式的搜索结果
searcher.setSearchCompleteCallback(this,
RawSearchControl.prototype.searchComplete,
[searcher]
);//搜索完成后的处理
this.searchers["web"] = searcher;

// do the same for local 本地搜索
searcher = new google.search.LocalSearch();
searcher.setNoHtmlGeneration();
searcher.setCenterPoint("98074");
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 google.search.SearchForm(true, this.searchform);
this.searchForm.setOnSubmitCallback(this,
RawSearchControl.prototype.onSubmit);
this.searchForm.setOnClearCallback(this,
RawSearchControl.prototype.onClear);
}

/**
* figure out which searcher is active by looking at the radio
* button array
* 判断激活的搜索类型
*/
RawSearchControl.prototype.computeActiveSearcher = function() {
for (var i=0; i this.searcherform["searcherType"].length; i++) {
if (this.searcherform["searcherType"][i].checked) {
this.activeSearcher = this.searcherform["searcherType"][i].value;
return;
}
}
}

/**
* onSubmit - called when the search form is "submitted" meaning that
* someone pressed the search button or hit enter. The form is passed
* as an argument
* 按下搜索按钮或键入回车之后的调用的函数
*/
RawSearchControl.prototype.onSubmit = function(form) {
this.computeActiveSearcher();//判断激活的搜索类型
if (form.input.value) {
// if there is an expression in the form, call the active searcher's
// .execute method 根据搜索类型执行搜索
this.searchers[this.activeSearcher].execute(form.input.value);
}

// always indicate that we handled the submit event
return false;
}

/**
* onClear - called when someone clicks on the clear button (the little x)
* 清除按钮的操作
*/
RawSearchControl.prototype.onClear = function(form) {
this.clearResults();
}

/**
* searchComplete - called when a search completed. Note the searcher
* that is completing is passes as an arg because thats what we arranged
* when we called setSearchCompleteCallback
* 搜索完成后的结果处理和显示
*/
RawSearchControl.prototype.searchComplete = function(searcher) {

// always clear old from the page
this.clearResults();//清除旧的结果页面

// if the searcher has results then process them
if (searcher.results && searcher.results.length > 0) {

// print the result titles
var div = createDiv("Result Titles", "header");//“header”是怎么出来的?
this.results.appendChild(div);//创建一个div存储结果的标题
for (var i=0; i searcher.results.length; i++) {
//对于每个结果,提取结果的标题
var result = searcher.results[i];
var titleLine = result.title;

// add in lat,lng for local results
//如果是本地搜索
if (result.GsearchResultClass == GlocalSearch.RESULT_CLASS) {
titleLine += " (" + result.lat + ", " + result.lng + ")";
}
//如果是网页搜索
if (result.html) {
titleLine += " ** html is present **";
}
div = createDiv(titleLine);//为每个标题创建一个div
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);
for (var i=0; i searcher.results.length; i++) {
var result = searcher.results[i];
searcher.createResultHtml(result);//重新生成HTML属性的结果
if (result.html) {
div = result.html.cloneNode(true);
} else {
div = createDiv("** failure to create html **");
}
this.results.appendChild(div);
}

// now, see if we have a cursor, and if so, create the
// cursor control
//处理分页链接信息
if (searcher.cursor) {
var cursorNode = createDiv(null, "gsc-cursor");
for (var i=0; i searcher.cursor.pages.length; i++) {//分页信息
var className = "gsc-cursor-page";
if (i == searcher.cursor.currentPageIndex) {
className = className + " gsc-cursor-current-page";
}
var pageNode = createDiv(searcher.cursor.pages[i].label, className);
pageNode.onclick = methodClosure(this, this.gotoPage,
[searcher, i]);
cursorNode.appendChild(pageNode);
}
this.cursor.appendChild(cursorNode);
//“更多结果”相关
var more = createLink(searcher.cursor.moreResultsUrl,
GSearch.strings["more-results"] + " »",
GSearch.LINK_TARGET_SELF,
"gsc-trailing-more-results");
this.cursor.appendChild(more);
}
}
}

RawSearchControl.prototype.gotoPage = function(searcher, page) {
searcher.gotoPage(page);//处理页面跳转
}

/**
* clearResults - clear out any old search results
*/
RawSearchControl.prototype.clearResults = function() {//清除旧的结果信息
removeChildren(this.results);//清除结果信息
removeChildren(this.cursor);//清除分页信息
}

/**
* Static DOM Helper Functions
*/
function removeChildren(parent) {
while (parent.firstChild) {//递归删除
parent.removeChild(parent.firstChild);
}
}
function createDiv(opt_text, opt_className) {//对特定内容创建div
var el = document.createElement("div");
if (opt_text) {
el.innerHTML = opt_text;
}
if (opt_className) { el.className = opt_className; }
return el;
}

function methodClosure(object, method, opt_argArray) {//分页相关的函数
return function() {
return method.apply(object, opt_argArray);
}
}

function createLink(href, opt_text, opt_target, opt_className, opt_divwrap) {
//创建和“更多结果”相关的链接
var el = document.createElement("a");
el.href = href;
if (opt_text) {
el.innerHTML = opt_text;
}
if (opt_className) {
el.className = opt_className;
}
if (opt_target) {
el.target = opt_target;
}
if (opt_divwrap) {
var div = this.createDiv(null, opt_className);
div.appendChild(el);
el = div;
}
return el;
}

// register to be called at OnLoad when the page loads
google.setOnLoadCallback(OnLoad, true);

0 评论: