2
* helper function to return a node containing the
3
* search summary for a given text. keywords is a list
4
* of stemmed words, hlwords is the list of normal, unstemmed
5
* words. the first one is used to find the occurance, the
6
* latter for highlighting it.
9
jQuery.makeSearchSummary = function(text, keywords, hlwords) {
10
var textLower = text.toLowerCase();
12
$.each(keywords, function() {
13
var i = textLower.indexOf(this.toLowerCase());
17
start = Math.max(start - 120, 0);
18
var excerpt = ((start > 0) ? '...' : '') +
19
$.trim(text.substr(start, 240)) +
20
((start + 240 - text.length) ? '...' : '');
21
var rv = $('<div class="context"></div>').text(excerpt);
22
$.each(hlwords, function() {
23
rv = rv.highlightText(this, 'highlight');
31
var PorterStemmer = function() {
67
var c = "[^aeiou]"; // consonant
68
var v = "[aeiouy]"; // vowel
69
var C = c + "[^aeiouy]*"; // consonant sequence
70
var V = v + "[aeiou]*"; // vowel sequence
72
var mgr0 = "^(" + C + ")?" + V + C; // [C]VC... is m>0
73
var meq1 = "^(" + C + ")?" + V + C + "(" + V + ")?$"; // [C]VC[V] is m=1
74
var mgr1 = "^(" + C + ")?" + V + C + V + C; // [C]VCVC... is m>1
75
var s_v = "^(" + C + ")?" + v; // vowel in stem
77
this.stemWord = function (w) {
91
firstch = w.substr(0,1);
93
w = firstch.toUpperCase() + w.substr(1);
96
re = /^(.+?)(ss|i)es$/;
97
re2 = /^(.+?)([^s])s$/;
100
w = w.replace(re,"$1$2");
101
else if (re2.test(w))
102
w = w.replace(re2,"$1$2");
106
re2 = /^(.+?)(ed|ing)$/;
109
re = new RegExp(mgr0);
110
if (re.test(fp[1])) {
112
w = w.replace(re,"");
115
else if (re2.test(w)) {
116
var fp = re2.exec(w);
118
re2 = new RegExp(s_v);
119
if (re2.test(stem)) {
122
re3 = new RegExp("([^aeiouylsz])\\1$");
123
re4 = new RegExp("^" + C + v + "[^aeiouwxy]$");
126
else if (re3.test(w)) {
128
w = w.replace(re,"");
130
else if (re4.test(w))
140
re = new RegExp(s_v);
146
re = /^(.+?)(ational|tional|enci|anci|izer|bli|alli|entli|eli|ousli|ization|ation|ator|alism|iveness|fulness|ousness|aliti|iviti|biliti|logi)$/;
151
re = new RegExp(mgr0);
153
w = stem + step2list[suffix];
157
re = /^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$/;
162
re = new RegExp(mgr0);
164
w = stem + step3list[suffix];
168
re = /^(.+?)(al|ance|ence|er|ic|able|ible|ant|ement|ment|ent|ou|ism|ate|iti|ous|ive|ize)$/;
169
re2 = /^(.+?)(s|t)(ion)$/;
173
re = new RegExp(mgr1);
177
else if (re2.test(w)) {
178
var fp = re2.exec(w);
179
stem = fp[1] + fp[2];
180
re2 = new RegExp(mgr1);
190
re = new RegExp(mgr1);
191
re2 = new RegExp(meq1);
192
re3 = new RegExp("^" + C + v + "[^aeiouwxy]$");
193
if (re.test(stem) || (re2.test(stem) && !(re3.test(stem))))
197
re2 = new RegExp(mgr1);
198
if (re.test(w) && re2.test(w)) {
200
w = w.replace(re,"");
203
// and turn initial Y back to y
205
w = firstch.toLowerCase() + w.substr(1);
217
_queued_query : null,
221
var params = $.getQueryParameters();
223
var query = params.q[0];
224
var sections = params.sections || [""];
225
$('input[name="q"]').attr('value', query);
226
this.performSearch(query);
228
// section "All" should be the default:
229
$($('#search_sections').children()[0]).attr("selected", "selected");
236
setIndex : function(index) {
239
if ((q = this._queued_query) !== null) {
240
this._queued_query = null;
245
hasIndex : function() {
246
return this._index !== null;
249
deferQuery : function(query) {
250
this._queued_query = query;
253
stopPulse : function() {
254
this._pulse_status = 0;
257
startPulse : function() {
258
if (this._pulse_status >= 0)
261
Search._pulse_status = (Search._pulse_status + 1) % 4;
263
for (var i = 0; i < Search._pulse_status; i++)
265
Search.dots.text(dotString);
266
if (Search._pulse_status > -1)
267
window.setTimeout(pulse, 500);
273
* perform a search for something
275
performSearch : function(query) {
276
// create the required interface elements
277
this.out = $('#search-results');
278
this.title = $('<h2>' + _('Searching') + '</h2>').appendTo(this.out);
279
this.dots = $('<span></span>').appendTo(this.title);
280
this.status = $('<p style="display: none"></p>').appendTo(this.out);
281
this.output = $('<ul class="search"/>').appendTo(this.out);
283
$('#search-progress').text(_('Preparing search...'));
286
// index already loaded, the browser was quick!
287
if (this.hasIndex()) {
290
this.setQuery(query);
294
query : function(query) {
295
// stem the searchterms and add them to the
297
var stemmer = new PorterStemmer();
298
var searchterms = [];
301
var tmp = query.split(/\s+/);
302
var object = (tmp.length == 1) ? tmp[0].toLowerCase() : null;
303
for (var i = 0; i < tmp.length; i++) {
305
var word = stemmer.stemWord(tmp[i]).toLowerCase();
306
// select the correct list
307
if (word[0] == '-') {
308
var toAppend = excluded;
309
word = word.substr(1);
312
var toAppend = searchterms;
313
hlterms.push(tmp[i].toLowerCase());
315
// only add if not already in the list
316
if (!$.contains(toAppend, word))
319
var highlightstring = '?highlight=' + $.urlencode(hlterms.join(" "));
321
//console.debug('SEARCH: searching for:');
322
//console.info('required: ', searchterms);
323
//console.info('excluded: ', excluded);
326
var filenames = this._index.filenames;
327
var titles = this._index.titles;
328
var terms = this._index.terms;
329
var descrefs = this._index.descrefs;
330
var modules = this._index.modules;
331
var desctypes = this._index.desctypes;
334
var objectResults = [];
335
var regularResults = [];
336
$('#search-progress').empty();
338
var params = $.getQueryParameters();
339
var sections = params.sections || [""];
342
if (object != null) {
343
for (var module in modules) {
344
if (module.indexOf(object) > -1) {
345
fn = modules[module];
346
if ( (sections.indexOf(filenames[fn].split('/')[0]) > -1) || (sections.indexOf("") > -1) ) {
347
descr = _('module, in ') + titles[fn];
348
objectResults.push([filenames[fn], module, '#module-'+module, descr]);
352
for (var prefix in descrefs) {
353
for (var name in descrefs[prefix]) {
354
if (name.toLowerCase().indexOf(object) > -1) {
355
match = descrefs[prefix][name];
356
if ( (sections.indexOf(match.split('/')[0]) > -1) || (sections.indexOf("") > -1) ) {
357
fullname = (prefix ? prefix + '.' : '') + name;
358
descr = desctypes[match[1]] + _(', in ') + titles[match[0]];
359
objectResults.push([filenames[match[0]], fullname, '#'+fullname, descr]);
366
// sort results descending
367
objectResults.sort(function(a, b) {
368
return (a[1] > b[1]) ? -1 : ((a[1] < b[1]) ? 1 : 0);
372
// perform the search on the required terms
373
for (var i = 0; i < searchterms.length; i++) {
374
var word = searchterms[i];
375
// no match but word was a required one
376
if ((files = terms[word]) == null)
378
if (files.length == undefined) {
381
// create the mapping
382
for (var j = 0; j < files.length; j++) {
385
fileMap[file].push(word);
387
fileMap[file] = [word];
391
// now check if the files don't contain excluded terms
392
for (var file in fileMap) {
395
// check if all requirements are matched
396
if (fileMap[file].length != searchterms.length)
399
// ensure that none of the excluded terms is in the
401
for (var i = 0; i < excluded.length; i++) {
402
if (terms[excluded[i]] == file ||
403
$.contains(terms[excluded[i]] || [], file)) {
409
// if we have still a valid result we can add it
410
// to the result list
411
var params = $.getQueryParameters();
412
var sections = params.sections || [""];
414
if ( (sections.indexOf(filenames[file].split('/')[0]) > -1) || (sections.indexOf("") > -1) ) {
415
regularResults.push([filenames[file], titles[file], '', null]);
420
// delete unused variables in order to not waste
421
// memory until list is retrieved completely
422
delete filenames, titles, terms;
424
// now sort the regular results descending by title
425
regularResults.sort(function(a, b) {
426
var left = a[1].toLowerCase();
427
var right = b[1].toLowerCase();
428
return (left > right) ? -1 : ((left < right) ? 1 : 0);
432
var results = regularResults.concat(objectResults);
435
var resultCount = results.length;
436
function getSectionLabel(section) {
438
'technical_guide': "Module Index",
439
'contribute': "Community Book",
440
'book': "Open ERP Book",
441
'install': "Installation Manuals",
442
'bi': "Business Intelligence",
443
'features': "Open ERP Features",
444
'developer': "Developer Book"
446
var label = labels[section];
447
return (label) ? label : section;
450
function setPreviousSearch() {
451
var section_field = $('#search_sections');
452
var section_field_options = section_field.children();
453
section_field_options.each(function(i){
454
var params = $.getQueryParameters();
455
if (params.sections) {
456
var current_option = $(section_field_options[i]);
457
if (params.sections.indexOf(current_option.val()) > -1) {
458
$(current_option).attr("selected", "selected");
462
$('input[name="q"]').attr('value', params.q[0]);
467
function displayNextItem() {
468
// results left, load the summary and display it
469
if (results.length) {
470
var item = results.pop();
471
var listItem = $('<li style="display:none"></li>');
473
listItem.append($('<span class="search-section-label"> (' + getSectionLabel(item[0].split('/')[0]) + ') </span>'));
475
listItem.append($('<a/>').attr(
477
item[0] + DOCUMENTATION_OPTIONS.FILE_SUFFIX +
478
highlightstring + item[2]).html(item[1]));
480
listItem.append($('<span> (' + item[3] + ')</span>'));
481
Search.output.append(listItem);
482
listItem.slideDown(5, function() {
485
} else if (DOCUMENTATION_OPTIONS.HAS_SOURCE) {
486
$.get('_sources/' + item[0] + '.txt', function(data) {
487
listItem.append($.makeSearchSummary(data, searchterms, hlterms));
488
Search.output.append(listItem);
489
listItem.slideDown(5, function() {
494
// no source available, just display title
495
Search.output.append(listItem);
496
listItem.slideDown(5, function() {
501
// search finished, update title and status message
503
Search.title.text(_('Search Results'));
505
Search.status.text(_('Your search did not match any documents. Please make sure that all words are spelled correctly and that you\'ve selected enough categories.'));
507
Search.status.text(_('Search finished, found %s page(s) matching the search query.').replace('%s', resultCount));
509
Search.status.fadeIn(500);
517
$(document).ready(function() {