1
0
mirror of synced 2024-11-22 21:16:10 +03:00

Move to a central key handler

This allows for any control to emit key events, and for the Select2
instance to handle them. So now the search box will allow for
results to be scrolled and selected, bringing back the old expected
functionality. This also stops triggering new queries when the
arrow keys are pressed.

The container will also be automatically focused when the dropdown
is closed, which is consistent with the old Select2.

The results will no longer be queried on initialization, but
instead will be queried when the dropdown is opened. This is more
consistent with the previous version of Select2.

The search box will now be cleared when the dropdown is closed.
This commit is contained in:
Kevin Brown 2014-11-01 20:35:23 -04:00
parent 3b575ad92f
commit e16c505c25
10 changed files with 513 additions and 338 deletions

View File

@ -107,6 +107,8 @@ window.$ = window.$ || {};(function() { if ($ && $.fn && $.fn.select2 && $.fn.se
}; };
Observable.prototype.on = function (event, callback) { Observable.prototype.on = function (event, callback) {
this.listeners = this.listeners || {};
if (event in this.listeners) { if (event in this.listeners) {
this.listeners[event].push(callback); this.listeners[event].push(callback);
} else { } else {
@ -117,6 +119,8 @@ window.$ = window.$ || {};(function() { if ($ && $.fn && $.fn.select2 && $.fn.se
Observable.prototype.trigger = function (event) { Observable.prototype.trigger = function (event) {
var slice = Array.prototype.slice; var slice = Array.prototype.slice;
this.listeners = this.listeners || {};
if (event in this.listeners) { if (event in this.listeners) {
this.invoke(this.listeners[event], slice.call(arguments, 1)); this.invoke(this.listeners[event], slice.call(arguments, 1));
} }
@ -569,51 +573,10 @@ define('select2/selection/base',[
return BaseSelection; return BaseSelection;
}); });
define('select2/keys',[
], function () {
var KEYS = {
BACKSPACE: 8,
TAB: 9,
ENTER: 13,
SHIFT: 16,
CTRL: 17,
ALT: 18,
ESC: 27,
SPACE: 32,
PAGE_UP: 33,
PAGE_DOWN: 34,
END: 35,
HOME: 36,
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
DELETE: 46,
isArrow: function (k) {
k = k.which ? k.which : k;
switch (k) {
case KEY.LEFT:
case KEY.RIGHT:
case KEY.UP:
case KEY.DOWN:
return true;
}
return false;
}
};
return KEYS;
});
define('select2/selection/single',[ define('select2/selection/single',[
'./base', './base',
'../utils', '../utils'
'../keys' ], function (BaseSelection, Utils) {
], function (BaseSelection, Utils, KEYS) {
function SingleSelection () { function SingleSelection () {
SingleSelection.__super__.constructor.apply(this, arguments); SingleSelection.__super__.constructor.apply(this, arguments);
} }
@ -667,6 +630,8 @@ define('select2/selection/single',[
// When the dropdown is closed, aria-expanded="false" // When the dropdown is closed, aria-expanded="false"
self.$selection.attr('aria-expanded', 'false'); self.$selection.attr('aria-expanded', 'false');
self.$selection.removeAttr('aria-activedescendant'); self.$selection.removeAttr('aria-activedescendant');
self.$selection.focus();
}); });
this.$selection.on('focus', function (evt) { this.$selection.on('focus', function (evt) {
@ -678,29 +643,7 @@ define('select2/selection/single',[
}); });
this.$selection.on('keydown', function (evt) { this.$selection.on('keydown', function (evt) {
var key = evt.which; self.trigger('keypress', evt);
if (container.isOpen()) {
if (key == KEYS.ENTER) {
self.trigger('results:select');
evt.preventDefault();
} else if (key == KEYS.UP) {
self.trigger('results:previous');
evt.preventDefault();
} else if (key == KEYS.DOWN) {
self.trigger('results:next');
evt.preventDefault();
}
} else {
if (key == KEYS.ENTER || key == KEYS.SPACE) {
self.trigger('open');
evt.preventDefault();
}
}
}); });
container.on('results:focus', function (params) { container.on('results:focus', function (params) {
@ -1375,8 +1318,8 @@ define('select2/dropdown',[
}); });
define('select2/dropdown/search',[ define('select2/dropdown/search',[
'../utils'
], function () { ], function (Utils) {
function Search () { } function Search () { }
Search.prototype.render = function (decorated) { Search.prototype.render = function (decorated) {
@ -1401,8 +1344,14 @@ define('select2/dropdown/search',[
decorated.call(this, container, $container); decorated.call(this, container, $container);
this.$search.on('keyup', function () { this.$search.on('keyup', function (evt) {
container.trigger('query', { self.trigger('keypress', evt);
if (evt.isDefaultPrevented()) {
return;
}
self.trigger('query', {
term: $(this).val() term: $(this).val()
}); });
}); });
@ -1413,6 +1362,8 @@ define('select2/dropdown/search',[
container.on('close', function () { container.on('close', function () {
self.$search.attr('tabindex', -1); self.$search.attr('tabindex', -1);
self.$search.val('');
}); });
container.on('results:all', function (params) { container.on('results:all', function (params) {
@ -1428,7 +1379,7 @@ define('select2/dropdown/search',[
}); });
}; };
Search.prototype.showSearch = function (params) { Search.prototype.showSearch = function (_, params) {
return true; return true;
}; };
@ -1642,11 +1593,52 @@ define('select2/options',[
return Options; return Options;
}); });
define('select2/keys',[
], function () {
var KEYS = {
BACKSPACE: 8,
TAB: 9,
ENTER: 13,
SHIFT: 16,
CTRL: 17,
ALT: 18,
ESC: 27,
SPACE: 32,
PAGE_UP: 33,
PAGE_DOWN: 34,
END: 35,
HOME: 36,
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
DELETE: 46,
isArrow: function (k) {
k = k.which ? k.which : k;
switch (k) {
case KEY.LEFT:
case KEY.RIGHT:
case KEY.UP:
case KEY.DOWN:
return true;
}
return false;
}
};
return KEYS;
});
define('select2/core',[ define('select2/core',[
'jquery', 'jquery',
'./options', './options',
'./utils' './utils',
], function ($, Options, Utils) { './keys'
], function ($, Options, Utils, KEYS) {
var Select2 = function ($element, options) { var Select2 = function ($element, options) {
this.$element = $element; this.$element = $element;
@ -1700,6 +1692,7 @@ define('select2/core',[
// Register any internal event handlers // Register any internal event handlers
this._registerSelectionEvents(); this._registerSelectionEvents();
this._registerDropdownEvents();
this._registerResultsEvents(); this._registerResultsEvents();
this._registerEvents(); this._registerEvents();
@ -1711,8 +1704,6 @@ define('select2/core',[
}); });
}); });
this.trigger('query', {});
// Hide the original select // Hide the original select
$element.hide(); $element.hide();
@ -1809,6 +1800,22 @@ define('select2/core',[
self.close(); self.close();
}); });
this.selection.on('keypress', function (e) {
self.trigger('keypress', e);
});
};
Select2.prototype._registerDropdownEvents = function () {
var self = this;
this.dropdown.on('query', function (params) {
self.trigger('query', params);
});
this.dropdown.on('keypress', function (e) {
self.trigger('keypress', e);
});
}; };
Select2.prototype._registerResultsEvents = function () { Select2.prototype._registerResultsEvents = function () {
@ -1850,6 +1857,32 @@ define('select2/core',[
}); });
}); });
}); });
this.on('keypress', function (evt) {
var key = evt.which;
if (self.isOpen()) {
if (key == KEYS.ENTER) {
self.trigger('results:select');
evt.preventDefault();
} else if (key == KEYS.UP) {
self.trigger('results:previous');
evt.preventDefault();
} else if (key == KEYS.DOWN) {
self.trigger('results:next');
evt.preventDefault();
}
} else {
if (key == KEYS.ENTER || key == KEYS.SPACE) {
self.trigger('open');
evt.preventDefault();
}
}
});
}; };
Select2.prototype.toggleDropdown = function () { Select2.prototype.toggleDropdown = function () {
@ -1865,6 +1898,8 @@ define('select2/core',[
return; return;
} }
this.trigger('query', {});
this.trigger('open'); this.trigger('open');
}; };

185
dist/js/select2.amd.js vendored
View File

@ -107,6 +107,8 @@ window.$ = window.$ || {};(function() { if ($ && $.fn && $.fn.select2 && $.fn.se
}; };
Observable.prototype.on = function (event, callback) { Observable.prototype.on = function (event, callback) {
this.listeners = this.listeners || {};
if (event in this.listeners) { if (event in this.listeners) {
this.listeners[event].push(callback); this.listeners[event].push(callback);
} else { } else {
@ -117,6 +119,8 @@ window.$ = window.$ || {};(function() { if ($ && $.fn && $.fn.select2 && $.fn.se
Observable.prototype.trigger = function (event) { Observable.prototype.trigger = function (event) {
var slice = Array.prototype.slice; var slice = Array.prototype.slice;
this.listeners = this.listeners || {};
if (event in this.listeners) { if (event in this.listeners) {
this.invoke(this.listeners[event], slice.call(arguments, 1)); this.invoke(this.listeners[event], slice.call(arguments, 1));
} }
@ -569,51 +573,10 @@ define('select2/selection/base',[
return BaseSelection; return BaseSelection;
}); });
define('select2/keys',[
], function () {
var KEYS = {
BACKSPACE: 8,
TAB: 9,
ENTER: 13,
SHIFT: 16,
CTRL: 17,
ALT: 18,
ESC: 27,
SPACE: 32,
PAGE_UP: 33,
PAGE_DOWN: 34,
END: 35,
HOME: 36,
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
DELETE: 46,
isArrow: function (k) {
k = k.which ? k.which : k;
switch (k) {
case KEY.LEFT:
case KEY.RIGHT:
case KEY.UP:
case KEY.DOWN:
return true;
}
return false;
}
};
return KEYS;
});
define('select2/selection/single',[ define('select2/selection/single',[
'./base', './base',
'../utils', '../utils'
'../keys' ], function (BaseSelection, Utils) {
], function (BaseSelection, Utils, KEYS) {
function SingleSelection () { function SingleSelection () {
SingleSelection.__super__.constructor.apply(this, arguments); SingleSelection.__super__.constructor.apply(this, arguments);
} }
@ -667,6 +630,8 @@ define('select2/selection/single',[
// When the dropdown is closed, aria-expanded="false" // When the dropdown is closed, aria-expanded="false"
self.$selection.attr('aria-expanded', 'false'); self.$selection.attr('aria-expanded', 'false');
self.$selection.removeAttr('aria-activedescendant'); self.$selection.removeAttr('aria-activedescendant');
self.$selection.focus();
}); });
this.$selection.on('focus', function (evt) { this.$selection.on('focus', function (evt) {
@ -678,29 +643,7 @@ define('select2/selection/single',[
}); });
this.$selection.on('keydown', function (evt) { this.$selection.on('keydown', function (evt) {
var key = evt.which; self.trigger('keypress', evt);
if (container.isOpen()) {
if (key == KEYS.ENTER) {
self.trigger('results:select');
evt.preventDefault();
} else if (key == KEYS.UP) {
self.trigger('results:previous');
evt.preventDefault();
} else if (key == KEYS.DOWN) {
self.trigger('results:next');
evt.preventDefault();
}
} else {
if (key == KEYS.ENTER || key == KEYS.SPACE) {
self.trigger('open');
evt.preventDefault();
}
}
}); });
container.on('results:focus', function (params) { container.on('results:focus', function (params) {
@ -1375,8 +1318,8 @@ define('select2/dropdown',[
}); });
define('select2/dropdown/search',[ define('select2/dropdown/search',[
'../utils'
], function () { ], function (Utils) {
function Search () { } function Search () { }
Search.prototype.render = function (decorated) { Search.prototype.render = function (decorated) {
@ -1401,8 +1344,14 @@ define('select2/dropdown/search',[
decorated.call(this, container, $container); decorated.call(this, container, $container);
this.$search.on('keyup', function () { this.$search.on('keyup', function (evt) {
container.trigger('query', { self.trigger('keypress', evt);
if (evt.isDefaultPrevented()) {
return;
}
self.trigger('query', {
term: $(this).val() term: $(this).val()
}); });
}); });
@ -1413,6 +1362,8 @@ define('select2/dropdown/search',[
container.on('close', function () { container.on('close', function () {
self.$search.attr('tabindex', -1); self.$search.attr('tabindex', -1);
self.$search.val('');
}); });
container.on('results:all', function (params) { container.on('results:all', function (params) {
@ -1428,7 +1379,7 @@ define('select2/dropdown/search',[
}); });
}; };
Search.prototype.showSearch = function (params) { Search.prototype.showSearch = function (_, params) {
return true; return true;
}; };
@ -1642,11 +1593,52 @@ define('select2/options',[
return Options; return Options;
}); });
define('select2/keys',[
], function () {
var KEYS = {
BACKSPACE: 8,
TAB: 9,
ENTER: 13,
SHIFT: 16,
CTRL: 17,
ALT: 18,
ESC: 27,
SPACE: 32,
PAGE_UP: 33,
PAGE_DOWN: 34,
END: 35,
HOME: 36,
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
DELETE: 46,
isArrow: function (k) {
k = k.which ? k.which : k;
switch (k) {
case KEY.LEFT:
case KEY.RIGHT:
case KEY.UP:
case KEY.DOWN:
return true;
}
return false;
}
};
return KEYS;
});
define('select2/core',[ define('select2/core',[
'jquery', 'jquery',
'./options', './options',
'./utils' './utils',
], function ($, Options, Utils) { './keys'
], function ($, Options, Utils, KEYS) {
var Select2 = function ($element, options) { var Select2 = function ($element, options) {
this.$element = $element; this.$element = $element;
@ -1700,6 +1692,7 @@ define('select2/core',[
// Register any internal event handlers // Register any internal event handlers
this._registerSelectionEvents(); this._registerSelectionEvents();
this._registerDropdownEvents();
this._registerResultsEvents(); this._registerResultsEvents();
this._registerEvents(); this._registerEvents();
@ -1711,8 +1704,6 @@ define('select2/core',[
}); });
}); });
this.trigger('query', {});
// Hide the original select // Hide the original select
$element.hide(); $element.hide();
@ -1809,6 +1800,22 @@ define('select2/core',[
self.close(); self.close();
}); });
this.selection.on('keypress', function (e) {
self.trigger('keypress', e);
});
};
Select2.prototype._registerDropdownEvents = function () {
var self = this;
this.dropdown.on('query', function (params) {
self.trigger('query', params);
});
this.dropdown.on('keypress', function (e) {
self.trigger('keypress', e);
});
}; };
Select2.prototype._registerResultsEvents = function () { Select2.prototype._registerResultsEvents = function () {
@ -1850,6 +1857,32 @@ define('select2/core',[
}); });
}); });
}); });
this.on('keypress', function (evt) {
var key = evt.which;
if (self.isOpen()) {
if (key == KEYS.ENTER) {
self.trigger('results:select');
evt.preventDefault();
} else if (key == KEYS.UP) {
self.trigger('results:previous');
evt.preventDefault();
} else if (key == KEYS.DOWN) {
self.trigger('results:next');
evt.preventDefault();
}
} else {
if (key == KEYS.ENTER || key == KEYS.SPACE) {
self.trigger('open');
evt.preventDefault();
}
}
});
}; };
Select2.prototype.toggleDropdown = function () { Select2.prototype.toggleDropdown = function () {
@ -1865,6 +1898,8 @@ define('select2/core',[
return; return;
} }
this.trigger('query', {});
this.trigger('open'); this.trigger('open');
}; };

View File

@ -9642,6 +9642,8 @@ define('select2/utils',[], function () {
}; };
Observable.prototype.on = function (event, callback) { Observable.prototype.on = function (event, callback) {
this.listeners = this.listeners || {};
if (event in this.listeners) { if (event in this.listeners) {
this.listeners[event].push(callback); this.listeners[event].push(callback);
} else { } else {
@ -9652,6 +9654,8 @@ define('select2/utils',[], function () {
Observable.prototype.trigger = function (event) { Observable.prototype.trigger = function (event) {
var slice = Array.prototype.slice; var slice = Array.prototype.slice;
this.listeners = this.listeners || {};
if (event in this.listeners) { if (event in this.listeners) {
this.invoke(this.listeners[event], slice.call(arguments, 1)); this.invoke(this.listeners[event], slice.call(arguments, 1));
} }
@ -10104,51 +10108,10 @@ define('select2/selection/base',[
return BaseSelection; return BaseSelection;
}); });
define('select2/keys',[
], function () {
var KEYS = {
BACKSPACE: 8,
TAB: 9,
ENTER: 13,
SHIFT: 16,
CTRL: 17,
ALT: 18,
ESC: 27,
SPACE: 32,
PAGE_UP: 33,
PAGE_DOWN: 34,
END: 35,
HOME: 36,
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
DELETE: 46,
isArrow: function (k) {
k = k.which ? k.which : k;
switch (k) {
case KEY.LEFT:
case KEY.RIGHT:
case KEY.UP:
case KEY.DOWN:
return true;
}
return false;
}
};
return KEYS;
});
define('select2/selection/single',[ define('select2/selection/single',[
'./base', './base',
'../utils', '../utils'
'../keys' ], function (BaseSelection, Utils) {
], function (BaseSelection, Utils, KEYS) {
function SingleSelection () { function SingleSelection () {
SingleSelection.__super__.constructor.apply(this, arguments); SingleSelection.__super__.constructor.apply(this, arguments);
} }
@ -10202,6 +10165,8 @@ define('select2/selection/single',[
// When the dropdown is closed, aria-expanded="false" // When the dropdown is closed, aria-expanded="false"
self.$selection.attr('aria-expanded', 'false'); self.$selection.attr('aria-expanded', 'false');
self.$selection.removeAttr('aria-activedescendant'); self.$selection.removeAttr('aria-activedescendant');
self.$selection.focus();
}); });
this.$selection.on('focus', function (evt) { this.$selection.on('focus', function (evt) {
@ -10213,29 +10178,7 @@ define('select2/selection/single',[
}); });
this.$selection.on('keydown', function (evt) { this.$selection.on('keydown', function (evt) {
var key = evt.which; self.trigger('keypress', evt);
if (container.isOpen()) {
if (key == KEYS.ENTER) {
self.trigger('results:select');
evt.preventDefault();
} else if (key == KEYS.UP) {
self.trigger('results:previous');
evt.preventDefault();
} else if (key == KEYS.DOWN) {
self.trigger('results:next');
evt.preventDefault();
}
} else {
if (key == KEYS.ENTER || key == KEYS.SPACE) {
self.trigger('open');
evt.preventDefault();
}
}
}); });
container.on('results:focus', function (params) { container.on('results:focus', function (params) {
@ -10910,8 +10853,8 @@ define('select2/dropdown',[
}); });
define('select2/dropdown/search',[ define('select2/dropdown/search',[
'../utils'
], function () { ], function (Utils) {
function Search () { } function Search () { }
Search.prototype.render = function (decorated) { Search.prototype.render = function (decorated) {
@ -10936,8 +10879,14 @@ define('select2/dropdown/search',[
decorated.call(this, container, $container); decorated.call(this, container, $container);
this.$search.on('keyup', function () { this.$search.on('keyup', function (evt) {
container.trigger('query', { self.trigger('keypress', evt);
if (evt.isDefaultPrevented()) {
return;
}
self.trigger('query', {
term: $(this).val() term: $(this).val()
}); });
}); });
@ -10948,6 +10897,8 @@ define('select2/dropdown/search',[
container.on('close', function () { container.on('close', function () {
self.$search.attr('tabindex', -1); self.$search.attr('tabindex', -1);
self.$search.val('');
}); });
container.on('results:all', function (params) { container.on('results:all', function (params) {
@ -10963,7 +10914,7 @@ define('select2/dropdown/search',[
}); });
}; };
Search.prototype.showSearch = function (params) { Search.prototype.showSearch = function (_, params) {
return true; return true;
}; };
@ -11177,11 +11128,52 @@ define('select2/options',[
return Options; return Options;
}); });
define('select2/keys',[
], function () {
var KEYS = {
BACKSPACE: 8,
TAB: 9,
ENTER: 13,
SHIFT: 16,
CTRL: 17,
ALT: 18,
ESC: 27,
SPACE: 32,
PAGE_UP: 33,
PAGE_DOWN: 34,
END: 35,
HOME: 36,
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
DELETE: 46,
isArrow: function (k) {
k = k.which ? k.which : k;
switch (k) {
case KEY.LEFT:
case KEY.RIGHT:
case KEY.UP:
case KEY.DOWN:
return true;
}
return false;
}
};
return KEYS;
});
define('select2/core',[ define('select2/core',[
'jquery', 'jquery',
'./options', './options',
'./utils' './utils',
], function ($, Options, Utils) { './keys'
], function ($, Options, Utils, KEYS) {
var Select2 = function ($element, options) { var Select2 = function ($element, options) {
this.$element = $element; this.$element = $element;
@ -11235,6 +11227,7 @@ define('select2/core',[
// Register any internal event handlers // Register any internal event handlers
this._registerSelectionEvents(); this._registerSelectionEvents();
this._registerDropdownEvents();
this._registerResultsEvents(); this._registerResultsEvents();
this._registerEvents(); this._registerEvents();
@ -11246,8 +11239,6 @@ define('select2/core',[
}); });
}); });
this.trigger('query', {});
// Hide the original select // Hide the original select
$element.hide(); $element.hide();
@ -11344,6 +11335,22 @@ define('select2/core',[
self.close(); self.close();
}); });
this.selection.on('keypress', function (e) {
self.trigger('keypress', e);
});
};
Select2.prototype._registerDropdownEvents = function () {
var self = this;
this.dropdown.on('query', function (params) {
self.trigger('query', params);
});
this.dropdown.on('keypress', function (e) {
self.trigger('keypress', e);
});
}; };
Select2.prototype._registerResultsEvents = function () { Select2.prototype._registerResultsEvents = function () {
@ -11385,6 +11392,32 @@ define('select2/core',[
}); });
}); });
}); });
this.on('keypress', function (evt) {
var key = evt.which;
if (self.isOpen()) {
if (key == KEYS.ENTER) {
self.trigger('results:select');
evt.preventDefault();
} else if (key == KEYS.UP) {
self.trigger('results:previous');
evt.preventDefault();
} else if (key == KEYS.DOWN) {
self.trigger('results:next');
evt.preventDefault();
}
} else {
if (key == KEYS.ENTER || key == KEYS.SPACE) {
self.trigger('open');
evt.preventDefault();
}
}
});
}; };
Select2.prototype.toggleDropdown = function () { Select2.prototype.toggleDropdown = function () {
@ -11400,6 +11433,8 @@ define('select2/core',[
return; return;
} }
this.trigger('query', {});
this.trigger('open'); this.trigger('open');
}; };

File diff suppressed because one or more lines are too long

185
dist/js/select2.js vendored
View File

@ -535,6 +535,8 @@ define('select2/utils',[], function () {
}; };
Observable.prototype.on = function (event, callback) { Observable.prototype.on = function (event, callback) {
this.listeners = this.listeners || {};
if (event in this.listeners) { if (event in this.listeners) {
this.listeners[event].push(callback); this.listeners[event].push(callback);
} else { } else {
@ -545,6 +547,8 @@ define('select2/utils',[], function () {
Observable.prototype.trigger = function (event) { Observable.prototype.trigger = function (event) {
var slice = Array.prototype.slice; var slice = Array.prototype.slice;
this.listeners = this.listeners || {};
if (event in this.listeners) { if (event in this.listeners) {
this.invoke(this.listeners[event], slice.call(arguments, 1)); this.invoke(this.listeners[event], slice.call(arguments, 1));
} }
@ -997,51 +1001,10 @@ define('select2/selection/base',[
return BaseSelection; return BaseSelection;
}); });
define('select2/keys',[
], function () {
var KEYS = {
BACKSPACE: 8,
TAB: 9,
ENTER: 13,
SHIFT: 16,
CTRL: 17,
ALT: 18,
ESC: 27,
SPACE: 32,
PAGE_UP: 33,
PAGE_DOWN: 34,
END: 35,
HOME: 36,
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
DELETE: 46,
isArrow: function (k) {
k = k.which ? k.which : k;
switch (k) {
case KEY.LEFT:
case KEY.RIGHT:
case KEY.UP:
case KEY.DOWN:
return true;
}
return false;
}
};
return KEYS;
});
define('select2/selection/single',[ define('select2/selection/single',[
'./base', './base',
'../utils', '../utils'
'../keys' ], function (BaseSelection, Utils) {
], function (BaseSelection, Utils, KEYS) {
function SingleSelection () { function SingleSelection () {
SingleSelection.__super__.constructor.apply(this, arguments); SingleSelection.__super__.constructor.apply(this, arguments);
} }
@ -1095,6 +1058,8 @@ define('select2/selection/single',[
// When the dropdown is closed, aria-expanded="false" // When the dropdown is closed, aria-expanded="false"
self.$selection.attr('aria-expanded', 'false'); self.$selection.attr('aria-expanded', 'false');
self.$selection.removeAttr('aria-activedescendant'); self.$selection.removeAttr('aria-activedescendant');
self.$selection.focus();
}); });
this.$selection.on('focus', function (evt) { this.$selection.on('focus', function (evt) {
@ -1106,29 +1071,7 @@ define('select2/selection/single',[
}); });
this.$selection.on('keydown', function (evt) { this.$selection.on('keydown', function (evt) {
var key = evt.which; self.trigger('keypress', evt);
if (container.isOpen()) {
if (key == KEYS.ENTER) {
self.trigger('results:select');
evt.preventDefault();
} else if (key == KEYS.UP) {
self.trigger('results:previous');
evt.preventDefault();
} else if (key == KEYS.DOWN) {
self.trigger('results:next');
evt.preventDefault();
}
} else {
if (key == KEYS.ENTER || key == KEYS.SPACE) {
self.trigger('open');
evt.preventDefault();
}
}
}); });
container.on('results:focus', function (params) { container.on('results:focus', function (params) {
@ -1803,8 +1746,8 @@ define('select2/dropdown',[
}); });
define('select2/dropdown/search',[ define('select2/dropdown/search',[
'../utils'
], function () { ], function (Utils) {
function Search () { } function Search () { }
Search.prototype.render = function (decorated) { Search.prototype.render = function (decorated) {
@ -1829,8 +1772,14 @@ define('select2/dropdown/search',[
decorated.call(this, container, $container); decorated.call(this, container, $container);
this.$search.on('keyup', function () { this.$search.on('keyup', function (evt) {
container.trigger('query', { self.trigger('keypress', evt);
if (evt.isDefaultPrevented()) {
return;
}
self.trigger('query', {
term: $(this).val() term: $(this).val()
}); });
}); });
@ -1841,6 +1790,8 @@ define('select2/dropdown/search',[
container.on('close', function () { container.on('close', function () {
self.$search.attr('tabindex', -1); self.$search.attr('tabindex', -1);
self.$search.val('');
}); });
container.on('results:all', function (params) { container.on('results:all', function (params) {
@ -1856,7 +1807,7 @@ define('select2/dropdown/search',[
}); });
}; };
Search.prototype.showSearch = function (params) { Search.prototype.showSearch = function (_, params) {
return true; return true;
}; };
@ -2070,11 +2021,52 @@ define('select2/options',[
return Options; return Options;
}); });
define('select2/keys',[
], function () {
var KEYS = {
BACKSPACE: 8,
TAB: 9,
ENTER: 13,
SHIFT: 16,
CTRL: 17,
ALT: 18,
ESC: 27,
SPACE: 32,
PAGE_UP: 33,
PAGE_DOWN: 34,
END: 35,
HOME: 36,
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
DELETE: 46,
isArrow: function (k) {
k = k.which ? k.which : k;
switch (k) {
case KEY.LEFT:
case KEY.RIGHT:
case KEY.UP:
case KEY.DOWN:
return true;
}
return false;
}
};
return KEYS;
});
define('select2/core',[ define('select2/core',[
'jquery', 'jquery',
'./options', './options',
'./utils' './utils',
], function ($, Options, Utils) { './keys'
], function ($, Options, Utils, KEYS) {
var Select2 = function ($element, options) { var Select2 = function ($element, options) {
this.$element = $element; this.$element = $element;
@ -2128,6 +2120,7 @@ define('select2/core',[
// Register any internal event handlers // Register any internal event handlers
this._registerSelectionEvents(); this._registerSelectionEvents();
this._registerDropdownEvents();
this._registerResultsEvents(); this._registerResultsEvents();
this._registerEvents(); this._registerEvents();
@ -2139,8 +2132,6 @@ define('select2/core',[
}); });
}); });
this.trigger('query', {});
// Hide the original select // Hide the original select
$element.hide(); $element.hide();
@ -2237,6 +2228,22 @@ define('select2/core',[
self.close(); self.close();
}); });
this.selection.on('keypress', function (e) {
self.trigger('keypress', e);
});
};
Select2.prototype._registerDropdownEvents = function () {
var self = this;
this.dropdown.on('query', function (params) {
self.trigger('query', params);
});
this.dropdown.on('keypress', function (e) {
self.trigger('keypress', e);
});
}; };
Select2.prototype._registerResultsEvents = function () { Select2.prototype._registerResultsEvents = function () {
@ -2278,6 +2285,32 @@ define('select2/core',[
}); });
}); });
}); });
this.on('keypress', function (evt) {
var key = evt.which;
if (self.isOpen()) {
if (key == KEYS.ENTER) {
self.trigger('results:select');
evt.preventDefault();
} else if (key == KEYS.UP) {
self.trigger('results:previous');
evt.preventDefault();
} else if (key == KEYS.DOWN) {
self.trigger('results:next');
evt.preventDefault();
}
} else {
if (key == KEYS.ENTER || key == KEYS.SPACE) {
self.trigger('open');
evt.preventDefault();
}
}
});
}; };
Select2.prototype.toggleDropdown = function () { Select2.prototype.toggleDropdown = function () {
@ -2293,6 +2326,8 @@ define('select2/core',[
return; return;
} }
this.trigger('query', {});
this.trigger('open'); this.trigger('open');
}; };

File diff suppressed because one or more lines are too long

View File

@ -1,8 +1,9 @@
define([ define([
'jquery', 'jquery',
'./options', './options',
'./utils' './utils',
], function ($, Options, Utils) { './keys'
], function ($, Options, Utils, KEYS) {
var Select2 = function ($element, options) { var Select2 = function ($element, options) {
this.$element = $element; this.$element = $element;
@ -56,6 +57,7 @@ define([
// Register any internal event handlers // Register any internal event handlers
this._registerSelectionEvents(); this._registerSelectionEvents();
this._registerDropdownEvents();
this._registerResultsEvents(); this._registerResultsEvents();
this._registerEvents(); this._registerEvents();
@ -67,8 +69,6 @@ define([
}); });
}); });
this.trigger('query', {});
// Hide the original select // Hide the original select
$element.hide(); $element.hide();
@ -165,6 +165,22 @@ define([
self.close(); self.close();
}); });
this.selection.on('keypress', function (e) {
self.trigger('keypress', e);
});
};
Select2.prototype._registerDropdownEvents = function () {
var self = this;
this.dropdown.on('query', function (params) {
self.trigger('query', params);
});
this.dropdown.on('keypress', function (e) {
self.trigger('keypress', e);
});
}; };
Select2.prototype._registerResultsEvents = function () { Select2.prototype._registerResultsEvents = function () {
@ -206,6 +222,32 @@ define([
}); });
}); });
}); });
this.on('keypress', function (evt) {
var key = evt.which;
if (self.isOpen()) {
if (key == KEYS.ENTER) {
self.trigger('results:select');
evt.preventDefault();
} else if (key == KEYS.UP) {
self.trigger('results:previous');
evt.preventDefault();
} else if (key == KEYS.DOWN) {
self.trigger('results:next');
evt.preventDefault();
}
} else {
if (key == KEYS.ENTER || key == KEYS.SPACE) {
self.trigger('open');
evt.preventDefault();
}
}
});
}; };
Select2.prototype.toggleDropdown = function () { Select2.prototype.toggleDropdown = function () {
@ -221,6 +263,8 @@ define([
return; return;
} }
this.trigger('query', {});
this.trigger('open'); this.trigger('open');
}; };

View File

@ -1,6 +1,6 @@
define([ define([
'../utils'
], function () { ], function (Utils) {
function Search () { } function Search () { }
Search.prototype.render = function (decorated) { Search.prototype.render = function (decorated) {
@ -25,8 +25,14 @@ define([
decorated.call(this, container, $container); decorated.call(this, container, $container);
this.$search.on('keyup', function () { this.$search.on('keyup', function (evt) {
container.trigger('query', { self.trigger('keypress', evt);
if (evt.isDefaultPrevented()) {
return;
}
self.trigger('query', {
term: $(this).val() term: $(this).val()
}); });
}); });
@ -37,6 +43,8 @@ define([
container.on('close', function () { container.on('close', function () {
self.$search.attr('tabindex', -1); self.$search.attr('tabindex', -1);
self.$search.val('');
}); });
container.on('results:all', function (params) { container.on('results:all', function (params) {
@ -52,7 +60,7 @@ define([
}); });
}; };
Search.prototype.showSearch = function (params) { Search.prototype.showSearch = function (_, params) {
return true; return true;
}; };

View File

@ -1,8 +1,7 @@
define([ define([
'./base', './base',
'../utils', '../utils'
'../keys' ], function (BaseSelection, Utils) {
], function (BaseSelection, Utils, KEYS) {
function SingleSelection () { function SingleSelection () {
SingleSelection.__super__.constructor.apply(this, arguments); SingleSelection.__super__.constructor.apply(this, arguments);
} }
@ -56,6 +55,8 @@ define([
// When the dropdown is closed, aria-expanded="false" // When the dropdown is closed, aria-expanded="false"
self.$selection.attr('aria-expanded', 'false'); self.$selection.attr('aria-expanded', 'false');
self.$selection.removeAttr('aria-activedescendant'); self.$selection.removeAttr('aria-activedescendant');
self.$selection.focus();
}); });
this.$selection.on('focus', function (evt) { this.$selection.on('focus', function (evt) {
@ -67,29 +68,7 @@ define([
}); });
this.$selection.on('keydown', function (evt) { this.$selection.on('keydown', function (evt) {
var key = evt.which; self.trigger('keypress', evt);
if (container.isOpen()) {
if (key == KEYS.ENTER) {
self.trigger('results:select');
evt.preventDefault();
} else if (key == KEYS.UP) {
self.trigger('results:previous');
evt.preventDefault();
} else if (key == KEYS.DOWN) {
self.trigger('results:next');
evt.preventDefault();
}
} else {
if (key == KEYS.ENTER || key == KEYS.SPACE) {
self.trigger('open');
evt.preventDefault();
}
}
}); });
container.on('results:focus', function (params) { container.on('results:focus', function (params) {

View File

@ -107,6 +107,8 @@ define([], function () {
}; };
Observable.prototype.on = function (event, callback) { Observable.prototype.on = function (event, callback) {
this.listeners = this.listeners || {};
if (event in this.listeners) { if (event in this.listeners) {
this.listeners[event].push(callback); this.listeners[event].push(callback);
} else { } else {
@ -117,6 +119,8 @@ define([], function () {
Observable.prototype.trigger = function (event) { Observable.prototype.trigger = function (event) {
var slice = Array.prototype.slice; var slice = Array.prototype.slice;
this.listeners = this.listeners || {};
if (event in this.listeners) { if (event in this.listeners) {
this.invoke(this.listeners[event], slice.call(arguments, 1)); this.invoke(this.listeners[event], slice.call(arguments, 1));
} }