/modules/mod_base/lib/js/modules/z.listfilter.js

https://code.google.com/p/zotonic/ · JavaScript · 127 lines · 78 code · 20 blank · 29 comment · 22 complexity · 31df9bfcdee4c998a4637425a145fde5 MD5 · raw file

  1. /* listfilter js
  2. ----------------------------------------------------------
  3. @package: Zotonic 2009
  4. @Author: Tim Benniks <tim@timbenniks.nl>
  5. Copyright 2009 Tim Benniks
  6. Licensed under the Apache License, Version 2.0 (the "License");
  7. you may not use this file except in compliance with the License.
  8. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. qs_score - Quicksilver Score
  16. A port of the Quicksilver string ranking algorithm
  17. "hello world".score("axl") //=> 0.0
  18. "hello world".score("ow") //=> 0.6
  19. "hello world".score("hello world") //=> 1.0
  20. ---------------------------------------------------------- */
  21. String.prototype.score = function(abbreviation,offset) {
  22. offset = offset || 0 // TODO: I think this is unused... remove
  23. if(abbreviation.length == 0) return 0.9
  24. if(abbreviation.length > this.length) return 0.0
  25. for (var i = abbreviation.length; i > 0; i--) {
  26. var sub_abbreviation = abbreviation.substring(0,i)
  27. var index = this.indexOf(sub_abbreviation)
  28. if(index < 0) continue;
  29. if(index + abbreviation.length > this.length + offset) continue;
  30. var next_string = this.substring(index+sub_abbreviation.length)
  31. var next_abbreviation = null
  32. if(i >= abbreviation.length)
  33. next_abbreviation = ''
  34. else
  35. next_abbreviation = abbreviation.substring(i)
  36. var remaining_score = next_string.score(next_abbreviation,offset+index)
  37. if (remaining_score > 0) {
  38. var score = this.length-next_string.length;
  39. if(index != 0) {
  40. var j = 0;
  41. var c = this.charCodeAt(index-1)
  42. if(c==32 || c == 9) {
  43. for(var j=(index-2); j >= 0; j--) {
  44. c = this.charCodeAt(j)
  45. score -= ((c == 32 || c == 9) ? 1 : 0.15)
  46. }
  47. } else {
  48. score -= index
  49. }
  50. }
  51. score += remaining_score * next_string.length
  52. score /= this.length;
  53. return score
  54. }
  55. }
  56. return 0.0
  57. }
  58. jQuery.fn.listfilter = function(options)
  59. {
  60. list = jQuery(options.list);
  61. if(list.length)
  62. {
  63. var rows = list.children()
  64. var cache = rows.map(function()
  65. {
  66. return $(this).text().toLowerCase();
  67. });
  68. this.keyup(filter).keyup().parents('form').submit(function()
  69. {
  70. return false;
  71. });
  72. }
  73. return this;
  74. function filter()
  75. {
  76. var term = jQuery.trim(jQuery(this).val().toLowerCase()), scores = [];
  77. if(!term)
  78. {
  79. rows.show();
  80. }
  81. else
  82. {
  83. rows.hide();
  84. cache.each(function(i)
  85. {
  86. var score = this.score(term);
  87. if(score > 0)
  88. {
  89. scores.push([score, i]);
  90. }
  91. });
  92. jQuery.each(scores.sort(function(a, b){return b[0] - a[0];}), function()
  93. {
  94. jQuery(rows[ this[1] ]).show();
  95. });
  96. }
  97. }
  98. };