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

Commit 096457a

Browse files
committed
Merge branch 'master' of github.com:loopj/jquery-tokeninput
2 parents dc4c6ea + 3138de1 commit 096457a

File tree

3 files changed

+69
-17
lines changed

3 files changed

+69
-17
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
_site
22
_layouts
33
.DS_Store
4-
gh-pages
4+
gh-pages
5+
*.sw*

component.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name" : "jquery-tokeninput",
3+
"version" : "1.6.1",
4+
"description" : "Tokeninput is a jQuery plugin which allows your users to select multiple items from a predefined list, using autocompletion as they type to find each item.",
5+
"main" : [ "./src/jquery.tokeninput.js", "./styles/token-input.css" ],
6+
"homepage" : "http://loopj.com/jquery-tokeninput",
7+
"dependencies" : {
8+
"jquery" : ">= 1.5.x"
9+
},
10+
"keywords" : [
11+
"tokeninput",
12+
"input",
13+
"autocomplete"
14+
],
15+
"author" : {
16+
"name" : "James Smith",
17+
"web" : "http://loopj.com"
18+
},
19+
"license": [
20+
"http://www.opensource.org/licenses/mit-license.php",
21+
"http://www.opensource.org/licenses/gpl-license.php"
22+
]
23+
}

src/jquery.tokeninput.js

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* jQuery Plugin: Tokenizing Autocomplete Text Entry
3-
* Version 1.6.0
3+
* Version 1.6.1
44
*
55
* Copyright (c) 2009 James Smith (http://loopj.com)
66
* Licensed jointly under the GPL and MIT licenses,
@@ -30,6 +30,7 @@ var DEFAULT_SETTINGS = {
3030
searchingText: "Searching...",
3131
deleteText: "×",
3232
animateDropdown: true,
33+
placeholder: null,
3334
theme: null,
3435
zindex: 999,
3536
resultsLimit: null,
@@ -54,6 +55,7 @@ var DEFAULT_SETTINGS = {
5455

5556
// Behavioral settings
5657
allowFreeTagging: false,
58+
allowTabOut: false,
5759

5860
// Callbacks
5961
onResult: null,
@@ -167,7 +169,7 @@ var methods = {
167169
$(this).data("settings", $.extend({}, $(this).data("settings"), options || {}));
168170
return this;
169171
}
170-
}
172+
};
171173

172174
// Expose the .tokenInput function to jQuery as a plugin
173175
$.fn.tokenInput = function (method) {
@@ -251,14 +253,12 @@ $.TokenList = function (input, url_or_data, settings) {
251253
})
252254
.blur(function () {
253255
hide_dropdown();
254-
$(this).val("");
255-
token_list.removeClass($(input).data("settings").classes.focused);
256-
256+
257257
if ($(input).data("settings").allowFreeTagging) {
258258
add_freetagging_tokens();
259-
} else {
260-
$(this).val("");
261259
}
260+
261+
$(this).val("");
262262
token_list.removeClass($(input).data("settings").classes.focused);
263263
})
264264
.bind("keyup keydown blur update", resize_input)
@@ -334,9 +334,16 @@ $.TokenList = function (input, url_or_data, settings) {
334334
hidden_input.change();
335335
} else {
336336
if ($(input).data("settings").allowFreeTagging) {
337-
add_freetagging_tokens();
337+
if($(input).data("settings").allowTabOut && $(this).val() === "") {
338+
return true;
339+
} else {
340+
add_freetagging_tokens();
341+
}
338342
} else {
339343
$(this).val("");
344+
if($(input).data("settings").allowTabOut) {
345+
return true;
346+
}
340347
}
341348
event.stopPropagation();
342349
event.preventDefault();
@@ -356,6 +363,10 @@ $.TokenList = function (input, url_or_data, settings) {
356363
}
357364
});
358365

