/* search.js This module implements all dynamic functionality for search.html, by providing various functions used in callbacks. External objects: init_function() - Function that is executed while search.html smod(event) - Function executed when the search term changes. change_index(URI) - Change the current index used for searching to the given URI. */ var chart; var q = ""; // global of last run query var cache_base = "http://search.isc.swlabs.org"; var un = false; //username global var tok = false; // token global /* Function executed while loading search.html*/ function init_function() { read_cookies(); set_login(); hasher(); window.setInterval("hasher()",500); } /* Change search term to that specified in "mq" */ function smod(e) { //$("sr-results").innerHTML += "Loading more results for '"+$("mq").value.escapeHTML(); cache_base = "http://search.isc.swlabs.org"; perform_full_search($("mq").value); if(e) e.returnValue = false; try {if(e) e.preventDefault();} catch (ex) {} return false; } /* Change the indexer URL to base */ function change_index(base, c_base) { if (typeof c_base == 'undefined' ) c_base = "http://search.isc.swlabs.org"; cache_base = c_base; $("sr-results").innerHTML = "Loading results for '"+q+"'"; var s = document.createElement('script'); s.src = base + "?query="+ q +"&hitsPerSite=2&lang=en&hitsPerPage=10&type=json&start=0"; document.getElementsByTagName('head')[0].appendChild(s); return false; } function set_login() { if (un && tok) { $("li-welcome").innerHTML = "Hello " + un; $("li-box").style.display = "block"; $("lo-box").style.display = "none"; $("tabs").style.display = "block"; } else { $("lo-box").style.display = "block"; $("tabs").style.display = "none"; } } function read_cookies() { un = getCookie("wikicitiesUserName"); tok = getCookie("wikicitiesToken"); } function getCookie(name) { var C = document.cookie.split("; "); for (i=0; i < C.length; i++) { kv = C[i].split("="); if(kv[0] == name){ return kv[1]; } } return false; } function comma(number) { number = '' + number; if (number.length > 3) { var mod = number.length % 3; var output = (mod > 0 ? (number.substring(0,mod)) : ''); for (i=0 ; i < Math.floor(number.length / 3); i++) { if ((mod == 0) && (i == 0)) { output += number.substring(mod+ 3 * i, mod + 3 * i + 3); } else { output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3); } } return (output); } else return number; } function srfetch(query, start) { q = query; if(q == ""){ return; } if(document.location.hash != "#"+q && document.location.hash != "#"+encodeURIComponent(q)) document.location.hash = "#"+encodeURIComponent(q); $("q").value = q; $("mq").value = q; $("query").innerHTML = q.escapeHTML(); $("result-q").innerHTML = q.escapeHTML(); $("discuss-result-link").innerHTML = '(discuss these results)'; $("mini-title").innerHTML = q.escapeHTML(); var head = document.getElementsByTagName('head')[0]; cache_base = "http://search.isc.swlabs.org"; // get results //alert("http://search.isc.swlabs.org/nutchsearch?query="+ encodeURIComponent(q) +"&hitsPerSite=2&lang=en&hitsPerPage=10&type=json&start=" + start); var s = document.createElement('script'); s.src = "http://not-quite-ready-yet.index.swlabs.org/nutchsearch?query="+ encodeURIComponent(q) +"&hitsPerSite=1&lang=en&hitsPerPage=10&type=json&start=" + start; head.appendChild(s); // also get any mini article var s = document.createElement('script'); s.src = "http://search.wikia.com/index.php?action=ajax&rs=wfGetArticleJSON&rsargs%5B%5D=Mini:" + encodeURIComponent(q); head.appendChild(s); // also get any people var s = document.createElement('script'); s.src = "http://alpha.search.wikia.com:8983/solr/select/?start=0&row=20&wt=json&json.wrf=processPeople&indent=true&q=" + encodeURIComponent(q); head.appendChild(s); } var lasthash = ""; function hasher() { if(document.location.hash == lasthash) { return; } lasthash = document.location.hash; try { var newq = document.location.hash.substr(1); } catch (e) { throw "Error failed to decode URI:" + document.location.hash.substr(1); } perform_full_search(newq); } function perform_full_search(query) { RedirectQuery.handle(query); $("sr-results").innerHTML = "Loading results for '"+query.escapeHTML()+"'"; srfetch(query,0); DictionaryQuery.handle(query); MathQuery.handle(query); IntegralQuery.handle(query); // DictionaryQuery.handle(query) coming soon. } // break out results since we accidentially (and temporarily) overloaded this function function processJSON(j) { if(j) { if(j.search) { processResult(j.search); }else if(j.html){ processMini(j); } }else{ // only mini article so far doesn't return anything $("sr-mini-tr").innerHTML = ''; $("sr-mini-controls").innerHTML = ""; $("sr-mini-content").innerHTML = 'We do not have a mini article about "'+q.escapeHTML()+'". Improve the search results for everyone by starting this article!'; } } function processPeople(r) { var docs = r.response.docs; var p = $("sr-people-content"); p.innerHTML = ''; if (docs.length != 0) { for(var i=0; i < docs.length; i++) { if (docs[i]) { if((docs[i].defaultPhotoSet)) { p.innerHTML += ''+docs[i].name+''; } else { p.innerHTML += ''+docs[i].name+''; } if (((i+1)%4==0) || (i==docs.length-1)) { p.innerHTML += '
'; } } } } else { p.innerHTML += '
List yourself here
'; } } // star rating stuffs function iStar(n) { return ''; } var MaxRating = 5; var clearRatingTimer = ""; var voted_new = new Array(); var id=0; var last_id = 0; function startClearRating(id,rating,voted) { clearRatingTimer = setTimeout("clearRating('" + id + "',0," + rating + "," + voted + ")",200); } function clearRating(id,num,prev_rating,voted) { if(voted_new[id])voted=voted_new[id]; for (var x=1;x<=MaxRating;x++) { if(voted){ star_on = "voted"; old_rating = voted; }else{ star_on = "on"; old_rating = prev_rating; } if(!num && old_rating >= x){ $("rating_" + id + "_" + x).src = "http://images.wikia.com/openserving/sports/images/star_" + star_on + ".gif"; }else{ $("rating_" + id + "_" + x).src = "http://images.wikia.com/openserving/sports/images/star_off.gif"; } } } function updateRating(id,num,prev_rating) { if(clearRatingTimer && last_id==id)clearTimeout(clearRatingTimer); clearRating(id,num,prev_rating); for (var x=1;x<=num;x++) { $("rating_" + id + "_" + x).src = "http://images.wikia.com/openserving/sports/images/star_voted.gif"; } last_id = id; } var showWarn = true; function clickVoteStars(id,rating) { if(showWarn) alert("We're currently using these just to test out the star rating system and are not yet storing them permanently."); showWarn = false; voted_new[id] = rating; new Ajax.Request("/do/log", { method: 'post', postBody: q+'\n'+rating+'\n'+results[id].fields.url+'\n\n' }); } function voteShow(id) { $('stars_'+id).style.display='inline'; } function voteHide(id) { if(voted_new[id]) return; $('stars_'+id).style.display='none'; } var rid=0; var results = new Array(); function processResult(search) { var sr = $("sr-results"); if (sr.innerHTML.substr(0,7) == "Loading") { sr.innerHTML = ""; } if(search.numberOfHits == 0) { $("count").innerHTML = ":("; $("span").innerHTML = "0"; $("sr-bottom").hide(); return; } // update/show fields on results screen $("sr-bottom").show(); $("count").innerHTML = comma(search.numberOfHits); $("span").innerHTML = (search.numberOfHits > 9)?"1-10":"1-"+search.numberOfHits+" "; var last1=""; var rehost = /http:\/\/([^\/]+)/i; var rekw = new RegExp("("+search.query+")","ig"); // String created for each result. var template = '\
\
#{docTitle}
\
#{docSummary}
\
\ \ #{docUrlStr}\ \ Cached - \ \ #{docBoost}\ \ \
\
'; for (var i=0;i < search.documents.length; i++) { rid++; // unique result id results[rid] = search.documents[i]; var doc = search.documents[i]; var url = new String(doc.fields.url); var host = url.match(rehost); var docSummary = doc.summary != "" ? doc.summary: "..."; // Hilight keywords. var keyword_split = "$1" docSummary = docSummary.replace(rekw, keyword_split); docSummary = ''+docSummary+''; var params = { 'rid' : rid, 'cache_base' : cache_base, 'docUrl' : doc.fields.url, 'docTitle' : doc.fields.title != ""? doc.fields.title : search.query, 'docSummary' : docSummary, 'docUrlStr' : url.length > 52? url.substr(0,52) + "... - " : doc.fields.url + ' - ', 'docIndexNo' : doc.indexNo, 'docIndexDocNo' : doc.indexDocumentNo, 'searchQuery' : search.Query, 'docBoost' : doc.fields.boost, 'starSeq' : iStar(1)+iStar(2)+iStar(3)+iStar(4)+iStar(5) } var htm = template.interpolate(params); if(host[0] == last1) { htm = '
' + htm + '
'; } last1 = host[0]; // for next result indentation sr.innerHTML += htm; } var hitsPerPage = 10; if(search.end - search.start >= hitsPerPage) { sr.innerHTML += '
'; } else { sr.innerHTML += '
No more results have been found. Perhaps try less restrictive search terms...
' } } function convert_to_span(id) { var element = document.getElementById(id); var parent = element.parentNode; var replacement = document.createElement("span"); replacement.class = "frozen-link"; replacement.innerHTML = element.innerHTML; parent.insertBefore(replacement, element); parent.removeChild(element); } function processMini(j) { $("sr-mini-content").innerHTML = j.html; $("sr-mini-controls").innerHTML = ""; $("sr-mini-tr").innerHTML = ' Edit'; if (j.html!="") { $("sr-mini-controls").innerHTML = ''; $("sr-mini-controls").innerHTML += 'Expand - '; $("sr-mini-controls").innerHTML += 'History - Full Article'; } } function context(e) { var kw = $(e); var more; if(kw.next() && kw.next().tagName == "SPAN") { var stub = kw.next().innerHTML.match(/(.+?)\W{2,}/); more = (stub)?stub[0]:kw.next().innerHTML; if(more.length > 3) { kw.style.textDecoration = "underline"; kw.next().style.textDecoration = "underline"; kw.onmouseout = function (){ this.style.textDecoration = ""; this.next().style.textDecoration = ""; } kw.onclick = function () { $("sr-results").innerHTML = ""; srfetch(kw.innerHTML + more.unescapeHTML(),0); } } } return false; } function boost(amount) { amount -= 0; amount = (Math.round(amount*100))/100; return (amount == Math.floor(amount)) ? amount + '.00' : ( (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount); } function toggleMini() { if ($("sr-mini-content").hasClassName("collapsed")) { $("sr-mini-content").removeClassName("collapsed"); $("sr-mini-content").addClassName("expanded"); $("sr-mini-toggle-button").innerHTML = 'Collapse'; } else { $("sr-mini-content").removeClassName("expanded"); $("sr-mini-content").addClassName("collapsed"); $("sr-mini-toggle-button").innerHTML = 'Expand'; } } function load_search_terms() { window.location.href= '#' + encodeURIComponent(document.forms[0].q.value); } /* Perform testing for all modules. */ function test() { Calculate.test(); } //test();