| 1 |
// $Id: livesearch.js,v 1.17 2008/07/13 17:50:48 kourge Exp $
|
| 2 |
if (Drupal.jsEnabled) { // global killswitch
|
| 3 |
(function($) { // Dollar namespace scoping
|
| 4 |
Drupal.LiveSearch = {
|
| 5 |
timer: null,
|
| 6 |
localizedTerm: null,
|
| 7 |
id: 'live-search-results',
|
| 8 |
|
| 9 |
eventListener: function(event) {
|
| 10 |
if (event.keyCode == 27) { // KEY_ESC = 27
|
| 11 |
$(this).val('');
|
| 12 |
// I'm fed up with debugging animations. ITFC if you like.
|
| 13 |
$('#' + Drupal.LiveSearch.id).empty().hide();
|
| 14 |
}
|
| 15 |
else {
|
| 16 |
try {window.clearTimeout(Drupal.LiveSearch.timer);} catch (e) {}
|
| 17 |
// Throttle and delay
|
| 18 |
Drupal.LiveSearch.timer = window.setTimeout(
|
| 19 |
'Drupal.LiveSearch.initiateSearch()',
|
| 20 |
parseInt(Drupal.settings.liveSearch.delayDuration) || 1250
|
| 21 |
);
|
| 22 |
}
|
| 23 |
},
|
| 24 |
|
| 25 |
initiateSearch: function() {
|
| 26 |
var id = this.id;
|
| 27 |
// Typing this over and over drives me over the edge.
|
| 28 |
var settings = Drupal.settings.liveSearch;
|
| 29 |
var type = settings.targetSearchBox || 'theme';
|
| 30 |
var searchBox = $('input#edit-search-' + type + '-form-keys');
|
| 31 |
var keyword = Drupal.encodeURIComponent(searchBox.val());
|
| 32 |
|
| 33 |
if (!keyword ||
|
| 34 |
keyword.length < settings.minimumWordSize ||
|
| 35 |
keyword == this.localizedTerm) {
|
| 36 |
$('#' + id).empty().hide();
|
| 37 |
return;
|
| 38 |
} else {
|
| 39 |
searchBox.addClass('throbbing');
|
| 40 |
this.requestAndInject(settings.queryURL + '/' + keyword);
|
| 41 |
}
|
| 42 |
},
|
| 43 |
|
| 44 |
requestAndInject: function(url) {
|
| 45 |
var settings = Drupal.settings.liveSearch;
|
| 46 |
var type = settings.targetSearchBox || 'theme';
|
| 47 |
var id = this.id, scrollTo = this.scrollTo;
|
| 48 |
$.getJSON(url, function(data) {
|
| 49 |
var target = $('#' + id);
|
| 50 |
target.html(data.found ? data.results : data.message).show();
|
| 51 |
$('input#edit-search-' + type + '-form-keys').removeClass('throbbing');
|
| 52 |
// Handle paging.
|
| 53 |
$('a[@href^=' + settings.queryURL + ']', target).click(function(event) {
|
| 54 |
Drupal.LiveSearch.requestAndInject($(this).attr('href'));
|
| 55 |
return false;
|
| 56 |
});
|
| 57 |
// Scroll to the top of the search results.
|
| 58 |
if (settings.scrollToResults) {
|
| 59 |
scrollTo(target.get(0));
|
| 60 |
}
|
| 61 |
});
|
| 62 |
},
|
| 63 |
|
| 64 |
scrollTo: function(element) {
|
| 65 |
var t = 0, l = 0;
|
| 66 |
// Retrieve cumulative offset of the search results element.
|
| 67 |
do {
|
| 68 |
t += element.offsetTop || 0;
|
| 69 |
l += element.offsetLeft || 0;
|
| 70 |
element = element.offsetParent;
|
| 71 |
} while (element);
|
| 72 |
// Scroll to the element.
|
| 73 |
window.scrollTo(l, t);
|
| 74 |
}
|
| 75 |
};
|
| 76 |
|
| 77 |
$(document).ready(function() {
|
| 78 |
var settings = Drupal.settings.liveSearch;
|
| 79 |
// User permission killswitch
|
| 80 |
if (!settings.searchAllowed) {
|
| 81 |
return;
|
| 82 |
}
|
| 83 |
var type = settings.targetSearchBox || 'auto';
|
| 84 |
var searchBox = $('#edit-search-' + type + '-form-keys');
|
| 85 |
var searchForm = $('form#search-' + type + '-form');
|
| 86 |
if (type == 'auto') {
|
| 87 |
searchBox = $('#edit-search-block-form-keys, #edit-search-theme-form-keys, #search-box, #searchbox');
|
| 88 |
if (searchBox.length == 0) {
|
| 89 |
return;
|
| 90 |
}
|
| 91 |
searchBox = $(searchBox.get(0));
|
| 92 |
searchForm = $(searchBox).parents('form');
|
| 93 |
}
|
| 94 |
var localizedTerm;
|
| 95 |
|
| 96 |
// Search box killswitch, in case a search block is optionally visible.
|
| 97 |
if (searchBox.length == 0) {
|
| 98 |
return;
|
| 99 |
}
|
| 100 |
|
| 101 |
Drupal.LiveSearch.localizedTerm = localizedTerm =
|
| 102 |
$('input[@type=submit]', searchForm).val() || "Search";
|
| 103 |
|
| 104 |
searchForm.addClass('live-search');
|
| 105 |
|
| 106 |
if (!settings.useCustomElement) {
|
| 107 |
searchForm.append('<div id="' + Drupal.LiveSearch.id + '"></div>');
|
| 108 |
} else {
|
| 109 |
Drupal.LiveSearch.id = settings.customElementId;
|
| 110 |
}
|
| 111 |
var target = $('#' + Drupal.LiveSearch.id).hide();
|
| 112 |
|
| 113 |
if (settings.hideSnippets) {
|
| 114 |
target.addClass('hide-snippet');
|
| 115 |
}
|
| 116 |
target.addClass(settings.showItemInfo ? 'show-item-info' : 'hide-item-info');
|
| 117 |
|
| 118 |
// Apple HIG fans should appreciate this option.
|
| 119 |
if (settings.compactSearchBox) {
|
| 120 |
searchForm.addClass('compact');
|
| 121 |
searchBox.val(
|
| 122 |
localizedTerm
|
| 123 |
).focus(function() {
|
| 124 |
if ($(this).val() == localizedTerm) {
|
| 125 |
$(this).val('');
|
| 126 |
}
|
| 127 |
}).blur(function() {
|
| 128 |
if (!$(this).val()) {
|
| 129 |
$(this).val(localizedTerm);
|
| 130 |
}
|
| 131 |
});
|
| 132 |
}
|
| 133 |
|
| 134 |
searchBox.addClass('form-autocomplete').
|
| 135 |
keyup(Drupal.LiveSearch.eventListener).
|
| 136 |
attr('autocomplete', 'off');
|
| 137 |
});
|
| 138 |
})(jQuery); // End dollar namespace scoping
|
| 139 |
} // End global killswitch
|