| 1 |
/** |
/** |
| 2 |
* Much of this code was borrowed from the Drupal 5 autocomplete module and modified to |
* Register DynoSearcho behavior |
| 3 |
* suit this application. Standing on the shoulders of giants... |
*/ |
| 4 |
*/ |
Drupal.behaviors.dynosearcho = function (context) { |
| 5 |
|
$('input.dynosearcho-searchbox').each(function () { |
| 6 |
var dts = {}; |
var delta = $(this).parent().siblings("input[name='dynosearcho_delta']").val(); |
| 7 |
|
var input = '#edit-dynosearcho-search-' + delta; |
| 8 |
/** |
var expres = '#edit-dynosearcho-exp-' + delta; |
| 9 |
* Attaches the search behaviour to all dynosearcho fields |
var wrapper = '#dynosearcho-results-' + delta; |
| 10 |
*/ |
var searchon = $('#edit-dynosearcho-integrated-search-' + delta).val(); |
| 11 |
dts.searchAutoAttach = function () { |
dynoPreventEnter (input, searchon); |
| 12 |
var dtsdb = []; |
dynoTriggerAhah(input, expres, wrapper); |
|
$('input.dynosearcho_searchbox').each(function () { |
|
|
// hide the search button |
|
|
$('div.block-dynosearcho input.form-submit').hide() |
|
|
|
|
|
// add the autocomplete icon to the input box |
|
|
$(this).addClass('form-autocomplete'); |
|
|
|
|
|
// get the delta |
|
|
var delta = $(this).parent().siblings('#edit-dynosearcho-delta').val(); |
|
|
|
|
|
// hide results div to start |
|
|
var temp = "#dynosearcho-results-" + delta; |
|
|
$(temp).hide(); |
|
|
|
|
|
// get the correct url for AHAH |
|
|
temp = '#edit-dynosearcho-url-' + delta; |
|
|
var uri = $(temp).val() + "/" + delta; |
|
|
|
|
|
if (!dtsdb[uri]) { |
|
|
dtsdb[uri] = new dts.dtsDB(uri,delta); |
|
|
} |
|
|
new dts.jsAC(this, dtsdb[uri]); |
|
| 13 |
}); |
}); |
| 14 |
} |
} |
|
|
|
|
/** |
|
|
* An AutoComplete object |
|
|
*/ |
|
|
dts.jsAC = function (input, db) { |
|
|
var ac = this; |
|
|
this.input = input; |
|
|
this.db = db; |
|
|
|
|
|
$(this.input).keypress(function (event) { ac.onkeypress(this, event) }); |
|
|
$(this.input).keyup(function (event) { ac.onkeyup(this, event) }); |
|
|
|
|
|
}; |
|
|
|
|
|
/** |
|
|
* Handler for the "keypress" event |
|
|
* Used to prevent the enter key used to submit the form here |
|
|
*/ |
|
|
dts.jsAC.prototype.onkeypress = function (input, e) { |
|
|
if (!e) { |
|
|
e = window.event; |
|
|
} |
|
|
switch (e.keyCode) { |
|
|
case 13: // enter |
|
|
input.blur(); |
|
|
e.preventDefault(); |
|
|
|
|
|
//----- If the search module is installed, and the config option is checked, then pass the search terms |
|
|
//----- along to the search module if the "enter" key is pressed. |
|
|
var delta = $(input).parent().siblings('#edit-dynosearcho-delta').val(); |
|
|
var searchon = $('#edit-dynosearcho-integrated-search-' + delta).val(); |
|
|
if (searchon == 1) { |
|
|
//----- figure out the correct path to the Drupal search |
|
|
var basepath = $('#edit-dynosearcho-basepath-' + delta).val(); |
|
|
var useq = $('form#dynosearcho_search_form_' + delta).attr('action'); |
|
|
if (useq.indexOf("?q=") > 0) { |
|
|
window.location.href = basepath + "?q=search/node/" + Drupal.encodeURIComponent(this.input.value); |
|
|
} else { |
|
|
window.location.href = basepath + "search/node/" + Drupal.encodeURIComponent(this.input.value); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
| 15 |
/** |
/** |
| 16 |
* Handler for the "keyup" event |
* Register DynoSearcho block configuration behavior |
| 17 |
*/ |
*/ |
| 18 |
dts.jsAC.prototype.onkeyup = function (input, e) { |
Drupal.behaviors.dynosearchoAdmin = function (context) { |
|
if (!e) { |
|
|
e = window.event; |
|
|
} |
|
|
switch (e.keyCode) { |
|
|
case 16: // shift |
|
|
case 17: // ctrl |
|
|
case 18: // alt |
|
|
case 20: // caps lock |
|
|
case 33: // page up |
|
|
case 34: // page down |
|
|
case 35: // end |
|
|
case 36: // home |
|
|
case 37: // left arrow |
|
|
case 38: // up arrow |
|
|
case 39: // right arrow |
|
|
case 40: // down arrow |
|
|
case 9: // tab |
|
|
case 27: // esc |
|
|
return true; |
|
|
|
|
|
default: // all other keys |
|
|
if (input.value.length > 0) { |
|
|
this.doSearch(); |
|
|
} else { |
|
|
this.popup.hide(); |
|
|
this.popup.empty(); |
|
|
} |
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* Starts a search |
|
|
*/ |
|
|
dts.jsAC.prototype.doSearch = function () { |
|
|
// Show popup |
|
|
var resultsDiv = "div#dynosearcho_results_" + this.db.delta; |
|
|
this.popup = $(resultsDiv); |
|
|
this.popup.hide(); |
|
|
this.popup.empty(); |
|
|
|
|
|
this.db.owner = this; |
|
|
this.db.search(this.input.value); |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
* Fills the div with any matches received |
|
|
*/ |
|
|
dts.jsAC.prototype.found = function (matches) { |
|
|
// If no value in the textfield, do not show the results. |
|
|
if (!this.input.value.length) { |
|
|
return false; |
|
|
} |
|
|
|
|
|
// Prepare matches |
|
|
var ul = document.createElement('ul'); |
|
|
var ac = this; |
|
|
var len = matches.length; |
|
|
for (var key = 0; key < len; key++) { |
|
|
var li = document.createElement('li'); |
|
|
$(li).html(matches[key]); |
|
|
li.autocompleteValue = key; |
|
|
$(ul).append(li); |
|
|
} |
|
|
|
|
|
// Show popup with matches, if any |
|
|
if (this.popup) { |
|
|
if (ul.childNodes.length > 0) { |
|
|
$(this.popup).empty().append(ul).fadeIn('slow'); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
dts.jsAC.prototype.setStatus = function (status) { |
|
|
switch (status) { |
|
|
case 'begin': |
|
|
$(this.input).addClass('throbbing'); |
|
|
break; |
|
|
case 'cancel': |
|
|
case 'error': |
|
|
case 'found': |
|
|
$(this.input).removeClass('throbbing'); |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* An dynosearcho DataBase object |
|
|
*/ |
|
|
dts.dtsDB = function (uri, delta) { |
|
|
this.uri = uri; |
|
|
this.delta = delta; |
|
|
this.delay = 300; |
|
|
this.cache = {}; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Performs a cached and delayed search |
|
|
*/ |
|
|
dts.dtsDB.prototype.search = function (searchString) { |
|
|
var db = this; |
|
|
this.searchString = searchString; |
|
|
|
|
|
// See if this key has been searched for before |
|
|
if (this.cache[searchString]) { |
|
|
return this.owner.found(this.cache[searchString]); |
|
|
} |
|
|
|
|
|
// Initiate delayed search |
|
|
if (this.timer) { |
|
|
clearTimeout(this.timer); |
|
|
} |
|
|
this.timer = setTimeout(function() { |
|
|
db.owner.setStatus('begin'); |
|
|
|
|
|
// Ajax GET request for autocompletion |
|
|
$.ajax({ |
|
|
type: "GET", |
|
|
url: db.uri +'/'+ Drupal.encodeURIComponent(searchString), |
|
|
success: function (data) { |
|
|
// Parse back result |
|
|
var matches = Drupal.parseJson(data); |
|
|
if (typeof matches['status'] == 'undefined' || matches['status'] != 0) { |
|
|
db.cache[searchString] = matches; |
|
|
// Verify if these are still the matches the user wants to see |
|
|
if (db.searchString == searchString) { |
|
|
db.owner.found(matches); |
|
|
} |
|
|
db.owner.setStatus('found'); |
|
|
} |
|
|
}, |
|
|
error: function (xmlhttp) { |
|
|
alert('An HTTP error '+ xmlhttp.status +' occured.\n'+ db.uri); |
|
|
} |
|
|
}); |
|
|
}, this.delay); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Cancels the current autocomplete request |
|
|
*/ |
|
|
dts.dtsDB.prototype.cancel = function() { |
|
|
if (this.owner) this.owner.setStatus('cancel'); |
|
|
if (this.timer) clearTimeout(this.timer); |
|
|
this.searchString = ''; |
|
|
} |
|
|
|
|
|
dts.searchTypeAutoAttach = function () { |
|
| 19 |
//----- set the default state |
//----- set the default state |
| 20 |
$('fieldset#dynosearcho_nodesearch').hide(); |
$('fieldset#dynosearcho-nodesearch').hide(); |
| 21 |
$('fieldset#dynosearcho_menusearch').hide(); |
$('fieldset#dynosearcho-usersearch').hide(); |
| 22 |
switch($('input.dynosearcho_searchtype:checked').val()) { |
switch($('input.dynosearcho-searchtype:checked').val()) { |
| 23 |
case 'node': |
case 'node': |
| 24 |
$('fieldset#dynosearcho_nodesearch').show(); |
$('fieldset#dynosearcho-nodesearch').show(); |
| 25 |
break; |
break; |
| 26 |
case 'menu': |
case 'user': |
| 27 |
$('fieldset#dynosearcho_menusearch').show(); |
$('fieldset#dynosearcho-usersearch').show(); |
| 28 |
break; |
break; |
| 29 |
} |
} |
| 30 |
|
//----- update the boxes as radio buttons are clicked |
| 31 |
//----- update the boxes as radio buttons are clicked |
$('input.dynosearcho-searchtype').click(function () { |
|
$('input.dynosearcho_searchtype').click(function () { |
|
| 32 |
if ($(this).val() == 'node') { |
if ($(this).val() == 'node') { |
| 33 |
$('fieldset#dynosearcho_menusearch').hide(); |
$('fieldset#dynosearcho-usersearch').hide(); |
| 34 |
$('fieldset#dynosearcho_nodesearch').slideDown(); |
$('fieldset#dynosearcho-nodesearch').slideDown(); |
| 35 |
} |
} |
| 36 |
else { |
else { |
| 37 |
$('fieldset#dynosearcho_nodesearch').hide(); |
$('fieldset#dynosearcho-nodesearch').hide(); |
| 38 |
$('fieldset#dynosearcho_menusearch').slideDown(); |
$('fieldset#dynosearcho-usersearch').slideDown(); |
| 39 |
} |
} |
| 40 |
}); |
}); |
| 41 |
} |
} |
| 42 |
|
/** |
| 43 |
// Global Killswitch |
* Helper function to prevent the enter key used to submit the form here, if integrated search is off |
| 44 |
if (Drupal.jsEnabled) { |
*/ |
| 45 |
$(document).ready(dts.searchAutoAttach); |
dynoPreventEnter = function(input, searchon) { |
| 46 |
$(document).ready(dts.searchTypeAutoAttach); |
$(input).bind('keydown', function(e) { |
| 47 |
|
if (!e) { |
| 48 |
|
e = window.event; |
| 49 |
|
} |
| 50 |
|
switch (e.keyCode) { |
| 51 |
|
case 13: // enter |
| 52 |
|
if (searchon != 1) { |
| 53 |
|
e.preventDefault(); |
| 54 |
|
}else{ |
| 55 |
|
$(this).unbind('keyup'); |
| 56 |
|
} |
| 57 |
|
} |
| 58 |
|
}); |
| 59 |
|
} |
| 60 |
|
/** |
| 61 |
|
* Helper function - triggers blur() event on ahah form element to execute ahah search. |
| 62 |
|
*/ |
| 63 |
|
dynoTriggerAhah = function(input, expres, wrapper) { |
| 64 |
|
var delay = 300; |
| 65 |
|
$(input).unbind('keyup').bind('keyup', function () { |
| 66 |
|
if (this.value.length == 0) { |
| 67 |
|
$(wrapper).hide(); |
| 68 |
|
$(wrapper).empty(); |
| 69 |
|
}else{ |
| 70 |
|
$(wrapper).show(); |
| 71 |
|
$('.throbber').remove(); |
| 72 |
|
$(expres).val(this.value); |
| 73 |
|
if (this.timer) { |
| 74 |
|
clearTimeout(this.timer); |
| 75 |
|
} |
| 76 |
|
this.timer = setTimeout(function() { |
| 77 |
|
$(expres).blur(); |
| 78 |
|
}, delay); |
| 79 |
|
} |
| 80 |
|
}); |
| 81 |
} |
} |
|
|
|