Skip to content

Commit 0e30fde

Browse files
committed
1:1 behavior on touch devices (#6).
Fixes focus issues + the expandable keyboard hiding while using the control.
1 parent 5a7fff2 commit 0e30fde

File tree

6 files changed

+116
-74
lines changed

6 files changed

+116
-74
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "selectize",
33
"keywords": ["select", "ui", "form", "input", "control", "autocomplete", "tagging", "tag"],
44
"description": "Selectize is a jQuery-based custom <select> UI control. Useful for tagging, contact lists, country selectors, etc.",
5-
"version": "0.1.6",
5+
"version": "0.1.7",
66
"license": "Apache License, Version 2.0",
77
"readmeFilename": "README.md",
88
"repository": {

selectize.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@
205205
padding: 0 !important;
206206
margin: 0;
207207
line-height: inherit !important;
208+
-webkit-user-select: auto !important;
208209
-webkit-box-shadow: none !important;
209210
-moz-box-shadow: none !important;
210211
box-shadow: none !important;

selectize.jquery.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "selectize",
3-
"version": "0.1.6",
3+
"version": "0.1.7",
44
"title": "Selectize.js",
55
"author": {
66
"name": "Brian Reavis",

selectize.js

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@
396396
this.isLocked = false;
397397
this.isFocused = false;
398398
this.isInputFocused = false;
399+
this.isInputHidden = false;
399400
this.isSetup = false;
400401
this.isShiftDown = false;
401402
this.isCtrlDown = false;
@@ -644,7 +645,7 @@
644645
this.deleteSelection(e);
645646
break;
646647
default:
647-
if (this.isFull()) {
648+
if (this.isFull() || this.isInputHidden) {
648649
e.preventDefault();
649650
return;
650651
}
@@ -703,15 +704,15 @@
703704
/**
704705
* Triggered on <input> focus.
705706
*
706-
* @param {object} e
707+
* @param {object} e (optional)
707708
* @returns {boolean}
708709
*/
709710
Selectize.prototype.onFocus = function(e) {
710-
this.showInput();
711711
this.isInputFocused = true;
712712
this.isFocused = true;
713713
if (this.ignoreFocus) return;
714714

715+
this.showInput();
715716
this.setActiveItem(null);
716717
this.$control.addClass('focus');
717718
this.refreshOptions(!!this.settings.openOnFocus);
@@ -756,6 +757,10 @@
756757
* @returns {boolean}
757758
*/
758759
Selectize.prototype.onOptionSelect = function(e) {
760+
e.preventDefault && e.preventDefault();
761+
e.stopPropagation && e.stopPropagation();
762+
this.focus(false);
763+
759764
var $target = $(e.currentTarget);
760765
if ($target.hasClass('create')) {
761766
this.createItem();
@@ -777,9 +782,12 @@
777782
*/
778783
Selectize.prototype.onItemSelect = function(e) {
779784
if (this.settings.mode === 'multi') {
780-
this.$control_input.trigger('blur');
781-
this.setActiveItem(e.currentTarget, e);
785+
e.preventDefault();
782786
e.stopPropagation();
787+
this.$control_input.triggerHandler('blur');
788+
this.setActiveItem(e.currentTarget, e);
789+
this.focus(false);
790+
this.hideInput();
783791
}
784792
};
785793

@@ -789,7 +797,7 @@
789797
* @param {string} value
790798
*/
791799
Selectize.prototype.setTextboxValue = function(value) {
792-
this.$control_input.val(value);
800+
this.$control_input.val(value).triggerHandler('update');
793801
this.lastValue = value;
794802
};
795803

@@ -922,15 +930,18 @@
922930
* retaining its focus.
923931
*/
924932
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});
926935
this.isInputFocused = false;
936+
this.isInputHidden = true;
927937
};
928938

929939
/**
930940
* Restores input visibility.
931941
*/
932942
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;
934945
};
935946

936947
/**
@@ -942,8 +953,11 @@
942953
*/
943954
Selectize.prototype.focus = function(trigger) {
944955
var ignoreFocus = this.ignoreFocus;
956+
var fire = trigger && !this.isInputFocused;
957+
945958
this.ignoreFocus = !trigger;
946959
this.$control_input[0].focus();
960+
if (fire) this.onFocus();
947961
this.ignoreFocus = ignoreFocus;
948962
};
949963

@@ -1458,7 +1472,7 @@
14581472
var caret = this.caretPos;
14591473
if (!input.length) return;
14601474
this.lock();
1461-
this.$control_input[0].blur();
1475+
this.close();
14621476

14631477
var setup = (typeof this.settings.create === 'function') ? this.settings.create : function(input) {
14641478
var data = {};
@@ -1477,8 +1491,9 @@
14771491
self.addOption(value, data);
14781492
self.setCaret(caret, false);
14791493
self.addItem(value);
1480-
self.refreshOptions(false);
14811494
self.setTextboxValue('');
1495+
self.refreshOptions(true);
1496+
self.focus(false);
14821497
});
14831498

14841499
var output = setup(input, create);
@@ -1730,19 +1745,24 @@
17301745
i = Math.max(0, Math.min(this.items.length, i));
17311746
}
17321747

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+
}
17391760
}
1740-
this.ignoreFocus = false;
1761+
1762+
this.caretPos = i;
17411763
if (focus && this.isSetup) {
17421764
this.focus(true);
17431765
}
1744-
1745-
this.caretPos = i;
17461766
};
17471767

17481768
/**

0 commit comments

Comments
 (0)