jssearch.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // polyfills for IE<9
  2. (function(fn) {
  3. if (!fn.map) {
  4. fn.map = function(f/*, thisArg */) {
  5. if (this === void 0 || this === null)
  6. throw new TypeError();
  7. var t = Object(this);
  8. var len = t.length >>> 0;
  9. if (typeof f !== "function")
  10. throw new TypeError();
  11. var res = new Array(len);
  12. var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
  13. for (var i = 0; i < len; i++) {
  14. if (i in t)
  15. res[i] = f.call(thisArg, t[i], i, t);
  16. }
  17. return res;
  18. }
  19. }
  20. if (!fn.forEach) {
  21. fn.forEach = function (f/*, thisArg */) {
  22. if (this === void 0 || this === null)
  23. throw new TypeError();
  24. var t = Object(this);
  25. var len = t.length >>> 0;
  26. if (typeof f !== "function")
  27. throw new TypeError();
  28. var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
  29. for (var i = 0; i < len; i++) {
  30. if (i in t)
  31. f.call(thisArg, t[i], i, t);
  32. }
  33. }
  34. }
  35. })(Array.prototype);
  36. var jssearch = {
  37. /**
  38. * the actual words finally used to query (set by last search call)
  39. */
  40. queryWords: [],
  41. search: function(query) {
  42. var words = jssearch.tokenizeString(query);
  43. var result = {};
  44. jssearch.queryWords = words.map(function(i) { return i.t; });
  45. // do not search when no words given
  46. if (!words.length) {
  47. return result;
  48. }
  49. // result = jssearch.searchForWords(words);
  50. // if ($.isEmptyObject(result)) {
  51. words = jssearch.completeWords(words);
  52. jssearch.queryWords = words.map(function(i) { return i.t; });
  53. result = jssearch.searchForWords(words);
  54. // }
  55. var res = [];
  56. for (var i in result) {
  57. res.push(result[i]);
  58. }
  59. res.sort(function(a,b) { return b.weight - a.weight; });
  60. return res;
  61. },
  62. searchForWords: function(words) {
  63. var result = {};
  64. words.forEach(function(word) {
  65. if (jssearch.index[word.t]) {
  66. jssearch.index[word.t].forEach(function(file) {
  67. if (result[file.f]) {
  68. result[file.f].weight *= file.w * word.w;
  69. } else {
  70. result[file.f] = {
  71. file: jssearch.files[file.f],
  72. weight: file.w * word.w
  73. };
  74. }
  75. });
  76. }
  77. });
  78. return result;
  79. },
  80. completeWords: function(words) {
  81. var result = [];
  82. words.forEach(function(word) {
  83. if (!jssearch.index[word.t] && word.t.length > 2) {
  84. // complete words that are not in the index
  85. for(var w in jssearch.index) {
  86. if (w.substr(0, word.t.length) === word.t) {
  87. result.push({t: w, w: 1});
  88. }
  89. }
  90. } else {
  91. // keep existing words
  92. result.push(word);
  93. }
  94. });
  95. return result;
  96. },
  97. tokenizeString: function(string)
  98. {
  99. if (console) {
  100. console.log('Error: tokenizeString should have been overwritten by index JS file.')
  101. }
  102. return [{t: string, w: 1}];
  103. }
  104. };