1
0
mirror of synced 2024-12-01 17:16:02 +03:00

Better key event handling

This commit is contained in:
Kevin Brown 2014-11-01 21:04:31 -04:00
parent e16c505c25
commit 6d2b462e2b
9 changed files with 293 additions and 228 deletions

View File

@ -573,10 +573,51 @@ 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',
], function (BaseSelection, Utils) { '../keys'
], function (BaseSelection, Utils, KEYS) {
function SingleSelection () { function SingleSelection () {
SingleSelection.__super__.constructor.apply(this, arguments); SingleSelection.__super__.constructor.apply(this, arguments);
} }
@ -644,6 +685,10 @@ define('select2/selection/single',[
this.$selection.on('keydown', function (evt) { this.$selection.on('keydown', function (evt) {
self.trigger('keypress', evt); self.trigger('keypress', evt);
if (evt.which === KEYS.SPACE) {
evt.preventDefault();
}
}); });
container.on('results:focus', function (params) { container.on('results:focus', function (params) {
@ -1344,16 +1389,20 @@ define('select2/dropdown/search',[
decorated.call(this, container, $container); decorated.call(this, container, $container);
this.$search.on('keyup', function (evt) { this.$search.on('keydown', function (evt) {
self.trigger('keypress', evt); self.trigger('keypress', evt);
if (evt.isDefaultPrevented()) { self._keyUpPrevented = evt.isDefaultPrevented();
return; });
}
this.$search.on('keyup', function (evt) {
if (!self._keyUpPrevented) {
self.trigger('query', { self.trigger('query', {
term: $(this).val() term: $(this).val()
}); });
}
self._keyUpPrevented = false;
}); });
container.on('open', function () { container.on('open', function () {
@ -1593,46 +1642,6 @@ 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',
@ -1862,22 +1871,26 @@ define('select2/core',[
var key = evt.which; var key = evt.which;
if (self.isOpen()) { if (self.isOpen()) {
if (key == KEYS.ENTER) { if (key === KEYS.ENTER) {
self.trigger('results:select'); self.trigger('results:select');
evt.preventDefault(); evt.preventDefault();
} else if (key == KEYS.UP) { } else if (key === KEYS.UP) {
self.trigger('results:previous'); self.trigger('results:previous');
evt.preventDefault(); evt.preventDefault();
} else if (key == KEYS.DOWN) { } else if (key === KEYS.DOWN) {
self.trigger('results:next'); self.trigger('results:next');
evt.preventDefault();
} else if (key === KEYS.ESC || key === KEYS.TAB) {
self.close();
evt.preventDefault(); evt.preventDefault();
} }
} else { } else {
if (key == KEYS.ENTER || key == KEYS.SPACE) { if (key === KEYS.ENTER || key === KEYS.SPACE) {
self.trigger('open'); self.open();
evt.preventDefault(); evt.preventDefault();
} }

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

@ -573,10 +573,51 @@ 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',
], function (BaseSelection, Utils) { '../keys'
], function (BaseSelection, Utils, KEYS) {
function SingleSelection () { function SingleSelection () {
SingleSelection.__super__.constructor.apply(this, arguments); SingleSelection.__super__.constructor.apply(this, arguments);
} }
@ -644,6 +685,10 @@ define('select2/selection/single',[
this.$selection.on('keydown', function (evt) { this.$selection.on('keydown', function (evt) {
self.trigger('keypress', evt); self.trigger('keypress', evt);
if (evt.which === KEYS.SPACE) {
evt.preventDefault();
}
}); });
container.on('results:focus', function (params) { container.on('results:focus', function (params) {
@ -1344,16 +1389,20 @@ define('select2/dropdown/search',[
decorated.call(this, container, $container); decorated.call(this, container, $container);
this.$search.on('keyup', function (evt) { this.$search.on('keydown', function (evt) {
self.trigger('keypress', evt); self.trigger('keypress', evt);
if (evt.isDefaultPrevented()) { self._keyUpPrevented = evt.isDefaultPrevented();
return; });
}
this.$search.on('keyup', function (evt) {
if (!self._keyUpPrevented) {
self.trigger('query', { self.trigger('query', {
term: $(this).val() term: $(this).val()
}); });
}
self._keyUpPrevented = false;
}); });
container.on('open', function () { container.on('open', function () {
@ -1593,46 +1642,6 @@ 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',
@ -1862,22 +1871,26 @@ define('select2/core',[
var key = evt.which; var key = evt.which;
if (self.isOpen()) { if (self.isOpen()) {
if (key == KEYS.ENTER) { if (key === KEYS.ENTER) {
self.trigger('results:select'); self.trigger('results:select');
evt.preventDefault(); evt.preventDefault();
} else if (key == KEYS.UP) { } else if (key === KEYS.UP) {
self.trigger('results:previous'); self.trigger('results:previous');
evt.preventDefault(); evt.preventDefault();
} else if (key == KEYS.DOWN) { } else if (key === KEYS.DOWN) {
self.trigger('results:next'); self.trigger('results:next');
evt.preventDefault();
} else if (key === KEYS.ESC || key === KEYS.TAB) {
self.close();
evt.preventDefault(); evt.preventDefault();
} }
} else { } else {
if (key == KEYS.ENTER || key == KEYS.SPACE) { if (key === KEYS.ENTER || key === KEYS.SPACE) {
self.trigger('open'); self.open();
evt.preventDefault(); evt.preventDefault();
} }

View File

@ -10108,10 +10108,51 @@ 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',
], function (BaseSelection, Utils) { '../keys'
], function (BaseSelection, Utils, KEYS) {
function SingleSelection () { function SingleSelection () {
SingleSelection.__super__.constructor.apply(this, arguments); SingleSelection.__super__.constructor.apply(this, arguments);
} }
@ -10179,6 +10220,10 @@ define('select2/selection/single',[
this.$selection.on('keydown', function (evt) { this.$selection.on('keydown', function (evt) {
self.trigger('keypress', evt); self.trigger('keypress', evt);
if (evt.which === KEYS.SPACE) {
evt.preventDefault();
}
}); });
container.on('results:focus', function (params) { container.on('results:focus', function (params) {
@ -10879,16 +10924,20 @@ define('select2/dropdown/search',[
decorated.call(this, container, $container); decorated.call(this, container, $container);
this.$search.on('keyup', function (evt) { this.$search.on('keydown', function (evt) {
self.trigger('keypress', evt); self.trigger('keypress', evt);
if (evt.isDefaultPrevented()) { self._keyUpPrevented = evt.isDefaultPrevented();
return; });
}
this.$search.on('keyup', function (evt) {
if (!self._keyUpPrevented) {
self.trigger('query', { self.trigger('query', {
term: $(this).val() term: $(this).val()
}); });
}
self._keyUpPrevented = false;
}); });
container.on('open', function () { container.on('open', function () {
@ -11128,46 +11177,6 @@ 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',
@ -11397,22 +11406,26 @@ define('select2/core',[
var key = evt.which; var key = evt.which;
if (self.isOpen()) { if (self.isOpen()) {
if (key == KEYS.ENTER) { if (key === KEYS.ENTER) {
self.trigger('results:select'); self.trigger('results:select');
evt.preventDefault(); evt.preventDefault();
} else if (key == KEYS.UP) { } else if (key === KEYS.UP) {
self.trigger('results:previous'); self.trigger('results:previous');
evt.preventDefault(); evt.preventDefault();
} else if (key == KEYS.DOWN) { } else if (key === KEYS.DOWN) {
self.trigger('results:next'); self.trigger('results:next');
evt.preventDefault();
} else if (key === KEYS.ESC || key === KEYS.TAB) {
self.close();
evt.preventDefault(); evt.preventDefault();
} }
} else { } else {
if (key == KEYS.ENTER || key == KEYS.SPACE) { if (key === KEYS.ENTER || key === KEYS.SPACE) {
self.trigger('open'); self.open();
evt.preventDefault(); evt.preventDefault();
} }

File diff suppressed because one or more lines are too long

115
dist/js/select2.js vendored
View File

@ -1001,10 +1001,51 @@ 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',
], function (BaseSelection, Utils) { '../keys'
], function (BaseSelection, Utils, KEYS) {
function SingleSelection () { function SingleSelection () {
SingleSelection.__super__.constructor.apply(this, arguments); SingleSelection.__super__.constructor.apply(this, arguments);
} }
@ -1072,6 +1113,10 @@ define('select2/selection/single',[
this.$selection.on('keydown', function (evt) { this.$selection.on('keydown', function (evt) {
self.trigger('keypress', evt); self.trigger('keypress', evt);
if (evt.which === KEYS.SPACE) {
evt.preventDefault();
}
}); });
container.on('results:focus', function (params) { container.on('results:focus', function (params) {
@ -1772,16 +1817,20 @@ define('select2/dropdown/search',[
decorated.call(this, container, $container); decorated.call(this, container, $container);
this.$search.on('keyup', function (evt) { this.$search.on('keydown', function (evt) {
self.trigger('keypress', evt); self.trigger('keypress', evt);
if (evt.isDefaultPrevented()) { self._keyUpPrevented = evt.isDefaultPrevented();
return; });
}
this.$search.on('keyup', function (evt) {
if (!self._keyUpPrevented) {
self.trigger('query', { self.trigger('query', {
term: $(this).val() term: $(this).val()
}); });
}
self._keyUpPrevented = false;
}); });
container.on('open', function () { container.on('open', function () {
@ -2021,46 +2070,6 @@ 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',
@ -2290,22 +2299,26 @@ define('select2/core',[
var key = evt.which; var key = evt.which;
if (self.isOpen()) { if (self.isOpen()) {
if (key == KEYS.ENTER) { if (key === KEYS.ENTER) {
self.trigger('results:select'); self.trigger('results:select');
evt.preventDefault(); evt.preventDefault();
} else if (key == KEYS.UP) { } else if (key === KEYS.UP) {
self.trigger('results:previous'); self.trigger('results:previous');
evt.preventDefault(); evt.preventDefault();
} else if (key == KEYS.DOWN) { } else if (key === KEYS.DOWN) {
self.trigger('results:next'); self.trigger('results:next');
evt.preventDefault();
} else if (key === KEYS.ESC || key === KEYS.TAB) {
self.close();
evt.preventDefault(); evt.preventDefault();
} }
} else { } else {
if (key == KEYS.ENTER || key == KEYS.SPACE) { if (key === KEYS.ENTER || key === KEYS.SPACE) {
self.trigger('open'); self.open();
evt.preventDefault(); evt.preventDefault();
} }

File diff suppressed because one or more lines are too long

View File

@ -227,22 +227,26 @@ define([
var key = evt.which; var key = evt.which;
if (self.isOpen()) { if (self.isOpen()) {
if (key == KEYS.ENTER) { if (key === KEYS.ENTER) {
self.trigger('results:select'); self.trigger('results:select');
evt.preventDefault(); evt.preventDefault();
} else if (key == KEYS.UP) { } else if (key === KEYS.UP) {
self.trigger('results:previous'); self.trigger('results:previous');
evt.preventDefault(); evt.preventDefault();
} else if (key == KEYS.DOWN) { } else if (key === KEYS.DOWN) {
self.trigger('results:next'); self.trigger('results:next');
evt.preventDefault();
} else if (key === KEYS.ESC || key === KEYS.TAB) {
self.close();
evt.preventDefault(); evt.preventDefault();
} }
} else { } else {
if (key == KEYS.ENTER || key == KEYS.SPACE) { if (key === KEYS.ENTER || key === KEYS.SPACE) {
self.trigger('open'); self.open();
evt.preventDefault(); evt.preventDefault();
} }

View File

@ -25,16 +25,20 @@ define([
decorated.call(this, container, $container); decorated.call(this, container, $container);
this.$search.on('keyup', function (evt) { this.$search.on('keydown', function (evt) {
self.trigger('keypress', evt); self.trigger('keypress', evt);
if (evt.isDefaultPrevented()) { self._keyUpPrevented = evt.isDefaultPrevented();
return; });
}
this.$search.on('keyup', function (evt) {
if (!self._keyUpPrevented) {
self.trigger('query', { self.trigger('query', {
term: $(this).val() term: $(this).val()
}); });
}
self._keyUpPrevented = false;
}); });
container.on('open', function () { container.on('open', function () {

View File

@ -1,7 +1,8 @@
define([ define([
'./base', './base',
'../utils' '../utils',
], function (BaseSelection, Utils) { '../keys'
], function (BaseSelection, Utils, KEYS) {
function SingleSelection () { function SingleSelection () {
SingleSelection.__super__.constructor.apply(this, arguments); SingleSelection.__super__.constructor.apply(this, arguments);
} }
@ -69,6 +70,10 @@ define([
this.$selection.on('keydown', function (evt) { this.$selection.on('keydown', function (evt) {
self.trigger('keypress', evt); self.trigger('keypress', evt);
if (evt.which === KEYS.SPACE) {
evt.preventDefault();
}
}); });
container.on('results:focus', function (params) { container.on('results:focus', function (params) {