Skip to content
This repository has been archived by the owner on Apr 28, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:vdepizzol/jquery-tokeninput
Browse files Browse the repository at this point in the history
  • Loading branch information
vdepizzol committed May 10, 2011
2 parents e8ba00d + bbe0c18 commit f1b6e7c
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 10 deletions.
26 changes: 26 additions & 0 deletions demo.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript" src="src/jquery.tokeninput.js"></script>

Expand Down Expand Up @@ -225,5 +227,29 @@ <h2 id="allowcustomentry">Allowing Custom Entry</h2>
});
</script>
</div>

<h2 id="parsename">Using local data with parseName function and searchColumns setting</h2>
<div>
<input type="text" id="demo-input-parsename" name="blah" />
<input type="button" value="Submit" />
<script type="text/javascript">
$(document).ready(function() {
$("#demo-input-parsename").tokenInput([
{id: 1, name: 'New York', state: 'NY'},
{id: 2, name: 'Palo Alto', state: 'CA'},
{id: 3, name: 'San Francisco', state: 'CA'},
{id: 4, name: 'Boston', state: 'MA'},
{id: 5, name: 'São Paulo', state: 'SP'},
{id: 6, name: 'Vitória', state: 'ES'}
],
{
parseName: function(element) {
return element.name + ', '+ element.state;
},
searchColumns: ['name', 'state']
});
});
</script>
</div>
</body>
</html>
123 changes: 113 additions & 10 deletions src/jquery.tokeninput.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ var DEFAULT_SETTINGS = {
preventDuplicates: false,
prePopulate: null,
processPrePopulate: false,
parseName: null,
searchColumns: ['name'],
animateDropdown: true,
onResult: null,
onAdd: null,
Expand Down Expand Up @@ -231,7 +233,7 @@ $.TokenList = function (input, url_or_data, settings) {
dropdown_item = $(selected_dropdown_item).prev();
}

if(dropdown_item.length) {
if(dropdown_item && dropdown_item.length) {
select_dropdown_item(dropdown_item);
}

Expand Down Expand Up @@ -432,7 +434,14 @@ $.TokenList = function (input, url_or_data, settings) {

var uniqueid = get_unique_id();

var this_token = $("<li><p>"+ object.name +"</p></li>")
var token_name;
if(settings.parseName) {
token_name = settings.parseName(object);
} else {
token_name = object.name;
}

var this_token = $("<li><p>"+ token_name +"</p></li>")
.addClass(settings.classes.token)
.insertBefore(input_token)
.attr('data-uniqueid', uniqueid);
Expand Down Expand Up @@ -470,6 +479,11 @@ $.TokenList = function (input, url_or_data, settings) {
} else {
var li_data = $.data(item.get(0), "tokeninput");
}

if(!li_data) {
return false;
}

var callback = settings.onAdd;

// See if the token already exists and select it if we don't want duplicates
Expand Down Expand Up @@ -654,13 +668,39 @@ $.TokenList = function (input, url_or_data, settings) {
}
}

RegExp.escape = function(text) {
return text.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&");
};

// Highlight the query part of the search term
function highlight_term(value, term) {
return value.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + RegExp.escape(term) + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<b>$1</b>");
// from http://www.alistapart.com/articles/accent-folding-for-auto-complete/
function highlight_term(str, q) {
var str_folded = str.toString().removeDiacritics().toLowerCase().replace(/[<>]+/g, '');
var q_folded = q.toString().removeDiacritics().toLowerCase().replace(/[<>]+/g, '');

// create an intermediary string with hilite hints
// example: fulani<lo> <lo>pez
var re = new RegExp(q_folded, 'g');
var hilite_hints = str_folded.replace(re, '<'+q_folded+'>');

var spos = 0;
var highlighted = '';

// walk down the original string and the hilite hint
// string in parallel. when you encounter a < or > hint,
// append the opening / closing tag in our final string.
// if the current char is not a hint, append the corresponding
// char from the *original* string to our final string and
// advance the original string's pointer.
for (var i=0; i<hilite_hints.length; i++) {
var c = str.charAt(spos);
var h = hilite_hints.charAt(i);
if (h === '<') {
highlighted += '<b>';
} else if (h === '>') {
highlighted += '</b>';
} else {
spos += 1;
highlighted += c;
}
}
return highlighted;
}

// Populate the results dropdown with some results
Expand All @@ -679,7 +719,15 @@ $.TokenList = function (input, url_or_data, settings) {
.hide();

$.each(results, function(index, value) {
var this_li = $("<li>" + highlight_term(value.name, query) + "</li>")

var token_name;
if(settings.parseName) {
token_name = settings.parseName(value);
} else {
token_name = value.name;
}

var this_li = $("<li>" + highlight_term(token_name, query) + "</li>")
.appendTo(dropdown_ul);

if(index % 2) {
Expand Down Expand Up @@ -809,7 +857,13 @@ $.TokenList = function (input, url_or_data, settings) {
} else if(settings.local_data) {
// Do the search through local data
var results = $.grep(settings.local_data, function (row) {
return row.name.toLowerCase().indexOf(query.toLowerCase()) > -1;
var founded = false;
$(settings.searchColumns).each(function(i, item) {
if(row[item].toString().toLowerCase().removeDiacritics().indexOf(query.toString().toLowerCase().removeDiacritics()) > -1) {
founded = true;
}
});
return founded;
});

if($.isFunction(settings.onResult)) {
Expand All @@ -822,6 +876,55 @@ $.TokenList = function (input, url_or_data, settings) {
}
};

// Remove Diacritics
// from http://www.alistapart.com/articles/accent-folding-for-auto-complete/
String.prototype.removeDiacritics = function () {

var str = this;

if (!str) {
return '';
}

var accent_map = {
'ẚ':'a', 'Á':'a', 'á':'a', 'À':'a', 'à':'a', 'Ă':'a', 'ă':'a', 'Ắ':'a', 'ắ':'a', 'Ằ':'a', 'ằ':'a', 'Ẵ':'a', 'ẵ':'a', 'Ẳ':'a', 'ẳ':'a', 'Â':'a', 'â':'a', 'Ấ':'a', 'ấ':'a', 'Ầ':'a', 'ầ':'a', 'Ẫ':'a', 'ẫ':'a', 'Ẩ':'a', 'ẩ':'a', 'Ǎ':'a', 'ǎ':'a', 'Å':'a', 'å':'a', 'Ǻ':'a', 'ǻ':'a', 'Ä':'a', 'ä':'a', 'Ǟ':'a', 'ǟ':'a', 'Ã':'a', 'ã':'a', 'Ȧ':'a', 'ȧ':'a', 'Ǡ':'a', 'ǡ':'a', 'Ą':'a', 'ą':'a', 'Ā':'a', 'ā':'a', 'Ả':'a', 'ả':'a', 'Ȁ':'a', 'ȁ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Ṷ':'u', 'ṷ':'u', 'Ṵ':'u', 'ṵ':'u', 'Ʉ':'u', 'ʉ':'u',
'Ṽ':'v', 'ṽ':'v', 'Ṿ':'v', 'ṿ':'v', 'Ʋ':'v', 'ʋ':'v',
'Ẃ':'w', 'ẃ':'w', 'Ẁ':'w', 'ẁ':'w', 'Ŵ':'w', 'ŵ':'w', 'W':'w', '̊':'w', 'ẘ':'w', 'Ẅ':'w', 'ẅ':'w', 'Ẇ':'w', 'ẇ':'w', 'Ẉ':'w', 'ẉ':'w',
'Ẍ':'x', 'ẍ':'x', 'Ẋ':'x', 'ẋ':'x',
'Ý':'y', 'ý':'y', 'Ỳ':'y', 'ỳ':'y', 'Ŷ':'y', 'ŷ':'y', 'Y':'y', '̊':'y', 'ẙ':'y', 'Ÿ':'y', 'ÿ':'y', 'Ỹ':'y', 'ỹ':'y', 'Ẏ':'y', 'ẏ':'y', 'Ȳ':'y', 'ȳ':'y', 'Ỷ':'y', 'ỷ':'y', 'Ỵ':'y', 'ỵ':'y', 'ʏ':'y', 'Ɏ':'y', 'ɏ':'y', 'Ƴ':'y', 'ƴ':'y',
'Ź':'z', 'ź':'z', 'Ẑ':'z', 'ẑ':'z', 'Ž':'z', 'ž':'z', 'Ż':'z', 'ż':'z', 'Ẓ':'z', 'ẓ':'z', 'Ẕ':'z', 'ẕ':'z', 'Ƶ':'z', 'ƶ':'z', 'Ȥ':'z', 'ȥ':'z', 'ʐ':'z', 'ʑ':'z', 'Ⱬ':'z', 'ⱬ':'z', 'Ǯ':'z', 'ǯ':'z', 'ƺ':'z',
// Roman fullwidth ascii equivalents: 0xff00 to 0xff5e
'2':'2', '6':'6', 'B':'B', 'F':'F', 'J':'J', 'N':'N', 'R':'R', 'V':'V', 'Z':'Z', 'b':'b', 'f':'f', 'j':'j', 'n':'n', 'r':'r', 'v':'v', 'z':'z', '1':'1', '5':'5', '9':'9', 'A':'A', 'E':'E', 'I':'I', 'M':'M', 'Q':'Q', 'U':'U', 'Y':'Y', 'a':'a', 'e':'e', 'i':'i', 'm':'m', 'q':'q', 'u':'u', 'y':'y', '0':'0', '4':'4', '8':'8', 'D':'D', 'H':'H', 'L':'L', 'P':'P', 'T':'T', 'X':'X', 'd':'d', 'h':'h', 'l':'l', 'p':'p', 't':'t', 'x':'x', '3':'3', '7':'7', 'C':'C', 'G':'G', 'K':'K', 'O':'O', 'S':'S', 'W':'W', 'c':'c', 'g':'g', 'k':'k', 'o':'o', 's':'s', 'w':'w'
};

var ret = '';
for (var i = 0; i < str.length; i++) {
ret += accent_map[str.charAt(i)] || str.charAt(i);
}
return ret;

};

// Really basic cache for the results
$.TokenList.Cache = function (options) {
var settings = $.extend({
Expand Down

0 comments on commit f1b6e7c

Please sign in to comment.