@@ -38,6 +38,7 @@ var Selectize = function($input, settings) {
38
38
currentResults : null ,
39
39
lastValue : '' ,
40
40
lastValidValue : '' ,
41
+ lastOpenTarget : false ,
41
42
caretPos : 0 ,
42
43
loading : 0 ,
43
44
loadedSearches : { } ,
@@ -192,7 +193,11 @@ $.extend(Selectize.prototype, {
192
193
} ) ;
193
194
194
195
$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
+ } ,
196
201
keydown : function ( ) { return self . onKeyDown . apply ( self , arguments ) ; } ,
197
202
keypress : function ( ) { return self . onKeyPress . apply ( self , arguments ) ; } ,
198
203
input : function ( ) { return self . onInput . apply ( self , arguments ) ; } ,
@@ -398,36 +403,40 @@ $.extend(Selectize.prototype, {
398
403
var defaultPrevented = e . isDefaultPrevented ( ) ;
399
404
var $target = $ ( e . target ) ;
400
405
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 ) {
424
407
// give control focus
425
408
if ( ! defaultPrevented ) {
426
409
window . setTimeout ( function ( ) {
427
410
self . focus ( ) ;
428
411
} , 0 ) ;
429
412
}
430
413
}
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
+ }
431
440
} ,
432
441
433
442
/**
0 commit comments