Skip to content

Commit 5d496bd

Browse files
committed
Merge branch 'master' of github.com:ivaynberg/select2
2 parents f06aae3 + d4e95ee commit 5d496bd

1 file changed

Lines changed: 52 additions & 18 deletions

File tree

select2.js

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,8 @@
10921092
this makes sure the search field is focussed even if the current event would blur it */
10931093
window.setTimeout(this.bind(function () {
10941094
this.search.focus();
1095+
// reset the value so IE places the cursor at the end of the input box
1096+
this.search.val(this.search.val());
10951097
}), 10);
10961098
},
10971099

@@ -1206,7 +1208,7 @@
12061208
// single
12071209
focus: function () {
12081210
this.close();
1209-
this.search.focus();
1211+
this.container.focus();
12101212
},
12111213

12121214
// single
@@ -1217,7 +1219,7 @@
12171219
// single
12181220
cancel: function () {
12191221
this.parent.cancel.apply(this, arguments);
1220-
this.search.focus();
1222+
this.container.focus();
12211223
},
12221224

12231225
// single
@@ -1256,18 +1258,6 @@
12561258
killEvent(e);
12571259
return;
12581260
}
1259-
} else {
1260-
if (e.which === KEY.TAB || KEY.isControl(e) || KEY.isFunctionKey(e) || e.which === KEY.ESC) {
1261-
return;
1262-
}
1263-
1264-
this.open();
1265-
1266-
if (e.which === KEY.ENTER) {
1267-
// do not propagate the event otherwise we open, and propagate enter which closes
1268-
killEvent(e);
1269-
return;
1270-
}
12711261
}
12721262
}));
12731263

@@ -1276,7 +1266,7 @@
12761266

12771267
if (this.opened()) {
12781268
this.close();
1279-
this.search.focus();
1269+
this.container.focus();
12801270
} else if (this.enabled) {
12811271
this.open();
12821272
}
@@ -1286,14 +1276,58 @@
12861276
}));
12871277

12881278
dropdown.bind("click", this.bind(function() { this.search.focus(); }));
1279+
1280+
container.bind("focus", this.bind(function() {
1281+
// allows the container to recieve the keyup event
1282+
this.container.attr("tabindex", 1);
1283+
}));
1284+
1285+
container.bind("blur", this.bind(function() {
1286+
// remove the tabindex so the tab key works properly
1287+
this.container.attr("tabindex", -1);
1288+
}));
1289+
1290+
container.bind("keydown", this.bind(function(e) {
1291+
if (!this.enabled) return;
1292+
1293+
this.container.attr("tabindex", -1);
1294+
1295+
if (e.which === KEY.PAGE_UP || e.which === KEY.PAGE_DOWN) {
1296+
// prevent the page from scrolling
1297+
killEvent(e);
1298+
return;
1299+
}
1300+
1301+
if (!this.opened()) {
1302+
if (e.which === KEY.TAB || KEY.isControl(e) || KEY.isFunctionKey(e) || e.which === KEY.ESC) {
1303+
return;
1304+
}
1305+
1306+
this.open();
1307+
1308+
if (e.which === KEY.ENTER) {
1309+
// do not propagate the event otherwise we open, and propagate enter which closes
1310+
killEvent(e);
1311+
return;
1312+
}
1313+
1314+
var keyWritten = String.fromCharCode(e.which).toLowerCase();
1315+
1316+
if (e.shiftKey) {
1317+
keyWritten = keyWritten.toUpperCase();
1318+
}
1319+
1320+
this.search.val(keyWritten);
1321+
}
1322+
}));
12891323

12901324
selection.delegate("abbr", "click", this.bind(function (e) {
12911325
if (!this.enabled) return;
12921326
this.clear();
12931327
killEvent(e);
12941328
this.close();
12951329
this.triggerChange();
1296-
this.search.focus();
1330+
this.container.focus();
12971331
}));
12981332

12991333
selection.bind("focus", this.bind(function() { this.search.focus(); }));
@@ -1406,7 +1440,7 @@
14061440
this.opts.element.val(this.id(data));
14071441
this.updateSelection(data);
14081442
this.close();
1409-
this.search.focus();
1443+
this.container.focus();
14101444

14111445
if (!equal(old, this.id(data))) { this.triggerChange(); }
14121446
},
@@ -1700,7 +1734,7 @@
17001734
// multi
17011735
focus: function () {
17021736
this.close();
1703-
this.search.focus();
1737+
this.container.focus();
17041738
},
17051739

17061740
// multi

0 commit comments

Comments
 (0)