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

Commit 665ecd7

Browse files
committed
Merge pull request #367 from kourge/freetagging
Freetagging
2 parents 641b6dd + 4d90c73 commit 665ecd7

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

src/jquery.tokeninput.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ 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,
51+
onFreeTaggingAdd: null,
4852
onDelete: null,
4953
onReady: null,
5054

@@ -210,7 +214,12 @@ $.TokenList = function (input, url_or_data, settings) {
210214
})
211215
.blur(function () {
212216
hide_dropdown();
213-
$(this).val("");
217+
218+
if (settings.allowFreeTagging) {
219+
add_freetagging_tokens();
220+
} else {
221+
$(this).val("");
222+
}
214223
token_list.removeClass(settings.classes.focused);
215224
})
216225
.bind("keyup keydown blur update", resize_input)
@@ -284,9 +293,12 @@ $.TokenList = function (input, url_or_data, settings) {
284293
if(selected_dropdown_item) {
285294
add_token($(selected_dropdown_item).data("tokeninput"));
286295
hidden_input.change();
287-
return false;
296+
} else {
297+
add_freetagging_tokens();
298+
event.stopPropagation();
299+
event.preventDefault();
288300
}
289-
break;
301+
return false;
290302

291303
case KEY.ESCAPE:
292304
hide_dropdown();
@@ -485,6 +497,23 @@ $.TokenList = function (input, url_or_data, settings) {
485497
(keycode >= 219 && keycode <= 222)); // ( \ ) '
486498
}
487499

500+
function add_freetagging_tokens() {
501+
var value = $.trim(input_box.val());
502+
var tokens = value.split(settings.tokenDelimiter);
503+
$.each(tokens, function(i, token) {
504+
if (!token) {
505+
return;
506+
}
507+
508+
if ($.isFunction(settings.onFreeTaggingAdd)) {
509+
token = settings.onFreeTaggingAdd.call(hidden_input, token);
510+
}
511+
var object = {};
512+
object[settings.tokenValue] = object[settings.propertyToSearch] = token;
513+
add_token(object);
514+
});
515+
}
516+
488517
// Inner function to a token to the list
489518
function insert_token(item) {
490519
var $this_token = $(settings.tokenFormatter(item));

0 commit comments

Comments
 (0)