Skip to content

Commit 00bbab8

Browse files
committed
General bugfixes.
Fixes for IE8/9 issues (resolves selectize#21) Fixes input being hidden after removing all items. Fixes onDelete false-positive.
1 parent 15a7a30 commit 00bbab8

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

src/selectize.js

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,6 @@ Selectize.prototype.onKeyDown = function(e) {
292292
return;
293293
}
294294
}
295-
if (!this.isFull()) {
296-
this.focus(true);
297-
}
298295
};
299296

300297
/**
@@ -373,7 +370,7 @@ Selectize.prototype.onBlur = function(e) {
373370
this.close();
374371
this.setTextboxValue('');
375372
this.setActiveOption(null);
376-
this.setCaret(this.items.length, false);
373+
this.setCaret(this.items.length);
377374
if (!this.$activeItems.length) {
378375
this.$control.removeClass('focus');
379376
this.isFocused = false;
@@ -576,7 +573,6 @@ Selectize.prototype.setActiveOption = function($option, scroll, animate) {
576573
Selectize.prototype.hideInput = function() {
577574
this.setTextboxValue('');
578575
this.$control_input.css({opacity: 0, position: 'absolute', left: -10000});
579-
this.isInputFocused = false;
580576
this.isInputHidden = true;
581577
};
582578

@@ -597,12 +593,12 @@ Selectize.prototype.showInput = function() {
597593
*/
598594
Selectize.prototype.focus = function(trigger) {
599595
var self = this;
600-
var fire = trigger && !this.isInputFocused;
601-
self.ignoreFocus = !trigger;
596+
self.ignoreFocus = true;
602597
self.$control_input[0].focus();
603-
if (fire) self.onFocus();
598+
self.isInputFocused = true;
604599
window.setTimeout(function() {
605600
self.ignoreFocus = false;
601+
if (trigger) self.onFocus();
606602
}, 0);
607603
};
608604

@@ -1087,14 +1083,16 @@ Selectize.prototype.removeItem = function(value) {
10871083
this.removeOption(value);
10881084
}
10891085
this.setCaret(i);
1090-
this.positionDropdown();
10911086
this.refreshOptions(false);
10921087
this.refreshClasses();
10931088

10941089
if (!this.hasOptions) { this.close(); }
10951090
else if (this.isInputFocused) { this.open(); }
10961091

10971092
this.updatePlaceholder();
1093+
if (!this.items.length) this.showInput();
1094+
1095+
this.positionDropdown();
10981096
this.updateOriginalInput();
10991097
this.trigger('onItemRemove', value);
11001098
}
@@ -1131,7 +1129,7 @@ Selectize.prototype.createItem = function() {
11311129

11321130
self.setTextboxValue('');
11331131
self.addOption(value, data);
1134-
self.setCaret(caret, false);
1132+
self.setCaret(caret);
11351133
self.addItem(value);
11361134
self.refreshOptions(true);
11371135
self.focus(false);
@@ -1316,6 +1314,10 @@ Selectize.prototype.deleteSelection = function(e) {
13161314
for (i = 0, n = this.$activeItems.length; i < n; i++) {
13171315
values.push($(this.$activeItems[i]).attr('data-value'));
13181316
}
1317+
if (e) {
1318+
e.preventDefault();
1319+
e.stopPropagation();
1320+
}
13191321
} else if ((this.isInputFocused || this.settings.mode === 'single') && this.items.length) {
13201322
if (direction < 0 && selection.start === 0 && selection.length === 0) {
13211323
values.push(this.items[this.caretPos - 1]);
@@ -1325,7 +1327,7 @@ Selectize.prototype.deleteSelection = function(e) {
13251327
}
13261328

13271329
// allow the callback to abort
1328-
if (typeof this.settings.onDelete === 'function' && this.settings.onDelete(values) === false) {
1330+
if (!values.length || (typeof this.settings.onDelete === 'function' && this.settings.onDelete(values) === false)) {
13291331
return;
13301332
}
13311333

@@ -1335,10 +1337,7 @@ Selectize.prototype.deleteSelection = function(e) {
13351337
}
13361338
if (typeof caret !== 'undefined') {
13371339
this.setCaret(caret);
1338-
}
1339-
if (e) {
1340-
e.preventDefault();
1341-
e.stopPropagation();
1340+
this.showInput();
13421341
}
13431342
};
13441343

@@ -1353,24 +1352,30 @@ Selectize.prototype.deleteSelection = function(e) {
13531352
* @param {object} e (optional)
13541353
*/
13551354
Selectize.prototype.advanceSelection = function(direction, e) {
1355+
var tail, selection, idx, valueLength, cursorAtEdge, $tail, $items;
1356+
13561357
if (direction === 0) return;
1357-
var tail = direction > 0 ? 'last' : 'first';
1358-
var selection = getSelection(this.$control_input[0]);
13591358

1360-
if (this.isInputFocused) {
1361-
var valueLength = this.$control_input.val().length;
1362-
var cursorAtEdge = direction < 0
1359+
tail = direction > 0 ? 'last' : 'first';
1360+
selection = getSelection(this.$control_input[0]);
1361+
1362+
if (this.isInputFocused && !this.isInputHidden) {
1363+
valueLength = this.$control_input.val().length;
1364+
cursorAtEdge = direction < 0
13631365
? selection.start === 0 && selection.length === 0
13641366
: selection.start === valueLength;
13651367

13661368
if (cursorAtEdge && !valueLength) {
13671369
this.advanceCaret(direction, e);
13681370
}
13691371
} else {
1370-
var $tail = this.$control.children('.active:' + tail);
1372+
$tail = this.$control.children('.active:' + tail);
13711373
if ($tail.length) {
1372-
var idx = Array.prototype.indexOf.apply(this.$control[0].childNodes, [$tail[0]]);
1374+
$items = this.$control.children(':not(input)');
1375+
idx = Array.prototype.indexOf.apply($items, [$tail[0]]);
1376+
this.setActiveItem(null);
13731377
this.setCaret(direction > 0 ? idx + 1 : idx);
1378+
this.showInput();
13741379
}
13751380
}
13761381
};
@@ -1387,7 +1392,7 @@ Selectize.prototype.advanceCaret = function(direction, e) {
13871392
if (this.isShiftDown) {
13881393
var $adj = this.$control_input[fn]();
13891394
if ($adj.length) {
1390-
this.blur();
1395+
this.hideInput();
13911396
this.setActiveItem($adj);
13921397
e && e.preventDefault();
13931398
}
@@ -1400,9 +1405,8 @@ Selectize.prototype.advanceCaret = function(direction, e) {
14001405
* Moves the caret to the specified index.
14011406
*
14021407
* @param {int} i
1403-
* @param {boolean} focus
14041408
*/
1405-
Selectize.prototype.setCaret = function(i, focus) {
1409+
Selectize.prototype.setCaret = function(i) {
14061410
if (this.settings.mode === 'single') {
14071411
i = this.items.length;
14081412
} else {
@@ -1424,9 +1428,6 @@ Selectize.prototype.setCaret = function(i, focus) {
14241428
}
14251429

14261430
this.caretPos = i;
1427-
if (focus && this.isSetup) {
1428-
this.focus(true);
1429-
}
14301431
};
14311432

14321433
/**

0 commit comments

Comments
 (0)