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

Commit 2d6bd18

Browse files
author
John Gerhardt
committed
Support placeholders
1 parent 2d1958c commit 2d6bd18

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,
@@ -364,6 +365,10 @@ $.TokenList = function (input, url_or_data, settings) {
364365
}
365366
});
366367

368+
// Keep reference for placeholder
369+
if (settings.placeholder)
370+
input_box.attr("placeholder", settings.placeholder)
371+
367372
// Keep a reference to the original input box
368373
var hidden_input = $(input)
369374
.hide()
@@ -503,6 +508,9 @@ $.TokenList = function (input, url_or_data, settings) {
503508
toggleDisabled(disable);
504509
};
505510

511+
// Resize input to maximum width so the placeholder can be seen
512+
resize_input();
513+
506514
//
507515
// Private functions
508516
//
@@ -539,9 +547,13 @@ $.TokenList = function (input, url_or_data, settings) {
539547
function resize_input() {
540548
if(input_val === (input_val = input_box.val())) {return;}
541549

550+
// Get width left on the current line
551+
var width_left = token_list.width() - input_box.offset().left - token_list.offset().left;
542552
// Enter new content into resizer and resize input accordingly
543553
input_resizer.html(_escapeHTML(input_val));
544-
input_box.width(input_resizer.width() + 30);
554+
// Get maximum width, minimum the size of input and maximum the widget's width
555+
input_box.width(Math.min(token_list.width(),
556+
Math.max(width_left, input_resizer.width() + 30)));
545557
}
546558

547559
function is_printable_character(keycode) {
@@ -637,9 +649,14 @@ $.TokenList = function (input, url_or_data, settings) {
637649
}
638650
}
639651

652+
// Squeeze input_box so we force no unnecessary line break
653+
input_box.width(0);
654+
640655
// Insert the new tokens
641656
if($(input).data("settings").tokenLimit == null || token_count < $(input).data("settings").tokenLimit) {
642657
insert_token(item);
658+
// Remove the placeholder so it's not seen after you've added a token
659+
input_box.attr("placeholder", null)
643660
checkTokenLimit();
644661
}
645662

@@ -722,6 +739,10 @@ $.TokenList = function (input, url_or_data, settings) {
722739

723740
// Remove this token from the saved list
724741
saved_tokens = saved_tokens.slice(0,index).concat(saved_tokens.slice(index+1));
742+
// If there are no tokens left, restore the placeholder
743+
if (saved_tokens.length == 0) {
744+
input_box.attr("placeholder", settings.placeholder)
745+
}
725746
if(index < selected_token_index) selected_token_index--;
726747

727748
// Update the hidden input
@@ -1021,3 +1042,4 @@ $.TokenList.Cache = function (options) {
10211042
};
10221043
};
10231044
}(jQuery));
1045+

0 commit comments

Comments
 (0)