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

Commit 3138de1

Browse files
committed
Merge pull request #439 from market76/master
Add Support For Placeholders
2 parents 5fb6105 + 375bab2 commit 3138de1

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/jquery.tokeninput.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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,
@@ -362,6 +363,10 @@ $.TokenList = function (input, url_or_data, settings) {
362363
}
363364
});
364365

366+
// Keep reference for placeholder
367+
if (settings.placeholder)
368+
input_box.attr("placeholder", settings.placeholder)
369+
365370
// Keep a reference to the original input box
366371
var hidden_input = $(input)
367372
.hide()
@@ -446,6 +451,7 @@ $.TokenList = function (input, url_or_data, settings) {
446451
$.each(li_data, function (index, value) {
447452
insert_token(value);
448453
checkTokenLimit();
454+
input_box.attr("placeholder", null)
449455
});
450456
}
451457

@@ -501,6 +507,9 @@ $.TokenList = function (input, url_or_data, settings) {
501507
toggleDisabled(disable);
502508
};
503509

510+
// Resize input to maximum width so the placeholder can be seen
511+
resize_input();
512+
504513
//
505514
// Private functions
506515
//
@@ -537,9 +546,13 @@ $.TokenList = function (input, url_or_data, settings) {
537546
function resize_input() {
538547
if(input_val === (input_val = input_box.val())) {return;}
539548

549+
// Get width left on the current line
550+
var width_left = token_list.width() - input_box.offset().left - token_list.offset().left;
540551
// Enter new content into resizer and resize input accordingly
541552
input_resizer.html(_escapeHTML(input_val));
542-
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)));
543556
}
544557

545558
function is_printable_character(keycode) {
@@ -635,9 +648,14 @@ $.TokenList = function (input, url_or_data, settings) {
635648
}
636649
}
637650

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

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

721739
// Remove this token from the saved list
722740
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+
}
723744
if(index < selected_token_index) selected_token_index--;
724745

725746
// Update the hidden input
@@ -1019,3 +1040,4 @@ $.TokenList.Cache = function (options) {
10191040
};
10201041
};
10211042
}(jQuery));
1043+

0 commit comments

Comments
 (0)