Skip to content

Commit 8c440b0

Browse files
rotdroprisadams
authored andcommitted
Open drop-down on mouse-click if openOnFocus is false and when clicking on the options.
Otherwise: how can the drop-down be opened if openOnFocus is false?
1 parent d2564a0 commit 8c440b0

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

src/selectize.js

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ var Selectize = function($input, settings) {
3838
currentResults : null,
3939
lastValue : '',
4040
lastValidValue : '',
41+
lastOpenTarget : false,
4142
caretPos : 0,
4243
loading : 0,
4344
loadedSearches : {},
@@ -192,7 +193,11 @@ $.extend(Selectize.prototype, {
192193
});
193194

194195
$control_input.on({
195-
mousedown : function(e) { e.stopPropagation(); },
196+
mousedown : function(e) {
197+
if (self.$control_input.val() !== '' || self.settings.openOnFocus) {
198+
e.stopPropagation();
199+
}
200+
},
196201
keydown : function() { return self.onKeyDown.apply(self, arguments); },
197202
keypress : function() { return self.onKeyPress.apply(self, arguments); },
198203
input : function() { return self.onInput.apply(self, arguments); },
@@ -398,36 +403,40 @@ $.extend(Selectize.prototype, {
398403
var defaultPrevented = e.isDefaultPrevented();
399404
var $target = $(e.target);
400405

401-
if (self.isFocused) {
402-
// retain focus by preventing native handling. if the
403-
// event target is the input it should not be modified.
404-
// otherwise, text selection within the input won't work.
405-
if (e.target !== self.$control_input[0]) {
406-
if (self.settings.mode === 'single') {
407-
// toggle dropdown
408-
self.isOpen ? self.close() : self.open();
409-
410-
// when closing the dropdown, we set a isDropdownClosing
411-
// varible temporaily to prevent the dropdown from reopening
412-
// from the onClick event
413-
self.isDropdownClosing = true;
414-
setTimeout(function() {
415-
self.isDropdownClosing = false;
416-
}, self.settings.closeDropdownThreshold);
417-
418-
} else if (!defaultPrevented) {
419-
self.setActiveItem(null);
420-
}
421-
return false;
422-
}
423-
} else {
406+
if (!self.isFocused) {
424407
// give control focus
425408
if (!defaultPrevented) {
426409
window.setTimeout(function() {
427410
self.focus();
428411
}, 0);
429412
}
430413
}
414+
// retain focus by preventing native handling. if the
415+
// event target is the input it should not be modified.
416+
// otherwise, text selection within the input won't work.
417+
if (e.target !== self.$control_input[0] || self.$control_input.val() === '') {
418+
if (self.settings.mode === 'single') {
419+
// toggle dropdown
420+
self.isOpen ? self.close() : self.open();
421+
} else {
422+
if (!defaultPrevented) {
423+
self.setActiveItem(null);
424+
}
425+
if (!self.settings.openOnFocus) {
426+
if (self.isOpen && e.target === self.lastOpenTarget) {
427+
self.close();
428+
self.lastOpenTarget = false;
429+
} else if (!self.isOpen) {
430+
self.refreshOptions();
431+
self.open();
432+
self.lastOpenTarget = e.target;
433+
} else {
434+
self.lastOpenTarget = e.target;
435+
}
436+
}
437+
}
438+
return false;
439+
}
431440
},
432441

433442
/**

0 commit comments

Comments
 (0)