| 1 |
/**
|
| 2 |
* Jonathan Hendler
|
| 3 |
* The author can be contacted at:
|
| 4 |
* - jonathan [at] civicactions (dot) com
|
| 5 |
*
|
| 6 |
* there are hooks which can be called via extending functions
|
| 7 |
*
|
| 8 |
* semantic_searchSearchStartedHook()
|
| 9 |
* semantic_searchSearchCompletedHook()
|
| 10 |
* semantic_searchSearchSuccessfulHook();
|
| 11 |
* semantic_searchSearchRefresh();
|
| 12 |
*
|
| 13 |
* simply define the above functions in the JS of an extending module
|
| 14 |
*
|
| 15 |
*
|
| 16 |
* @license http://www.affero.org/oagpl.html
|
| 17 |
* @link http://drupal.org/project/semantic_search
|
| 18 |
*
|
| 19 |
*/
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
/**
|
| 24 |
* ASAP check if there is a redirect set
|
| 25 |
*/
|
| 26 |
function checkRedirect(){
|
| 27 |
if ( Get_Cookie( 'SEMANTIC_SEARCH_REDIRECT' )){
|
| 28 |
var new_uri = Get_Cookie( 'SEMANTIC_SEARCH_REDIRECT' );
|
| 29 |
Delete_Cookie( 'SEMANTIC_SEARCH_REDIRECT' );
|
| 30 |
//IE7 remote doesn't respect this
|
| 31 |
//if (new_uri != window.location)
|
| 32 |
//window.location = new_uri;
|
| 33 |
}
|
| 34 |
}
|
| 35 |
checkRedirect();
|
| 36 |
|
| 37 |
////settings TODO move these to settings page
|
| 38 |
var default_search_page = '/search/ahirc';
|
| 39 |
var search_home = 'http://ahirc-dev.civicactions.net'+default_search_page;
|
| 40 |
var default_url_prefix = '/semantic_search_ajax/search';
|
| 41 |
var url_prefix = null;
|
| 42 |
//current RPC location
|
| 43 |
var rpc_uri= null;
|
| 44 |
//enable debugging messages
|
| 45 |
var semantic_search_debugging = true;
|
| 46 |
//enable benchmarking
|
| 47 |
var semantic_search_benchmarking = false;
|
| 48 |
//enable snap
|
| 49 |
var semantic_search_snap = false;
|
| 50 |
|
| 51 |
//globals /constants
|
| 52 |
/* url separator*/
|
| 53 |
var sep = '&';
|
| 54 |
//this needs to be set dynamically
|
| 55 |
//signal updates
|
| 56 |
var semantic_search_updating = true;
|
| 57 |
//signal for snap
|
| 58 |
var snapped = false;
|
| 59 |
|
| 60 |
$(document).unload(function() {
|
| 61 |
});
|
| 62 |
|
| 63 |
/**
|
| 64 |
* abstraction for saving in local cache amd history
|
| 65 |
*/
|
| 66 |
function initialize() {
|
| 67 |
$.historyInit(SEMANTIC_SEARCHTestURL);
|
| 68 |
if (window.SEMANTIC_SEARCHHistory == undefined) window.SEMANTIC_SEARCHHistory = new Array();
|
| 69 |
}
|
| 70 |
|
| 71 |
/**
|
| 72 |
* use javascript to set links and set paginataion links to
|
| 73 |
* behave as specified
|
| 74 |
*/
|
| 75 |
function setLinks(){
|
| 76 |
$(".bookmarkSearch a").click( function(){
|
| 77 |
var new_uri = $(this).attr('href');
|
| 78 |
if ($.browser.msie){
|
| 79 |
window.external.AddFavorite(new_uri,window.title);
|
| 80 |
window.location = new_uri;
|
| 81 |
return false;
|
| 82 |
}
|
| 83 |
//else if (window.sidebar) {
|
| 84 |
// window.sidebar.addPanel(window.title, new_uri,'');
|
| 85 |
// window.location = new_uri;
|
| 86 |
// return false;
|
| 87 |
//}
|
| 88 |
else {
|
| 89 |
alert('Use the URL above as a link to the this page.');
|
| 90 |
window.location = new_uri;
|
| 91 |
return false;
|
| 92 |
}
|
| 93 |
|
| 94 |
});
|
| 95 |
|
| 96 |
$(".semantic_searchPagination a").click( function( ){
|
| 97 |
var new_uri = $(this).attr('href');
|
| 98 |
new_uri = new_uri.replace(default_search_page,default_url_prefix);
|
| 99 |
semantic_searchSearchSubmitLink(new_uri);
|
| 100 |
return false;
|
| 101 |
});
|
| 102 |
|
| 103 |
//perhaps these three can be made into one function?
|
| 104 |
$(".NinaSearchResultTitle a").click( function( ){
|
| 105 |
var new_uri = $(this).attr('href');
|
| 106 |
|
| 107 |
var bookmark_uri = $(".bookmarkSearch a").attr('href');
|
| 108 |
Set_Cookie( 'SEMANTIC_SEARCH_REDIRECT', bookmark_uri );
|
| 109 |
if ($(this).attr('target') == '_blank'){
|
| 110 |
// $(this).attr('target,');
|
| 111 |
// window.open(new_uri,'_blank');
|
| 112 |
} else {
|
| 113 |
window.location = new_uri;
|
| 114 |
return false;
|
| 115 |
}
|
| 116 |
});
|
| 117 |
|
| 118 |
$(".completeInfoLink a").click( function( ){
|
| 119 |
var new_uri = $(this).attr('href');
|
| 120 |
var bookmark_uri = $(".bookmarkSearch a").attr('href');
|
| 121 |
Set_Cookie( 'SEMANTIC_SEARCH_REDIRECT', bookmark_uri );
|
| 122 |
if ($(this).attr('target') == '_blank'){
|
| 123 |
window.open(new_uri,'_blank');
|
| 124 |
} else {
|
| 125 |
window.location = new_uri;
|
| 126 |
}
|
| 127 |
return false;
|
| 128 |
});
|
| 129 |
|
| 130 |
$("._external").click( function( ){
|
| 131 |
var new_uri = $(this).attr('href');
|
| 132 |
var bookmark_uri = $(".bookmarkSearch a").attr('href');
|
| 133 |
Set_Cookie( 'SEMANTIC_SEARCH_REDIRECT', bookmark_uri );
|
| 134 |
window.location = new_uri;
|
| 135 |
return false;
|
| 136 |
});
|
| 137 |
}
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
$(document).ready(function(){
|
| 142 |
|
| 143 |
//cache functions
|
| 144 |
window.initialize();
|
| 145 |
|
| 146 |
setLinks();
|
| 147 |
|
| 148 |
$("#advanced-search-box").hide();
|
| 149 |
/* disable certain anchor tags*/
|
| 150 |
$(".ahirc-title-link").click(function(){
|
| 151 |
return false;
|
| 152 |
});
|
| 153 |
|
| 154 |
|
| 155 |
// change the href so there is no redirect or location change
|
| 156 |
$("#ahirc-advanced-search-link").click( function (){
|
| 157 |
$("#advanced-search-box").toggle("slow");
|
| 158 |
}
|
| 159 |
);
|
| 160 |
|
| 161 |
|
| 162 |
$("#semantic_search-search-button").click(function (){
|
| 163 |
|
| 164 |
semantic_searchSearchSubmitButton();
|
| 165 |
return false;
|
| 166 |
});
|
| 167 |
|
| 168 |
bindToggleHelpText();
|
| 169 |
$(".hiddenSearchResults").hide();
|
| 170 |
|
| 171 |
//makes the first page accesible via back button
|
| 172 |
setSEMANTIC_SEARCHHistory();
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
});
|
| 177 |
|
| 178 |
/**
|
| 179 |
* a function call to embed in links so that onClick, onFocus,
|
| 180 |
* the links or selects can submit a search form.
|
| 181 |
*/
|
| 182 |
function semantic_searchSearchSubmitLink(uri, updateHistory) {
|
| 183 |
//pause the history monitor
|
| 184 |
//clearInterval( historyIntervalID );
|
| 185 |
//save the rpc link for benchmarking stats and current location
|
| 186 |
rpc_uri = uri;
|
| 187 |
semantic_search_updating = true;
|
| 188 |
if(updateHistory == undefined || updateHistory )setSEMANTIC_SEARCHHistory(rpc_uri);
|
| 189 |
|
| 190 |
//$("#dev-studies").html('Searching...');
|
| 191 |
$("#header").prepend('<div class="ActiveProgress" id="ActiveProgress" > </div>');
|
| 192 |
//$("#ahirc-results-content").html('Searching...');
|
| 193 |
$(".semantic_searchPagination").html('');
|
| 194 |
|
| 195 |
if (semantic_search_benchmarking) nStart();
|
| 196 |
|
| 197 |
var url_hash = hex_sha1(uri);
|
| 198 |
|
| 199 |
//check cache
|
| 200 |
if ($.cacheCheck(url_hash)) {
|
| 201 |
// pull from cache
|
| 202 |
try{
|
| 203 |
var xdata = $.cacheGet(url_hash);
|
| 204 |
var values = decodeReturnArray(xdata);
|
| 205 |
renderChanges(values);
|
| 206 |
}
|
| 207 |
catch(e){alert(e.message +" \n 'used cache'\n "+e.description +"\n " +uri+"\n aaaaaaaaaa");}
|
| 208 |
}
|
| 209 |
else {
|
| 210 |
// pull from server
|
| 211 |
$.get(uri, function(data){
|
| 212 |
try{
|
| 213 |
var values = decodeReturnArray(data);
|
| 214 |
$.cachePut(url_hash,data);
|
| 215 |
}
|
| 216 |
catch(e){alert(e.message +"\n "+e.description +"\n " +uri+"\n aaaaaaaaaa");}
|
| 217 |
renderChanges(values);
|
| 218 |
|
| 219 |
} );
|
| 220 |
}
|
| 221 |
}
|
| 222 |
|
| 223 |
function renderChanges(values)
|
| 224 |
{
|
| 225 |
if (values != undefined){
|
| 226 |
$("#adev-results-content").fadeOut("fast",
|
| 227 |
function (){
|
| 228 |
$("#adev-results-content").html(decodeSEMANTIC_SEARCHHtml(values[0]));
|
| 229 |
$(".hiddenSearchResults").hide();
|
| 230 |
$("#adev-results-content").fadeIn("slow");
|
| 231 |
});
|
| 232 |
|
| 233 |
$("#reg-search-box").fadeOut("fast",
|
| 234 |
function (){
|
| 235 |
$("#reg-search-box").html(decodeSEMANTIC_SEARCHHtml(values[1]));
|
| 236 |
$("#reg-search-box").fadeIn("slow");
|
| 237 |
});
|
| 238 |
|
| 239 |
$("#dev-studies").fadeOut("fast",
|
| 240 |
function (){
|
| 241 |
$("#ActiveProgress").remove();
|
| 242 |
$("#dev-studies").html(decodeSEMANTIC_SEARCHHtml(values[2]));
|
| 243 |
$("#dev-studies").fadeIn("slow", function (){
|
| 244 |
//bind
|
| 245 |
bindToggleHelpText();
|
| 246 |
$(".hiddenSearchResults").hide();
|
| 247 |
$.hookExecute('semantic_searchSearchSuccessfulHook');
|
| 248 |
|
| 249 |
});
|
| 250 |
|
| 251 |
$("#search-results-title").html(decodeSEMANTIC_SEARCHHtml( values[4] ) );
|
| 252 |
if (values[0] != '') {
|
| 253 |
$("#search-results-title-b").html(decodeSEMANTIC_SEARCHHtml( values[4] ) );
|
| 254 |
}
|
| 255 |
else {
|
| 256 |
$("#search-results-title-b").html('');
|
| 257 |
}
|
| 258 |
|
| 259 |
semantic_search_updating = false;
|
| 260 |
if (semantic_search_snap) window.reloadSnap();
|
| 261 |
snapped = false;
|
| 262 |
setLinks();
|
| 263 |
|
| 264 |
if (semantic_search_benchmarking) nEnd('roundtrip');
|
| 265 |
|
| 266 |
|
| 267 |
}
|
| 268 |
);
|
| 269 |
}
|
| 270 |
}
|
| 271 |
|
| 272 |
/***/
|
| 273 |
function semantic_searchToggle(id){
|
| 274 |
$("#"+id).slideToggle("slow");
|
| 275 |
}
|
| 276 |
|
| 277 |
/**
|
| 278 |
* when js changes occur on the page
|
| 279 |
* this function refreshes SNAP
|
| 280 |
*/
|
| 281 |
|
| 282 |
function reloadSnap(){
|
| 283 |
if (window.snap_preview_anywhere == undefined){
|
| 284 |
window.snap_preview_anywhere=new snapPreviewAnywhere();
|
| 285 |
}
|
| 286 |
|
| 287 |
if(window.snap_preview_anywhere != undefined){
|
| 288 |
if ($.browser.msie){
|
| 289 |
$("a").hover(function(){
|
| 290 |
try{
|
| 291 |
if (!snapped) window.snap_preview_anywhere.add_link_events();
|
| 292 |
} catch(e){ }
|
| 293 |
}, function(){});
|
| 294 |
}
|
| 295 |
|
| 296 |
try{
|
| 297 |
window.snap_preview_anywhere.add_link_events();
|
| 298 |
} catch(e){ }
|
| 299 |
|
| 300 |
}
|
| 301 |
}
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
/**
|
| 306 |
* a function call to embed in links so that or onChange,
|
| 307 |
* the links or selects can submit a search form.
|
| 308 |
*/
|
| 309 |
function semantic_searchSearchSubmitSelect(id,prefix) {
|
| 310 |
if (url_prefix != null){
|
| 311 |
var turl = url_prefix;
|
| 312 |
}
|
| 313 |
else{
|
| 314 |
var turl = default_url_prefix+'?edit[sesu]=1&edit[1980]=1';
|
| 315 |
}
|
| 316 |
var furi = turl+'&'+ $('#'+id).attr('name') +'='+ $('#'+id).val();
|
| 317 |
semantic_searchSearchSubmitLink(furi);
|
| 318 |
}
|
| 319 |
|
| 320 |
function semantic_searchSearchSubmitButton(){
|
| 321 |
//var furi = uri+ $('#semantic_search-search-fulltext').attr('name') +'='+ $('#semantic_search-search-fulltext').val();
|
| 322 |
semantic_searchSearchSubmitLink(_semantic_searchSearchURIBuilder());
|
| 323 |
}
|
| 324 |
|
| 325 |
/**
|
| 326 |
* display an error
|
| 327 |
*/
|
| 328 |
function _semantic_search_err(e,msg){
|
| 329 |
alert(msg+' ' +e.message );
|
| 330 |
}
|
| 331 |
|
| 332 |
function _semantic_searchFormIterator(type){
|
| 333 |
var turl = '';
|
| 334 |
$(type,document._SEMANTIC_SEARCHSearch).each(
|
| 335 |
function(i){
|
| 336 |
switch(this.type){
|
| 337 |
case 'text':
|
| 338 |
if ($(this).val() != ''){
|
| 339 |
turl += _semantic_searchURLAppend($(this).attr("name"),$(this).val())
|
| 340 |
}
|
| 341 |
|
| 342 |
break;
|
| 343 |
case 'select-one':
|
| 344 |
if ($(this).val() != 0){
|
| 345 |
turl += _semantic_searchURLAppend($(this).attr("name"),$(this).val());
|
| 346 |
}
|
| 347 |
|
| 348 |
break;
|
| 349 |
}
|
| 350 |
}
|
| 351 |
);
|
| 352 |
return turl;
|
| 353 |
}
|
| 354 |
|
| 355 |
function _semantic_searchURLAppend(element_name,element_value){
|
| 356 |
return sep+element_name+'='+SEMANTIC_SEARCHUrl.encode(element_value);
|
| 357 |
}
|
| 358 |
|
| 359 |
/**
|
| 360 |
* @TODO remove
|
| 361 |
* all because I don't know how, after a .post to reset the pages functions
|
| 362 |
*/
|
| 363 |
function setUrlPrefix(pf) {
|
| 364 |
url_prefix = pf;
|
| 365 |
}
|
| 366 |
|
| 367 |
/**
|
| 368 |
* Two purposes
|
| 369 |
* build query string
|
| 370 |
* update form itself
|
| 371 |
*
|
| 372 |
* @return the query string for a SEMANTIC_SEARCH search
|
| 373 |
*
|
| 374 |
*/
|
| 375 |
function _semantic_searchSearchURIBuilder(){
|
| 376 |
|
| 377 |
//Sadly, These signals come from sonia.const.inc and will break if changed
|
| 378 |
//see also var sep= '&';
|
| 379 |
if (url_prefix != null){
|
| 380 |
var turl = url_prefix;
|
| 381 |
}
|
| 382 |
else{
|
| 383 |
var turl = default_url_prefix+'?edit[sesu]=1&edit[1980]=1';
|
| 384 |
}
|
| 385 |
var theElement = '';
|
| 386 |
var element_type = '';
|
| 387 |
|
| 388 |
//TODO select-multiple radio, textarea
|
| 389 |
turl += _semantic_searchFormIterator("select");
|
| 390 |
turl += _semantic_searchFormIterator("input");
|
| 391 |
turl += _semantic_searchFormIterator("checkbox");
|
| 392 |
//turl += _semantic_searchFormIterator("textarea");
|
| 393 |
return turl;
|
| 394 |
|
| 395 |
}
|
| 396 |
|
| 397 |
/**
|
| 398 |
* work around for back button navigation issue
|
| 399 |
*/
|
| 400 |
function setSEMANTIC_SEARCHHistory(semantic_search_rpc_url){
|
| 401 |
//needs to pause the SEMANTIC_SEARCHTestURL
|
| 402 |
try{
|
| 403 |
var temploc = new String(document.location);
|
| 404 |
var temparray = temploc.split('#');
|
| 405 |
var hash = new String(window.SEMANTIC_SEARCHHistory.length);
|
| 406 |
if (semantic_search_rpc_url != null) window.SEMANTIC_SEARCHHistory[hash] = semantic_search_rpc_url;
|
| 407 |
else if (semantic_search_rpc_url == null) window.SEMANTIC_SEARCHHistory[hash] = temploc;
|
| 408 |
$.historyLoad(hash);
|
| 409 |
|
| 410 |
} catch(e){if(semantic_search_debugging) alert(e + '\n setSEMANTIC_SEARCHHistory()');}
|
| 411 |
}
|
| 412 |
|
| 413 |
function SEMANTIC_SEARCHTestURL(newLocation){
|
| 414 |
if (newLocation != '' && window.SEMANTIC_SEARCHHistory != undefined)
|
| 415 |
{
|
| 416 |
try{
|
| 417 |
//now get link from storage
|
| 418 |
var newlink = window.SEMANTIC_SEARCHHistory[newLocation];
|
| 419 |
if (newlink != undefined && !semantic_search_updating && newlink != rpc_uri){
|
| 420 |
if (newLocation != 0) semantic_searchSearchSubmitLink(newlink,false);
|
| 421 |
else window.location = newlink;
|
| 422 |
}
|
| 423 |
}catch(e){ if(semantic_search_debugging) alert(e +'/n @ SEMANTIC_SEARCHTestURL');}
|
| 424 |
}
|
| 425 |
}
|
| 426 |
|
| 427 |
function bindToggleHelpText(){
|
| 428 |
// $("#NinaSearchNavHelp").show('slow');
|
| 429 |
$("#NinaSearchNavHelpTitle").bind("click",function(){
|
| 430 |
$("#NinaSearchNavHelp").toggle();
|
| 431 |
});
|
| 432 |
}
|