PageRenderTime 22ms CodeModel.GetById 2ms app.highlight 14ms RepoModel.GetById 2ms app.codeStats 0ms

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