366+
// Keep reference for placeholder
367+
if (settings.placeholder)
368+
input_box.attr("placeholder", settings.placeholder)
369+
359370
// Keep a reference to the original input box
360371
var hidden_input = $(input)
361372
.hide()
@@ -440,6 +451,7 @@ $.TokenList = function (input, url_or_data, settings) {
440451
$.each(li_data, function (index, value) {
441452
insert_token(value);
442453
checkTokenLimit();
454+
input_box.attr("placeholder", null)
443455
});
444456
}
445457

@@ -463,11 +475,11 @@ $.TokenList = function (input, url_or_data, settings) {
463475
delete_token($(this));
464476
}
465477
});
466-
}
478+
};
467479

468480
this.add = function(item) {
469481
add_token(item);
470-
}
482+
};
471483

472484
this.remove = function(item) {
473485
token_list.children("li").each(function() {
@@ -485,15 +497,18 @@ $.TokenList = function (input, url_or_data, settings) {
485497
}
486498
}
487499
});
488-
}
500+
};
489501

490502
this.getTokens = function() {
491503
return saved_tokens;
492-
}
504+
};
493505

494506
this.toggleDisabled = function(disable) {
495507
toggleDisabled(disable);
496-
}
508+
};
509+
510+
// Resize input to maximum width so the placeholder can be seen
511+
resize_input();
497512

498513
//
499514
// Private functions
@@ -531,9 +546,13 @@ $.TokenList = function (input, url_or_data, settings) {
531546
function resize_input() {
532547
if(input_val === (input_val = input_box.val())) {return;}
533548

549+
// Get width left on the current line
550+
var width_left = token_list.width() - input_box.offset().left - token_list.offset().left;
534551
// Enter new content into resizer and resize input accordingly
535552
input_resizer.html(_escapeHTML(input_val));
536-
input_box.width(input_resizer.width() + 30);
553+
// Get maximum width, minimum the size of input and maximum the widget's width
554+
input_box.width(Math.min(token_list.width(),
555+
Math.max(width_left, input_resizer.width() + 30)));
537556
}
538557

539558
function is_printable_character(keycode) {
@@ -615,7 +634,7 @@ $.TokenList = function (input, url_or_data, settings) {
615634
token_list.children().each(function () {
616635
var existing_token = $(this);
617636
var existing_data = $.data(existing_token.get(0), "tokeninput");
618-
if(existing_data && existing_data.id === item.id) {
637+
if(existing_data && existing_data[settings.tokenValue] === item[settings.tokenValue]) {
619638
found_existing_token = existing_token;
620639
return false;
621640
}
@@ -629,9 +648,14 @@ $.TokenList = function (input, url_or_data, settings) {
629648
}
630649
}
631650

651+
// Squeeze input_box so we force no unnecessary line break
652+
input_box.width(0);
653+
632654
// Insert the new tokens
633655
if($(input).data("settings").tokenLimit == null || token_count < $(input).data("settings").tokenLimit) {
634656
insert_token(item);
657+
// Remove the placeholder so it's not seen after you've added a token
658+
input_box.attr("placeholder", null)
635659
checkTokenLimit();
636660
}
637661

@@ -714,6 +738,9 @@ $.TokenList = function (input, url_or_data, settings) {
714738

715739
// Remove this token from the saved list
716740
saved_tokens = saved_tokens.slice(0,index).concat(saved_tokens.slice(index+1));
741+
if (saved_tokens.length == 0) {
742+
input_box.attr("placeholder", settings.placeholder)
743+
}
717744
if(index < selected_token_index) selected_token_index--;
718745

719746
// Update the hidden input
@@ -756,7 +783,7 @@ $.TokenList = function (input, url_or_data, settings) {
756783
dropdown
757784
.css({
758785
position: "absolute",
759-
top: $(token_list).offset().top + $(token_list).outerHeight(),
786+
top: $(token_list).offset().top + $(token_list).height(),
760787
left: $(token_list).offset().left,
761788
width: $(token_list).width(),
762789
'z-index': $(input).data("settings").zindex
@@ -1013,3 +1040,4 @@ $.TokenList.Cache = function (options) {
10131040
};
10141041
};
10151042
}(jQuery));
1043+

0 commit comments

Comments
 (0)