|
396 | 396 | this.isLocked = false;
|
397 | 397 | this.isFocused = false;
|
398 | 398 | this.isInputFocused = false;
|
| 399 | + this.isInputHidden = false; |
399 | 400 | this.isSetup = false;
|
400 | 401 | this.isShiftDown = false;
|
401 | 402 | this.isCtrlDown = false;
|
|
644 | 645 | this.deleteSelection(e);
|
645 | 646 | break;
|
646 | 647 | default:
|
647 |
| - if (this.isFull()) { |
| 648 | + if (this.isFull() || this.isInputHidden) { |
648 | 649 | e.preventDefault();
|
649 | 650 | return;
|
650 | 651 | }
|
|
703 | 704 | /**
|
704 | 705 | * Triggered on <input> focus.
|
705 | 706 | *
|
706 |
| - * @param {object} e |
| 707 | + * @param {object} e (optional) |
707 | 708 | * @returns {boolean}
|
708 | 709 | */
|
709 | 710 | Selectize.prototype.onFocus = function(e) {
|
710 |
| - this.showInput(); |
711 | 711 | this.isInputFocused = true;
|
712 | 712 | this.isFocused = true;
|
713 | 713 | if (this.ignoreFocus) return;
|
714 | 714 |
|
| 715 | + this.showInput(); |
715 | 716 | this.setActiveItem(null);
|
716 | 717 | this.$control.addClass('focus');
|
717 | 718 | this.refreshOptions(!!this.settings.openOnFocus);
|
|
756 | 757 | * @returns {boolean}
|
757 | 758 | */
|
758 | 759 | Selectize.prototype.onOptionSelect = function(e) {
|
| 760 | + e.preventDefault && e.preventDefault(); |
| 761 | + e.stopPropagation && e.stopPropagation(); |
| 762 | + this.focus(false); |
| 763 | + |
759 | 764 | var $target = $(e.currentTarget);
|
760 | 765 | if ($target.hasClass('create')) {
|
761 | 766 | this.createItem();
|
|
777 | 782 | */
|
778 | 783 | Selectize.prototype.onItemSelect = function(e) {
|
779 | 784 | if (this.settings.mode === 'multi') {
|
780 |
| - this.$control_input.trigger('blur'); |
781 |
| - this.setActiveItem(e.currentTarget, e); |
| 785 | + e.preventDefault(); |
782 | 786 | e.stopPropagation();
|
| 787 | + this.$control_input.triggerHandler('blur'); |
| 788 | + this.setActiveItem(e.currentTarget, e); |
| 789 | + this.focus(false); |
| 790 | + this.hideInput(); |
783 | 791 | }
|
784 | 792 | };
|
785 | 793 |
|
|
789 | 797 | * @param {string} value
|
790 | 798 | */
|
791 | 799 | Selectize.prototype.setTextboxValue = function(value) {
|
792 |
| - this.$control_input.val(value); |
| 800 | + this.$control_input.val(value).triggerHandler('update'); |
793 | 801 | this.lastValue = value;
|
794 | 802 | };
|
795 | 803 |
|
|
922 | 930 | * retaining its focus.
|
923 | 931 | */
|
924 | 932 | Selectize.prototype.hideInput = function() {
|
925 |
| - this.$control_input.css({opacity: 0}); |
| 933 | + this.setTextboxValue(''); |
| 934 | + this.$control_input.css({opacity: 0, position: 'absolute', left: -10000}); |
926 | 935 | this.isInputFocused = false;
|
| 936 | + this.isInputHidden = true; |
927 | 937 | };
|
928 | 938 |
|
929 | 939 | /**
|
930 | 940 | * Restores input visibility.
|
931 | 941 | */
|
932 | 942 | Selectize.prototype.showInput = function() {
|
933 |
| - this.$control_input.css({opacity: 1}); |
| 943 | + this.$control_input.css({opacity: 1, position: 'relative', left: 0}); |
| 944 | + this.isInputHidden = false; |
934 | 945 | };
|
935 | 946 |
|
936 | 947 | /**
|
|
942 | 953 | */
|
943 | 954 | Selectize.prototype.focus = function(trigger) {
|
944 | 955 | var ignoreFocus = this.ignoreFocus;
|
| 956 | + var fire = trigger && !this.isInputFocused; |
| 957 | + |
945 | 958 | this.ignoreFocus = !trigger;
|
946 | 959 | this.$control_input[0].focus();
|
| 960 | + if (fire) this.onFocus(); |
947 | 961 | this.ignoreFocus = ignoreFocus;
|
948 | 962 | };
|
949 | 963 |
|
|
1458 | 1472 | var caret = this.caretPos;
|
1459 | 1473 | if (!input.length) return;
|
1460 | 1474 | this.lock();
|
1461 |
| - this.$control_input[0].blur(); |
| 1475 | + this.close(); |
1462 | 1476 |
|
1463 | 1477 | var setup = (typeof this.settings.create === 'function') ? this.settings.create : function(input) {
|
1464 | 1478 | var data = {};
|
|
1477 | 1491 | self.addOption(value, data);
|
1478 | 1492 | self.setCaret(caret, false);
|
1479 | 1493 | self.addItem(value);
|
1480 |
| - self.refreshOptions(false); |
1481 | 1494 | self.setTextboxValue('');
|
| 1495 | + self.refreshOptions(true); |
| 1496 | + self.focus(false); |
1482 | 1497 | });
|
1483 | 1498 |
|
1484 | 1499 | var output = setup(input, create);
|
|
1730 | 1745 | i = Math.max(0, Math.min(this.items.length, i));
|
1731 | 1746 | }
|
1732 | 1747 |
|
1733 |
| - this.ignoreFocus = true; |
1734 |
| - this.$control_input.detach(); |
1735 |
| - if (i === this.items.length) { |
1736 |
| - this.$control.append(this.$control_input); |
1737 |
| - } else { |
1738 |
| - this.$control_input.insertBefore(this.$control.children(':not(input)')[i]); |
| 1748 | + // the input must be moved by leaving it in place and moving the |
| 1749 | + // siblings, due to the fact that focus cannot be restored once lost |
| 1750 | + // on mobile webkit devices |
| 1751 | + var j, n, fn, $children, $child; |
| 1752 | + $children = this.$control.children(':not(input)'); |
| 1753 | + for (j = 0, n = $children.length; j < n; j++) { |
| 1754 | + $child = $($children[j]).detach(); |
| 1755 | + if (j < i) { |
| 1756 | + this.$control_input.before($child); |
| 1757 | + } else { |
| 1758 | + this.$control.append($child); |
| 1759 | + } |
1739 | 1760 | }
|
1740 |
| - this.ignoreFocus = false; |
| 1761 | + |
| 1762 | + this.caretPos = i; |
1741 | 1763 | if (focus && this.isSetup) {
|
1742 | 1764 | this.focus(true);
|
1743 | 1765 | }
|
1744 |
| - |
1745 |
| - this.caretPos = i; |
1746 | 1766 | };
|
1747 | 1767 |
|
1748 | 1768 | /**
|
|
0 commit comments