Skip to content
Snippets Groups Projects
Select Git revision
  • optimize-images
  • 5.0
  • master default protected
  • 4.1
4 results

searchFunc.js

Blame
  • Forked from Digital Experience Group / UNL_Search
    233 commits behind the upstream repository.
    searchFunc.js 8.21 KiB
    
    // Load the Google Search AJAX API
    google.load("search", "1");
    
    function searchInit() {
    	UNL_Search.peoplefinderCache = new UNL_Search.Cache();
    	UNL_Search.hcardCache        = new UNL_Search.Cache();
    
    	//Parse the querystring for q
    	var qs = location.search.substring(1,location.search.length);
    	qs = qs.replace(/\+/g, ' ');
    	var args = qs.split('&');
    	for (var i=0;i<args.length;i++) {
    		var pair = args[i].split('=');
    		var name = unescape(pair[0]);
    		if (name == "q") {
    			if (pair.length == 2) {
    				UNL_Search.query = unescape(pair[1])
    			} else {
    				UNL_Search.query = "";
    			}
    			break;
    		}
    	}
    	
    	UNL_Search.unl_search = new google.search.WebSearch();
    	//Apply the Custom Search Engine
    	UNL_Search.unl_search.setSiteRestriction("015236299699564929946:nk1siew10ie");
    	UNL_Search.unl_search.setResultSetSize(GSearch.LARGE_RESULTSET)
    	UNL_Search.unl_search.setSearchCompleteCallback(UNL_Search, UNL_Search.handleUNLResults)
    	UNL_Search.unl_search.setLinkTarget(GSearch.LINK_TARGET_SELF);
    
    	if (UNL_Search.do_local_search) {
    	
    		UNL_Search.local_search = new google.search.WebSearch();
    		//Apply the Custom Search Engine
    		UNL_Search.local_search.setSiteRestriction(UNL_Search.local_search_context);
    		//UNL_Search.local_search.setSiteRestriction('000150017203523626721:-1etga1qfci');
    		UNL_Search.local_search.setResultSetSize(GSearch.SMALL_RESULTSET)
    		UNL_Search.local_search.setSearchCompleteCallback(UNL_Search, UNL_Search.handleLocalResults)
    		UNL_Search.local_search.setLinkTarget(GSearch.LINK_TARGET_SELF);
    		
    	}
    	
    	var gSearchForm = new GSearchForm(false, document.getElementById("searchform"));
    	gSearchForm.setOnSubmitCallback(UNL_Search, UNL_Search.doQuery);
    	gSearchForm.input.value = (UNL_Search.query ? UNL_Search.query : "");
    	
    	// Execute an inital search
    	if (UNL_Search.query && UNL_Search.query != "") {
    		UNL_Search.unl_search.execute(UNL_Search.query);
    		if (UNL_Search.do_local_search) {
    			UNL_Search.local_search.execute(UNL_Search.query);
    		}
    		UNL_Search.doPeoplefinderQuery(UNL_Search.query);
    	}
    }
    
    //Attach search initializer to onLoad
    google.setOnLoadCallback(searchInit);
    
    
    //
    // UNL_Search Namespace
    //
    if (typeof(UNL_Search) == "undefined")
    	UNL_Search = {};
    
    UNL_Search.query              	 = null;
    UNL_Search.do_local_search    	 = false;
    UNL_Search.unl_search         	 = null;
    UNL_Search.local_search       	 = null;
    UNL_Search.local_search_context  = null;
    UNL_Search.scrolldelay        	 = null;
    UNL_Search.hcardTimeout       	 = null;
    UNL_Search.hcardCache         	 = null;
    UNL_Search.peoplefinderCache  	 = null;
    
    //Caching Class
    //
    UNL_Search.Cache = {};
    UNL_Search.Cache = function() {
    	this.storage = new Object();
    };
    
    UNL_Search.Cache.prototype.save = function(key, data) {
    	this.storage[key] = data;
    };
    
    UNL_Search.Cache.prototype.get = function (key) {
    	var val = null;
    	
    	if (this.storage[key] != null) {
    		val = this.storage[key];
    	}
    	
    	return val;
    };
    
    UNL_Search.handleUNLResults = function () {
    	UNL_Search.handleResults('unl_results', UNL_Search.unl_search);
    };
    
    UNL_Search.handleLocalResults = function () {
    	UNL_Search.handleResults('local_results', UNL_Search.local_search);
    };
    
    UNL_Search.handleResults = function (result_div, searchobj) {
    	var resultsDIV = document.getElementById(result_div);
    	
    	if (searchobj.results && searchobj.results.length > 0) {
    		resultsDIV.innerHTML = '';
    		WDN.jQuery(resultsDIV).css('background', 'none');
    		//var resultHead = '<p>Results <strong>' + (1 + 8 * searchobj.cursor.currentPageIndex) + '</strong> - <strong>' + (8 + 8 * searchobj.cursor.currentPageIndex) + '</strong> of about <strong>' + searchobj.cursor.estimatedResultCount + '</strong> for <em>' + query + '</em></p>';
    		//resultsDIV.innerHTML += resultHead;
    		for (var i = 0; i < searchobj.results.length; i++) {
    			if (i % 2 != 0) {
    				searchobj.results[i].html.className += " alt";
    			}
    			resultsDIV.appendChild(searchobj.results[i].html);
    		}
    		var nav = document.createElement("div");
    		nav.className = "gs-search-nav";
    		var search_name = 'unl_search';
    		if (result_div.substring(0,5) == 'local') {
    			search_name = 'local_search';
    		}
    		for (var i = 0; i < searchobj.cursor.pages.length; i++) {
    			var pageText = '';
    			if (searchobj.cursor.currentPageIndex != i) {
    				pageText += '<a onclick="UNL_Search.'+search_name+'.gotoPage(' + i + '); return false;" href="#">' + searchobj.cursor.pages[i].label + '</a>&nbsp;&nbsp;';
    			} else {
    				pageText += searchobj.cursor.pages[i].label + '&nbsp;&nbsp;';
    			}
    			nav.innerHTML += pageText;
    		}
    		if (searchobj.cursor.pages.length >= 8) {
    			nav.innerHTML += '<a href="' + searchobj.cursor.moreResultsUrl + '">More Results &gt;&gt;</a>';
    		}
    		resultsDIV.appendChild(nav);
    	} else {
    		WDN.jQuery(resultsDIV).html('<p>No results found.</p>');
    	}
    };
    
    
    UNL_Search.doPeoplefinderQuery = function (val) {
    	var cacheData = this.peoplefinderCache.get(val)
    	if (cacheData) {
    		this.handlePeoplefinderResults(cacheData);
    	} else {
    		var pointer = this;
    		WDN.get("http://directory.unl.edu/service.php?q=" + val, null, function(data, textStatus) {
    			pointer.peoplefinderCache.save(val, data);
    			UNL_Search.handlePeoplefinderResults(data);
    		});
    	}
    };
    
    UNL_Search.handlePeoplefinderResults = function (peoplefinderText) {
    	WDN.jQuery('#ppl_results').html(peoplefinderText);
    };
    
    UNL_Search.doQuery = function (form) {
    	if (form.input.value != "") {
    		this.query = form.input.value;
    		if (this.do_local_search) {
    			this.local_search.execute(form.input.value);
    		}
    		this.unl_search.execute(form.input.value);
    		this.doPeoplefinderQuery(form.input.value);
    	} else {
    		document.getElementById("searchcontrol").innerHTML = '';
    		document.getElementById("ppl_results").innerHTML = '';
    	}
    	
    	return false;
    };
    
    UNL_Search.showHCard = function (ele, href) {
    	var uid = href.substr(href.lastIndexOf("=") + 1);
    	
    	var cacheData = this.hcardCache.get(uid)
    	if (cacheData) {
    		this.buildHCard(cacheData, ele);
    	} else {
    		var http = new XMLHTTP();
    		
    		http.open("GET", "http://directory.unl.edu/hcards/" + uid, true);
    		var pointer = this;
    		http.onreadystatechange = function() {
    			if (http.readyState == 4) {
    				if (http.status == 200) {
    					var data = http.responseText;
    					
    					pointer.hcardCache.save(uid, data);
    					pointer.buildHCard(data, ele);
    				} else {
    					// Error loading file!
    				}
    			}
    		};
    		
    		http.send(null);
    	}
    	
    	return false;
    };
    
    UNL_Search.buildHCard = function (hcardText, ele) {
    	_b.DOM.remE("peoplePopup");
    	this.killHCardTimeout();
    				
    	var div = _b.DOM.cE("div", {id:"peoplePopup", className:"hcardPopup"});
    	
    	var hcorner = _b.DOM.cE("div", {className:"as_corner"});
    	var hbar = _b.DOM.cE("div", {className:"as_bar"});
    	var header = _b.DOM.cE("div", {className:"as_header"});
    	header.appendChild(hcorner);
    	header.appendChild(hbar);
    	div.appendChild(header);
    	
    	var ul = _b.DOM.cE("ul");
    	var theCard = _b.DOM.cE("li");
    	theCard.innerHTML = hcardText;
    	
    	ul.appendChild(theCard);
    	div.appendChild(ul);
    	
    	var fcorner = _b.DOM.cE("div", {className:"as_corner"});
    	var fbar = _b.DOM.cE("div", {className:"as_bar"});
    	var footer = _b.DOM.cE("div", {className:"as_footer"});
    	footer.appendChild(fcorner);
    	footer.appendChild(fbar);
    	div.appendChild(footer);
    	
    	var pos = _b.DOM.getPos(ele);
    	
    	div.style.left = (pos.x) + "px";
    	div.style.top = (pos.y + ele.offsetHeight) + "px";
    	
    	div.style.visibility = "hidden";
    	
    	document.getElementsByTagName("body")[0].appendChild(div);
    					
    	div.style.left = (pos.x - div.offsetWidth + 5) + "px";
    	div.style.top = (pos.y + ele.offsetHeight / 2 - div.offsetHeight / 2) + "px";
    	
    	div.style.visibility = "visible";
    	
    	div.onmouseover  = function(){ UNL_Search.killHCardTimeout() };
    	div.onmouseout   = function(){ UNL_Search.resetHCardTimeout() };
    	
    	this.hcardTimeout = setTimeout(function() { UNL_Search.clearHCard() }, 5000);
    };
    
    //Sets up a fader to remove the hcard popup
    UNL_Search.clearHCard = function () {
    	this.killHCardTimeout();
    	
    	var popup = _b.DOM.gE("peoplePopup");
    	if (popup) {
    		var fade = new _b.Fader(popup,1,0,250, function() { _b.DOM.remE("peoplePopup") });
    	}
    };
    
    //Stops the timer for the removal of the hcard popup
    UNL_Search.killHCardTimeout = function () {
    	clearTimeout(this.hcardTimeout);
    };
    
    //Starts a new timer for the removal of the hcard popup
    UNL_Search.resetHCardTimeout = function () {
    	clearTimeout(this.hcardTimeout);
    	this.hcardTimeout = setTimeout(function () { UNL_Search.clearHCard() }, 500);
    };