Skip to content
This repository was archived by the owner on Sep 10, 2023. It is now read-only.

Commit fa9ad32

Browse files
committed
Allow values that don't come from the backend, prevent accidental form submission, and use case-insensitive matching
1 parent 985d095 commit fa9ad32

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

src/jquery.tokeninput.js

+28-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var DEFAULT_SETTINGS = {
2020
jsonContainer: null,
2121
contentType: "json",
2222

23-
// Prepopulation settings
23+
// Prepopulation settings
2424
prePopulate: null,
2525
processPrePopulate: false,
2626

@@ -51,7 +51,10 @@ var DEFAULT_SETTINGS = {
5151
idPrefix: "token-input-",
5252

5353
// Keep track if the input is currently in disabled mode
54-
disabled: false
54+
disabled: false,
55+
56+
// Set to true if you need to enter new tokens (not only from backend)
57+
allowNewItems: false
5558
};
5659

5760
// Default classes to use when theming
@@ -248,8 +251,8 @@ $.TokenList = function (input, url_or_data, settings) {
248251
if(dropdown_item.length) {
249252
select_dropdown_item(dropdown_item);
250253
}
254+
return false;
251255
}
252-
return false;
253256
break;
254257

255258
case KEY.BACKSPACE:
@@ -276,11 +279,21 @@ $.TokenList = function (input, url_or_data, settings) {
276279
case KEY.ENTER:
277280
case KEY.NUMPAD_ENTER:
278281
case KEY.COMMA:
279-
if(selected_dropdown_item) {
282+
if(selected_dropdown_item && $(selected_dropdown_item).data("tokeninput") != undefined) {
280283
add_token($(selected_dropdown_item).data("tokeninput"));
281284
hidden_input.change();
282285
return false;
286+
} else if ($(this).val().length && settings.allowNewItems) {
287+
token_object = {}
288+
token_object[settings.propertyToSearch] = token_object[settings.tokenValue] = $(this).val()
289+
add_token(token_object);
290+
return false;
291+
} else if (event.keyCode !== KEY.TAB) {
292+
// Prevent commas or accidental form submissions
293+
// via enter while keeping tabbing intact
294+
return false;
283295
}
296+
284297
break;
285298

286299
case KEY.ESCAPE:
@@ -531,7 +544,10 @@ $.TokenList = function (input, url_or_data, settings) {
531544
token_list.children().each(function () {
532545
var existing_token = $(this);
533546
var existing_data = $.data(existing_token.get(0), "tokeninput");
534-
if(existing_data && existing_data.id === item.id) {
547+
if(existing_data && existing_data.id && existing_data.id === item.id) {
548+
found_existing_token = existing_token;
549+
return false;
550+
} else if (existing_data && existing_data.name.toLowerCase() === item.name.toLowerCase()) {
535551
found_existing_token = existing_token;
536552
return false;
537553
}
@@ -751,6 +767,11 @@ $.TokenList = function (input, url_or_data, settings) {
751767
dropdown.html("<p>"+settings.noResultsText+"</p>");
752768
show_dropdown();
753769
}
770+
// If we're allowing new items hide the dropdown results
771+
// when there's no matching results
772+
if(settings.allowNewItems) {
773+
dropdown.empty();
774+
}
754775
}
755776
}
756777

@@ -775,7 +796,7 @@ $.TokenList = function (input, url_or_data, settings) {
775796
// Do a search and show the "searching" dropdown if the input is longer
776797
// than settings.minChars
777798
function do_search() {
778-
var query = input_box.val();
799+
var query = input_box.val().toLowerCase();
779800

780801
if(query && query.length) {
781802
if(selected_token) {
@@ -909,4 +930,4 @@ $.TokenList.Cache = function (options) {
909930
return data[query];
910931
};
911932
};
912-
}(jQuery));
933+
}(jQuery));

0 commit comments

Comments
 (0)