|
806 | 806 |
|
807 | 807 | if (!this.shouldOpen()) return false; |
808 | 808 |
|
| 809 | + window.setTimeout(this.bind(this.opening), 1); |
| 810 | + |
| 811 | + return true; |
| 812 | + }, |
| 813 | + |
| 814 | + /** |
| 815 | + * Performs the opening of the dropdown |
| 816 | + */ |
| 817 | + // abstract |
| 818 | + opening: function() { |
809 | 819 | this.clearDropdownAlignmentPreference(); |
810 | 820 |
|
811 | 821 | if (this.search.val() === " ") { this.search.val(""); } |
812 | 822 |
|
813 | | - this.container.addClass("select2-dropdown-open").addClass("select2-container-active"); |
814 | | - if(this.dropdown[0] !== this.body.children().last()[0]) { |
815 | | - // ensure the dropdown is the last child of body, so the z-index is always respected correctly |
816 | | - this.dropdown.detach().appendTo(this.body); |
817 | | - } |
818 | | - |
819 | 823 | this.dropdown.addClass("select2-drop-active"); |
820 | 824 |
|
821 | 825 | this.positionDropdown(); |
822 | 826 |
|
823 | 827 | this.updateResults(true); |
| 828 | + |
| 829 | + if(this.dropdown[0] !== this.body.children().last()[0]) { |
| 830 | + this.dropdown.detach().appendTo(this.body); |
| 831 | + } |
| 832 | + |
824 | 833 | this.dropdown.show(); |
825 | 834 | this.ensureHighlightVisible(); |
826 | 835 | this.focusSearch(); |
827 | | - |
828 | | - return true; |
| 836 | + this.container.addClass("select2-dropdown-open").addClass("select2-container-active"); |
829 | 837 | }, |
830 | 838 |
|
831 | 839 | // abstract |
|
1132 | 1140 | "class": "select2-container", |
1133 | 1141 | "style": "width: " + this.getContainerWidth() |
1134 | 1142 | }).html([ |
1135 | | - " <a href='javascript:void(0)' class='select2-choice'><input type='text' class='select2-offscreen select2-focusser'/>", |
| 1143 | + " <a href='javascript:void(0)' class='select2-choice'>", |
1136 | 1144 | " <span></span><abbr class='select2-search-choice-close' style='display:none;'></abbr>", |
1137 | 1145 | " <div><b></b></div>" , |
1138 | 1146 | "</a>", |
1139 | | - " <div class='select2-drop' style='display:none;'>" , |
| 1147 | + " <div class='select2-drop select2-offscreen'>" , |
1140 | 1148 | " <div class='select2-search'>" , |
1141 | 1149 | " <input type='text' autocomplete='off' class='select2-input'/>" , |
1142 | 1150 | " </div>" , |
|
1145 | 1153 | "</div>"].join("")); |
1146 | 1154 | }, |
1147 | 1155 |
|
| 1156 | + // single |
| 1157 | + opening: function () { |
| 1158 | + this.parent.opening.apply(this, arguments); |
| 1159 | + this.dropdown.removeClass("select2-offscreen"); |
| 1160 | + }, |
| 1161 | + |
1148 | 1162 | // single |
1149 | 1163 | close: function () { |
1150 | 1164 | if (!this.opened()) return; |
1151 | 1165 | this.parent.close.apply(this, arguments); |
| 1166 | + this.dropdown.removeAttr("style").addClass("select2-offscreen").insertAfter(this.selection).show(); |
1152 | 1167 | }, |
1153 | 1168 |
|
1154 | 1169 | // single |
1155 | 1170 | focus: function () { |
1156 | 1171 | this.close(); |
1157 | | - this.selection.focus(); |
| 1172 | + this.search.focus(); |
1158 | 1173 | }, |
1159 | 1174 |
|
1160 | 1175 | // single |
|
1165 | 1180 | // single |
1166 | 1181 | cancel: function () { |
1167 | 1182 | this.parent.cancel.apply(this, arguments); |
1168 | | - this.selection.focus(); |
| 1183 | + this.search.focus(); |
1169 | 1184 | }, |
1170 | 1185 |
|
1171 | 1186 | // single |
|
1174 | 1189 | var selection, |
1175 | 1190 | container = this.container, |
1176 | 1191 | dropdown = this.dropdown, |
1177 | | - containers = $([this.container.get(0), this.dropdown.get(0)]), |
1178 | | - clickingInside = false, |
1179 | | - selector = ".select2-choice", |
1180 | | - focusser=container.find("input.select2-focusser"); |
| 1192 | + clickingInside = false; |
1181 | 1193 |
|
1182 | | - this.selection = selection = container.find(selector); |
| 1194 | + this.selection = selection = container.find(".select2-choice"); |
1183 | 1195 |
|
1184 | 1196 | this.search.bind("keydown", this.bind(function (e) { |
1185 | | - switch (e.which) { |
1186 | | - case KEY.UP: |
1187 | | - case KEY.DOWN: |
1188 | | - this.moveHighlight((e.which === KEY.UP) ? -1 : 1); |
1189 | | - killEvent(e); |
1190 | | - return; |
1191 | | - case KEY.TAB: |
1192 | | - case KEY.ENTER: |
1193 | | - this.selectHighlighted(); |
1194 | | - killEvent(e); |
1195 | | - return; |
1196 | | - case KEY.ESC: |
1197 | | - this.cancel(e); |
| 1197 | + if (!this.enabled) return; |
| 1198 | + |
| 1199 | + if (e.which === KEY.PAGE_UP || e.which === KEY.PAGE_DOWN || e.which === KEY.SPACE) { |
| 1200 | + // prevent the page from scrolling |
1198 | 1201 | killEvent(e); |
1199 | 1202 | return; |
1200 | 1203 | } |
| 1204 | + |
| 1205 | + if (this.opened()) { |
| 1206 | + switch (e.which) { |
| 1207 | + case KEY.UP: |
| 1208 | + case KEY.DOWN: |
| 1209 | + this.moveHighlight((e.which === KEY.UP) ? -1 : 1); |
| 1210 | + killEvent(e); |
| 1211 | + return; |
| 1212 | + case KEY.TAB: |
| 1213 | + case KEY.ENTER: |
| 1214 | + this.selectHighlighted(); |
| 1215 | + killEvent(e); |
| 1216 | + return; |
| 1217 | + case KEY.ESC: |
| 1218 | + this.cancel(e); |
| 1219 | + killEvent(e); |
| 1220 | + return; |
| 1221 | + } |
| 1222 | + } else { |
| 1223 | + if (e.which === KEY.TAB || KEY.isControl(e) || KEY.isFunctionKey(e) || e.which === KEY.ESC) { |
| 1224 | + return; |
| 1225 | + } |
| 1226 | + |
| 1227 | + this.open(); |
| 1228 | + |
| 1229 | + if (e.which === KEY.ENTER) { |
| 1230 | + // do not propagate the event otherwise we open, and propagate enter which closes |
| 1231 | + killEvent(e); |
| 1232 | + return; |
| 1233 | + } |
| 1234 | + } |
1201 | 1235 | })); |
1202 | 1236 |
|
1203 | | - containers.delegate(selector, "click", this.bind(function (e) { |
| 1237 | + selection.bind("click", this.bind(function (e) { |
1204 | 1238 | clickingInside = true; |
1205 | 1239 |
|
1206 | 1240 | if (this.opened()) { |
1207 | 1241 | this.close(); |
1208 | | - selection.focus(); |
| 1242 | + this.search.focus(); |
1209 | 1243 | } else if (this.enabled) { |
1210 | 1244 | this.open(); |
1211 | 1245 | } |
1212 | | - e.preventDefault(); |
| 1246 | + killEvent(e); |
1213 | 1247 |
|
1214 | 1248 | clickingInside = false; |
1215 | 1249 | })); |
1216 | | - containers.delegate(selector, "keydown", this.bind(function (e) { |
1217 | | - if (!this.enabled || e.which === KEY.TAB || KEY.isControl(e) || KEY.isFunctionKey(e) || e.which === KEY.ESC) { |
1218 | | - return; |
1219 | | - } |
1220 | | - this.open(); |
1221 | | - if (e.which === KEY.PAGE_UP || e.which === KEY.PAGE_DOWN || e.which === KEY.SPACE) { |
1222 | | - // prevent the page from scrolling |
1223 | | - killEvent(e); |
1224 | | - } |
1225 | | - if (e.which === KEY.ENTER) { |
1226 | | - // do not propagate the event otherwise we open, and propagate enter which closes |
1227 | | - killEvent(e); |
1228 | | - } |
1229 | | - })); |
1230 | | - containers.delegate(selector, "focus", function () { if (this.enabled) { containers.addClass("select2-container-active"); dropdown.addClass("select2-drop-active"); }}); |
1231 | | - containers.delegate(selector, "blur", this.bind(function (e) { |
1232 | | - if (clickingInside) return; |
1233 | | - if (e.target===focusser.get(0)) return; // ignore blurs from focusser |
1234 | | - if (!this.opened()) { this.blur(); } |
1235 | | - })); |
| 1250 | + |
| 1251 | + dropdown.bind("click", this.bind(function() { this.search.focus(); })); |
1236 | 1252 |
|
1237 | 1253 | selection.delegate("abbr", "click", this.bind(function (e) { |
1238 | 1254 | if (!this.enabled) return; |
1239 | 1255 | this.clear(); |
1240 | 1256 | killEvent(e); |
1241 | 1257 | this.close(); |
1242 | 1258 | this.triggerChange(); |
1243 | | - selection.focus(); |
| 1259 | + this.search.focus(); |
1244 | 1260 | })); |
1245 | 1261 |
|
| 1262 | + selection.bind("focus", this.bind(function() { this.search.focus(); })); |
| 1263 | + |
1246 | 1264 | this.setPlaceholder(); |
1247 | 1265 |
|
1248 | | - focusser.bind("focus", function() { selection.focus(); }); |
1249 | | - selection.bind("focus", this.bind(function() { |
1250 | | - focusser.hide(); |
| 1266 | + this.search.bind("focus", this.bind(function() { |
1251 | 1267 | this.container.addClass("select2-container-active"); |
1252 | 1268 | })); |
1253 | | - selection.bind("blur", function() { focusser.show(); }); |
1254 | | - this.opts.element.bind("open", function() { focusser.hide(); }); |
1255 | 1269 | }, |
1256 | 1270 |
|
1257 | 1271 | clear: function() { |
|
1358 | 1372 | this.opts.element.val(this.id(data)); |
1359 | 1373 | this.updateSelection(data); |
1360 | 1374 | this.close(); |
1361 | | - this.selection.focus(); |
| 1375 | + this.search.focus(); |
1362 | 1376 |
|
1363 | 1377 | if (!equal(old, this.id(data))) { this.triggerChange(); } |
1364 | 1378 | }, |
|
1623 | 1637 | }, |
1624 | 1638 |
|
1625 | 1639 | // multi |
1626 | | - open: function () { |
1627 | | - if (this.parent.open.apply(this, arguments) === false) return false; |
| 1640 | + opening: function () { |
| 1641 | + this.parent.opening.apply(this, arguments); |
1628 | 1642 |
|
1629 | 1643 | this.clearPlaceholder(); |
1630 | 1644 | this.resizeSearch(); |
1631 | 1645 | this.focusSearch(); |
1632 | | - return true; |
1633 | 1646 | }, |
1634 | 1647 |
|
1635 | 1648 | // multi |
|
0 commit comments