|
1 |
| -/* jquery Selectboxit - v2.4.0 - 2013-1-2 |
| 1 | +/* jquery Selectboxit - v2.5.0 - 2013-1-6 |
2 | 2 | * http://www.gregfranko.com/jQuery.selectBoxIt.js/
|
3 | 3 | * Copyright (c) 2012 Greg Franko; Licensed MIT */
|
4 | 4 |
|
|
26 | 26 |
|
27 | 27 | // Plugin version
|
28 | 28 |
|
29 |
| - VERSION: "2.4.0", |
| 29 | + VERSION: "2.5.0", |
30 | 30 |
|
31 | 31 | // These options will be used as defaults
|
32 | 32 | options: {
|
|
81 | 81 | "nostyle": false,
|
82 | 82 |
|
83 | 83 | // **native**: Triggers the native select box when a user interacts with the drop down
|
84 |
| - "native": false |
| 84 | + "native": false, |
| 85 | + |
| 86 | + // **aggressiveChange**: Will select a drop down item (and trigger a change event) when a user navigates to the item via the keyboard (up and down arrow or search), before a user selects an option with a click or the enter key |
| 87 | + "aggressiveChange": false |
85 | 88 |
|
86 | 89 | },
|
87 | 90 |
|
|
995 | 998 | // If the user presses the `enter key`
|
996 | 999 | case enterKey:
|
997 | 1000 |
|
| 1001 | + var activeElem = self.list.find("li." + self.focusClass); |
| 1002 | + |
| 1003 | + // If there is no active Elem yet |
| 1004 | + if(!activeElem.length) { |
| 1005 | + |
| 1006 | + activeElem = self.listItems.first(); |
| 1007 | + |
| 1008 | + } |
| 1009 | + |
| 1010 | + // Updates the dropdown list value |
| 1011 | + self._update(activeElem); |
| 1012 | + |
998 | 1013 | // Prevents the default event from being triggered
|
999 | 1014 | e.preventDefault();
|
1000 | 1015 |
|
|
1006 | 1021 |
|
1007 | 1022 | }
|
1008 | 1023 |
|
1009 |
| - self._checkDefaultText(); |
1010 |
| - |
1011 | 1024 | // Triggers the `enter` events on the original select box
|
1012 | 1025 | self.selectBox.trigger("enter");
|
1013 | 1026 |
|
|
1125 | 1138 | // Select box individual options events bound with the jQuery `delegate` method. `Delegate` was used because binding individual events to each list item (since we don't know how many there will be) would decrease performance. Instead, we bind each event to the unordered list, provide the list item context, and allow the list item events to bubble up (`event bubbling`). This greatly increases page performance because we only have to bind an event to one element instead of x number of elements. Delegates the `click` event with the `selectBoxIt` namespace to the list items
|
1126 | 1139 | .delegate("li", "click.selectBoxIt", function() {
|
1127 | 1140 |
|
1128 |
| - if ($(this).attr("data-disabled") === "false") { |
1129 |
| - |
1130 |
| - // Sets the original dropdown list value and triggers the `change` event on the original select box |
1131 |
| - self.selectBox.val($(this).attr("data-val")); |
1132 |
| - |
1133 |
| - // Sets `currentFocus` to the currently focused dropdown list option. |
1134 |
| - // The unary `+` operator casts the string to a number |
1135 |
| - // [James Padolsey Blog Post](http://james.padolsey.com/javascript/terse-javascript-101-part-2/) |
1136 |
| - self.currentFocus = +this.id; |
1137 |
| - |
1138 |
| - // Closes the list after selecting an option |
1139 |
| - self.close(); |
1140 |
| - |
1141 |
| - // Triggers the dropdown list `change` event if a value change occurs |
1142 |
| - if (self.originalElem.value !== self.divText.attr("data-val")) { |
| 1141 | + self._update($(this)); |
1143 | 1142 |
|
1144 |
| - self.selectBox.trigger("change", true); |
| 1143 | + var currentIndex = self.options["showFirstOption"] ? self.currentFocus : ((self.currentFocus - 1) >= 0 ? self.currentFocus: 0 ); |
1145 | 1144 |
|
1146 |
| - } |
| 1145 | + // Triggers the custom option-click event on the original select box and passes the select box option |
| 1146 | + self.selectBox.trigger("option-click", { "elem": self.selectBox.eq(currentIndex), "dropdown-elem": self.listItems.eq(self.currentFocus) }); |
1147 | 1147 |
|
1148 |
| - self._checkDefaultText(); |
| 1148 | + // If the current drop down option is not disabled |
| 1149 | + if ($(this).attr("data-disabled") === "false") { |
1149 | 1150 |
|
1150 |
| - var currentIndex = self.options["showFirstOption"] ? self.currentFocus : ((self.currentFocus - 1) >= 0 ? self.currentFocus: 0 ); |
| 1151 | + // Closes the drop down list |
| 1152 | + self.close(); |
1151 | 1153 |
|
1152 |
| - // Triggers the custom option-click event on the original select box and passes the select box option |
1153 |
| - self.selectBox.trigger("option-click", { elem: self.selectBox.eq(currentIndex) }); |
1154 | 1154 | }
|
| 1155 | + |
1155 | 1156 | })
|
1156 | 1157 |
|
1157 | 1158 | // Delegates the `focus` event with the `selectBoxIt` namespace to the list items
|
1158 | 1159 | .delegate("li", "focus.selectBoxIt", function() {
|
1159 | 1160 |
|
1160 |
| - if ($(this).attr("data-disabled") === "false") { |
| 1161 | + // Removes the hover class from the previous drop down option |
| 1162 | + self.listItems.not($(this)).removeAttr("data-active"); |
1161 | 1163 |
|
1162 |
| - // Sets the original select box current value and triggers the change event |
1163 |
| - self.originalElem.value = $(this).attr("data-val"); |
| 1164 | + $(this).attr("data-active", ""); |
1164 | 1165 |
|
1165 |
| - // Triggers the dropdown list `change` event if a value change occurs |
1166 |
| - if (self.originalElem.value !== self.divText.attr("data-val")) { |
| 1166 | + if(self.options["aggressiveChange"]) { |
1167 | 1167 |
|
1168 |
| - self.selectBox.trigger("change", true); |
| 1168 | + self._update($(this)); |
1169 | 1169 |
|
1170 |
| - } |
1171 | 1170 | }
|
1172 | 1171 |
|
1173 | 1172 | });
|
|
1216 | 1215 |
|
1217 | 1216 | // Adds the `disabled` CSS class to the new dropdown list to visually show that it is disabled
|
1218 | 1217 | self.div.addClass(self.disabledClasses);
|
| 1218 | + |
1219 | 1219 | },
|
1220 | 1220 |
|
1221 | 1221 | // `enable` event with the `selectBoxIt` namespace
|
1222 | 1222 | "enable.selectBoxIt": function() {
|
1223 | 1223 |
|
1224 | 1224 | // Removes the `disabled` CSS class from the new dropdown list to visually show that it is enabled
|
1225 | 1225 | self.div.removeClass(self.disabledClasses);
|
| 1226 | + |
1226 | 1227 | }
|
1227 | 1228 |
|
1228 | 1229 | });
|
|
1232 | 1233 |
|
1233 | 1234 | },
|
1234 | 1235 |
|
| 1236 | + // _update |
| 1237 | + // ------- |
| 1238 | + // Updates the drop down and select box with the current value |
| 1239 | + _update: function(elem) { |
| 1240 | + |
| 1241 | + var self = this; |
| 1242 | + |
| 1243 | + if (elem.attr("data-disabled") === "false") { |
| 1244 | + |
| 1245 | + // If the default text option is set and the current drop down option is not disabled |
| 1246 | + if ((self.options["defaultText"] && self.divText.text() === self.options["defaultText"])) { |
| 1247 | + |
| 1248 | + // Updates the dropdown list value |
| 1249 | + self.divText.text(self.listItems.eq(self.currentFocus).text()). |
| 1250 | + |
| 1251 | + trigger("internal-change", true); |
| 1252 | + |
| 1253 | + } |
| 1254 | + |
| 1255 | + else { |
| 1256 | + |
| 1257 | + // Sets the original dropdown list value and triggers the `change` event on the original select box |
| 1258 | + self.selectBox.val(elem.attr("data-val")); |
| 1259 | + |
| 1260 | + // Sets `currentFocus` to the currently focused dropdown list option. |
| 1261 | + // The unary `+` operator casts the string to a number |
| 1262 | + // [James Padolsey Blog Post](http://james.padolsey.com/javascript/terse-javascript-101-part-2/) |
| 1263 | + self.currentFocus = +elem.attr("id"); |
| 1264 | + |
| 1265 | + // Triggers the dropdown list `change` event if a value change occurs |
| 1266 | + if (self.originalElem.value !== self.divText.attr("data-val")) { |
| 1267 | + |
| 1268 | + self.selectBox.trigger("change", true); |
| 1269 | + |
| 1270 | + } |
| 1271 | + |
| 1272 | + } |
| 1273 | + |
| 1274 | + } |
| 1275 | + |
| 1276 | + }, |
| 1277 | + |
1235 | 1278 | // _addClasses
|
1236 | 1279 | // -----------
|
1237 | 1280 | // Adds SelectBoxIt CSS classes
|
|
1241 | 1284 |
|
1242 | 1285 | focusClass = obj.focusClasses || "selectboxit-focus",
|
1243 | 1286 |
|
1244 |
| - hoverClass = obj.hoverClasses || "selectboxit-hover", |
1245 |
| - |
1246 | 1287 | buttonClass = obj.buttonClasses || "selectboxit-btn",
|
1247 | 1288 |
|
1248 | 1289 | listClass = obj.listClasses || "selectboxit-dropdown";
|
|
1287 | 1328 | // `click` event with the `selectBoxIt` namespace
|
1288 | 1329 | "open.selectBoxIt": function() {
|
1289 | 1330 |
|
1290 |
| - // Removes the jQueryUI hover class from the dropdown list and adds the jQueryUI focus class for both the dropdown list and the currently selected dropdown list option |
1291 |
| - self.div.removeClass(hoverClass).add(self.listItems.eq(self.currentFocus)). |
| 1331 | + var currentElem = self.list.find("li[data-val='" + self.divText.attr("data-val") + "']"); |
| 1332 | + |
| 1333 | + // If no current element can be found, then select the first drop down option |
| 1334 | + if(!currentElem.length) { |
| 1335 | + |
| 1336 | + currentElem = self.listItems.first(); |
| 1337 | + |
| 1338 | + } |
| 1339 | + |
| 1340 | + self.currentFocus = +currentElem.attr("id"); |
| 1341 | + |
| 1342 | + var activeElem = self.listItems.eq(self.currentFocus); |
| 1343 | + |
| 1344 | + // Removes the focus class from the dropdown list and adds the library focus class for both the dropdown list and the currently selected dropdown list option |
| 1345 | + self.div.removeClass(focusClass); |
1292 | 1346 |
|
1293 |
| - addClass(focusClass); |
| 1347 | + self.listItems.removeAttr("data-active").not(activeElem).removeClass(focusClass); |
| 1348 | + |
| 1349 | + activeElem.addClass(focusClass); |
1294 | 1350 |
|
1295 | 1351 | },
|
1296 | 1352 |
|
|
1303 | 1359 | // `mousenter` event with the `selectBoxIt` namespace
|
1304 | 1360 | "mouseenter.selectBoxIt": function() {
|
1305 | 1361 |
|
1306 |
| - self.div.addClass(hoverClass); |
| 1362 | + self.div.addClass(focusClass); |
1307 | 1363 |
|
1308 | 1364 | },
|
1309 | 1365 |
|
1310 | 1366 | // `mouseleave` event with the `selectBoxIt` namespace
|
1311 | 1367 | "mouseleave.selectBoxIt": function() {
|
1312 | 1368 |
|
1313 |
| - // Removes the hover CSS class on the previously hovered dropdown list option |
1314 |
| - self.div.removeClass(hoverClass); |
| 1369 | + // Removes the focus CSS class on the previously hovered dropdown list option |
| 1370 | + self.div.removeClass(focusClass); |
1315 | 1371 |
|
1316 | 1372 | }
|
1317 | 1373 |
|
|
1324 | 1380 | // If the currently moused over drop down option is not disabled
|
1325 | 1381 | if($(this).attr("data-disabled") === "false") {
|
1326 | 1382 |
|
1327 |
| - // Sets the dropdown list individual options back to the default state and sets the hover CSS class on the currently hovered option |
| 1383 | + self.listItems.removeAttr("data-active"); |
| 1384 | + |
| 1385 | + $(this).addClass(focusClass).attr("data-active", ""); |
| 1386 | + |
| 1387 | + // Sets the dropdown list individual options back to the default state and sets the focus CSS class on the currently hovered option |
1328 | 1388 | self.listItems.not($(this)).removeClass(focusClass);
|
1329 | 1389 |
|
1330 |
| - $(this).addClass(hoverClass); |
| 1390 | + $(this).addClass(focusClass); |
| 1391 | + |
| 1392 | + self.currentFocus = +$(this).attr("id"); |
1331 | 1393 |
|
1332 | 1394 | }
|
1333 | 1395 |
|
1334 | 1396 | },
|
1335 | 1397 |
|
1336 | 1398 | "mouseleave.selectBoxIt": function() {
|
1337 | 1399 |
|
1338 |
| - // Removes the hover class from the previous drop down option |
1339 |
| - $(this).removeClass(hoverClass); |
| 1400 | + // If the currently moused over drop down option is not disabled |
| 1401 | + if($(this).attr("data-disabled") === "false") { |
| 1402 | + |
| 1403 | + // Removes the focus class from the previous drop down option |
| 1404 | + self.listItems.not($(this)).removeClass(focusClass).removeAttr("data-active"); |
| 1405 | + |
| 1406 | + } |
1340 | 1407 |
|
1341 | 1408 | }
|
1342 | 1409 |
|
|
1360 | 1427 |
|
1361 | 1428 | focusClasses: "ui-state-focus",
|
1362 | 1429 |
|
1363 |
| - hoverClasses: "ui-state-hover", |
1364 |
| - |
1365 | 1430 | arrowClasses: "ui-icon ui-icon-triangle-1-s",
|
1366 | 1431 |
|
1367 | 1432 | buttonClasses: "ui-widget ui-state-default",
|
|
1388 | 1453 |
|
1389 | 1454 | focusClasses: "active",
|
1390 | 1455 |
|
1391 |
| - hoverClasses: "", |
1392 |
| - |
1393 | 1456 | arrowClasses: "caret",
|
1394 | 1457 |
|
1395 | 1458 | buttonClasses: "btn",
|
|
1417 | 1480 |
|
1418 | 1481 | focusClasses: "ui-btn-active-" + theme + " ui-btn-down-" + theme,
|
1419 | 1482 |
|
1420 |
| - hoverClasses: "ui-btn-hover-" + theme, |
1421 |
| - |
1422 | 1483 | arrowClasses: "ui-icon ui-icon-arrow-d ui-icon-shadow",
|
1423 | 1484 |
|
1424 | 1485 | buttonClasses: "ui-btn ui-btn-icon-right ui-btn-corner-all ui-shadow ui-btn-up-" + theme,
|
|
1585 | 1646 | .replace(/</g, '<')
|
1586 | 1647 | .replace(/>/g, '>');
|
1587 | 1648 |
|
1588 |
| - }, |
1589 |
| - |
1590 |
| - // Check Default Text |
1591 |
| - // ------------------ |
1592 |
| - // Default Text Option Logic |
1593 |
| - _checkDefaultText: function() { |
1594 |
| - |
1595 |
| - var self = this; |
1596 |
| - |
1597 |
| - // If the default text option is set and the current drop down option is not disabled |
1598 |
| - if (self.options["defaultText"] && self.divText.text() === self.options["defaultText"]) { |
1599 |
| - |
1600 |
| - // Updates the dropdown list value |
1601 |
| - self.divText.text(self.listItems.eq(self.currentFocus).text()). |
1602 |
| - |
1603 |
| - trigger("internal-change", true); |
1604 |
| - |
1605 |
| - } |
1606 |
| - |
1607 | 1649 | }
|
1608 | 1650 |
|
1609 | 1651 | });
|
|
0 commit comments