1
0
mirror of synced 2024-11-22 13:06:08 +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) {
this.listeners = this.listeners || {};
if (event in this.listeners) {
this.listeners[event].push(callback);
} else {
@ -117,6 +119,8 @@ window.$ = window.$ || {};(function() { if ($ && $.fn && $.fn.select2 && $.fn.se
Observable.prototype.trigger = function (event) {
var slice = Array.prototype.slice;
this.listeners = this.listeners || {};
if (event in this.listeners) {
this.invoke(this.listeners[event], slice.call(arguments, 1));
}
@ -569,51 +573,10 @@ define('select2/selection/base',[
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',[
'./base',
'../utils',
'../keys'
], function (BaseSelection, Utils, KEYS) {
'../utils'
], function (BaseSelection, Utils) {
function SingleSelection () {
SingleSelection.__super__.constructor.apply(this, arguments);
}
@ -667,6 +630,8 @@ define('select2/selection/single',[
// When the dropdown is closed, aria-expanded="false"
self.$selection.attr('aria-expanded', 'false');
self.$selection.removeAttr('aria-activedescendant');
self.$selection.focus();
});
this.$selection.on('focus', function (evt) {
@ -678,29 +643,7 @@ define('select2/selection/single',[
});
this.$selection.on('keydown', function (evt) {
var key = evt.which;
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();
}
}
self.trigger('keypress', evt);
});
container.on('results:focus', function (params) {
@ -1375,8 +1318,8 @@ define('select2/dropdown',[
});
define('select2/dropdown/search',[
], function () {
'../utils'
], function (Utils) {
function Search () { }
Search.prototype.render = function (decorated) {
@ -1401,8 +1344,14 @@ define('select2/dropdown/search',[
decorated.call(this, container, $container);
this.$search.on('keyup', function () {
container.trigger('query', {
this.$search.on('keyup', function (evt) {
self.trigger('keypress', evt);
if (evt.isDefaultPrevented()) {
return;
}
self.trigger('query', {
term: $(this).val()
});
});
@ -1413,6 +1362,8 @@ define('select2/dropdown/search',[
container.on('close', function () {
self.$search.attr('tabindex', -1);
self.$search.val('');
});
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;
};
@ -1642,11 +1593,52 @@ define('select2/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',[
'jquery',
'./options',
'./utils'
], function ($, Options, Utils) {
'./utils',
'./keys'
], function ($, Options, Utils, KEYS) {
var Select2 = function ($element, options) {
this.$element = $element;
@ -1700,6 +1692,7 @@ define('select2/core',[
// Register any internal event handlers
this._registerSelectionEvents();
this._registerDropdownEvents();
this._registerResultsEvents();
this._registerEvents();
@ -1711,8 +1704,6 @@ define('select2/core',[
});
});
this.trigger('query', {});
// Hide the original select
$element.hide();
@ -1809,6 +1800,22 @@ define('select2/core',[
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 () {
@ -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 () {
@ -1865,6 +1898,8 @@ define('select2/core',[
return;
}
this.trigger('query', {});
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) {
this.listeners = this.listeners || {};
if (event in this.listeners) {
this.listeners[event].push(callback);
} else {
@ -117,6 +119,8 @@ window.$ = window.$ || {};(function() { if ($ && $.fn && $.fn.select2 && $.fn.se
Observable.prototype.trigger = function (event) {
var slice = Array.prototype.slice;
this.listeners = this.listeners || {};
if (event in this.listeners) {
this.invoke(this.listeners[event], slice.call(arguments, 1));
}
@ -569,51 +573,10 @@ define('select2/selection/base',[
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',[
'./base',
'../utils',
'../keys'
], function (BaseSelection, Utils, KEYS) {
'../utils'
], function (BaseSelection, Utils) {
function SingleSelection () {
SingleSelection.__super__.constructor.apply(this, arguments);
}
@ -667,6 +630,8 @@ define('select2/selection/single',[
// When the dropdown is closed, aria-expanded="false"
self.$selection.attr('aria-expanded', 'false');
self.$selection.removeAttr('aria-activedescendant');
self.$selection.focus();
});
this.$selection.on('focus', function (evt) {
@ -678,29 +643,7 @@ define('select2/selection/single',[
});
this.$selection.on('keydown', function (evt) {
var key = evt.which;
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();
}
}
self.trigger('keypress', evt);
});
container.on('results:focus', function (params) {
@ -1375,8 +1318,8 @@ define('select2/dropdown',[
});
define('select2/dropdown/search',[
], function () {
'../utils'
], function (Utils) {
function Search () { }
Search.prototype.render = function (decorated) {
@ -1401,8 +1344,14 @@ define('select2/dropdown/search',[
decorated.call(this, container, $container);
this.$search.on('keyup', function () {
container.trigger('query', {
this.$search.on('keyup', function (evt) {
self.trigger('keypress', evt);
if (evt.isDefaultPrevented()) {
return;
}
self.trigger('query', {
term: $(this).val()
});
});
@ -1413,6 +1362,8 @@ define('select2/dropdown/search',[
container.on('close', function () {
self.$search.attr('tabindex', -1);
self.$search.val('');
});
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;
};
@ -1642,11 +1593,52 @@ define('select2/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',[
'jquery',
'./options',
'./utils'
], function ($, Options, Utils) {
'./utils',
'./keys'
], function ($, Options, Utils, KEYS) {
var Select2 = function ($element, options) {
this.$element = $element;
@ -1700,6 +1692,7 @@ define('select2/core',[
// Register any internal event handlers
this._registerSelectionEvents();
this._registerDropdownEvents();
this._registerResultsEvents();
this._registerEvents();
@ -1711,8 +1704,6 @@ define('select2/core',[
});
});
this.trigger('query', {});
// Hide the original select
$element.hide();
@ -1809,6 +1800,22 @@ define('select2/core',[
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 () {
@ -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 () {
@ -1865,6 +1898,8 @@ define('select2/core',[
return;
}
this.trigger('query', {});
this.trigger('open');
};

View File

@ -9642,6 +9642,8 @@ define('select2/utils',[], function () {
};
Observable.prototype.on = function (event, callback) {
this.listeners = this.listeners || {};
if (event in this.listeners) {
this.listeners[event].push(callback);
} else {
@ -9652,6 +9654,8 @@ define('select2/utils',[], function () {
Observable.prototype.trigger = function (event) {
var slice = Array.prototype.slice;
this.listeners = this.listeners || {};
if (event in this.listeners) {
this.invoke(this.listeners[event], slice.call(arguments, 1));
}
@ -10104,51 +10108,10 @@ define('select2/selection/base',[
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',[
'./base',
'../utils',
'../keys'
], function (BaseSelection, Utils, KEYS) {
'../utils'
], function (BaseSelection, Utils) {
function SingleSelection () {
SingleSelection.__super__.constructor.apply(this, arguments);
}
@ -10202,6 +10165,8 @@ define('select2/selection/single',[
// When the dropdown is closed, aria-expanded="false"
self.$selection.attr('aria-expanded', 'false');
self.$selection.removeAttr('aria-activedescendant');
self.$selection.focus();
});
this.$selection.on('focus', function (evt) {
@ -10213,29 +10178,7 @@ define('select2/selection/single',[
});
this.$selection.on('keydown', function (evt) {
var key = evt.which;
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();
}
}
self.trigger('keypress', evt);
});
container.on('results:focus', function (params) {
@ -10910,8 +10853,8 @@ define('select2/dropdown',[
});
define('select2/dropdown/search',[
], function () {
'../utils'
], function (Utils) {
function Search () { }
Search.prototype.render = function (decorated) {
@ -10936,8 +10879,14 @@ define('select2/dropdown/search',[
decorated.call(this, container, $container);
this.$search.on('keyup', function () {
container.trigger('query', {
this.$search.on('keyup', function (evt) {
self.trigger('keypress', evt);
if (evt.isDefaultPrevented()) {
return;
}
self.trigger('query', {
term: $(this).val()
});
});
@ -10948,6 +10897,8 @@ define('select2/dropdown/search',[
container.on('close', function () {
self.$search.attr('tabindex', -1);
self.$search.val('');
});
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;
};
@ -11177,11 +11128,52 @@ define('select2/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',[
'jquery',
'./options',
'./utils'
], function ($, Options, Utils) {
'./utils',
'./keys'
], function ($, Options, Utils, KEYS) {
var Select2 = function ($element, options) {
this.$element = $element;
@ -11235,6 +11227,7 @@ define('select2/core',[
// Register any internal event handlers
this._registerSelectionEvents();
this._registerDropdownEvents();
this._registerResultsEvents();
this._registerEvents();
@ -11246,8 +11239,6 @@ define('select2/core',[
});
});
this.trigger('query', {});
// Hide the original select
$element.hide();
@ -11344,6 +11335,22 @@ define('select2/core',[
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 () {
@ -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 () {
@ -11400,6 +11433,8 @@ define('select2/core',[
return;
}
this.trigger('query', {});
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) {
this.listeners = this.listeners || {};
if (event in this.listeners) {
this.listeners[event].push(callback);
} else {
@ -545,6 +547,8 @@ define('select2/utils',[], function () {
Observable.prototype.trigger = function (event) {
var slice = Array.prototype.slice;
this.listeners = this.listeners || {};
if (event in this.listeners) {
this.invoke(this.listeners[event], slice.call(arguments, 1));
}
@ -997,51 +1001,10 @@ define('select2/selection/base',[
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',[
'./base',
'../utils',
'../keys'
], function (BaseSelection, Utils, KEYS) {
'../utils'
], function (BaseSelection, Utils) {
function SingleSelection () {
SingleSelection.__super__.constructor.apply(this, arguments);
}
@ -1095,6 +1058,8 @@ define('select2/selection/single',[
// When the dropdown is closed, aria-expanded="false"
self.$selection.attr('aria-expanded', 'false');
self.$selection.removeAttr('aria-activedescendant');
self.$selection.focus();
});
this.$selection.on('focus', function (evt) {
@ -1106,29 +1071,7 @@ define('select2/selection/single',[
});
this.$selection.on('keydown', function (evt) {
var key = evt.which;
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();
}
}
self.trigger('keypress', evt);
});
container.on('results:focus', function (params) {
@ -1803,8 +1746,8 @@ define('select2/dropdown',[
});
define('select2/dropdown/search',[
], function () {
'../utils'
], function (Utils) {
function Search () { }
Search.prototype.render = function (decorated) {
@ -1829,8 +1772,14 @@ define('select2/dropdown/search',[
decorated.call(this, container, $container);
this.$search.on('keyup', function () {
container.trigger('query', {
this.$search.on('keyup', function (evt) {
self.trigger('keypress', evt);
if (evt.isDefaultPrevented()) {
return;
}
self.trigger('query', {
term: $(this).val()
});
});
@ -1841,6 +1790,8 @@ define('select2/dropdown/search',[
container.on('close', function () {
self.$search.attr('tabindex', -1);
self.$search.val('');
});
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;
};
@ -2070,11 +2021,52 @@ define('select2/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',[
'jquery',
'./options',
'./utils'
], function ($, Options, Utils) {
'./utils',
'./keys'
], function ($, Options, Utils, KEYS) {
var Select2 = function ($element, options) {
this.$element = $element;
@ -2128,6 +2120,7 @@ define('select2/core',[
// Register any internal event handlers
this._registerSelectionEvents();
this._registerDropdownEvents();
this._registerResultsEvents();
this._registerEvents();
@ -2139,8 +2132,6 @@ define('select2/core',[
});
});
this.trigger('query', {});
// Hide the original select
$element.hide();
@ -2237,6 +2228,22 @@ define('select2/core',[
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 () {
@ -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 () {
@ -2293,6 +2326,8 @@ define('select2/core',[
return;
}
this.trigger('query', {});
this.trigger('open');
};

File diff suppressed because one or more lines are too long

View File

@ -1,8 +1,9 @@
define([
'jquery',
'./options',
'./utils'
], function ($, Options, Utils) {
'./utils',
'./keys'
], function ($, Options, Utils, KEYS) {
var Select2 = function ($element, options) {
this.$element = $element;
@ -56,6 +57,7 @@ define([
// Register any internal event handlers
this._registerSelectionEvents();
this._registerDropdownEvents();
this._registerResultsEvents();
this._registerEvents();
@ -67,8 +69,6 @@ define([
});
});
this.trigger('query', {});
// Hide the original select
$element.hide();
@ -165,6 +165,22 @@ define([
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 () {
@ -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 () {
@ -221,6 +263,8 @@ define([
return;
}
this.trigger('query', {});
this.trigger('open');
};

View File

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

View File

@ -1,8 +1,7 @@
define([
'./base',
'../utils',
'../keys'
], function (BaseSelection, Utils, KEYS) {
'../utils'
], function (BaseSelection, Utils) {
function SingleSelection () {
SingleSelection.__super__.constructor.apply(this, arguments);
}
@ -56,6 +55,8 @@ define([
// When the dropdown is closed, aria-expanded="false"
self.$selection.attr('aria-expanded', 'false');
self.$selection.removeAttr('aria-activedescendant');
self.$selection.focus();
});
this.$selection.on('focus', function (evt) {
@ -67,29 +68,7 @@ define([
});
this.$selection.on('keydown', function (evt) {
var key = evt.which;
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();
}
}
self.trigger('keypress', evt);
});
container.on('results:focus', function (params) {

View File

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