Commit 74a91f4bdee91bc793343255e6fabe322f28259d
1 parent
16c07fb9
Exists in
master
and in
28 other branches
Fix version of token input
From https://github.com/brauliobo/jquery-tokeninput
Showing
1 changed file
with
53 additions
and
38 deletions
Show diff stats
public/javascripts/jquery.tokeninput.js
| 1 | /* | 1 | /* |
| 2 | * jQuery Plugin: Tokenizing Autocomplete Text Entry | 2 | * jQuery Plugin: Tokenizing Autocomplete Text Entry |
| 3 | * Version 1.5.0 | 3 | * Version 1.5.0 |
| 4 | + * Requires jQuery 1.6+ | ||
| 4 | * | 5 | * |
| 5 | * Copyright (c) 2009 James Smith (http://loopj.com) | 6 | * Copyright (c) 2009 James Smith (http://loopj.com) |
| 6 | * Licensed jointly under the GPL and MIT licenses, | 7 | * Licensed jointly under the GPL and MIT licenses, |
| @@ -18,6 +19,7 @@ var DEFAULT_SETTINGS = { | @@ -18,6 +19,7 @@ var DEFAULT_SETTINGS = { | ||
| 18 | searchDelay: 300, | 19 | searchDelay: 300, |
| 19 | minChars: 1, | 20 | minChars: 1, |
| 20 | permanentDropdown: false, | 21 | permanentDropdown: false, |
| 22 | + showAllResults: false, | ||
| 21 | tokenLimit: null, | 23 | tokenLimit: null, |
| 22 | jsonContainer: null, | 24 | jsonContainer: null, |
| 23 | method: "GET", | 25 | method: "GET", |
| @@ -170,17 +172,20 @@ $.TokenList = function (input, url_or_data, options) { | @@ -170,17 +172,20 @@ $.TokenList = function (input, url_or_data, options) { | ||
| 170 | .attr("id", settings.idPrefix + input.id) | 172 | .attr("id", settings.idPrefix + input.id) |
| 171 | .focus(function () { | 173 | .focus(function () { |
| 172 | if (settings.tokenLimit === null || settings.tokenLimit !== token_count) { | 174 | if (settings.tokenLimit === null || settings.tokenLimit !== token_count) { |
| 173 | - if(settings.permanentDropdown) | 175 | + if(settings.permanentDropdown || settings.showAllResults) { |
| 174 | hide_dropdown_hint(); | 176 | hide_dropdown_hint(); |
| 175 | - else | 177 | + } else |
| 176 | show_dropdown_hint(); | 178 | show_dropdown_hint(); |
| 179 | + if (settings.showAllResults) | ||
| 180 | + do_search(); | ||
| 177 | } | 181 | } |
| 178 | }) | 182 | }) |
| 179 | .blur(function () { | 183 | .blur(function () { |
| 180 | if(settings.permanentDropdown) | 184 | if(settings.permanentDropdown) |
| 181 | show_dropdown_hint(); | 185 | show_dropdown_hint(); |
| 182 | - else | 186 | + else { |
| 183 | hide_dropdown(); | 187 | hide_dropdown(); |
| 188 | + } | ||
| 184 | }) | 189 | }) |
| 185 | .bind("keyup keydown blur update", resize_input) | 190 | .bind("keyup keydown blur update", resize_input) |
| 186 | .keydown(function (event) { | 191 | .keydown(function (event) { |
| @@ -235,9 +240,10 @@ $.TokenList = function (input, url_or_data, options) { | @@ -235,9 +240,10 @@ $.TokenList = function (input, url_or_data, options) { | ||
| 235 | previous_token = input_token.prev(); | 240 | previous_token = input_token.prev(); |
| 236 | next_token = input_token.next(); | 241 | next_token = input_token.next(); |
| 237 | 242 | ||
| 238 | - if(!$(this).val().length) { | 243 | + if(!$(this).val().length && settings.backspaceDeleteItem) { |
| 239 | if(selected_token) { | 244 | if(selected_token) { |
| 240 | delete_token($(selected_token)); | 245 | delete_token($(selected_token)); |
| 246 | + input_box.focus(); | ||
| 241 | } else if(KEY.DELETE && next_token.length) { | 247 | } else if(KEY.DELETE && next_token.length) { |
| 242 | select_token($(next_token.get(0))); | 248 | select_token($(next_token.get(0))); |
| 243 | } else if(KEY.BACKSPACE && previous_token.length) { | 249 | } else if(KEY.BACKSPACE && previous_token.length) { |
| @@ -259,6 +265,7 @@ $.TokenList = function (input, url_or_data, options) { | @@ -259,6 +265,7 @@ $.TokenList = function (input, url_or_data, options) { | ||
| 259 | case KEY.COMMA: | 265 | case KEY.COMMA: |
| 260 | if(selected_dropdown_item) { | 266 | if(selected_dropdown_item) { |
| 261 | add_token($(selected_dropdown_item).data("tokeninput")); | 267 | add_token($(selected_dropdown_item).data("tokeninput")); |
| 268 | + input_box.focus(); | ||
| 262 | return false; | 269 | return false; |
| 263 | } | 270 | } |
| 264 | break; | 271 | break; |
| @@ -305,8 +312,9 @@ $.TokenList = function (input, url_or_data, options) { | @@ -305,8 +312,9 @@ $.TokenList = function (input, url_or_data, options) { | ||
| 305 | deselect_token($(selected_token), POSITION.END); | 312 | deselect_token($(selected_token), POSITION.END); |
| 306 | } | 313 | } |
| 307 | 314 | ||
| 308 | - // Focus input box | ||
| 309 | - input_box.focus(); | 315 | + // Transfer focus |
| 316 | + if (!input_box.is(':focus')) | ||
| 317 | + input_box.focus(); | ||
| 310 | } | 318 | } |
| 311 | }) | 319 | }) |
| 312 | .mouseover(function (event) { | 320 | .mouseover(function (event) { |
| @@ -336,13 +344,17 @@ $.TokenList = function (input, url_or_data, options) { | @@ -336,13 +344,17 @@ $.TokenList = function (input, url_or_data, options) { | ||
| 336 | dropdown.appendTo("body"); | 344 | dropdown.appendTo("body"); |
| 337 | if (!settings.permanentDropdown) | 345 | if (!settings.permanentDropdown) |
| 338 | dropdown.appendTo("body"); | 346 | dropdown.appendTo("body"); |
| 339 | - else { | 347 | + else |
| 340 | $(input).after(dropdown.show()); | 348 | $(input).after(dropdown.show()); |
| 341 | - do_search(); | 349 | + |
| 350 | + if (settings.permanentDropdown || settings.showAllResults) { | ||
| 351 | + do_search(); | ||
| 352 | + if (!settings.permanentDropdown && settings.showAllResults) | ||
| 353 | + hide_dropdown(); | ||
| 342 | } | 354 | } |
| 343 | 355 | ||
| 344 | // Hint for permanentDropdown | 356 | // Hint for permanentDropdown |
| 345 | - if(settings.permanentDropdown) | 357 | + if (settings.permanentDropdown || settings.showAllResults) |
| 346 | show_dropdown_hint(); | 358 | show_dropdown_hint(); |
| 347 | 359 | ||
| 348 | // Magic element to help us resize the text input | 360 | // Magic element to help us resize the text input |
| @@ -430,7 +442,7 @@ $.TokenList = function (input, url_or_data, options) { | @@ -430,7 +442,7 @@ $.TokenList = function (input, url_or_data, options) { | ||
| 430 | input_resizer.html(escaped); | 442 | input_resizer.html(escaped); |
| 431 | input_box.width(input_resizer.width() + 30); | 443 | input_box.width(input_resizer.width() + 30); |
| 432 | 444 | ||
| 433 | - if(settings.permanentDropdown && input_box.hasClass(settings.classes.blurText)) | 445 | + if((settings.permanentDropdown || settings.showAllResults) && input_box.hasClass(settings.classes.blurText)) |
| 434 | input_val = ''; | 446 | input_val = ''; |
| 435 | } | 447 | } |
| 436 | 448 | ||
| @@ -497,7 +509,6 @@ $.TokenList = function (input, url_or_data, options) { | @@ -497,7 +509,6 @@ $.TokenList = function (input, url_or_data, options) { | ||
| 497 | if(found_existing_token) { | 509 | if(found_existing_token) { |
| 498 | select_token(found_existing_token); | 510 | select_token(found_existing_token); |
| 499 | input_token.insertAfter(found_existing_token); | 511 | input_token.insertAfter(found_existing_token); |
| 500 | - input_box.focus(); | ||
| 501 | return; | 512 | return; |
| 502 | } | 513 | } |
| 503 | } | 514 | } |
| @@ -578,9 +589,6 @@ $.TokenList = function (input, url_or_data, options) { | @@ -578,9 +589,6 @@ $.TokenList = function (input, url_or_data, options) { | ||
| 578 | token.remove(); | 589 | token.remove(); |
| 579 | selected_token = null; | 590 | selected_token = null; |
| 580 | 591 | ||
| 581 | - // Show the input box and give it focus again | ||
| 582 | - input_box.focus(); | ||
| 583 | - | ||
| 584 | // Remove this token from the saved list | 592 | // Remove this token from the saved list |
| 585 | saved_tokens = saved_tokens.slice(0,index).concat(saved_tokens.slice(index+1)); | 593 | saved_tokens = saved_tokens.slice(0,index).concat(saved_tokens.slice(index+1)); |
| 586 | if(index < selected_token_index) selected_token_index--; | 594 | if(index < selected_token_index) selected_token_index--; |
| @@ -593,12 +601,8 @@ $.TokenList = function (input, url_or_data, options) { | @@ -593,12 +601,8 @@ $.TokenList = function (input, url_or_data, options) { | ||
| 593 | 601 | ||
| 594 | token_count -= 1; | 602 | token_count -= 1; |
| 595 | 603 | ||
| 596 | - if(settings.tokenLimit !== null) { | ||
| 597 | - input_box | ||
| 598 | - .show() | ||
| 599 | - .val("") | ||
| 600 | - .focus(); | ||
| 601 | - } | 604 | + if(settings.tokenLimit !== null) |
| 605 | + input_box.show().val(""); | ||
| 602 | 606 | ||
| 603 | // Execute the onDelete callback if defined | 607 | // Execute the onDelete callback if defined |
| 604 | if($.isFunction(callback)) { | 608 | if($.isFunction(callback)) { |
| @@ -609,27 +613,27 @@ $.TokenList = function (input, url_or_data, options) { | @@ -609,27 +613,27 @@ $.TokenList = function (input, url_or_data, options) { | ||
| 609 | // Hide and clear the results dropdown | 613 | // Hide and clear the results dropdown |
| 610 | function hide_dropdown () { | 614 | function hide_dropdown () { |
| 611 | if (!settings.permanentDropdown) { | 615 | if (!settings.permanentDropdown) { |
| 612 | - dropdown.hide().empty(); | 616 | + dropdown.hide(); |
| 617 | + if (!settings.showAllResults) | ||
| 618 | + dropdown.empty(); | ||
| 613 | selected_dropdown_item = null; | 619 | selected_dropdown_item = null; |
| 614 | } | 620 | } |
| 621 | + if (settings.showAllResults) | ||
| 622 | + show_dropdown_hint(); | ||
| 615 | } | 623 | } |
| 616 | 624 | ||
| 617 | function show_dropdown() { | 625 | function show_dropdown() { |
| 618 | if (!settings.permanentDropdown) | 626 | if (!settings.permanentDropdown) |
| 619 | - dropdown | ||
| 620 | - .css({ | ||
| 621 | - position: "absolute", | ||
| 622 | - top: $(token_list).offset().top + $(token_list).outerHeight(), | ||
| 623 | - left: $(token_list).offset().left, | ||
| 624 | - zindex: 999 | ||
| 625 | - }) | ||
| 626 | - .show(); | 627 | + dropdown.css({ |
| 628 | + position: "absolute", | ||
| 629 | + top: $(token_list).offset().top + $(token_list).outerHeight(), | ||
| 630 | + left: $(token_list).offset().left, | ||
| 631 | + zindex: 999 | ||
| 632 | + }).show(); | ||
| 627 | else | 633 | else |
| 628 | - dropdown | ||
| 629 | - .css({ | ||
| 630 | - position: "relative", | ||
| 631 | - }) | ||
| 632 | - .show(); | 634 | + dropdown.css({ |
| 635 | + position: "relative", | ||
| 636 | + }).show(); | ||
| 633 | } | 637 | } |
| 634 | 638 | ||
| 635 | function show_dropdown_searching () { | 639 | function show_dropdown_searching () { |
| @@ -641,10 +645,10 @@ $.TokenList = function (input, url_or_data, options) { | @@ -641,10 +645,10 @@ $.TokenList = function (input, url_or_data, options) { | ||
| 641 | 645 | ||
| 642 | function show_dropdown_hint () { | 646 | function show_dropdown_hint () { |
| 643 | if(settings.hintText) { | 647 | if(settings.hintText) { |
| 644 | - if(settings.permanentDropdown) { | 648 | + if(settings.permanentDropdown || settings.showAllResults) { |
| 645 | if (input_val == '') { | 649 | if (input_val == '') { |
| 646 | input_box.val(settings.hintText); | 650 | input_box.val(settings.hintText); |
| 647 | - input_box.toggleClass(settings.classes.blurText); | 651 | + input_box.addClass(settings.classes.blurText); |
| 648 | } | 652 | } |
| 649 | } else { | 653 | } else { |
| 650 | dropdown.html("<p>"+settings.hintText+"</p>"); | 654 | dropdown.html("<p>"+settings.hintText+"</p>"); |
| @@ -656,7 +660,7 @@ $.TokenList = function (input, url_or_data, options) { | @@ -656,7 +660,7 @@ $.TokenList = function (input, url_or_data, options) { | ||
| 656 | function hide_dropdown_hint () { | 660 | function hide_dropdown_hint () { |
| 657 | if (input_box.hasClass(settings.classes.blurText)) { | 661 | if (input_box.hasClass(settings.classes.blurText)) { |
| 658 | input_box.val(''); | 662 | input_box.val(''); |
| 659 | - input_box.toggleClass(settings.classes.blurText); | 663 | + input_box.removeClass(settings.classes.blurText); |
| 660 | } | 664 | } |
| 661 | } | 665 | } |
| 662 | 666 | ||
| @@ -676,6 +680,7 @@ $.TokenList = function (input, url_or_data, options) { | @@ -676,6 +680,7 @@ $.TokenList = function (input, url_or_data, options) { | ||
| 676 | }) | 680 | }) |
| 677 | .mousedown(function (event) { | 681 | .mousedown(function (event) { |
| 678 | add_token($(event.target).closest("li").data("tokeninput")); | 682 | add_token($(event.target).closest("li").data("tokeninput")); |
| 683 | + input_box.blur(); | ||
| 679 | return false; | 684 | return false; |
| 680 | }) | 685 | }) |
| 681 | .hide(); | 686 | .hide(); |
| @@ -721,6 +726,16 @@ $.TokenList = function (input, url_or_data, options) { | @@ -721,6 +726,16 @@ $.TokenList = function (input, url_or_data, options) { | ||
| 721 | 726 | ||
| 722 | item.addClass(settings.classes.selectedDropdownItem); | 727 | item.addClass(settings.classes.selectedDropdownItem); |
| 723 | selected_dropdown_item = item.get(0); | 728 | selected_dropdown_item = item.get(0); |
| 729 | + | ||
| 730 | + isBefore = item[0].offsetTop <= (dropdown[0].scrollTop + dropdown[0].scrollWidth); | ||
| 731 | + isAfter = item[0].offsetTop >= dropdown[0].scrollTop; | ||
| 732 | + visible = isBefore && isAfter; | ||
| 733 | + if (!visible) { | ||
| 734 | + if (isBefore) | ||
| 735 | + dropdown[0].scrollTop = item[0].offsetTop; | ||
| 736 | + else //isAfter | ||
| 737 | + dropdown[0].scrollTop = item[0].offsetTop - dropdown[0].scrollWidth; | ||
| 738 | + } | ||
| 724 | } | 739 | } |
| 725 | } | 740 | } |
| 726 | 741 | ||
| @@ -750,7 +765,7 @@ $.TokenList = function (input, url_or_data, options) { | @@ -750,7 +765,7 @@ $.TokenList = function (input, url_or_data, options) { | ||
| 750 | } else { | 765 | } else { |
| 751 | hide_dropdown(); | 766 | hide_dropdown(); |
| 752 | } | 767 | } |
| 753 | - } else if (settings.permanentDropdown) | 768 | + } else if (settings.permanentDropdown || settings.showAllResults) |
| 754 | run_search(''); | 769 | run_search(''); |
| 755 | } | 770 | } |
| 756 | 771 |