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

Commit 4c1fedf

Browse files
author
Wilson Lee
committed
Add freetagging feature
1 parent 641b6dd commit 4c1fedf

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

src/jquery.tokeninput.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ var DEFAULT_SETTINGS = {
4242
preventDuplicates: false,
4343
tokenValue: "id",
4444

45+
// Behavioral settings
46+
allowFreeTagging: false,
47+
4548
// Callbacks
4649
onResult: null,
4750
onAdd: null,
@@ -210,7 +213,12 @@ $.TokenList = function (input, url_or_data, settings) {
210213
})
211214
.blur(function () {
212215
hide_dropdown();
213-
$(this).val("");
216+
217+
if (settings.allowFreeTagging) {
218+
add_freetagging_tokens();
219+
} else {
220+
$(this).val("");
221+
}
214222
token_list.removeClass(settings.classes.focused);
215223
})
216224
.bind("keyup keydown blur update", resize_input)
@@ -284,9 +292,12 @@ $.TokenList = function (input, url_or_data, settings) {
284292
if(selected_dropdown_item) {
285293
add_token($(selected_dropdown_item).data("tokeninput"));
286294
hidden_input.change();
287-
return false;
295+
} else {
296+
add_freetagging_tokens();
297+
event.stopPropagation();
298+
event.preventDefault();
288299
}
289-
break;
300+
return false;
290301

291302
case KEY.ESCAPE:
292303
hide_dropdown();
@@ -485,6 +496,20 @@ $.TokenList = function (input, url_or_data, settings) {
485496
(keycode >= 219 && keycode <= 222)); // ( \ ) '
486497
}
487498

499+
function add_freetagging_tokens() {
500+
var value = $.trim(input_box.val());
501+
var tokens = value.split(settings.tokenDelimiter);
502+
$.each(tokens, function(i, token) {
503+
if (!token) {
504+
return;
505+
}
506+
507+
var object = {};
508+
object[settings.tokenValue] = object[settings.propertyToSearch] = token;
509+
add_token(object);
510+
});
511+
}
512+
488513
// Inner function to a token to the list
489514
function insert_token(item) {
490515
var $this_token = $(settings.tokenFormatter(item));

0 commit comments

Comments
 (0)