Skip to content

Commit c58cd12

Browse files
author
Kevin Brown
committed
Reworks search field. Fixes select2#239
Uses the correct EOLs now, fixes iPad keyboard issue.
1 parent 1165289 commit c58cd12

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
@@ -1089,6 +1089,8 @@
10891089
this makes sure the search field is focussed even if the current event would blur it */
10901090
window.setTimeout(this.bind(function () {
10911091
this.search.focus();
1092+
// reset the value so IE places the cursor at the end of the input box
1093+
this.search.val(this.search.val());
10921094
}), 10);
10931095
},
10941096

@@ -1203,7 +1205,7 @@
12031205
// single
12041206
focus: function () {
12051207
this.close();
1206-
this.search.focus();
1208+
this.container.focus();
12071209
},
12081210

12091211
// single
@@ -1214,7 +1216,7 @@
12141216
// single
12151217
cancel: function () {
12161218
this.parent.cancel.apply(this, arguments);
1217-
this.search.focus();
1219+
this.container.focus();
12181220
},
12191221

12201222
// single
@@ -1253,18 +1255,6 @@
12531255
killEvent(e);
12541256
return;
12551257
}
1256-
} else {
1257-
if (e.which === KEY.TAB || KEY.isControl(e) || KEY.isFunctionKey(e) || e.which === KEY.ESC) {
1258-
return;
1259-
}
1260-
1261-
this.open();
1262-
1263-
if (e.which === KEY.ENTER) {
1264-
// do not propagate the event otherwise we open, and propagate enter which closes
1265-
killEvent(e);
1266-
return;
1267-
}
12681258
}
12691259
}));
12701260

@@ -1273,7 +1263,7 @@
12731263

12741264
if (this.opened()) {
12751265
this.close();
1276-
this.search.focus();
1266+
this.container.focus();
12771267
} else if (this.enabled) {
12781268
this.open();
12791269
}
@@ -1283,14 +1273,58 @@
12831273
}));
12841274

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

12871321
selection.delegate("abbr", "click", this.bind(function (e) {
12881322
if (!this.enabled) return;
12891323
this.clear();
12901324
killEvent(e);
12911325
this.close();
12921326
this.triggerChange();
1293-
this.search.focus();
1327+
this.container.focus();
12941328
}));
12951329

12961330
selection.bind("focus", this.bind(function() { this.search.focus(); }));
@@ -1403,7 +1437,7 @@
14031437
this.opts.element.val(this.id(data));
14041438
this.updateSelection(data);
14051439
this.close();
1406-
this.search.focus();
1440+
this.container.focus();
14071441

14081442
if (!equal(old, this.id(data))) { this.triggerChange(); }
14091443
},
@@ -1697,7 +1731,7 @@
16971731
// multi
16981732
focus: function () {
16991733
this.close();
1700-
this.search.focus();
1734+
this.container.focus();
17011735
},
17021736

17031737
// multi

0 commit comments

Comments
 (0)